Sunday, February 5, 2012

Meta-Trader - Neural Nets - Part 3

Welcome back Meta-Traders. Here is part 3 of my series on efforts to build an expert advisor in MetaTrader that uses Neural Network technology.

When we last left off, I was building and training networks using FannUtil.exe, an EXE compiled using Microsoft Visual Studio 2008. The next challenge was incorporating it into MetaTrader.

My plan was to output the training data to a file in the format that FANN accepts and call the FannUtil EXE from MT4. Finding the Windows API to launch the program was easy enough, but I soon found out that it will not call Windows console programs. So I recoded FannUtil to a new program called FannWin.exe that did exactly the same thing as FannUtil, except was a Windows 32 binary.

Next up was an issue with file paths. Meta-Trader only allows you to write output to the .\experts\files sub-folder. So I had to resort to the Windows low-level file API’s to output to other locations. Next complication was the fact that everything that a program outputs under “C:\Program Files” gets redirected to “%appdata%\VirtualStore\Program Files” which is a feature in Windows Vista and Windows 7. I finally resorted to sticking everything under c:\windows\temp, not very portable, but at least it was easy to find.

Next step was to start calling FannWinn to train and execute the network. After a few tries at this, I realized that the back-tester was launching FannWin.exe before the prior instance of FannWin.exe had finished. I had multiple instances of FannWin in memory. Once I started thinking of ways for MQL to “block” waiting for FannWin to finish, I knew I was headed in the wrong direction. It was also a reminder that launching a Windows EXE requires many times more computer overhead than calling a Windows DLL function. It might work during expert execution, but would never work inside the backtester.

Why not just write a DLL to do all the Neural Network stuff and call the DLL from MQL4 like Daniel did with Sunqu? I could have gone this direction, but didn’t want to have to re-compile the DLL for every change in network topology, such as number input nodes, etc, etc.

So I went back to the Internet and quickly stumbled upon Fann2MQL here: (http://fann2mql.wordpress.com/http://fann2mql.wordpress.com/) which allows you to call the FANN APIs Functions directly from MQL4. After some trial and error with that, I was up and running and had my network training and executing. Along the way, I used a separate program called FannTool.exe to do some troubleshooting and test the networks created by my expert.

After further testing, I decided to re-work the original problem stated in my first post in the series as follows:

dH1 = (iClose(Symbol(), PERIOD_D1, 1)-iOpen(Symbol(), PERIOD_H1, 1)) / dMorningATR;
dD1 = (iClose(Symbol(), PERIOD_D1, 1)-iClose(Symbol(), PERIOD_D1, 2)) / dMorningATR;
dD2 = (iClose(Symbol(), PERIOD_D1, 2)-iClose(Symbol(), PERIOD_D1, 3)) / dMorningATR;
dD3 = (iClose(Symbol(), PERIOD_D1, 3)-iClose(Symbol(), PERIOD_D1, 4)) / dMorningATR;
dW1 = (iClose(Symbol(), PERIOD_W1, 1)-iClose(Symbol(), PERIOD_W1, 2)) / dMorningATR;

Instead of feeding it the last 3 hourly bar changes before the days open, feed it just the difference between yesterday’s close and the morning open. This should be a more indicative value of potential price action for the following day. To put it in words, the inputs are:

  • Change between yesterday’s close and morning open as % of ATR
  • Change between 1 day ago close and 2 day ago close as % of ATR
  • Change between 2 day ago close and 3 day ago close as % of ATR
  • Change between 3 day ago close and 4 day ago close as % of ATR
  • Change between prior week’s close and 2 weeks prior as % of ATR



Network output (and what we are trying to predict) is the daily change between the day open at 8:00 GMT and the day close at 21:00 GMT and will range generally between -1 and +1. Note that in all cases we are using the ATR calculated in the morning at the starting hour since it will likely be different at the end of the day.

Before we get to the results, there are further parameters to consider such as:

- How many days input should I train the network with?

- What is the optimal stop loss value as a % of the ATR?

- What is the optimal threshold for entering new positions in terms of the network prediction?

Let’s take these one at a time.

As for lookback period, I tested 10-years, 90 days, 60 days and 30 days. What I found was that the shortest lookback time period produced the network with the lowest error rate and the most profitable results. The results were not great, but we will get to that later. This was a bit counter-intuitive, because you would think that more data would produce a better results, but it was the opposite and it brings up the perspective that the network isn't really learning actual associations in the data. Its just learning the tendency in the data over the test period and it's predictions change over time based on an underlying trend or bias in the data.

As for stop loss, I tried 0.5 ATR, 1 ATR and 2 ATR. I decided to settle on 1 ATR since 0.5 seemed to get hit too often, and 2 ATR seemed to big a loss to accept.

As for Threshold to take positions, I added 2 properties to the EA, Long Threshold and Short Threshold. I take long positions of the prediction is greater than the Log Threshold and close out longs and go short if the prediction is less than the Short Threshold.

As for values, for the threshold, I tried as low as 0.1 and as high as 0.5. I also coded an option to calculate the 30-day average of the ATR change between 8:00 and 21:00 and take positions if the prediction exceeds the Average Change. I haven't go the full results of those tests yet.

What about performance?

The table on the left shows a 10-year year-by year test using a Threshold of 0.1 for longs and -0.l for shorts. There were some good years, but also some disastrous ones. In all cases the Drawdown exceeds the profits, and given the size of the profits versus the draw downs, it doesn't seem like the network has an edge in predicting future prices.

I also did 10-year back-tests of the system on USD/CAD and USD/CHF and found the results to be about break-even with draw down as high as 50%. So the fact that showed any profit at all on EUR/USD is because that pair tends to have very pronounced trends.

In conclusion, we do not appear to have a profitable system on our hands, and much more work needs to be done.

Check back later and have a great week.

No comments:

Post a Comment