Trouble testing MA cross

 

Hello all

I am coding an EA which trades when price closes below/above a 10 SMA, along with confirmation from 2 other indicators. The only problem is, that sometimes all criteria are met several candles AFTER the initial cross, so simply saying "if((TenSMA > Ask) && (etc, etc))" doesnt suffice. I have written a script to test for this, but no matter what the candle position is, the result the script gives me is the same:

//+------------------------------------------------------------------+
//|                                                         test.mq4 |
//|                      Copyright © 2010, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
//----
   bool TradeOrNotA = LongSMAtest();
   bool TradeOrNotB = ShortSMAtest();
   
   if((TradeOrNotA == true) || (TradeOrNotB == true)) Alert("You can take a trade");
   else Alert("Conditions not optimal");
//----
   return(0);
  }
      
      bool ShortSMAtest()
      {
      bool TradeS;
      double SMAval1 = iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,1);
      double SMAval2 = iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,2);
      double SMAval3 = iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,3);
      double Close1 = iClose(NULL,0,1);
      double Close2 = iClose(NULL,0,2);
      double Close3 = iClose(NULL,0,3);
      if((SMAval1 > Close1) && (SMAval2 > Close2) && (SMAval3 > Close3)) TradeS = false;
      else TradeS = true;
      return(TradeS);
      }
      
      bool LongSMAtest()
      {
      bool TradeL;
      double SMAval1 = iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,1);
      double SMAval2 = iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,2);
      double SMAval3 = iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,3);
      double Close1 = iClose(NULL,0,1);
      double Close2 = iClose(NULL,0,2);
      double Close3 = iClose(NULL,0,3);
      if((SMAval1 < Close1) && (SMAval2 < Close2) && (SMAval3 < Close3)) TradeL = false;
      else TradeL = true;
      return(TradeL);
      }
      
      
//+------------------------------------------------------------------+
 

I hope I understood you correctly

   if(TradeOrNotA == true)
      {
      Alert("You can take a trade TradeOrNotA");
      }
   else if (TradeOrNotB == true)
      {
      Alert("You can take a trade TradeOrNotB");
      }
   else
      {
      Alert("Conditions not optimal");
      }
 
@qjol - thanks for the feedback, i actually had trouble in the function call. I managed to fix it though by combining both functions, the problem was that there were too many possible conditions for TradeS or TradeL to be true, so just tightened the test a little bit. cheers
Reason: