Why Does my Buy order place Multiple times on same candle

 

Hi,

Please help.

I've got my EA to place an Buy Order when the Ask Price hits a set price. But now it places multiple trades not just the one.

I need it to place only the one trade per candle.

I have tried 

static datetime prevTime=0;                                                    
       datetime lastTime[1];                                                   
                              
    if (CopyTime(_Symbol,_Period,0,1,lastTime)==1 && prevTime!=lastTime[0])        
    { 
        ///.....
        prevTime=lastTime[0];   
    }                   

But if I do the above, the Buy Order only places the trade when new candle opens and not when the Ask price hits the set price.

what am I doing wrong?? My code below

// import Trade Library
#include<Trade\Trade.mqh>
//Create an instance of the Ctrade called trade
CTrade trade;
   MqlRates PriceInfo[];                                                         //Create an array for the prices
   string signal = "";                                                           //Global string for the signal

void OnTick()
  {
   static double NextBuyPrice;                                                   //Static NextBuyPrice
   double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);     //Get Ask price

   ArraySetAsSeries(PriceInfo,true);                                             //Sort the Price Array from the Current candle downwards
   int PriceData = CopyRates(_Symbol,_Period,0,3,PriceInfo);                     //Fill Array with Price Data
    
   if (PositionsTotal()==0)                                                      //No Open Positions
   NextBuyPrice = 638200.00; 
   signal = CheckEntrySignal();                                                  //Check Entry Signal
   
   if (Ask <= NextBuyPrice)                                                      //if the Ask price is >= Next Buy Price   
   if (signal == "buy")
   {  
     trade.Buy(0.002,NULL,Ask,Ask-1200,Ask+1200,NULL);                           //Open a Buy Order  
   }                                          
    
  }//End
  string CheckEntrySignal()
   {  
    if (PriceInfo[1].close > PriceInfo[1].open)                                 //Sell when candle is Bullish
    signal="sell"; 
    
    if (PriceInfo[1].close < PriceInfo[1].open)                                 //Buy when candle is Bearsih
    signal="buy"; 

    return signal;                                                              //Return signal 
   }//End CheckEntrySignal
 
Help with code to execute code once per bar in MQL5
Help with code to execute code once per bar in MQL5
  • 2021.04.05
  • www.mql5.com
I've been using this code on a multi symbol EA trying to make it execute only once per bar but it executes on every tick , please help...
 
// import Trade Library
#include<Trade\Trade.mqh>
//Create an instance of the Ctrade called trade
CTrade trade;
   MqlRates PriceInfo[];                                                         //Create an array for the prices
   string signal = "";                                                           //Global string for the signal

//---Global Variable
datetime signalTime;
datetime currentTime[];
void OnTick()
  {
   static double NextBuyPrice;                                                   //Static NextBuyPrice
   double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);     //Get Ask price

   ArraySetAsSeries(PriceInfo,true);                                             //Sort the Price Array from the Current candle downwards
   int PriceData = CopyRates(_Symbol,_Period,0,3,PriceInfo);                     //Fill Array with Price Data

   if(PositionsTotal()==0)                                                       //No Open Positions
      NextBuyPrice = 638200.00;
   signal = CheckEntrySignal();                                                  //Check Entry Signal


   CopyTime(_Symbol,_Period,0,1,currentTime);
   if(Ask <= NextBuyPrice)                                                       //if the Ask price is >= Next Buy Price
      if(signal == "buy" && signalTime != currentTime[0])
        {
         trade.Buy(0.002,NULL,Ask,Ask-1200,Ask+1200,NULL);                           //Open a Buy Order
         signalTime = currentTime[0];
        }

  }//End
string CheckEntrySignal()
  {
   if(PriceInfo[1].close > PriceInfo[1].open)                                  //Sell when candle is Bullish
      signal="sell";

   if(PriceInfo[1].close < PriceInfo[1].open)                                  //Buy when candle is Bearsih
      signal="buy";

   return signal;                                                              //Return signal
  }//End CheckEntrySignal
//+------------------------------------------------------------------+


You can try this

 
Nirav Patel


You can try this


Thank You so Much Nirav. 


I get it now .. :-)

Reason: