My EA doesn't open/close any trades in Strategy Tester

 

Hi there,

This is my first post to this forum.

I created an EA based on the following system http://forums.babypips.com/free-forex-trading-systems/507-45-pips-per-day-system-eur-usd.html.

Entry is crossover of macd over signal followed by SAR and ADX confirmations, or flip of SAR followed by macd and ADX confirmations.

Exit is to TP at 30 pips or be stopped out at SAR.  Will upgrade to incorporating BE functionality if I can get this piece up and running.

Not sure why the Tester doesn't show the trades.

I created a tester.tpl template with settings as below:

Tester settings 

 The outcome was no trades placed.

The journal appears as below:

Tester journal 

The code (MQL4) is as below and I the file is attached.

If anyone could point me in the right direction, that'd be lovely.

Thank you. 

//+------------------------------------------------------------------+

//|                                                     SAR+MACD.mq4 |

//|                                                            Brett |

//|                                                               NA |

//+------------------------------------------------------------------+

#property copyright "Brett"

#property link      "NA"



// Date and time related variables.

// Below dates and times are in GMT.

string DLS_STARTS = "2013.03.31 01:00";

string DLS_ENDS = "2013.10.27 01:00";

int SUMMER_LONDON_SESSION_STARTS = 7;

int SUMMER_LONDON_SESSION_ENDS = 16;

int WINTER_LONDON_SESSION_STARTS = 8;

int WINTER_LONDON_SESSION_ENDS = 17;

string TimeOfThisTick = "";

bool MarketOpen;



bool TradingConditionsChecked;



extern int TIMEFRAME = 0;



// MACD signal related variables.

extern int FAST_MACD_EMA = 12;

extern int SLOW_MACD_EMA = 26;

extern int SIGNAL_PERIOD = 9;

double FastEMAArray[10];

double SlowEMAArray[10];

double PreviousFastEMA = 0;

double CurrentFastEMA = 0;

double PreviousSlowEMA = 0;

double CurrentSlowEMA = 0;

double MACDLineArray[9];

double PreviousMACD = 0;

double CurrentMACD = 0;

double PreviousSignal = 0;

double CurrentSignal = 0;

bool MACDCrossedUp;

bool MACDCrossedDown;



// Variables related to SAR.

double SAR = 0;

extern double SAR_STEP = 0.02;

extern double SAR_MAX = 0.2;

double PreviousSAR = 0;

double CurrentSAR = 0;



// Variables related to ADX

extern double ADX_PERIOD = 14;

double PreviousADX = 0;

double CurrentADX = 0;



// Variables related to orders.

int OrderDirection = 0;

extern double LotSize = 0.1;

extern double StopLoss = 0;

extern double TakeProfit = 30;

double PipSize = 0;

extern int MagicNumber = 123;

extern int SLIPPAGE = 3;

string COMMENT = "";

string EXPIRATION = "";





//+------------------------------------------------------------------+

//| expert initialization function                                   |

//+------------------------------------------------------------------+

int init()

  {

   // Print("Min StopLevel is: " + MarketInfo(Symbol(), MODE_STOPLEVEL));

   CalculatePipSize();

   return(0);

  }

//+------------------------------------------------------------------+

//| expert deinitialization function                                 |

//+------------------------------------------------------------------+

int deinit()

  {



   return(0);

  }



//+------------------------------------------------------------------+

//| expert start function                                            |

//+------------------------------------------------------------------+



int start()

   {

   // for each tick check for trading conditions, if market is open and place trade if possible.

   // Check if trading conditions confirm entry.

   // Ensure check runs on candles instead of every tick

   if(IsNewCandle())

   TradingConditionsChecked = CheckTradingConditions();

      if(TradingConditionsChecked)

      // if yes then check if market is open according to relevant session Times.

         MarketOpen = CheckIfMarketOpen();

            // if yes then place trade

               if(MarketOpen)

                  OrderEntry(OrderDirection);

   return(0);

  }

//+------------------------------------------------------------------+



void CalculatePipSize()

   {

   double TickSize = MarketInfo(Symbol(), MODE_TICKSIZE);

   if(TickSize == 0.00001 || TickSize == 0.001)

   PipSize = TickSize * 10;

   else PipSize = TickSize;

   }

   

bool IsNewCandle()

   {

   static int NumberOfBars = 0;

   if (NumberOfBars == Bars)

         return(false);

      NumberOfBars = Bars;

      return(true);

   }

   

bool CheckTradingConditions()

   {

   // determine if macd crossed signal up or down.

   if(MACDCrossedSignal())

      // Check for SAR confirmation of trend of long or short position.

      if(SARConfirmed())

         // Check for ADX confirmation of strength of trend

         if(ADXConfirmed(OrderDirection))

            return(true);

   

   // Check if SAR flipped above or below bars.

   if(SARFlipped())

      // Determine for MACD confirmation of trend of long or short position.

      if(MACDConfirmed())

         // Check for ADX confirmation of strength of trend

         if(ADXConfirmed(OrderDirection))

            return(true);

   }



bool ADXConfirmed(int OrderDirection)

   {

   PreviousADX = iADX(NULL, TIMEFRAME, ADX_PERIOD, PRICE_CLOSE, MODE_SIGNAL, 2);

   CurrentADX = iADX(NULL, TIMEFRAME, ADX_PERIOD, PRICE_CLOSE, MODE_SIGNAL, 1);

   

   if(OrderDirection == 0)

      if(CurrentADX > 20)  

         if(CurrentADX > PreviousADX)

            return(true);

   

   if(OrderDirection == 1)

      if(CurrentADX > 20)  

         if(CurrentADX < PreviousADX)

            return(true);

   }



bool MACDConfirmed()

   {

    // Calculate MACD and Signal Values for comparison  

    CalcMACDAndSignalValues();

    // Check if previous MACD > signal and current MACD > signal and vice versa.

    if(PreviousMACD > PreviousSignal)

       if(CurrentMACD > CurrentSignal)

         {

         OrderDirection = 0;

         return(true);

         }

    if(PreviousMACD < PreviousSignal)

       if(CurrentMACD < CurrentSignal)

         {

         OrderDirection = 1;

         return(true);

         }

   }

   

bool SARFlipped()

   {

   // Check if previous SAR < close and current SAR > close and vice versa.

   PreviousSAR = iSAR(NULL, TIMEFRAME, SAR_STEP, SAR_MAX, 2);

   CurrentSAR = iSAR(NULL, TIMEFRAME, SAR_STEP, SAR_MAX, 1);

   

   if(PreviousSAR < Close[2])

      if(PreviousSAR > Close[1])

         return(true);

   if(PreviousSAR > Close[2])

      if(PreviousSAR < Close[1])

         return(true);

   }



bool MACDCrossedSignal()

   {

   // Calculate MACD and Signal Values for comparison  

    CalcMACDAndSignalValues();

    

    // determine if previous macd < previous signal and current macd > current signal

    if(PreviousMACD < PreviousSignal)

       if(CurrentMACD > CurrentSignal)

          MACDCrossedUp = true;

    if(PreviousMACD > PreviousSignal)

       if(CurrentMACD < CurrentSignal)

          MACDCrossedDown = true;

   }



void CalcMACDAndSignalValues(){

   // populate arrays for the EMA values to to calculate the MACD Line and Signal Line      

    for(int s = 0; s < 10; s++)

       {

       FastEMAArray[s] = iMA(NULL, TIMEFRAME, FAST_MACD_EMA, 0, MODE_EMA, PRICE_CLOSE, s+1);

       SlowEMAArray[s] = iMA(NULL, TIMEFRAME, SLOW_MACD_EMA, 0, MODE_EMA, PRICE_CLOSE, s+1);

         

       // MACDlineArray holds values for the MACD Line and is used to calc the Signal Line.

       // The MACD Line will only use the current and previuos values but the signal will use 9 values.

       // The MACD Line = the diffence between the 2 EMAs.

       if(s < 9)

          {

          if(FastEMAArray[s] > SlowEMAArray[s])

             MACDLineArray[s] = FastEMAArray[s] - SlowEMAArray[s];

          if(FastEMAArray[s] < SlowEMAArray[s])

             MACDLineArray[s] = SlowEMAArray[s] - FastEMAArray[s];

          }

       }

         

    // Determine values of PreviousMACD, CurrentMACD, PreviousSignal, CurrentSignal

    PreviousMACD = MACDLineArray[2];

    CurrentMACD = MACDLineArray[1];

    PreviousSignal = iMAOnArray(MACDLineArray, 0, 9, 0, MODE_EMA, 2);

    CurrentSignal = iMAOnArray(MACDLineArray, 0, 9, 0, MODE_EMA, 1);

   }



bool SARConfirmed()

   {

   SAR = iSAR(NULL, TIMEFRAME, SAR_STEP, SAR_MAX, 1);

   if(MACDCrossedUp == true)

      if(SAR < PRICE_CLOSE)

         {

         OrderDirection = 0;

         return(true);

         }

   if(MACDCrossedDown == true)

      if(SAR > PRICE_CLOSE)

         {

         OrderDirection = 1;

         return(true);

         }

   }



bool CheckIfMarketOpen()

   {

   if(IsDaylightSavings())

      if(TimeCurrent() >= TimeHour(WINTER_LONDON_SESSION_STARTS))

         if(TimeCurrent() < TimeHour(WINTER_LONDON_SESSION_ENDS))

            return(true);

            

   if(!IsDaylightSavings())

      if(TimeCurrent() >= TimeHour(SUMMER_LONDON_SESSION_STARTS))

         if(TimeCurrent() < TimeHour(SUMMER_LONDON_SESSION_ENDS))

            return(true);

   }

   

bool IsDaylightSavings()

   {

   TimeOfThisTick = TimeToStr(TimeCurrent());

   if(TimeOfThisTick >= DLS_STARTS)

      if(TimeOfThisTick < DLS_ENDS)

         return(true);

   }

   

   

void OrderEntry(int OrderDirection)

   {

   /**

   There are two option's for taking profit

   1/ When they switch direction or

   2/ Stay in until the trend become's a range

   **/

   // Find stoploss level to include in order.

   CalcStopLoss(OrderDirection);

   

   // Place order.

   if(OrderDirection == 0)

      if(OrdersTotal() == 0)

         OrderSend(Symbol(),OP_BUY,LotSize,Ask, SLIPPAGE,StopLoss,Ask+(TakeProfit*PipSize),COMMENT,MagicNumber,EXPIRATION/**Pending orders only**/, Green);

   if(OrderDirection ==1)

      if(OrdersTotal()==0)

         OrderSend(Symbol(),OP_SELL,LotSize,Bid, SLIPPAGE,StopLoss,Bid-(TakeProfit*PipSize),COMMENT,MagicNumber,EXPIRATION/**Pending orders only**/, Red);

   }



double CalcStopLoss(int OrderDirection)

   {

   if(OrderDirection == 0)

      StopLoss = iSAR(NULL, TIMEFRAME, SAR_STEP, SAR_MAX, 0);

   if(OrderDirection == 1)

      StopLoss = iSAR(NULL, TIMEFRAME, SAR_STEP, SAR_MAX, 0);

   }
Files:
sarmmacdv5.mq4  10 kb
 
brad:

Hi there,

This is my first post to this forum.

I created an EA based on the following system http://forums.babypips.com/free-forex-trading-systems/507-45-pips-per-day-system-eur-usd.html.

Entry is crossover of macd over signal followed by SAR and ADX confirmations, or flip of SAR followed by macd and ADX confirmations.

Exit is to TP at 30 pips or be stopped out at SAR.  Will upgrade to incorporating BE functionality if I can get this piece up and running.

Not sure why the Tester doesn't show the trades.

I created a tester.tpl template with settings as below:

 

 The outcome was no trades placed.

The journal appears as below:

 

The code (MQL4) is as below and I the file is attached.

If anyone could point me in the right direction, that'd be lovely.

Thank you. 

<CODE DELETED>

Please read some other posts before posting . . .

Please edit your post . . .    please use the   SRC   button to post code: How to use the   SRC   button.

 

Read this and implement it:  What are Function return values ? How do I use them ?

 

You have a globally declared variable called OrderDirection,  you also have this variable declared locally in some Functions,  which one should your Functions use ?  why not make things clearer and use a different variable name ?

 

Thanks for both src button info and variable name suggestion.

I think I've only decalared and initialised that variable once in the global area and later reassigned values to it, although at times, reassigned it the same value it already had.

Thanks again. 

 
brad:

Thanks for both src button info and variable name suggestion.

I think I've only decalared and initialised that variable once in the global area and later reassigned values to it, although at times, reassigned it the same value it already had.

Thanks again. 

Please edit your first post.

You have declared the variable in the function declaration . .

double CalcStopLoss(   int OrderDirection  )
 

In an attempt to simplify this, I've excluded all of the time related funtions and only focusing on opening and closing orders with a simple MACD crossover of signal now.  The function that calculates the MACD line and signal line is as below.  It differs from the values of the IBFX Traditional MACD indicator and I'm not sure why.  Can you please confirm that my code and logic is accurately applied?  Am quite the noob at all this and although I thought I had it mostly figured out, am starting to wonder how to make progress with this now.  Any assistance much appreciated.  Thanks.

void CalcMACDAndSignalValues(){
   // populate arrays for the EMA values to to calculate the MACD Line and Signal Line      
    for(int s = 0; s < 10; s++)
       {
       FastEMAArray[s] = iMA(NULL, TIMEFRAME, FAST_MACD_EMA, 0, MODE_EMA, PRICE_CLOSE, s+1);
       SlowEMAArray[s] = iMA(NULL, TIMEFRAME, SLOW_MACD_EMA, 0, MODE_EMA, PRICE_CLOSE, s+1);
         
       // MACDlineArray holds values for the MACD Line and is used to calc the Signal Line.
       // The MACD Line will only use the current and previuos values but the signal will use 9 values.
       // The MACD Line = the diffence between the 2 EMAs.
       if(s < 9)
          {
          if(FastEMAArray[s] > SlowEMAArray[s])
             MACDLineArray[s] = FastEMAArray[s] - SlowEMAArray[s];
          if(FastEMAArray[s] < SlowEMAArray[s])
             MACDLineArray[s] = SlowEMAArray[s] - FastEMAArray[s];
          }
       }
         
    // Determine values of PreviousMACD, CurrentMACD, PreviousSignal, CurrentSignal
    PreviousMACD = MACDLineArray[2];
    CurrentMACD = MACDLineArray[1];
    PreviousSignal = iMAOnArray(MACDLineArray, 0, 9, 0, MODE_SMA, 2);
    CurrentSignal = iMAOnArray(MACDLineArray, 0, 9, 0, MODE_SMA, 1);
   }
 
brad:

In an attempt to simplify this, I've excluded all of the time related funtions and only focusing on opening and closing orders with a simple MACD crossover of signal now.  The function that calculates the MACD line and signal line is as below.  It differs from the values of the IBFX Traditional MACD indicator and I'm not sure why.  Can you please confirm that my code and logic is accurately applied?  Am quite the noob at all this and although I thought I had it mostly figured out, am starting to wonder how to make progress with this now.  Any assistance much appreciated.  Thanks.

 

How are you declaring the arrays ?  
 

Thanks RaptorUK.

double FastEMAArray[10];
double SlowEMAArray[10];
double MACDLineArray[9];

 The related variables in the global area look like this:

// MACD signal related variables.
extern int FAST_MACD_EMA = 12;
extern int SLOW_MACD_EMA = 26;
extern int SIGNAL_PERIOD = 9;
double FastEMAArray[10];
double SlowEMAArray[10];
double MACDLineArray[9];
double PreviousFastEMA = 0;
double CurrentFastEMA = 0;
double PreviousSlowEMA = 0;
double CurrentSlowEMA = 0;
double PreviousMACD = 0;
double CurrentMACD = 0;
double PreviousSignal = 0;
double CurrentSignal = 0;
bool MACDCrossedUp;
bool MACDCrossedDown;
 
brad:

Thanks RaptorUK.

 The related variables in the global area look like this:

OK,  so you are declaring the arrays with an initial size,  that is good otherwise you would have to ArrayResize().  

Add Print() statements everywhere you need to check that a variable has the correct value so you can debug your code.  IMAOnArray() is a bit of a pain,  it's not very clearly documented, especially the consequence of using/not using an array set as a series array,  you are going to have to suck it and see . . .  unless you find the documentation clear than I do.  I've never had to use iMAOnArray so it's not something I have ever had to get fully to grips with.  

 
Learn to use braces
Your Code
   if(IsNewCandle())
   TradingConditionsChecked = CheckTradingConditions(); <= Only this line is skipped
      if(TradingConditionsChecked)            <= if not a new candle what is this value?
      // if yes then check if market is open according to relevant session Times.
         MarketOpen = CheckIfMarketOpen();    
            // if yes then place trade
               if(MarketOpen)  <= if TradingConditionsChecked is false, what is this value?
                  OrderEntry(OrderDirection);  <= How many opens per bar? there is no check
Code now matches your indenting
   if(IsNewCandle()){
      TradingConditionsChecked = CheckTradingConditions();
      if(TradingConditionsChecked){
         // if yes then check if market is open according to relevant session Times.
         MarketOpen = CheckIfMarketOpen();
         if(MarketOpen){
            // if yes then place trade
            OrderEntry(OrderDirection);
         } // MarketOpen
      } // TradingConditionsChecked
   } // IsNewCandle
 

Thanks for the tips RaptorUK and WHRoeder.

Yes, the print statements help alot.

Oh yeah, I can totally see how the braces need to go in!

Thanks to you both for your assistance.  I'm slowly progressing with coding this little EA and learning heaps in the process.

Happy days.

Reason: