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

Forex RobotIf 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.

Trader's Tech - EA Testing 1Click 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.

 

 

 

Trader's Tech - EA Testing 2

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. :D

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

 

void OpenTrade(int TradeDir)
   {
   double TP,SL,TradeSize;
   int Ticket;
   bool ModifyResult;

   TradeSize = LotSize;  //We don't have to calculate the TradeSize 
                    //since it's in the external variables

   if(TradeDir == 1)  //Long Trade
      {
      TP = Bid + (TPPoints * Point);
      SL = Bid  - (SLPoints * Point);
      while(IsTradeContextBusy())  //our loop for the busy trade context
         Sleep(100);  //sleep for 100 ms and test the trade context again
      RefreshRates();  //refreshing all the variables when the 
                      //trade context is no longer busy
      Print("Long Trade Price=",Ask,", SL=",SL,", TP=",TP,", Size=",TradeSize);
      Ticket = OrderSend(Symbol(),OP_BUY,NormalizeDouble(TradeSize,2),
                        NormalizeDouble(Ask,Digits),2.0,0.0,0.0,"Trade Comment",
                        MagicNumber,Blue);
      if(Ticket >= 0)
         {
         while(IsTradeContextBusy())
            Sleep(100);
         RefreshRates();
         OrderSelect(Ticket,SELECT_BY_TICKET);
         ModifyResult = OrderModify(Ticket,OrderOpenPrice(),
                                    NormalizeDouble(SL,Digits),
                                    NormalizeDouble(TP,Digits),0,Blue);
         if(!ModifyResult)
            Alert("Stop Loss and Take Profit not set on order ",Ticket);
         }  //if(Ticket >= 0)
      else
         {
         Alert("Trade Not Entered");
         }  //else
      }  //if(TradeDir == 1)
   if(TradeDir == -1)
      {
      TP = Ask - (TPPoints * Point);
      SL = Ask + (SLPoints * Point);
      while(IsTradeContextBusy())  //our loop for the busy trade context
         Sleep(100);  //sleep for 100 ms and test the trade context again
      RefreshRates();  //refreshing all the variables when the 
                      //trade context is no longer busy
      Print("Short Trade Price=",Bid,", SL=",SL,", TP=",TP,", Size=",TradeSize);
      Ticket = OrderSend(Symbol(),OP_SELL,NormalizeDouble(TradeSize,2),
                        NormalizeDouble(Bid,Digits),2.0,0.0,0.0,"Trade Comment",
                        MagicNumber,Blue);
      if(Ticket >= 0)
         {
         while(IsTradeContextBusy())
            Sleep(100);
         RefreshRates();
         OrderSelect(Ticket,SELECT_BY_TICKET);
         ModifyResult = OrderModify(Ticket,OrderOpenPrice(),
                                    NormalizeDouble(SL,Digits),
                                    NormalizeDouble(TP,Digits),0,Blue);
         if(!ModifyResult)
            Alert("Stop Loss and Take Profit not set on order ",Ticket);
         }  //if(Ticket >= 0)
      else
         {
         Alert("Trade Not Entered");
         }  //else
      }  //if(TradeDir == -1)
   }  //void OpenTrade()

In case you didn’t get it, the four lines that have been changed were:

      TP = Ask - (TPPoints * Point);
      SL = Ask + (SLPoints * Point);

      TP = Ask - (TPPoints * Point);
      SL = Ask + (SLPoints * Point);

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.

Tim

 

here_for_you_001

1359774937_facebook 1359774984_linkedin 1359774949_twitter 1359774960_google 1359774973_rss 1360027635_youtube

Connect_With_Winners_Edge

Get trade set ups everyday!

The following two tabs change content below.

Latest posts by Tim Black (see all)

Follow our social media:
1359774937_facebook 1359774984_linkedin 1359774949_twitter 1359774960_google 1359774973_rss 1360027635_youtube

Winner’s Edge Trading, as seen on:

Winner's Edge Trading in the news

  • http://twitter.com/IsItCoffeeYet Tim Black

    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.

    Tim

  • Ebuka

    Please what EA are you talking about? Can I get it to test too?