Problems opening every opportunity

 

Hi all,


I've begun coding a simple EA for opening a trade every time price crosses an EMA and closes when it crosses in the opposite direction. It seems to work to a certain extent, but when it closes it will not immediately open another position in the other direction. I thought that it would have because it crossed the EMA again, but it doesn't. The attached image is a good example of the type of trades by backtesting misses out on because of this. You can see that at (1) the short trade closed because price crosses and closes on the other side of the EMA, but when the bar closes below the EMA again (2) it doesn't open another short position. (3) Is where that trade would have closed. Might have something to do with the fact that its the same bar, but I'm not sure how to get around that. You can find my code below.

Example


I'm very new to coding MQL4 (less then a week) and I'm building this on top of the MA sample EA that came with MT4. Any suggestions would be appreciated.

//--- sell conditions


   if((Open[1]>ma && Close[1]<ma) && (Open[1]-Close[1]) <= 0.0030)
     
     
     {
      res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGICMA,0,Red);
      return;
     }
//--- buy conditions


   if((Open[1]<ma && Close[1]>ma) && (Close[1]-Open[1]) <= 0.0030)


     {
      res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"",MAGICMA,0,Blue);
      return;
     }
//---
  }