Trader’s Tech – Writing Your Own EA Part 30 – Reusable Code

Forex RobotFriday again. Hard to believe that we’re back here. Well, it’s another beautiful day here.

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 29, we made some code modifications to allow for 4- or 5- digit brokers. In this installment I’m going to digress slightly and talk about reusable code. I’m also going to give away one (maybe more) of my super-secret, members only, deep, dark, enigmatic, most classified, did I say secret? reusable functions.  It’ll be epic. :) Remember, don’t tell anyone about it. I think I may have mentioned that it’s a secret.

Reusable Code

Sort of sounds like code that you write once and use over and over and over, doesn’t it? Well, that would be exactly right. If you’ve got a function that you use a lot (I have many), you can write it, make it kind of generic (allowing the user to send settings to it via parameters as well as including a mechanism for returning data) and put it in an include file to add to future EA projects. Include files are files containing code segments (a partial function, a full function or several full functions) in a file with a file extension of .mqh and usually residing in the experts/include folder under your MT4 installation. Using the #include command causes the compiler pre-processor to include the lines within the file to be inserted into the program at that specific location before compilation takes place:

#include <MAXover.mqh>

This will include the lines of the file MAXover.mqh to be included in the EA. The corner brackets (<>) indicate the pre-processor should look for the file in the include folder.  Well, that’s the one. MAXover stands for Moving Average Xover (Crossover.) It’s a generic include function that I wrote to detect moving average crossovers. Here’s the code:

 

//+------------------------------------------------------------------+
//|                                                      MAXover.mq4 |
//|                      Copyright © 2010, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, T Black and Associates"
#property link      "http://www.winnersedgetrading.com" 

//MAXover() - return -1 for bearish cross, 1 for bullish cross, 0 for no cross
//MAtype 0=sma, 1=ema
//MAPer1 first moving average Period
//MAPer2 second moving average Period
//LookBack how many candles to look back for another cross
//MultiXMaxX how many crosses are acceptable in the look back

int MAXover(int MAType,int MAPer1,int MAPer2,int LookBack,int MultiXMaxX)
   {
   int  XOver=0;      //-1=bearish cross, 0=no cross, 1=bullish cross
   double MA1_CurrVal,MA2_CurrVal;
   double MA1_PrevVal,MA2_PrevVal;
   double MA1_LookBackVal,MA2_LookBackVal;
   int  CurrLoc=0;   //-1 = below both, 0=between, 1 = above both
   int  PrevLoc=0;
   int  LookBackLoc=0;
   int  i;
   int XCount;

   //assign the vars
   MA1_CurrVal = iMA(NULL,0,MAPer1,0,MAType,PRICE_CLOSE,1); //most recently closed candle
   MA2_CurrVal = iMA(NULL,0,MAPer2,0,MAType,PRICE_CLOSE,1);
   MA1_PrevVal = iMA(NULL,0,MAPer1,0,MAType,PRICE_CLOSE,2); //1st prior to most recently closed candle
   MA2_PrevVal = iMA(NULL,0,MAPer2,0,MAType,PRICE_CLOSE,2);

   //are we above?
   if(MA1_CurrVal > MA2_CurrVal) //yes we're above
      CurrLoc=1;
   else if(MA1_CurrVal < MA2_CurrVal) //No, below       
      CurrLoc=-1;    
   else       
      CurrLoc=0;                    
   //Were we previously above    
   if(MA1_PrevVal > MA2_PrevVal) //yes we're above
      PrevLoc=1;
   else if(MA1_PrevVal < MA2_PrevVal) //no, below       
      PrevLoc=-1;    
   else       
      PrevLoc=0;             
   //cross?    
   if(PrevLoc == -1 && CurrLoc == 1)       
      XOver = 1;    
   else if(PrevLoc == 1 && CurrLoc == -1)       
      XOver = -1;    
   else       
      XOver = 0;           
   if(XOver != 0 && LookBack > 0) //check the lookbacks for no XOver - MA must be on same side for last "lookback" bars
      {
      XCount=0;
      for(i=3;i MA2_LookBackVal)
            LookBackLoc = 1;
         else if(MA1_LookBackVal < MA2_LookBackVal) //No, below
            LookBackLoc=-1;
         else
            LookBackLoc=0;

         if(PrevLoc != LookBackLoc)
            {
            XCount++;
            } // if(PrevLoc != LookBackLoc)
         } // for(i=3;iMultiXMaxX)
            XOver=0;
      } // if(XOver !=0)

   return(XOver);
   }

You’ll want to save this in an include file as follows:

Trader's Tech - Reusable Code 1Click the New toolbar button to get the Expert Advisor Wizard.

 

 

 

 

 

 

Trader's Tech - Reusable Code 2Select Include in the radio buttons on the Wizard dialog box and click Next >.

 

 

 

 

 

 

 

Trader's Tech - Reusable Code 3Enter the name of the include file in the Name: field and click Finish.

 

 

 

 

 

 

 

Trader's Tech - Reusable Code 4Copy the code above and paste it into the MetaEditor, replacing all the code there. Then click the Save toolbar button to save your work. You now have MAXover.mqh in your includes folder.

 

 

 

 

 

I think that will wrap us up for the week. Next week we’ll talk about including this in your EA and what we have to do to use it to check for the crossover in place of what we’ve already got. 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!

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