3-Bar Reversal
Introduction
3-Bar Reversal is a technical trading pattern used primarily in algorithmic trading. It is a type of price pattern that indicates a potential reversal in the current trend of a financial instrument. Although commonly employed in manual trading strategies, its rules-based structure makes it ideal for integration into automated trading systems. This strategy is popular among day traders and swing traders looking to capture quick reversals in the market.
Anatomy of the 3-Bar Reversal Pattern
The 3-Bar Reversal pattern consists of three consecutive bars (candlesticks):
- First Bar: The trend-setting bar which could be either bullish or bearish.
- Second Bar: A smaller bar that continues in the same direction as the first bar but has less momentum.
- Third Bar: The reversal bar, which moves in the opposite direction of the first two bars, signaling a reversal.
Bullish 3-Bar Reversal
- First Bar: A bearish bar, indicating a downtrend.
- Second Bar: A smaller bearish bar that continues the downtrend but with less momentum.
- Third Bar: A bullish bar that closes above the high of the first bar.
Bearish 3-Bar Reversal
- First Bar: A bullish bar, indicating an uptrend.
- Second Bar: A smaller bullish bar that continues the uptrend but with less momentum.
- Third Bar: A bearish bar that closes below the low of the first bar.
Mathematical Formulation
The pattern can be mathematically expressed for algorithmic implementation. Let’s denote the opening price, closing price, high, and low of the bar at time ( t ) as ( O_t ), ( C_t ), ( H_t ), ( L_t ), respectively.
Bullish Reversal Conditions:
- ( C_{t-2} < O_{t-2} ) (Bearish first bar)
- ( C_{t-1} < C_{t-2} ) and ( C_{t-1} > L_{t-2} ) (Bearish second bar with less momentum)
- ( C_{t} > O_{t} ) and ( C_{t} > H_{t-2} ) (Bullish reversal bar)
Bearish Reversal Conditions:
- ( C_{t-2} > O_{t-2} ) (Bullish first bar)
- ( C_{t-1} > C_{t-2} ) and ( C_{t-1} < H_{t-2} ) (Bullish second bar with less momentum)
- ( C_{t} < O_{t} ) and ( C_{t} < L_{t-2} ) (Bearish reversal bar)
Example
Consider a stock trading at various price points over three consecutive periods:
Bullish Reversal:
- Day 1: Open = $100, Close = $95, High = $101, Low = $94
- Day 2: Open = $94, Close = $92, High = $96, Low = $91
- Day 3: Open = $93, Close = $97, High = $98, Low = $92
In this example, the third day’s closing price ($97) is higher than the first day’s high ($101), confirming a bullish 3-bar reversal.
Bearish Reversal:
- Day 1: Open = $100, Close = $105, High = $106, Low = $99
- Day 2: Open = $104, Close = $107, High = $108, Low = $103
- Day 3: Open = $106, Close = $102, High = $107, Low = $100
Here, the third day’s closing price ($102) is lower than the first day’s low ($100), indicating a bearish 3-bar reversal.
Applications in Algorithmic Trading
Algorithmic traders use 3-bar reversal patterns to automate the decision-making process. The pattern is incorporated into trading algorithms to:
- Identify Entry Points: The completion of a 3-bar reversal pattern is treated as a signal to enter a trade in the direction of the reversal.
- Set Stop-Loss Orders: To manage risk, stop-loss orders are often set at the high (for bearish reversals) or low (for bullish reversals) of the reversal bar.
- Optimize Algorithms: Backtesting on historical data is used to optimize parameters and ensure the effectiveness of the 3-bar reversal strategy.
Coding the 3-Bar Reversal Strategy
Python Example with pandas
[import](../i/import.html) pandas as pd
def identify_3_bar_reversal(data):
signals = []
for i in [range](../r/range.html)(2, len(data)):
first_bar = data.iloc[i-2]
second_bar = data.iloc[i-1]
third_bar = data.iloc[i]
# [Check](../c/check.html) Bullish [Reversal](../r/reversal.html)
if (first_bar['Close'] < first_bar['[Open](../o/open.html)'] and
second_bar['Close'] < first_bar['Close'] and
third_bar['Close'] > third_bar['[Open](../o/open.html)'] and
third_bar['Close'] > first_bar['High']):
signals.append((data.[index](../i/index_instrument.html)[i], "BUY"))
# [Check](../c/check.html) Bearish [Reversal](../r/reversal.html)
elif (first_bar['Close'] > first_bar['[Open](../o/open.html)'] and
second_bar['Close'] > first_bar['Close'] and
third_bar['Close'] < third_bar['[Open](../o/open.html)'] and
third_bar['Close'] < first_bar['Low']):
signals.append((data.[index](../i/index_instrument.html)[i], "SELL"))
[return](../r/return.html) signals
# Sample Data
data = pd.DataFrame({
'[Open](../o/open.html)': [100, 94, 93, 100, 104, 106],
'Close': [95, 92, 97, 105, 107, 102],
'High': [101, 96, 98, 106, 108, 107],
'Low': [94, 91, 92, 99, 103, 100]
}, [index](../i/index_instrument.html)=pd.date_range(start='2023-01-01', periods=6))
signals = identify_3_bar_reversal(data)
print(signals)
Limitations of the 3-Bar Reversal
Although effective, the 3-bar reversal pattern has its limitations:
- False Signals: Like all trading patterns, it can generate false signals, leading to potential losses.
- Parameter Sensitivity: The effectiveness can vary with different market conditions and instrument types.
- Lagging Indicator: Being a reversal pattern, it lags behind the current price action and may miss the optimal entry or exit points.
Case Study: QuantConnect Integration
QuantConnect is a cloud-based platform that provides algorithmic trading infrastructure capable of implementing various trading strategies, including the 3-bar reversal pattern. Users can write code in Python, C#, or F# to backtest and deploy their strategies. For more details, visit their website: QuantConnect.
Implementation Example:
from AlgorithmImports [import](../i/import.html) *
class ThreeBarReversalAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2022, 1, 1) # Set Start Date
self.SetEndDate(2022, 12, 31) # Set End Date
self.SetCash(100000) # Set Strategy Cash
self.AddEquity("SPY", Resolution.Daily)
self.window = RollingWindow[QuoteBar](3)
def OnData(self, data):
self.window.Add(data["SPY"])
if self.window.IsReady:
first_bar = self.window[2]
second_bar = self.window[1]
third_bar = self.window[0]
# [Check](../c/check.html) for Bullish [Reversal](../r/reversal.html)
if (first_bar.Close < first_bar.[Open](../o/open.html) and
second_bar.Close < first_bar.Close and
third_bar.Close > third_bar.[Open](../o/open.html) and
third_bar.Close > first_bar.High):
self.SetHoldings("SPY", 1)
self.Debug(f"BUY Signal on {self.Time}")
# [Check](../c/check.html) for Bearish [Reversal](../r/reversal.html)
elif (first_bar.Close > first_bar.[Open](../o/open.html) and
second_bar.Close > first_bar.Close and
third_bar.Close < third_bar.[Open](../o/open.html) and
third_bar.Close < first_bar.Low):
self.SetHoldings("SPY", -1)
self.Debug(f"SELL Signal on {self.Time}")
Conclusion
The 3-Bar Reversal is a powerful pattern for detecting potential trend reversals in financial markets. Its structured nature facilitates easy integration into algorithmic trading systems, where it can be used to trigger buy and sell signals based on predefined criteria. However, it’s essential to consider its limitations and backtest thoroughly to ensure its effectiveness in different market conditions.
By leveraging platforms like QuantConnect, traders can develop, backtest, and deploy sophisticated strategies that incorporate the 3-bar reversal pattern, aligning automated decisions with their trading goals.