Adaptive Bollinger Bands ATR Trailing Stop Trading Strategy
This strategy is an adaptive trading system that combines Bollinger Bands and ATR trailing stop loss. It uses Bollinger Bands breakouts for entry signals while implementing an ATR-based dynamic trailing stop loss for…

Overview
This strategy is an adaptive trading system that combines Bollinger Bands and ATR trailing stop loss. It uses Bollinger Bands breakouts for entry signals while implementing an ATR-based dynamic trailing stop loss for risk management and exit timing. The strategy is designed to capture trending opportunities while providing protection in ranging markets.
Strategy Principles
The core logic consists of two main components:
- Entry Signal System: Uses Bollinger Bands (BB) as the primary indicator, generating long signals when price breaks above the lower band and short signals when price breaks below the upper band. BB parameters are set to 20-period moving average as the middle band with a standard deviation multiplier of 2.0.
- Stop Loss Management System: Employs a 14-period ATR with a multiplier of 3.0 for volatility reference. The stop loss line moves up with price increases during long positions and vice versa. This dynamic stop loss mechanism allows profits to grow naturally while effectively controlling drawdowns.
Strategy Advantages
- High Adaptability: Both Bollinger Bands and ATR are calculated based on actual market volatility, automatically adapting to different market conditions.
- Comprehensive Risk Control: The ATR dynamic stop loss enables timely exits while avoiding premature exits in strong trends.
- Clear Signals: Entry and exit signals are based on definitive price breakouts, requiring no subjective judgment.
- High Visualization: The strategy clearly marks all signal points on the chart, facilitating analysis and optimization.
Strategy Risks
- Ranging Market Risk: In markets without clear trends, frequent false breakouts may lead to consecutive stop losses.
- Slippage Risk: During high volatility periods, actual execution prices may significantly deviate from theoretical signal prices.
- Parameter Sensitivity: Strategy performance is sensitive to Bollinger Bands and ATR parameter settings, requiring optimization for different market environments.
Strategy Optimization Directions
- Add Trend Filters: Additional trend identification indicators can be incorporated to open positions only in clear trends, reducing false signals in ranging markets.
- Optimize Stop Loss Parameters: ATR multiplier can be dynamically adjusted based on different market conditions, using wider stops in high volatility periods.
- Implement Position Sizing: Design a dynamic position sizing system based on ATR to automatically adjust position size in different volatility environments.
- Include Time Filters: Avoid trading during high volatility periods such as major economic data releases.
Summary
The strategy combines Bollinger Bands and ATR trailing stop loss to create a trading system that balances trend capture and risk control capabilities. Its adaptive nature maintains stability across different market environments, while the clear signal system provides objective trading criteria. The suggested optimization directions offer room for further improvement. In practical application, investors should adjust parameters according to their risk preferences and specific trading instrument characteristics.
Source Pine Script
/*backtest
start: 2025-01-19 00:00:00
end: 2025-02-18 00:00:00
period: 1h
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("ATR Trailing Stop Loss with Bollinger Bands", overlay=true)
// Input parameters for Bollinger Bands
bb_length = input.int(20, title="Bollinger Bands Length")
bb_stddev = input.float(2.0, title="Bollinger Bands Std Dev")
// Input parameters for ATR Trailing Stop Loss
atr_length = input.int(14, title="ATR Length")
atr_multiplier = input.float(3.0, title="ATR Multiplier")
// Calculate Bollinger Bands
basis = ta.sma(close, bb_length)
upper_band = ta.sma(close, bb_length) + ta.stdev(close, bb_length) * bb_stddev
lower_band = ta.sma(close, bb_length) - ta.stdev(close, bb_length) * bb_stddev
// Calculate ATR
atr = ta.atr(atr_length)
// Trailing Stop Loss Calculation
var float long_stop = na // Explicitly define as float type
var float short_stop = na // Explicitly define as float type
if (strategy.position_size > 0)
long_stop := close - atr * atr_multiplier
long_stop := math.max(long_stop, nz(long_stop[1], long_stop))
else
long_stop := na
if (strategy.position_size < 0)
short_stop := close + atr * atr_multiplier
short_stop := math.min(short_stop, nz(short_stop[1], short_stop))
else
short_stop := na
// Entry and Exit Conditions
long_condition = ta.crossover(close, lower_band) // Enter long when price crosses above lower band
short_condition = ta.crossunder(close, upper_band) // Enter short when price crosses below upper band
exit_long_condition = ta.crossunder(close, long_stop) // Exit long when price crosses below trailing stop
exit_short_condition = ta.crossover(close, short_stop) // Exit short when price crosses above trailing stop
// Execute Trades
if (long_condition)
strategy.entry("Long", strategy.long)
if (short_condition)
strategy.entry("Short", strategy.short)
if (exit_long_condition)
strategy.close("Long")
if (exit_short_condition)
strategy.close("Short")
// Plot Bollinger Bands
plot(basis, color=color.blue, title="Basis")
plot(upper_band, color=color.red, title="Upper Band")
plot(lower_band, color=color.green, title="Lower Band")
// Plot Trailing Stop Loss
plot(strategy.position_size > 0 ? long_stop : na, color=color.orange, title="Long Trailing Stop")
plot(strategy.position_size < 0 ? short_stop : na, color=color.purple, title="Short Trailing Stop")
// Labels for Entry and Exit
if (long_condition)
label.new(bar_index, low, text="Entry Long", style=label.style_circle, color=color.green, textcolor=color.white, size=size.small)
if (short_condition)
label.new(bar_index, high, text="Entry Short", style=label.style_circle, color=color.red, textcolor=color.white, size=size.small)
if (exit_long_condition)
label.new(bar_index, low, text="Exit Long", style=label.style_circle, color=color.blue, textcolor=color.white, size=size.small)
if (exit_short_condition)
label.new(bar_index, high, text="Exit Short", style=label.style_circle, color=color.orange, textcolor=color.white, size=size.small)Published by ChaoZhang on the FMZ strategy square · last modified 2025-02-19. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.
Build this with AgenKit
A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Adaptive-Bollinger-Bands-ATR-Trailing-Stop-Trading-Strategy.md- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- Get tests, backtest, risk controls and a paper-trading switch
Understand it first
Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.
Related specs
- BB Breakout StrategyPine Script · Breakout · 1d
- Bollinger Band Breakout Trend Following StrategyPine Script · Breakout · 1d
- EMA and Bollinger Bands Breakout StrategyPine Script · Breakout · 1h
- Follow Line StrategyPine Script · Breakout · 10m
- Momentum Breakout Moving Average StrategyPine Script · Breakout · 1d
- Momentum Breakout Trading Strategy 440964Pine Script · Breakout · 5m