本策略是一个结合了布林带(Bollinger Bands)和ATR跟踪止损的自适应交易系统。策略通过布林带上下轨的突破来确定入场信号,同时使用基于ATR的动态跟踪止损来管理风险和确定出场时机。该策略能够在市场趋势明显时捕捉趋势性机会,同时在震荡市中提供防护。
策略的核心逻辑包含两个主要部分: 1. 入场信号系统:使用布林带(BB)作为主要指标,当价格突破下轨时产生做多信号,突破上轨时产生做空信号。布林带参数设置为20周期移动平均线作为中轨,标准差倍数为2.0。 2. 止损管理系统:采用14周期ATR作为波动率参考,乘数为3.0。在持有多头仓位时,止损线会随着价格上涨而上移,反之亦然。这种动态止损机制能够让利润自然增长的同时,有效控制回撤。
该策略通过结合布林带和ATR跟踪止损,构建了一个兼具趋势捕捉和风险控制能力的交易系统。策略的自适应特性使其能够在不同市场环境下保持稳定性,而清晰的信号系统则提供了客观的交易依据。通过建议的优化方向,策略还有进一步提升的空间。在实际应用中,建议投资者根据自身风险偏好和交易品种特点,对参数进行针对性调整。
/*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)