Download MetaTrader 5

Issues with the order execution time of my ea

To add comments, please log in or register
Find out the most discussed Forum topics. Have a look at the Wall section!
morne27
35
morne27 2015.05.29 12:09 

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 ?

Alain Verleyen
Moderator
30737
Alain Verleyen 2015.05.29 13:44  

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.


Alain Verleyen
Moderator
30737
Alain Verleyen 2015.05.29 13:47  
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 ?).

To add comments, please log in or register