AAPL Bollinger Band Rebound Strategy
Buying the dip is a classic contrarian strategy -- but can we validate it quantitatively?
Bollinger Bands are one of the most widely known tools for contrarian trading. They work by computing a moving average and standard deviation over a rolling window of d days, creating an upper and lower band at n standard deviations from the mean. Most price action stays within these bands, so excursions beyond them signal potential mean reversion opportunities.
- Larger
dvalues produce a smoother price envelope, filtering out short-term noise. - Larger
nvalues generate more extreme (and rarer) signals. Following the 68-95-99.7 rule,nis typically set between 1 and 3. Atn = 2, roughly 95% of price observations fall within the bands under a normal distribution.
When a stock's price drops below the lower band, it may be exhibiting an oversold signal. The hypothesis is that price will subsequently mean-revert and bounce back toward the middle band.
Bollinger Band Strategy
The following strategy uses a 40-day moving average with 2.5 standard deviations as the Bollinger Band parameters, applied to Apple (AAPL). The entry signal fires when the closing price falls below the lower band. The exit signal fires when price recovers above the middle band.
from finlab import data
from finlab.backtest import sim
from finlab.market import USMarket
data.set_market('us')
close = data.get('price:adj_close')
close = close[close.index.dayofweek < 5] # remove weekends
upperband, middleband, lowerband = data.indicator('BBANDS', resample='D', nbdevup=float(2.5), nbdevdn=float(2.5), timeperiod=40)
entries = lowerband > close
exits = close > middleband
position = entries.hold_until(exits)['AAPL']
report = sim(position, market=USMarket(), fee_ratio=0.001, tax_ratio=0, mae_mfe_window=40)
report.display()
MAE/MFE Analysis
MAE (Maximum Adverse Excursion) and MFE (Maximum Favorable Excursion) measure the worst drawdown and best run-up experienced during each trade's holding period. This analysis helps you understand the risk/reward profile of the strategy at a per-trade level.
Cumulative Return Chart
Trade Records
Adding Stop Loss and Take Profit
Catching a falling knife is risky -- whether or not you grab it, you should be ready to let go quickly.
In extreme sell-off conditions, this contrarian approach can achieve a high win rate with short holding periods. However, adding explicit exit rules can significantly improve risk management:
- If the position is profitable during the first 10 holding days, the dip was likely caught near the bottom, and mean reversion is playing out as expected.
- If the position is underwater during the first 10 days, the bottom may not have been reached yet, and further downside is possible.
- If the loss exceeds 15%, it may indicate a rare, out-of-sample crash event, and the position should be cut immediately.
The modified strategy adds a 10% take-profit target and a 15% stop-loss limit:
from finlab import data
from finlab.backtest import sim
from finlab.market import USMarket
data.set_market('us')
close = data.get('price:adj_close')
close = close[close.index.dayofweek < 5] # remove weekends
upperband, middleband, lowerband = data.indicator('BBANDS', resample='D', nbdevup=float(2.5), nbdevdn=float(2.5), timeperiod=40)
entries = lowerband > close
exits = close > middleband
position = entries.hold_until(exits, take_profit=0.1, stop_loss=-0.15)['AAPL']
report = sim(position, market=USMarket(), fee_ratio=0.001, tax_ratio=0, mae_mfe_window=40)
report.display_mae_mfe_analysis(violinmode='overlay').show()
report.display()
Key Parameters
nbdevup=2.5/nbdevdn=2.5: Uses 2.5 standard deviations for the upper and lower bands, generating relatively rare and extreme entry signals.timeperiod=40: A 40-day lookback window for the moving average and standard deviation, balancing smoothness with responsiveness.mae_mfe_window=40: Tracks MAE/MFE over a 40-day window after each entry, providing a detailed picture of per-trade risk dynamics.take_profit=0.1: Exits the position when it reaches a 10% gain from the entry price.stop_loss=-0.15: Exits the position when it reaches a 15% loss from the entry price.fee_ratio=0.001: Commission rate of 0.1%, a reasonable estimate for US brokerage fees.tax_ratio=0: No transaction tax on US stock trades.
Strategy Characteristics
- This is a single-stock contrarian strategy focused exclusively on AAPL. It waits for statistically extreme price drops and bets on mean reversion.
- Trades are infrequent -- AAPL may only trigger a few entries per year, so patience is required.
- The stop-loss/take-profit variant adds disciplined risk management, reducing tail risk at the cost of potentially capping some winning trades.
- The MAE/MFE analysis is especially valuable for calibrating stop-loss and take-profit levels based on historical trade behavior.