Bollinger Bands Strategy: Delay in execution with larger deviation settings

 
Hello. Any help and advice would be much appreciated.

i have created a strategy which opens a trade once the Bid prices is above/below the upper/lower bands of of a deviation of 2 for bollinger bands.

When the deviation is less then 2, the trade executes instantly. No issues here.

However, the larger the deviation (above 2, going to 3), the longer the delay in execution. Sometimes the trade executes 5, 10 minutes later.

Sometimes, when the Bid price is above/below the deviation level, the trade doesn't even execute (I have attached an image showign this).

What would cause this?

Sample code is below. Please note this issu is occuring whether I am using OnTick(), or OnTimer().


Thank you in advance.


void OnTick()
  {


double upband = iBands(NULL,0,20,2,0,PRICE_CLOSE,MODE_UPPER,0);
double lowerband = iBands(NULL,0,20,2,0,PRICE_CLOSE,MODE_LOWER,0); 

//--when the above deviations are more than 2, delays in trade execution occur--//
    
if ( (Hour() >= 4) && (Hour() < 21)  )
{ 
      if ( (Bid> upband) )
      {
        int sellOrder = OrderSend(Symbol(), OP_SELL, 0.01,Bid,0,0,Ask-(n),"sell order baby",MAGICSELL,0,0);
        //--n being the pip amount --//
        if(sellOrder!=0)
           {
            Print("OrderSend failed with error #",GetLastError());
           }
         else
            Print("OrderSend placed successfully");
        }

        if ( (Bid< lowerband) )
      {
        int buyOrder = OrderSend(Symbol(), OP_BUY, 0.01,Ask,0,0,Bid+(n),"buy order baby",MAGICBUY,0,0);
        //--n being the pip amount --//
        if(buyOrder!=0)
           {
            Print("OrderSend failed with error #",GetLastError());
           }
         else
            Print("OrderSend placed successfully");
        }       
     
}
}

Bollinger Bands - Trend Indicators - Technical Indicators - Price Charts, Technical and Fundamental Analysis - MetaTrader 5 Help
  • www.metatrader5.com
Bollinger Bands (BB) are similar to Envelopes. The only difference is that the bands of Envelopes are plotted a fixed distance (%) away from the...
 

For better accuracy on the strategy tester it is best to just use:

Close[0]
The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
What are the differences between the three modes of testing in MetaTrader 5, and what should be particularly looked for? How does the testing of an EA, trading simultaneously on multiple instruments, take place? When and how are the indicator values calculated during testing, and how are the events handled? How to synchronize the bars from different instruments during testing in an "open prices only" mode? This article aims to provide answers to these and many other questions.
 
  1. You have slippage set to zero. Hit or miss whether it will open any orders.
  2. Without checking whether orders are already open or a new bar, your code should open one order per tick once the condition has been met.
 
Adj007 #:

I am going to assume the spread is 0 in this instance, for better accuracy on the strategy tester it is best to just use:

The trade isn't executing because as far as I am aware this is the only indicator you have provided: 

You need an extra set of indicators under iBands:

If you understand how Bollinger bands work, you would know that 2 standard deviations above or below current price is closer to price than 3 standard deviations. 

I use the strategy tester for this, and set the spread to current for the trading pair.

This was an example of 3SD which does not open trades. When I use 2SD (like in the code posted in this post), the trade execution is hit or miss (mostly miss).

Also, isnt using Close[0] the close of that bar? This is not what I want. I want to execute the trade based on the live price of the bid.

Nonetheless, I have used Close[0], issue still persists.

 
William Roeder #:
  1. You have slippage set to zero. Hit or miss whether it will open any orders.
  2. Without checking whether orders are already open or a new bar, your code should open one order per tick once the condition has been met.

1. I have just added slippage of 20, and the issue still persists.

2. This is an excerpt of the code. I have included whether trades are open using the magic number in the full code, ensuring only 1 trade opens. I have also done it by checking if a order of the same symbol is open. Still the issue persists.

 
2OT 3OT #:

I use the strategy tester for this, and set the spread to current for the trading pair

This was an example. When I use 3 SD, this is when my problem occurs

So as of this moment you do know that the bid and ask prices on your broker are very wide and start to narrow after 23:00 UTC ? Meaning if you run the strategy tester using your brokers current spread between 22:00 & 23:00 UTC, if you see that price is above the upper bound and it is not executing, the bid price depending on the spread may be below the upper bound ?

 
Adj007 #:

So as of this moment you do know that the bid and ask prices on your broker are very wide and start to narrow after 23:00 UTC ? Meaning if you run the strategy tester using your brokers current spread, if you see that price is above the upper bound and it is not executing, the bid price depending on the spread may be below the upper bound ?

Yes, I understand this. I either use the current spread, or I set it using the predefined numbers in the ST that is close to the usual floating spread given.


"if you see that price is above the upper bound and it is not executing, the bid price depending on the spread may be below the upper bound ?" please elaborate on this, because I have used Close[0], and the issue still persists.

 

For example if the bid and ask is 1.01/1.10. Your current upper bound is 1.12. If the current price is above 1.12, the bid would be 1.03, the current spread of the Bid & Ask between the timings 22:00- 23:00 UTC is so wide that the Bid price is lower than the upper bound when in fact you visually see the price above the upperbound. 

You can try this code. 

if ((Hour() >= 4) && (Hour() < 21))
      if ( Close[0] > upband ) {
      {
        int sellOrder = OrderSend(Symbol(), OP_SELL, 0.01,Bid,0,0,Ask-(n),"sell order baby",MAGICSELL,0,0); } 
														}
	
	if((Hour() >= 4) && ( Hour() < 21))
        if (Close[0] < lowerband){
      {
        int buyOrder = OrderSend(Symbol(), OP_BUY, 0.01,Ask,0,0,Bid+(n),"buy order baby",MAGICBUY,0,0);
        //--n being the pip amount --// 
        }       	
		}
 
2OT 3OT #:

I use the strategy tester for this, and set the spread to current for the trading pair.

This was an example of 3SD which does not open trades. When I use 2SD (like in the code posted in this post), the trade execution is hit or miss (mostly miss).

Also, isnt using Close[0] the close of that bar? This is not what I want. I want to execute the trade based on the live price of the bid.

Nonetheless, I have used Close[0], issue still persists.

Close[0] is current Bid Price, OR the bid price of the current candle if it were to close on current tick.

Close[1] is the final bid price of the first closed candle.

Any moderators care to correct me?
 
Adj007 #:

For example if the bid and ask is 1.01/1.10. Your current upper bound is 1.12. If the current price is above 1.12, the bid would be 1.03, the current spread of the Bid & Ask between the timings 22:00- 23:00 UTC is so wide that the Bid price is lower than the upper bound when in fact you visually see the price above the upperbound. 

You can try this code. 

I have tried this, and still the issue persists.
 
Revo Trades #:

Close[0] is current Bid Price, OR the bid price of the current candle if it were to close on current tick.

Close[1] is the final bid price of the first closed candle.

Any moderators care to correct me?
I have tried Close[0] and the issue still persists