Issues with the order execution time of my ea

 

I'm currently coding my first binary ea which is based on price simply crossing above the upper and lower line of the Bollinger Bands.

The problem I'm having is I want the order to executed as soon as the candle starts. So for example for a sell trade the previous candle cross the upper line and as soon as the next candle starts the order must be taken.

My current ea takes trade 30 seconds or more after the candle has opened. 

Code for buy trade:

   if(Cross(0, Open[1] < iBands(NULL, PERIOD_CURRENT, 20, 3, 0, PRICE_CLOSE, MODE_LOWER, 0))
   )
     {
      RefreshRates();
      price = Ask;  
      if(IsTradeAllowed())
        {
         ticket = myOrderSend(OP_BUY, price, TradeSize, "BO exp:60");
         if(ticket <= 0) return;
        }
      else //not autotrading => only send alert
         myAlert("order", "BO exp:60");

     }
Code for a sell trade
if(Cross(1, Open[1] > iBands(NULL, PERIOD_CURRENT, 20, 3, 0, PRICE_CLOSE, MODE_UPPER, 0)) //Candlestick Open crosses above Bollinger Bands
   )
     {
      RefreshRates();
      price = Bid;  
      if(IsTradeAllowed())
        {
         ticket = myOrderSend(OP_SELL, price, TradeSize, "BO exp:60");
         if(ticket <= 0) return;
        }
      else //not autotrading => only send alert
         myAlert("order", "BO exp:60");
     }
  }

Any advise or correction I can do to let the order executed on the start on the candle ?

 

Forum on trading, automated trading systems and testing trading strategies


Hello,

Please use the SRC button when you post code. Thank you.


This time, I edited it for you.


 
morne27:

I'm currently coding my first binary ea which is based on price simply crossing above the upper and lower line of the Bollinger Bands.

The problem I'm having is I want the order to executed as soon as the candle starts. So for example for a sell trade the previous candle cross the upper line and as soon as the next candle starts the order must be taken.

My current ea takes trade 30 seconds or more after the candle has opened. 

Code for buy trade:

Code for a sell trade

Any advise or correction I can do to let the order executed on the start on the candle ?

When and where is this code executed ?

You have to detect a new bar and only execute this code when there is one. We don't see all the code so it's difficult to answer exactly (Cross() function ?).

 

I know it's been a while since this question was asked. But for anyone else facing similar issue, you need to use Open[0] for getting current candle.

Cross is an EA Builder function I think.

Reason: