Introduction
This thread could be of interest for those of you, who want to start or have already started developing their own custom Expert Advisor. This is not an "MetaTrader 4 MQL Expert Advisor Tutorial" or something like that, but could be of help for beginners, prevents from making some mistakes and provide working code for some of the most common functions.
A few months ago I have written my first, very simple EA just for learning purposes and I have posted it in this thread. I tried to keep it as simple as possible and have provided many comments to make it easier for beginners to follow the strategy of my EA and how I implemented it.
During the last weeks I read a lot about chart analysis, intermarket analysis and price forecasting with neural networks. Beside that, I have developed some libraries that contain often needed functionality like moneymanagement, ordermanagement, error handling and error logging. And I also learned a lot about backtesting, validation and curve fitting problems.
Common backtesting mistake I did in the beginning
And that's already the point that wasn't really smart with my last approach:
I have optimized the EA for a given period and then backtested the EA for the same period. Don't do that!
And if you do - don't take this results and believe that you have found the ultimate Holy-Grail-EA
We want to develop an EA that does not only create nice equity curves for our optimized period ("curve fitting"), but also for the future.
Better approach
Image we want to write an EA that uses the daily chart as timeframe and we have the following time series data available:
|--- 2003 ---|--- 2004 ---|--- 2005 ---|--- 2006 ---|--- 2007 ---|--- 2008 ---|
We now do backtesting and optimization of the EA. That means we determine those moving averages, risk%, trailing stop size (or whatever variables we use in our EA) that results in the highest balance (lowest drawdown, expected payoff...).
If we do this for the whole period 2003 to 2008, we can get a very nice curve, but we have no idea how the EA would perform in 2009. A possible solution good be the that we devide the time series data into two parts: one for optimization and one part for validation/testing:
|--- 2003 ---|--- 2004 ---|--- 2005 ---|--- 2006 ---|--- 2007 ---|--- 2008 ---|
|
|-------------- OPTIMIZE -------------|--- TEST ---|
Now we would an optimization for 2003 - 2005 and use this values for testing in 2006. If we still get good results, then our EA would have been successful in the future, though we tested it for the past. Because the EA has not been optimized for the year 2006.
We can repeat this for the next years and try to find those settings, that produced good results for all or most periods.
I don't know which period you should choose for optimization and which for testing. This is trial and error and also depends on the timeframe you choose to let your EA work on.
Getting better modelling quality
The more data you have, the better (means more exactly) the backtesting results will be. Here's a way of how to improve the modelling quality:
In the options menu of MetaTrader 4 (press Ctrl+O) select the tab "Charts" and set this values for "Max bars in history" and "Max bars in chart": 2147483647 - click ok.
Next, open the History Center in MT4 (press F2), select the currency pair in the symbol list on the left and select the lowest timeframe, 1 minute (double click it). Press "Download" and answer "OK" when you get the warning.
It can take a few minutes until all historic value have been downloaded. I have ~3.5 Mio 1-Minute candles, that's the last 10 years in minutes! Should be enough...
You can do this with every timeframe and currency, or use the included script "period_converter", that comes with MT4.
Here's a more detailed explanation: http://www.earnforex.com/blog/2008/0...storical-data/
This thread could be of interest for those of you, who want to start or have already started developing their own custom Expert Advisor. This is not an "MetaTrader 4 MQL Expert Advisor Tutorial" or something like that, but could be of help for beginners, prevents from making some mistakes and provide working code for some of the most common functions.
A few months ago I have written my first, very simple EA just for learning purposes and I have posted it in this thread. I tried to keep it as simple as possible and have provided many comments to make it easier for beginners to follow the strategy of my EA and how I implemented it.
During the last weeks I read a lot about chart analysis, intermarket analysis and price forecasting with neural networks. Beside that, I have developed some libraries that contain often needed functionality like moneymanagement, ordermanagement, error handling and error logging. And I also learned a lot about backtesting, validation and curve fitting problems.
Common backtesting mistake I did in the beginning
And that's already the point that wasn't really smart with my last approach:
I have optimized the EA for a given period and then backtested the EA for the same period. Don't do that!
And if you do - don't take this results and believe that you have found the ultimate Holy-Grail-EA
We want to develop an EA that does not only create nice equity curves for our optimized period ("curve fitting"), but also for the future.
Better approach
Image we want to write an EA that uses the daily chart as timeframe and we have the following time series data available:
|--- 2003 ---|--- 2004 ---|--- 2005 ---|--- 2006 ---|--- 2007 ---|--- 2008 ---|
We now do backtesting and optimization of the EA. That means we determine those moving averages, risk%, trailing stop size (or whatever variables we use in our EA) that results in the highest balance (lowest drawdown, expected payoff...).
If we do this for the whole period 2003 to 2008, we can get a very nice curve, but we have no idea how the EA would perform in 2009. A possible solution good be the that we devide the time series data into two parts: one for optimization and one part for validation/testing:
|--- 2003 ---|--- 2004 ---|--- 2005 ---|--- 2006 ---|--- 2007 ---|--- 2008 ---|
|
|-------------- OPTIMIZE -------------|--- TEST ---|
Now we would an optimization for 2003 - 2005 and use this values for testing in 2006. If we still get good results, then our EA would have been successful in the future, though we tested it for the past. Because the EA has not been optimized for the year 2006.
We can repeat this for the next years and try to find those settings, that produced good results for all or most periods.
I don't know which period you should choose for optimization and which for testing. This is trial and error and also depends on the timeframe you choose to let your EA work on.
Getting better modelling quality
The more data you have, the better (means more exactly) the backtesting results will be. Here's a way of how to improve the modelling quality:
In the options menu of MetaTrader 4 (press Ctrl+O) select the tab "Charts" and set this values for "Max bars in history" and "Max bars in chart": 2147483647 - click ok.
Next, open the History Center in MT4 (press F2), select the currency pair in the symbol list on the left and select the lowest timeframe, 1 minute (double click it). Press "Download" and answer "OK" when you get the warning.
It can take a few minutes until all historic value have been downloaded. I have ~3.5 Mio 1-Minute candles, that's the last 10 years in minutes! Should be enough...
You can do this with every timeframe and currency, or use the included script "period_converter", that comes with MT4.
Here's a more detailed explanation: http://www.earnforex.com/blog/2008/0...storical-data/