Bollinger Bands are widely used in algorithmic trading to measure volatility and identify overbought or oversold conditions. Developed by John Bollinger, this indicator consists of three lines—a middle band (usually a simple moving average), an upper band, and a lower band. In this article, we’ll explore how Bollinger Bands are calculated, their significance in volatility trading, and how to implement a Bollinger Band breakout strategy in MQL4.
What are Bollinger Bands?
Bollinger Bands consist of a moving average line (middle band) flanked by an upper and a lower band. The distance between these bands expands and contracts based on market volatility:
- When volatility is high, the bands widen.
- When volatility is low, the bands contract.
The bands can signal overbought and oversold conditions:
- Price above the upper band suggests overbought conditions.
- Price below the lower band indicates oversold conditions.
Calculating Bollinger Bands
1. Middle Band:
The middle band is typically a 20-period Simple Moving Average (SMA).
2. Upper Band:
The upper band is calculated by adding two standard deviations to the middle band.
3. Lower Band:
The lower band is calculated by subtracting two standard deviations from the middle band.
The standard deviation multiplier is usually set at 2, but this can be adjusted based on the asset’s volatility.
Applications of Bollinger Bands in Algorithmic Trading
1. Bollinger Band Squeeze:
- The “squeeze” occurs when the bands contract due to low volatility, signalling that a breakout may be imminent. When the bands start expanding, a strong price movement often follows.
2. Breakout Strategy:
- In a breakout strategy, traders buy when the price closes above the upper band (indicating a bullish breakout) and sell when it closes below the lower band (indicating a bearish breakout).
3. Mean Reversion:
- Bollinger Bands can also be used for mean reversion strategies, where trades are initiated on the expectation that price will return to the middle band after reaching the upper or lower band.
Bollinger Band Breakout Strategy Example in MQL4
The following MQL4 example demonstrates a Bollinger Band breakout strategy. The code initiates a buy order when the price crosses above the upper band and a sell order when the price crosses below the lower band.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | // Bollinger Band Breakout Strategy in MQL4 input int BandPeriod = 20; // Period for Bollinger Bands input double BandMultiplier = 2.0; // Multiplier for Standard Deviation input double LotSize = 0.1; // Lot size for orders // Global variables to track trade status bool buyTradeOpen = false; bool sellTradeOpen = false; void OnTick() { double upperBand = iBands(NULL, 0, BandPeriod, BandMultiplier, 0, PRICE_CLOSE, MODE_UPPER, 0); double lowerBand = iBands(NULL, 0, BandPeriod, BandMultiplier, 0, PRICE_CLOSE, MODE_LOWER, 0); double closePrice = iClose(NULL, 0, 0); // Buy signal: Price crosses above the upper Bollinger Band if (closePrice > upperBand && !buyTradeOpen) { if (sellTradeOpen) CloseSell(); OpenBuy(); } // Sell signal: Price crosses below the lower Bollinger Band else if (closePrice < lowerBand && !sellTradeOpen) { if (buyTradeOpen) CloseBuy(); OpenSell(); } } // Function to open a buy position void OpenBuy() { int ticket = OrderSend(Symbol(), OP_BUY, LotSize, Ask, 3, 0, 0, "Bollinger Buy", 0, 0, clrGreen); if (ticket > 0) { buyTradeOpen = true; sellTradeOpen = false; Print("Buy order opened."); } else { Print("Error opening buy order: ", GetLastError()); } } // Function to open a sell position void OpenSell() { int ticket = OrderSend(Symbol(), OP_SELL, LotSize, Bid, 3, 0, 0, "Bollinger Sell", 0, 0, clrRed); if (ticket > 0) { sellTradeOpen = true; buyTradeOpen = false; Print("Sell order opened."); } else { Print("Error opening sell order: ", GetLastError()); } } // Function to close any open buy position void CloseBuy() { for (int i = OrdersTotal() - 1; i >= 0; i--) { if (OrderSelect(i, SELECT_BY_POS) && OrderType() == OP_BUY) { OrderClose(OrderTicket(), OrderLots(), Bid, 3, clrGreen); buyTradeOpen = false; Print("Buy order closed."); } } } // Function to close any open sell position void CloseSell() { for (int i = OrdersTotal() - 1; i >= 0; i--) { if (OrderSelect(i, SELECT_BY_POS) && OrderType() == OP_SELL) { OrderClose(OrderTicket(), OrderLots(), Ask, 3, clrRed); sellTradeOpen = false; Print("Sell order closed."); } } } |
Explanation of the Bollinger Band Strategy
1. Parameters:
BandPeriod
andBandMultiplier
determine the period and standard deviation multiplier for calculating the Bollinger Bands.LotSize
specifies the trade size for each order.
2. Trade Execution:
- A buy signal occurs when the price closes above the upper band, indicating a bullish breakout.
- A sell signal occurs when the price closes below the lower band, indicating a bearish breakout.
3. Functions:
OpenBuy()
andOpenSell()
place buy and sell trades based on Bollinger Band breakout signals.CloseBuy()
andCloseSell()
close positions when an opposing signal appears.
This strategy aims to capture breakout trends, which are often followed by strong price movements, by trading the Bollinger Band breakout.
Tips for Optimising Bollinger Band Strategies
- Adjust Band Period and Multiplier: Experiment with the Bollinger Band period and multiplier based on the asset’s volatility to improve accuracy.
- Add Volume Indicators: Volume indicators can confirm breakouts, helping avoid false signals in low-volume markets.
- Use with Other Trend Indicators: Combine Bollinger Bands with a trend indicator like the EMA to ensure trades align with the broader market direction.
Conclusion
Bollinger Bands are essential for volatility trading in algorithmic strategies, offering insight into price extremes and potential breakouts. This MQL4 example provides a starting point for implementing Bollinger Band-based breakout strategies, allowing traders to capture momentum in volatile markets. In the next article, we’ll discuss Stochastic Oscillator in Mean Reversion Strategies, including calculations and MQL4 examples for identifying market reversals.