MT4 opening and closing trades at the same time? Why?

 

I'm working on creating an EA that purchases long when the price (close) crosses above the MA line and closes the trade when the price (close) crosses below the MA line.

My issue is that when I run the strategy tester, it is opening and closing the trade immediately.

I've used an online generator to create the EA, but can someone tell me what I need to add to not have it open and close at the same time?

 
Stop looking at the current bar. One tick above you open, one tick below you close. What do you expect to happen?
 
whroeder1:
Stop looking at the current bar. One tick above you open, one tick below you close. What do you expect to happen?

I think I figured out what is happening.  The buy is based on the MA(50)-CLOSE and the close trade is based on the MA(14)-CLOSE price.  The bar that I open the trade, 3 minutes later the CLOSE price drops below the MA(10)-CLOSE and it executes the sell order.

So...  How do I set it up to, once the BUY order has triggered, do not execute the SELL order for 2 or 3 bars?

 
What do I need to do to make the Expert only buy on DAILY signals?  Right now it is doing whatever timeframe I'm on.
 
if(Period()==PERIOD_D1)
 {
  // only buy here
 }
 
jthornton:

I'm working on creating an EA that purchases long when the price (close) crosses above the MA line and closes the trade when the price (close) crosses below the MA line.

My issue is that when I run the strategy tester, it is opening and closing the trade immediately.

I've used an online generator to create the EA, but can someone tell me what I need to add to not have it open and close at the same time?

I am having the same problem.
Please who has a perfect solution 
Reason: