EA Doesn't execute trades when tested in the strategy tester

 
Hello guys, I'm new to coding EA. I wanna create an EA that can buy when all three moving averages are crossed over to the upside and the distance between the FastMA (21 EMA) and SlowMA(50EMA) is at least 50 pips. The opposite is the same for selling condition. Another problem is that how do I code so that the price will buy at the FastMA(Whenever the price touches the FastMA) when all the conditions are met?
Example of buy trade
extern int TakeProfit=30;
extern int StopLoss=20;
extern double LotSize=0.01;

extern int FastMA=21;
extern int FastMaShift=0;
extern int FastMaMethod=1;
extern int FastMaAppliedTo=0;

extern int SlowMA=50;
extern int SlowMaShift=0;
extern int SlowMaMethod=1;
extern int SlowMaAppliedTo=0;

extern int SlowMA2=200;
extern int SlowMa2Shift=0;
extern int SlowMa2Method=1;
extern int SlowMa2AppliedTo=0;

extern int  MagicNumber=1234;
int pipsMABuy,pipsMASell;
double pips;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   double ticksize=MarketInfo(Symbol(),MODE_TICKSIZE);
   if(ticksize==0.00001 || ticksize==0.001)
      pips=ticksize*10;
   else pips=ticksize;

   return (0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
   if(IsNewCandle())CheckForMaTrade();

   return(0);
  }
//+------------------------------------------------------------------+
//insuring its a new candle function
//+------------------------------------------------------------------+
bool IsNewCandle()
  {
   static int BarsOnChart=0;
   if(Bars==BarsOnChart)
      return (false);
   BarsOnChart=Bars;
   return(true);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CheckForMaTrade()
  {
   double PreviousFast= iMA(NULL,0,FastMA,FastMaShift,FastMaMethod,FastMaAppliedTo,2);
   double CurrentFast = iMA(NULL,0,FastMA,FastMaShift,FastMaMethod,FastMaAppliedTo,1);
   double PreviousSlow= iMA(NULL,0,SlowMA,SlowMaShift,SlowMaMethod,SlowMaAppliedTo,2);
   double CurrentSlow = iMA(NULL,0,SlowMA,SlowMaShift,SlowMaMethod,SlowMaAppliedTo,1);
   double PreviousSlow2= iMA(NULL,0,SlowMA2,SlowMa2Shift,SlowMa2Method,SlowMa2AppliedTo,2);
   double CurrentSlow2 = iMA(NULL,0,SlowMA2,SlowMa2Shift,SlowMa2Method,SlowMa2AppliedTo,1);

   pipsMABuy=(int)MathFloor(MathAbs(CurrentFast-CurrentSlow)/(_Point));
   pipsMASell=(int)MathFloor(MathAbs(CurrentSlow-CurrentFast)/(_Point));

   if(PreviousFast<PreviousSlow && PreviousFast<PreviousSlow2 && PreviousSlow<PreviousSlow2)
      if(CurrentFast>CurrentSlow && CurrentFast>CurrentSlow2 && CurrentSlow>CurrentSlow2)
         if(pipsMABuy>=50) OrderEntry(0);
   if(PreviousFast>PreviousSlow && PreviousFast>PreviousSlow2 && PreviousSlow>PreviousSlow2)
      if(CurrentFast<CurrentSlow && CurrentFast<CurrentSlow2 && CurrentSlow<CurrentSlow2)
         if(pipsMABuy>=50) OrderEntry(1);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OrderEntry(int direction)
  {
   if(direction==0)
     {
      if(StopLoss== 0) double bsl =0;
      else bsl = Ask-(StopLoss*pips);

      if(TakeProfit==0) double btp=0;
      else btp=Ask+(TakeProfit*pips);

      if(OpenOrdersThisPair(Symbol())==0) int buyticket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,20,0,0,NULL,MagicNumber,0,clrGreen);
      if(buyticket>0) OrderModify(buyticket,OrderOpenPrice(),bsl,btp,0,clrNONE);
     }

   if(direction==1)
     {
      if(StopLoss== 0) double ssl =0;
      else ssl = Bid+(StopLoss*pips);

      if(TakeProfit==0) double stp=0;
      else stp=Bid-(TakeProfit*pips);

      if(OpenOrdersThisPair(Symbol())==0) int sellticket=OrderSend(Symbol(),OP_SELL,LotSize,Bid,20,0,0,NULL,MagicNumber,0,clrRed);
      if(sellticket>0) OrderModify(sellticket,OrderOpenPrice(),ssl,stp,0,clrNONE);
     }
  }
//+------------------------------------------------------------------+
//checks to see if any orders open on this currency pair.
//+------------------------------------------------------------------+
int OpenOrdersThisPair(string pair)
  {
   int total=0;
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol()==pair) total++;
     }
   return (total);
  }
//+------------------------------------------------------------------+
 

Your conditions for going into a trade are too strong:

   if(PreviousFast<PreviousSlow && PreviousFast<PreviousSlow2 && PreviousSlow<PreviousSlow2)
      if(CurrentFast>CurrentSlow && CurrentFast>CurrentSlow2 && CurrentSlow>CurrentSlow2)

See it? This will come true if a) fast MA crosses slow MA, b) fast MA crosses slow MA2 and c) slow MA crosses slow MA2 at the same tick. How realistic is that.

 
I tried to change to the code below and it does not work as well.
if(PreviousFast<PreviousSlow && CurrentFast>CurrentSlow)
  if(pipsMABuy>=50) OrderEntry(0);

if(PreviousFast>PreviousSlow && CurrentFast<CurrentSlow)
  if(pipsMABuy>=50) OrderEntry(0);
 
Gary Hon:
I tried to change to the code below and it does not work as well.

Comment out your pips check and test - your pips values are likely too big.

And btw, your new candle check is not reliable, should consider using time of latest bar, instead of bar.

 
  1.    if(Bars==BarsOnChart)
    For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              New candle - MQL4 programming forum

  2.    double PreviousFast= iMA(NULL,0,FastMA,FastMaShift,FastMaMethod,FastMaAppliedTo,2);
    
    Why did you post your MT4 question in the Root / MT5 EA section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  3.    pipsMABuy=(int)MathFloor(MathAbs(CurrentFast-CurrentSlow)/(_Point));
             if(pipsMABuy>=50) OrderEntry(0);
    
    A pip is not a point.  Your test is now 50 points not 50 pips.

  4. Price has touched fastMA when the Low <= fastMA and High >= fastMA.
 
Gary Hon:
I tried to change to the code below and it does not work as well.

Sure. What I meant is that you just check for the fast MA cross while the other 2 checks are reduced to a simple "need to be above" resp "need to be below". Like so:

   if(PreviousFast<PreviousSlow /* && PreviousFast<PreviousSlow2 && PreviousSlow<PreviousSlow2 */ )
      if(CurrentFast>CurrentSlow && CurrentFast>CurrentSlow2 && CurrentSlow>CurrentSlow2)

or if you clean it up a bit:

   if( PreviousFast<PreviousSlow && CurrentFast>CurrentSlow &&   // fast MA upcross
       CurrentFast>CurrentSlow2 && CurrentSlow>CurrentSlow2 &&   // further MA conditions
       (CurrentFast-CurrentSlow2)/_Point>=pointDistanceBuy )     // at least x points distance
       OrderEntry(OP_BUY);

The check whether price touched fast MA is not required I think because if fast MA moves up, the price must have surpassed it since MAs always lag.