I have decided to write an article on this topic. It's on my blog also: Step by Step Guide to Optimizing an Expert Advisor! : verbtheory trading blog
I hope you find this helpful, enjoy!
Ever wondered how to optimize an EA?
There certainly are a lot of brains on this planet and I'm sure there are better ways that others have devised but I havent found them readily available or they are not willing to share so...
Here's how I go about the basics of optimizing, backtesting and why.
In general, the idea is to get the profit maximum up. Next step is to optimize trade management variables like breakeven, targets and trailing to increase profit factor while reducing drawdown.
We will utilize control points to get a rough idea of optimization then use every tick to test our results from control point testing. This will enable us to get faster results as every tick modelling is VERY slow.
Make sure your expert is loaded into your metatrader/experts folder.
Make sure any indicators that the EA uses are in your metatrader/experts/indicators folder.
Goto Tools -> History Center and double click the symbol you want to test. Double click on M1 and download the data. Hit Ok at any prompts and recalculate. Double-click on each of the periods and it will recalculate all of them. Important step. This way you will have all of the data you need to test. It's a good idea to go into your metatrader folder and delete any .HST files you may have before doing this to ensure your data quality is acceptable.
Open up a chart and drag the EA from the navigator in metatrader onto the chart.
Hit F6, this will bring you to the strategy tester and it will look like this:
Choose the correct period for your EA and the symbol you wish to test.
Choose a date range to test with. Start with a small date range at first. It is much easier to optimize a small range to get an idea of how the EA could react in a bigger date range. It will help the optmization process move much faster. Start with 6 months then expand to one year, two years and so on in subsequent runs.
Select the "Expert properties" button and proceed to optimization steps.
Let's start with maximizing our profit!
Using default entry parameters, take off all trade management like breakeven, first targets, etc. Turn them all off.
Select Stop Loss and Take Profit
Enter basic value for stop loss and take profit then increment them by 5 all the way up to say 1000.
Example below, notice how you check the box then enter start (will start testing at), step (how much to increment by) then Stop (test up to)
Make sure to choose the Optimization tab and check mark the Profit box. That way it will test for maximum profit.
Additionally switch over to the Testing tab and enter your starting balance. Depending on the trading strategy this will be critical. I usually start with 10,000 in the balance. Hit OK.
Check the "Optimization" check box.
Hit the start button. It should look like the picture below. Notice how many passes it's going to test... lol. Might as well go get some lunch this is going to take awhile.
As the tester progresses through each pass, it will report back any profitable passes in the "Optimization Graph" and "Optimization Results" tabs. What I do is use the graph (right click in the graph and deselect 2D surface) to watch the optimization process and wait for the results. Looking at the graph helps me see the highest profit gains first then switch to the "Optimization Results" and sort by profit to see progress.
Here is an example:
3:1 reward to risk???? drooooool.
The tops represent the highest profit gained, then I switch to results and sort by Profit:
Once all of the passes have completed, I can examine the tester results that look interesting to me. By right clicking and on the result I want, I can now set that as the input parameters to test the with.
Ooooo... 1.31 profit factor with 9.34% drawdown! :P
Now it's time to test these crude results with 90% modelling quality and see how they react.
Go back out to the settings tab and uncheck the "Optimization" box. Select the "Model" dropdown box and choose "Every tick..". This will show us an accurate picture of how the EA will react to historical market conditions. By downloading the History of the symbol to be traded, the quality will be about 90%. Of course, data feeds are all over the place so this is really just a best guess.
Usually in the beginning of testing, I like to watch the trades as they progress so I check mark the "Visual mode" box and adjust the slider for the speed at which the chart will scroll. This way I can get an even better feel for how things might react.
Hit start and there you have it! A new chart will appear and the trades will get logged for you to watch and ponder how to reduce drawdown and maximize profit.
After watching the trades you get a sense for how and where you might take profit, set targets or even start trailing. You can even pause the test in real time by hitting the pause button and measure things out to devise some trade management strategies. Here is an example:
Reducing Drawdown & Increasing Profit Factor
Since there are so many combinations that are possible when it comes to trade management, it would be better to throw these things into the tester and have it optimize for us.
Simply follow the steps above for optimizing stop loss and take profit BUT only do it for the trailing stop, breakeven and targets.
Select control points for modelling quality again.
Check mark the "Optimization" box and hit start.
Analyze the results, pick the best one and set that as the input.
Test using "Every tick" modelling and check out the results.
Rinse & repeat until you have acceptable settings!
Now remember there are many trading strategies out there. For example, basket trading cannot be fully automated in backtesting on Metatrader but you can test individual pairs and accumulate their results then run it in excel. Just a caveat for you.
I'm going to end it here as this has been long enough of a read. I hope you have found this helpful and definitely know this isnt the A-Z guide but should get a majority of people ready to optimize and backtest!
Additionally, there are many great resources out there on backtesting that I'm sure folks will tag onto. I'll add them here on the first post but here's a good one to start with: MQL4 Articles: strategy tester.
Make sure to set maximum profit to 999999 or else you will limit the amount of profit it will optimize for lol. See below.
Enjoy and Ciao for Now!