Saturday, January 28, 2012

Meta-Trader - January 2012

Welcome back Meta-Traders.

With January 2012 almost in the books, let's take a broader look at what has been going on in money and markets.

In his book Reminiscences of a Stock Operator the main character spends a lot of time studying 'general conditions' and at times goes months without trading just waiting for that perfect setup. That's what I'm going to strive for in 2012, to be much more judicious in my actions, trade less often, do more analysis, and wait for that perfect setup before springing into action.

US stocks got off to a very solid start and are up about 4.6% so far for the year. I managed to come out about 1% point better in stocks due to being on the right side of blowout earnings in Apple computer this past week. Apple briefly eclipsed Exxon Mobile as the US stock with the largest capitalization. If you only listen to one earnings call this season, listen to the Apple call here. The analysts were giving management a hard time about what they are going to do with the nearly 100 billion in cash on the books. And the management doesn't even seem to care! That's what I call a high quality problem.

I spent some time and money in the Apple store in Stamford, CT this past quarter, and the place is hopping, even at 10AM on a Monday morning. I wouldn't even try to go in there on a Saturday. Anyway I bring it up because Apple is a mega-trend in itself. This stock has a 60% annual revenue growth rate, and is selling at only 15 times earnings! I had to restore my iPad back to factory configuration, and I went through the setup, and the regional selector default value was China! The numbers are staggering and Apple stock can easily double (or cross $1000 a share) before its run is over. Apple is mega-trend number one.

As for mega-trend number two, the US Fed came out this week and said rates will stay low possibly until late 2014! The Fed is going out of it way to accommodate, and there are signs its starting to work. There's a sense in the US that that the economy is starting to improve. Low interest rates invariably drive money to risky assets, so for the time being it's risk-on.

As for Forex, I made about 1.4% for the month or about $283 on my $20,000 invested.

The number could have been closer to 2% except I had lousy performances in both my FX-Regression and Regression plus Megadroid accounts. I've said before that I give a system about a year to prove itself, and if it doesn't make any money or doesn't work out, they get the boot. I'm ready to throw in the towel on these 2 accounts but I haven't figured out where to put the money to work yet.

On the demo side, Amachay started to trade a took a winner and a loser. I have added that account to the performance list over on the right side of the blog.

Check back later for part 4 in my series on my efforts to develop systems based on Neural Network technology. Enjoy your weekend.

Saturday, January 21, 2012

Meta-Trader - Neural Nets - Part 2

Welcome back Meta-Traders.

Here's part 2 of my series on my efforts to implement an Expert Advisor in Meta-Trader that uses neural network technology.

The easiest way to build neural networks is using Fann - which is a free, open source Neural network library that you can download for free here. Use of the library is via an API (Application Programming Interface) which has about a dozen function calls. There are routines to create, train, run and destroy networks. There are also routines to load and save networks to and from the disk.

My first thought was to build a sub-system to do all the neural network stuff outside of MetaTrader. This way I could build and test the network outside of MetaTrader and poke and prod them to see how they work. This would give me some transparency into the network operation and address one of my issues with Sunqu - that it creates trains, executes and disposes dozens of networks completely in memory and never saves them to disk or leaves them around for outside examination.

Using Microsoft Visual Studio 2008, I created FannUtil.exe, a command-line program with interface as follows:

FannUtil.exe []

FannUtil.exe -Train

FannUtil.exe -Execute

Returns 0 if success else 1

This was promising since I could create, train and execute networks with only a few commands.
I'll leave the specifics of the data file formats for you to look up, but they are all text files and can be edited with notepad or whatever text editor you have available. Within a few hours, I had FannUtil.exe working like a champ on the XOR (Exclusive Or) example.

Next step was to modify a MetaTrader expert to write training data to a text file so I could run them outside of Meta-Trader and see how well the networks would converge on a solution using the training data.

Neural networks may or may not find a solution to solve for the data they are trained on. For example, the data may not contain a discernible pattern or may contain contradictory information. The degree of success of the training is measured by the MSE or Mean Standard Error for the network being trained. The default "desired error" in the examples is 0.001 which is a very small error and indicative of a problem the can be solved with a high degree of certainty and a low degree of error - such as the XOR problem.

To see if your problem has a solution, run it through the network and see if you converge on a solution. I coded FannUtil such that if the network went through 2 successive training iterations without reducing the error rate by 0.001, then stop training and report the MSE. This way we can determine very quickly what is the lowest expected error rate for the problem being presented without a lot of excess CPU cycles.

Using FannUtil, I ran some data files using the problem described in my previous post and I found the following error rates:
  • 0.045 - Error rate for 45 days training data
  • 0.069 - Error rate for 1 year training data
  • 0.077 - Error rate for 10 years training data
These error rates seem pretty high versus the target of 0.001. Clearly, the more data we present, the higher error rate will result. But is an error rate of 7% really so bad? After all, we are are looking only for very narrow edge in forex trading and 7% error might still results in a tradable edge.

Anyway, once I had Meta-Trader writing the training file, and FannUtil.exe training the networks and making predictions, I figured it would be pretty easy to modify the expert to launch FannUtil.exe and interface with the networks completely via the EXE. That lead to another series of challenges that I did not expect, and we will cover those in my next post.

I uploaded FannUtil.exe and the training files from the cases above to my yahoo group at FX-Mon Yahoo Group in case anyone wants to play around with it.

Come back later for installment #3 and have a great week.

Friday, January 20, 2012

Meta-Trader - Neural Nets - Part 1

Welcome back Meta-Traders.

Over the holiday season I spent some quality time getting started with neural networks inside Meta-Trader. During that time, I did a review of Sunqu, the first neural net based system on Asirikuy. You can read that review here.

At the time I was thinking there are some things that I would do differently if I had the chance. Well I had the chance and here's what I came up with. Keep in mind this is still a work in progress. It is a functioning Expert Advisor, but needs a lot of work before its ready for demo testing. Let's go through some of the design points here.

While setting up some experts for Asirikuy systems, I noticed a daily pattern in the ATR where the 14-bar ATR seemed to bottom out at about 8:00 and peak at 20:00 Alpari server time. Those times are shown in the chart above with the red line at 8AM and the blue line at 20:00. That means that the bulk of the day's movement occurs between the the red line and the blue line with red signaling "wake up" and blue signaling "go to sleep".

So it occurred to me that the expert advisor should activate at 8:00 and make its prediction for the day based on the state of the indicators at that time. The system should predict the change between the day's open at 8:00 and the day's close at 22:00. At 22:00, the expert should activate and train the network to associate the inputs presented at 8:00 with the actual output experienced at 22:00.

What exactly are we trying to predict? From my experience with neural networks, each neuron's output is essentially binary in nature. Put another way, each neuron partitions the input space such that if the inputs fall on on half of the "decision boundary" neuron responds one way, otherwise the neuron responds the opposite way. In other works, and individual neuron can only be expected to answer a question in a Yes or No fashion. You can still train networks to output arbitrary values like 65.7, but you should structure the problem such that values we are trying to predict fall between +1 and -1.

So based on that, I decided to train the network to predict the daily change between 8:00 and 22:00 as a multiple of the ATR. If the price rose by +1 ATR - train the network to produce +1. If prices declined by one half an ATR, I train the network to produce -0.5. Sure price movements can easily exceed +1 or -1 ATR, but the concept seemed solid. After all, markets can only do 2 things, go up or down and by some magnitude, right?

What about input values? Output of course is the easy part since we only need one daily value generally between -1 and +1. The inputs is where the challenge is and any decent technician can up with almost an unlimited number of indicator values. So what did I come up with here?

I decided to stick with the same scheme on inputs as outputs to consider inputs as real numbers in the range of -1 to +1 measured as a percent change in the ATR. Over what period though? Multiple time periods of course. Here was my initial try:

dH1 = (Close[2]-Close[1]) / dATR;
dH2 = (Close[3]-Close[2]) / dATR;
dH3 = (Close[4]-Close[3]) / dATR;
dH4 = (Close[5]-Close[4]) / dATR;
dH5 = (Close[6]-Close[5]) / dATR;
dD1 = (iClose(Symbol(), PERIOD_D1, 1)-iClose(Symbol(), PERIOD_D1, 2)) / dATR;
dD2 = (iClose(Symbol(), PERIOD_D1, 2)-iClose(Symbol(), PERIOD_D1, 3)) / dATR;
dD3 = (iClose(Symbol(), PERIOD_D1, 3)-iClose(Symbol(), PERIOD_D1, 4)) / dATR;
dW1 = (iClose(Symbol(), PERIOD_W1, 1)-iClose(Symbol(), PERIOD_W1, 2)) / dATR;

To summarize, take as input the ATR changes in the last 5 hourly bars plus the last 3 daily bars plus the last 1 weekly bars. If there is a pattern to be detected based on changes in ATR values, that would surely capture it, right?

Here is the network topology on my first try:

9 Input neurons represented by the real number values above
13 hidden neurons based on 1.5 times the number of input neurons
1 output neuron trying to predict the expect output

Seems pretty easy right? Before we get to the expert coding, we have to deal with some implementation challenges, and we will get to those in part 2.

Enjoy your Saturday.

Sunday, January 15, 2012

Meta-Trader - FX-Regression 2012 - Part 2

Welcome back Meta-Traders.

This is the second in my 2 part post about my own Expert Advisor called FX-Regression.

We left off on yesterday’s post with a healthy degree of skepticism about this system. I made several attempts to improve the system, and let’s go through them here.

My first attempt (v1.2) was to increase the “Entry Window.” In the original system, the Expert would enter new positions only within the first hour of the daily close. For example, if the system took a profitable trade by 12:30 (one half hour after the close), the system would submit another set of limit orders. But if the system took closed a trade (win or loss) any time after 1PM, there would be no more trades that day.

I tried increasing the entry window up to as high as 8 hours. What happened is that profit increased, but so did drawdown. The system would take a winning trade or 2, then go into drawdown on the 3rd attempt, and wipe out profits from the other 2 trades.

So this improvement increased profit, but also increased draw down proportionally. So the improvement did not lead to a meaningful improvement it the systems edge. That was FX-Regression v1.2.

Next in v1.3, I tried adding a trend filter. It is clear that a trending market is the enemy of this system which benefits from back-and-forth trend less movement. We also know that with the EUR/USD instance, the system performed better when after a losing trade, we would take the rest of the week off. So there’s evidence that skipping trades can be a good thing. I tried logic that if the close was below last week’s low, or above last week’s high, avoid trading. That had some very positive effects on results particularly USD/CHF and USD/CAD. I eventually took out this improvement.

Next in v1.4, I modified the system such that the TP and SL values would be based on a percent of the daily ATR. After back testing, I found a sweet spot in the optimization with TP at 10% of the daily ATR, and SL at 40% of the ATR. After some investigation, I found that in many cases, the TP value of 10% of the ATR was less than 10 pips which made the problems with back testing simulations even worse or less reliable. Clearly systems with larger take profits (larger than 20 pips) are required to get any degree of reliability from the simulations.

After that, I completed another round of simulations using a 2x1 SL to TP ratio with higher percentage ATR values, 50x25 and 100x50 percent of the ATR. Except this time I used European based pairs EUR/GBP, GBP/CHF and EUR/CHF. Results at 100x50 were profitable, but profit was marginal and drawdown was high. Results at 50x25 were exponentially better, but clearly unrealistic due to issues with the backtester.

So my conclusion is that the system cannot be simulated with great enough accuracy to consider developing it further without a different set of logic. So clearly the system needs a major re-work in logic before it can be taken to the next level. This story didn't end with success, and its back to the drawing board.

I'm going to continue to trade it until I can find a better system or a better set of logic. I'm also going to monitor the system in demo mode on the new pairs EUR/GBP, GBP/CHF and EUR/CHF.

Have a great week.

Saturday, January 14, 2012

Meta-Trader - FX-Regression 2012

Welcome Back Meta-Traders.

In this post, let’s re-examine my own Expert Advisor FX-Regression. Albert Einstein said the definition of insanity is when you keep on doing the same thing over and over again and expecting a different result. So am I insane to keep trading this system? It hasn’t made much money, but it wasn’t wiped out either. So what can we learn from this system and how can we take it to the next level?

First let’s start with the basics. FX-Regression is based on the idea that at the end of the trading day, the price of a currency pair will deviate from its closing value by some amount then return to its closing value before the end of the next trading day a high percentage of the time. How often this condition occurs is of course dependent upon the distance of the deviation. Based on the live parameters I’m using, trades are closed at a profit approximately 75% of the time. That ties into the risk reward ratio as described below.

Here is the logic breakdown:
  • Wait for the daily closing hour - 12 Noon EST for most Europe-based pairs and 4PM EST for USD/CAD.
  • Place a sell order a TP distance above the market and a buy order a TP distance below the market. Wait for either order to fill or when it does cancel the opposite order. If neither order is filled in 6 hours, cancel both orders and wait for tomorrow.
  • Take profit when the price returns to the daily close
  • Stop loss when the price exceeds SL, generally between 2.5 to 3.0 times the Take Profit.
The premise behind this system is that absent economic influences, currency prices will complete at least one high-low rotation in each 12 hour period with approximately 6 hours between each peak and trough. The rotation is more pronounced in a trend less or low volume environment.

I can’t tell you what’s behind the movements, but the timing should be a pretty big clue. If you want to know more, you will have to buy the book “Delta Phenomenon: or The Hidden Order Behind All Markets” by Wells Wilder. The book has a $150 price tag and there is a pretty interesting story behind it, which you can read on the Delta Society web site here.

In my first version of FX-Regression, the TP and SL were calculated in terms of Pips. For EUR/USD and USD/CHF, I used a 20-pip TP and a 55-pip SL. For USD/CAD, I used a 15-pip TP and and a 55-pip SL. For EUR/USD and USD/CHF, note that the stop loss is just under 3 times the TP, so of the 75% winning ratio holds, the system should have a narrow advantage.

How do I arrive at these parameter values? I ran a 10-year backtest on EUR/USD and optimized for TP and SL values and the results are shown on the left. Take Profit pips are on the X or horizontal axis and Stop Loss Pips are on the Y or vertical axis. The solidly profitable areas of the system are in mostly in the lower TP values on the left side of the chart.

Now the first thing Daniel would say about this system is that the back test results are unreliable because the TP values are too small for the back test results to be considered reliable. 10-year optimization of the TP and SL values yields that most of the profitable area of the parameter space is toward the lower parameter sets, with take profits between 20 and 40 pips and solidly profitable for all stop values. This may be an illusion, however due to bar interpolation errors in the back tester.

What about other parameter values? As we go to higher parameter values, the system is still profitable, but trades much less often. Also, profit decreases and draw down increases considerably.

The system also suffers from a lack of reproducibility between back-tests and forward testing. In a prior post, I did a back-test to live test consistency check and found live trades that didn’t appear in the back-test and vice versa. This is another difficulty of systems which use small TP values.

So in the face of these difficulties, I was left with the choice of should I trade this system? I could not prove it was profitable, but neither could I prove it was unprofitable. I tested a lot of other systems that were a complete disaster in back testing, and there was clearly no point in pursuing those systems. So I started live trading at the beginning of 2011 with a small amount of money ($1000) to see what would happen.

Here it is, one year later and the system turned a small profit for 2011, but only as a result of pip rebates. I made several attempts to improve the system in 2011, and we will cover those efforts and the results in tomorrow’s post.

Enjoy your Saturday.

Sunday, January 8, 2012

Meta-Trader - Amachay Review

Welcome back Meta-Traders.

In this post we tackle the Amachay trading system from Asirikuy.

Amachay is a word in Quechua that roughly translates to “protector” and is named after an Asirikuy member named Wilson who gave Daniel the original idea for the system. Wilson means son of William which is a name of Germanic origin that means Will, + Desire + Helmet for protection before going into battle. We would not have this system without Wilson, so if you are reading this, thanks for your efforts!

This system is also significant since it’s the first one developed in combination with input from Asirikuy members, something we will see more of going forward. And as you get to understand the beauty and simplicity of this system, you will appreciate how the community effect in Asirikuy is really starting to bear fruit.

Amachay works against multiple currency pairs using the 4 Hour charts. Other than a Coatl H4 portfolio, this is the only other system on Asirikuy that runs on the 4-hour timeframe.

The logic begins in a way similar to some other systems on Asirikuy: Wait until a certain day and time and look back a certain number of bars and draw a box which is bounded by the highs and lows of the look back period. From that box, a number of levels are derived and shown as follows.

First we start with the black lines which mark the highs and lows experienced over the look back period. Next, the Stop Loss levels (red lines) are calculated which are some percentage of the box height above the box high for short and below the box low for longs. That’s one interesting thing about this system - it doesn’t use the Average True Range, but rather calculates levels based on some percentage of the box size.

Next, 3 price levels are determined which are some percentage of the box size, and those percentages are inputs to the system. Those lines are the blue, green and yellow lines shown above.

In the original entry logic, long positions are entered close to the the bottom of the range, and shorts are entered close the top of the range. There are some other entry conditions that make use of indicator and price bar configuration which ensure that price is not too extended and that position entry is somewhat counter to recent price action. Take profits are some fixed percentage of the box size above the green line for longs and below the green line for shorts.

Position size is calculated to risk a certain percentage of the account based on the distance between trade entry and stop loss. Most of Daniels system risk 1% per trade which is pretty conservative and built to survive a long sequence of losing trades.

One key thing about the system is that the distance from entry to the take-profit is roughly 3 times the distance to the stop loss. That means that the system will take profits at least 3 times as large as its losses. That means that the system has a very favorable risk to reward ratio. The flip side is that the system will lose more often than it wins, but we’ll get to that below.

So the cool thing about this system is that it is based on return to prior price levels so essentially Amachay is a counter-trending system. And since we are dealing 4-hour bars, these are large ranges. Amachay is going for a relatively large Take Profit - say a few hundred pips – with say a 50-60 pip stop loss.

After some testing, Daniel realized that this system – while profitable - could be improved by doing quite the opposite, buying close to the upper part of the range, and selling close to the bottom part of the range. The additional entry conditions still qualify the system as counter-trending on entry, but are essentially looking for breakouts of the range to reach the take-profit. This second logic set is the one posted on and tracked by live accounts on Asirikuy.

What about performance?

The table on the left shows the results of my 10-year back test for the 7 pairs traded by the system. In only one case (EUR/USD) did the returns exceed 10% per year. On per pair-basis, the performance is not that impressive, but when combined into a portfolio, (and using Daniel’s back tests), the system produces an average annual return of 44% with a maximum drawdown of about 25%. That puts it about in the middle range of performance of the other portfolios being tracked here. But it’s clearly within my 30 to 60% per year performance target.

Looking at the 10-year equity bar-chart, the system was profitable in 9 of 10 years (red bars) which is a lot more than we can say for the S&P 500 (orange bars)! Also notice how the system beat the S&P every single year in the past 10-years. It’s a performance like this (and other portfolios on Asirikuy) that shows how Forex would appear to be an excellent way to build wealth over time versus the stock market.

But don’t think that Amachay will be easy to trade. The test showed there was a period with 17 consecutive losing trades. The Win to loss ratio is 36 : 64 so the system loses 2 out of 3 trades on average.

I have had a demo of Amachay running for about a week, and it hasn’t placed any trades yet. Once I get some demo results, I’ll include a link on the right side of the blog, and you can follow along.

Friday, January 6, 2012

Meta-Trader - 2012 Week One

Welcome back Meta-Traders and Happy New Year!

The first week of the new year is typically explosive in the Forex markets and this year started off with a bang! The US Dollar bulls came out strong sending EUR/USD to 12-month lows. As for trading, we had excellent results and it felt good since it was a validation of my mission to deliver the best of Asirikuy to you, my loyal blog readers.

The week started badly on Monday when FX-Regression came out of the gate and landed flat on its face peeling off about 8% and also took down Megadroid Live which runs FX-Regression on USD/CHF and USD/CAD instances.

To put a positive spin on that, these accounts are now running on 5-digit pricing with price improvement from To see the effect, take a look at the account statement and note that trades that were previously closed at take-profits of 15 and 20 pips are now being closed a better rates that take into account slippage in my favor. It may not make much difference in the short term, but for frequently trading accounts such as these, it should make a big difference in the long run. In the Forex market, we'll take every edge available to improve results.

After Monday, it was all upside when the Atipaq (aka Watukushay #5) instance on USD/CHF had 3 consecutive daily winners which is a pretty rare. On top of that, I run this instance on 3 different accounts (Atipaq Full, Atinalla #3 and Atinalla Custom) so this trade was replicated across 3 accounts. That's about as good as it gets in automated forex trading and these 3 accounts gained 5% or better for the week. Particularly striking was Atinalla #3 which gained over 11% for the week and spiked out to a new 11-month high.

We need to keep these returns in perspective and we certainly don't expect this to happen every week. And we can always give it all back next week, its the nature of the business.

COATL H1, which went live late last year had a formative week in that it flushed out a bunch of postions taking about a 1.5% profit, then taking on a bunch of postions that went into drawdown.

I started Sunqu on Thursday and it went long EUR/USD and immediately went into drawdown. Too bad I didn't start it earlier in the week when it was short EUR/USD since it would have captured a good part of the downside move.

That's all for now, check back tomorrow for a review of the Amachay trading system.