Trader’s Tech – Writing Your Own EA Part 27 – Testing the EA

If you’re new to this series and want to check it out from the beginning, you can find that here. And look here for a list of all the programming articles.

In Part 26 we talked some about the MT4 Strategy Tester. Today we’ll run the strategy tester and see what results we get from our new EA.

So, if you haven’t already done so, click the Start button in the lower right corner of the Strategy Tester (ST) window. You will see a chart come up and candles start running across the screen. You may notice that it’s sort of slow. The slider to the right of the Visual mode checkbox controls the speed the candles appear on the chart. One of the several drawbacks of this tester is, when you move the speed slider to the very right, the last two positions, labeled 31 and 32, should be labeled “annoying slow” and “screaming fast”. A couple of speeds in between those would have been very nice.

As I mentioned in the last installment, I had you uncheck the Use date checkbox so the EA would use whatever data you already have available for this test. We don’t really care about the quality of the data at this point, only that there is some data that we can run the EA against.

When the test is complete, you’ll get the wonderful squeaky toy sound. My dog (the guys in the trading room call him Buddy the Pip dog) would jump out of the chair ready to play. Buddy is frequently disappointed. But I digress. When you hear the squeaky toy sound, you know your test is complete. You will also notice the additional tabs that appear on the bottom of the ST window.

Click on the Results tab at the bottom of the ST window to see what trades were entered by the EA. As you can see, I only got one trade with no SL or TP. Since the EA will only put a trade on if there are no other trades on, and this one had no SL or TP, then it makes sense that there was only one. Once it was entered, it never closed until the end of the test, so the EA would never have put another one on. So the problem here is, why no SL or TP? To get some understanding, click on the Journal tab.

Well, here’s our problem. You see our buy order was entered at 1.30085 OK, but then the OrderModify function generated an invalid stoploss error. What that means is, for some reason the SL we fed to the OrderModify() function was an invalid number. This could mean many things, since we didn’t do extensive checking of our numbers before we passed them to the built-in MQL4 functions. I’ll short circuit this just a little bit for now so we can look at other things. I made a couple more mistakes (*gasp*) in the code. The first was just a stupid, thoughtless error. I used the keyword Digits instead of Point to convert points to price in the OpenTrade() function. The other one is just a lazy error (I know, no one actually believes that I could be lazy. I usually say I’m ambitiously lazy.) This is one I make frequently, because, for some reason, I can never remember whether I should multiply or divide the Pips by Point to get Price. If I don’t check it, invariably I choose the wrong one. Such is the life of a programmer. I decided not to edit everything to make myself look better. This is real life, folks. 😀

Here is the corrected OpenTrade() function with the corrected lines highlighted:

If you have a “5-digit” broker, you will have to make another change to get a valid test. Remember in our external variables, we have TPPoints and SLPoints? If you have a 5 digit broker, you will have to change them. You can do it in either of two places; change the lines in the code or change them with the Expert properties button in the ST. I usually like to be able to test with the default numbers, so I would change them in the code. To do that, find the lines in your code (near the top) and change them thusly:

extern int SLPoints=150;
extern int TPPoints=250;

That way, the values will represent pips (point * 10.) Running the test once again should yield the correct results.

And with that, we’ll end this installment. Tomorrow we’ll go more into interpreting the results of our EA testing. Thanks for your attention and please follow me on Twitter and LinkedIn.

Hi Ebuka,
Sorry for the delay in responding. Somehow I missed your comment. The EA we’re talking about is the one we are writing in this series. It’s a simple moving average crossover, so it’s nothing fancy. You can see the series here if you would like more information. The source code for the EA appears in several places.

Disclaimer: Trading forex on margin carries a high level of risk, and may not be suitable for all investors. The high degree of leverage can work against you as well as for you. Before deciding to invest in foreign exchange you should carefully consider your investment objectives, level of experience, and risk appetite. The possibility exists that you could sustain a loss of some or all of your initial investment and therefore you should not invest money that you cannot afford to lose. You should be aware of all the risks associated with foreign exchange trading, and seek advice from an independent financial advisor if you have any doubts.