Tuesday, May 30, 2006

Strategy Testing in MetaTrader 4

I am sure anyone who is reading this document knows MetaTrader has a feature which allows you to back test your strategies. This is called the Strategy Tester, it can be accessed via the view menu or by the keyboard shortcut CTRL+R. Below is a screenshot showing the Strategy Tester interface:

Interface

This article is focused on improving the reliability of this tool. It does not however go into any detail about optimization. Firstly we will run a back test using a simple Expert Advisor with the standard default settings in MetaTrader the data centre being used is the InterBankFX data server. We will then import Alpari data and compare the results.

Standard Data

For the first test we set up with the Currency to be tested the AUD/USD. The period we use is daily and the model we use is every tick (based on all available least timeframes with fractal interpolation of every tick). The time period we will be testing is from 15/06/2004 to 28/05/2006. We use the simple MACD Expert adviser. The following summarizes the results obtained with the demo data:

Strategy Tester Report MACD Sample

Bars in test

559

Ticks modelled

4263759

Modelling quality

42.15%

Initial deposit

1000.00





Total net profit

470.00

Gross profit

635.00

Gross loss

-165.00

Profit factor

3.85

Expected payoff

26.11



Absolute drawdown

0.00

Maximal drawdown (%)

165.00 (11.1%)



Total trades

18

Short positions (won %)

12 (100.00%)

Long positions (won %)

6 (83.33%)


Profit trades (% of total)

17 (94.44%)

Loss trades (% of total)

1 (5.56%)

Largest

profit trade

50.00

loss trade

-165.00

Average

profit trade

37.35

loss trade

-165.00

Maximum

consecutive wins (profit in money)

13 (481.00)

consecutive losses (loss in money)

1 (-165.00)

Maximal

consecutive profit (count of wins)

481.00 (13)

consecutive loss (count of losses)

-165.00 (1)

Average

consecutive wins

9

consecutive losses

1



Equity Curve

Improving Modeling Quality

As we can see from our test the modeling quality was 42.15%. I for one would want to have a much better modeling quality then 42.15%. The one minute data we have from our demo feed has several gaps in it for one reason or another. These gaps reduce the quality of our modeling as the strategy tester will interpolate the areas between these gaps which may or may not trigger invalid buy or sell signals.

Gap filling (the free way)

If you want to improve modeling quality without spending vast sums of money you have come to the right place. A great free source of 1minute data for MetaTrader is alpari’s databank. Most currencies are available as well as Gold and various CFD’s.

Step By Step Setup Guide

Step One

The first step is to download the 1M currency data for the desired currency pair. You can access this data at http://www.alpari-idc.com/en/dc/databank.php. Or via the direct links below:

AUDUSD

CHFJPY

EURAUD

EURCAD

EURCHF

EURGBP

EURJPY

EURUSD

GBPCHF

GBPJPY

GBPUSD

NZDUSD

USDCAD

USDCHF

USDDKK

USDJPY

USDNOK

USDSEK

USDSGD

USDZAR

These files will be in a zip format. The first thing you will need to do is un-zip the file. You should now have a .hst file.

Step Two
We now need to prepare MetaTrader for this new data. We change the default max bars in history, in order for our modeling to be more reliable. To do this we go to the tools menu then options or use the keyboard shortcut CTRL+O. The following toolbox should popup:


Click charts tab, now in the charts tab in a field called Max bars in history change that field to 2147483647. (This is the maximum value MetaTrader will accept).


Step Three

Now we are ready to import our data. To do this we go to History Centre. This is in the tools menu or can be accessed via the keyboard shortcut F2, the History Centre screen will pop up as shown below:


Double click on the Forex button shown above this will cause the currency symbols to dropdown. The next step is to double click on the currency you have downloaded the data for. This will open a drop down menu with the different timeframes. Double click on the 1 minute timeframe. Then click on import button and then browse button to locate your .hst file then click OK (Note: If you don’t see your .hst files you need to click the file types dropdown menu in the open dialog box and select MetaQuotes files(*.hst) ). You can now close the History Centre window.

Step Four
Now we have our data imported we need to open it as an offline chart. To do this we go to the file menu and click open offline. This will open a new window click on the one minute timeframe for the currency and then click open. This will then open a new chart as shown below.
One Minute AUD/USDStep Five
The next step is to convert the period using period converter script. To do this with the offline chart shown above, Double click on the period converter script. This will bring up a new window like the one shown below:
Period conversion

You need to click the inputs tab shown above and then in the values field insert the number of minutes you wish to convert the data to, and then click Ok. Do this for the following values 5, 15, 30, 240 and 1440.

Note: There will be no visible change to chart window, however in the log file of the terminal window you can confirm that the records have been successfully written. When you run the period converter script for the second and subsequent times a dialog box will appear asking if you want to replace the current period converter script you need to click yes to convert the next timeframe, make sure the previous period converter script has already finished.

Step Six

Congratulations you got this far preferably without tearing your hair out. Now we have reached our final step testing our new data. We rerun Strategy Tester with our original settings (Make sure the recalculate textbox is highlighted or you will just be using old data). Now sit back with your beverage of choice as Strategy Tester go to work. Remember to always make sure the model you use is every tick (based on all available least timeframes with fractal interpolation of every tick).

Step Seven
Repeat steps 1 to 6 for every currency you wish to test.

Conclusions

As a point of comparison I ran strategy tester with exactly the same settings with Demo data as shown page 2 and the Alpri data shown on the following page. As you can see there is a 30% increase in modeling quality with the Alpri data, this is still only 72.4% modeling quality. This of course is not optimal however it is better then the alternative. In dollar terms using the Alpari data a historical profit of USD$385 was recorded. In contrast using the demo data a profit of USD$470 was recorded. This may seem like a small amount however if the tests were extended to longer timeframes it would be much more significant. A final note back testing is better then no testing however it should be used with caution even with 99.999% model quality and a profitable EA it is no guarantee that your strategy will perform well LIVE. It is always a good idea to forward test with a mini account.

Strategy Tester Report MACD Sample (Alpari)

Bars in test

511

Ticks modelled

994306

Modelling quality

72.41%


Initial deposit

1000.00





Total net profit

387.00

Gross profit

583.00

Gross loss

-196.00

Profit factor

2.97

Expected payoff

21.50



Absolute drawdown

0.00

Maximal drawdown (%)

196.00 (13.5%)




Total trades

18

Short positions (won %)

10 (100.00%)

Long positions (won %)

8 (87.50%)


Profit trades (% of total)

17 (94.44%)

Loss trades (% of total)

1 (5.56%)

Largest

profit trade

50.00

loss trade

-196.00

Average

profit trade

34.29

loss trade

-196.00

Maximum

consecutive wins (profit in money)

13 (452.00)

consecutive losses (loss in money)

1 (-196.00)

Maximal

consecutive profit (count of wins)

452.00 (13)

consecutive loss (count of losses)

-196.00 (1)

Average

consecutive wins

9

consecutive losses

1




Equity curve Alpari

Disclaimer: Forex trading is risky. The high degree of volatility within the Forex market largely within the intraday market, coupled with ability to leverage your positions means that losses can be greater then your initial investment. This article is for educational purposes only and does not constitute financial advice.


5 comments:

Anonymous said...

Found your link at www. StrategyBuilderFX.com

Very informative, thank you

MetaQuotes Software said...

You may probably be interested in the following: MetaQuotes Software organize the unique Automated Trading Championship 2006. Expert Advisors sent in by traders will compete for the prize of US$80 000.

You can find more details on the Championship website at:
http://championship.mql4.com

Jovan said...

Find you're site on one of the forums. It is very professional and informative - great work. Would you mind giving me an opinion (or advice) on my site - I've just started.

www.margin-strategies.info

All the best

J.

Mack Dodgey said...

Sure will email me. mackdodgey at gmail.com

Freddie Sirmans said...

Just browsing the internet, you have a very, very interesting blog.