How to change EA to open at start of candle only

 

Hi everyone:

I have an EA that opens trades throughout the formation of a candle (meaning on a 5 min chart, it can open an order during the duration of the candle). I would like

to have it where the EA can only open trades at the start of a new candle only, and not during the duration of the candle.

Below is the code that handles the opening of new trades. I'm assuming the code would go near (Time0!=Time[0]).

I have tried to search on these forums and others how to do this, to no avail. Any help would be greatly appreciated.


if((CountOrders(OP_BUY,Magic)+CountOrders(OP_SELL,Magic))<MaxOrders)

     {

      spread=Ask-Bid;

      if(SignalBUY)

        {



         if(HideSL==false && StopLoss>0){SL=Ask-StopLoss*point;/*OPP=Bid-StopLoss*point;SLP=Bid;*/}else {SL=0;/*SLP=0;*/}

         if(HideTP==false&&TakeProfit>0){TP=Ask+TakeProfit*point;/*TPP=Bid-(TakeProfit*2)*point;*/}else {TP=0;/*TPP=0;*/}

         if(HideSL==false&&HedgeSL>0)SLH=Bid+HedgeSL*point;else SLH=0;

         if(HideTP==false&&HedgeTP>0)TPH=Bid-HedgeTP*point;else TPH=0;

         if(Martingale)ILots=NormalizeDouble(Lots*MartingaleFactor(),2);else ILots=Lots;

         if(ILots<MinLots)ILots=MinLots;if(ILots>MaxLots)ILots=MaxLots;



         if((Time0!=Time[0]) || (MaxTradePerBar>1) && (!BuySignalCancelledLatch) && (!DontOpen))

           {



            Ticket=OrderSend(Symbol(),OP_BUY,ILots,Ask,Slippage,SL,TP,EAName,Magic,0,Blue);

            if(!MultipleOrdersPerSignal)

              {

               BuySignalCancelledLatch=true;

               Print("Buy Signal Cancelled Latch Set");

              }

            if(Hedge)TicketH=OrderSend(Symbol(),OP_SELL,ILots,Bid,Slippage,SLH,TPH,EAName,Magic,0,Red);

/*if(ReverseAtStop&&StopLoss>0)TicketP=OrderSend(Symbol(),OP_SELLSTOP,Lots,OPP,Slippage,SLP,TPP,EAName,Magic,Expire,Red);*/

            Time0=Time[0];if(Ticket>0)TradePerBar++;

           }
 

Please use the </> button to insert your above code.


 
datetime time;

if((CountOrders(OP_BUY,Magic)+CountOrders(OP_SELL,Magic))<MaxOrders)

     {
      
      if(time!=iTime(Symbol(),Period(),0))
      {

      spread=Ask-Bid;

      if(SignalBUY)

        {



         if(HideSL==false && StopLoss>0){SL=Ask-StopLoss*point;/*OPP=Bid-StopLoss*point;SLP=Bid;*/}else {SL=0;/*SLP=0;*/}

         if(HideTP==false&&TakeProfit>0){TP=Ask+TakeProfit*point;/*TPP=Bid-(TakeProfit*2)*point;*/}else {TP=0;/*TPP=0;*/}

         if(HideSL==false&&HedgeSL>0)SLH=Bid+HedgeSL*point;else SLH=0;

         if(HideTP==false&&HedgeTP>0)TPH=Bid-HedgeTP*point;else TPH=0;

         if(Martingale)ILots=NormalizeDouble(Lots*MartingaleFactor(),2);else ILots=Lots;

         if(ILots<MinLots)ILots=MinLots;if(ILots>MaxLots)ILots=MaxLots;



         if((Time0!=Time[0]) || (MaxTradePerBar>1) && (!BuySignalCancelledLatch) && (!DontOpen))

           {



            Ticket=OrderSend(Symbol(),OP_BUY,ILots,Ask,Slippage,SL,TP,EAName,Magic,0,Blue);

            if(!MultipleOrdersPerSignal)

              {

               BuySignalCancelledLatch=true;

               Print("Buy Signal Cancelled Latch Set");

              }

            if(Hedge)TicketH=OrderSend(Symbol(),OP_SELL,ILots,Bid,Slippage,SLH,TPH,EAName,Magic,0,Red);

/*if(ReverseAtStop&&StopLoss>0)TicketP=OrderSend(Symbol(),OP_SELLSTOP,Lots,OPP,Slippage,SLP,TPP,EAName,Magic,Expire,Red);*/

            Time0=Time[0];if(Ticket>0)TradePerBar++;

           }
          time=iTime(Symbol(),Period(),0);
        }
 
sheaosaurus:

Hi everyone:

I have an EA that opens trades throughout the formation of a candle (meaning on a 5 min chart, it can open an order during the duration of the candle). I would like

to have it where the EA can only open trades at the start of a new candle only, and not during the duration of the candle.

Below is the code that handles the opening of new trades. I'm assuming the code would go near (Time0!=Time[0]).

I have tried to search on these forums and others how to do this, to no avail. Any help would be greatly appreciated.


bool isNewBar(string symbol,int timeframe,bool forceStart=true) //timeframe=PERIOD_H1   etc
   static int prevTime;
   bool newBar=false; 
   if(iTime(symbol,timeframe,0)!=prevTime)
   {
      newBar=true;
      if(prevTime==0 && forceStart==false) newBar=false; 
      prevTime=iTime(symbol,timeframe,0); 
   }
   return(newBar);
}

Load this method every tick 

if returns true it is a new bar


PS the 'secret' is to use a static variable

Reason: