EA takes multiple trade on one candle stick in one direction


Hi coders !

well for a start, I'm not much of a professional coder, I'm still learning and I'm also sorry for my English language, i just hope you'll understand. thanks

OK. so i have this EA I've been working on for some time now on MQL5, The problem  is that it takes multiple trade on one candle stick in one direction.

for instance, lets say i have a buy signal, it then initiates a buy signal on a candle stick that's still forming and then when my stop loss or take profit is hit, it initiates another buy trade again so far the candlestick on which the trade was initiated is still forming. i hope you understand, so it kinda take up to 3-4 or even more trade on one candlestick especially the ones with a long body.

so i don't want it that way, rather i only need just one trade in one direction, only one candlestick  and after a tp or sl it should Waite for a revers signal in this case "sell"  even if the said candlestick is still forming.

so here is the code.



   double Ask;
   double Bid;

   CTrade trade;//-----------------------------------
  double stoploss =(60 * _Point); 
  double takeprofit =(80 * _Point);

  double stoplossbuytrade =(40 * _Point);  
  double Takeprofitbuytrade =(40 * _Point); 
void OnTick()

 MqlRates PriceInfo[];
  int PriceData =CopyRates(_Symbol,_Period,0,3,PriceInfo);

  string signal="";
double myMovingAverageArray1[],myMovingAverageArray2[];

int movingAverageDefinition1 = iMA (_Symbol,_Period,20,0,MODE_EMA ,PRICE_CLOSE);
int movingAverageDefinition2 = iMA (_Symbol,_Period,25,0,MODE_EMA,PRICE_CLOSE);



      && (myMovingAverageArray1[1]<myMovingAverageArray2[1])
          signal ="buy";
      && (myMovingAverageArray1[1]>myMovingAverageArray2[1])
          signal = "sell";
         if (signal =="sell" && PositionsTotal()<1)
         //trade.Sell(0.01,NULL,Bid,(Bid + stoploss),(Bid - takeprofit),NULL); 
         if (signal =="buy" && PositionsTotal()<1)
         //trade.Buy(0.01,NULL,Ask,(Ask + stoplossbuytrade),(Ask - Takeprofitbuytrade),NULL); 
someone answer me please !
William Roeder  
Zadicus5: it takes multiple trade on one candle stick
  1. Check for open orders. Or,
    Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum
              PositionClose is not working - MQL5 programming forum
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles

  2. Check for a new bar. Or,

    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

  3. You are looking at a signal. Act on a change of signal.
              MQL4 (in Strategy Tester) - double testing of entry conditions - Strategy Tester - Expert Advisors and Automated Trading - MQL5 programming forum #1