本策略是一个基于布林带和ATR指标的智能交易系统,结合了多层次的止盈止损机制。策略主要通过识别布林带下轨附近的反转信号进行多头入场,并采用动态跟踪止损方法管理风险。系统设计了20%获利目标和12%止损位,同时结合ATR指标实现动态跟踪止损,能够在保护利润的同时给予趋势足够的发展空间。
策略的核心逻辑包括以下几个关键部分: 1. 入场条件:要求红色蜡烛触及布林带下轨后出现绿色蜡烛,这种形态通常预示着可能的反转信号。 2. 移动平均线选择:支持多种移动平均线类型(SMA、EMA、SMMA、WMA、VWMA),默认使用20周期SMA。 3. 布林带参数:使用1.5倍标准差作为带宽,这个设置比传统的2倍标准差更为保守。 4. 止盈机制:设定20%的初始获利目标。 5. 止损机制:设置12%的固定止损位保护资金。 6. 动态跟踪止损: - 价格达到目标获利水平后激活ATR跟踪止损 - 触及布林带上轨后启动ATR动态跟踪止损 - 使用ATR乘数动态调整跟踪止损距离
该策略通过布林带和ATR指标构建了一个多层次的交易系统,在入场、止损和获利了结等方面都采用了动态管理方法。策略的优势在于其完善的风险控制体系和对市场波动的自适应能力。通过建议的优化方向,策略还有很大的提升空间。特别适合在较大时间周期上使用,对于持有优质资产的投资者来说,可以帮助优化建仓和减仓时机。
/*backtest
start: 2019-12-23 08:00:00
end: 2024-12-09 08:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("Demo GPT - Bollinger Bands Strategy with Tightened Trailing Stops", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_value=0.1, slippage=3)
// Input settings
length = input.int(20, minval=1)
maType = input.string("SMA", "Basis MA Type", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"])
src = input(close, title="Source")
mult = 1.5 // Standard deviation multiplier set to 1.5
offset = input.int(0, "Offset", minval=-500, maxval=500)
atrMultiplier = input.float(1.0, title="ATR Multiplier for Trailing Stop", minval=0.1) // ATR multiplier for trailing stop
// Time range filters
start_date = input(timestamp("2018-01-01 00:00"), title="Start Date")
end_date = input(timestamp("2069-12-31 23:59"), title="End Date")
in_date_range = true
// Moving average function
ma(source, length, _type) =>
switch _type
"SMA" => ta.sma(source, length)
"EMA" => ta.ema(source, length)
"SMMA (RMA)" => ta.rma(source, length)
"WMA" => ta.wma(source, length)
"VWMA" => ta.vwma(source, length)
// Calculate Bollinger Bands
basis = ma(src, length, maType)
dev = mult * ta.stdev(src, length)
upper = basis + dev
lower = basis - dev
// ATR Calculation
atr = ta.atr(length) // Use ATR for trailing stop adjustments
// Plotting
plot(basis, "Basis", color=#2962FF, offset=offset)
p1 = plot(upper, "Upper", color=#F23645, offset=offset)
p2 = plot(lower, "Lower", color=#089981, offset=offset)
fill(p1, p2, title="Background", color=color.rgb(33, 150, 243, 95))
// Candle color detection
isGreen = close > open
isRed = close < open
// Flags for entry and exit conditions
var bool redTouchedLower = false
var float targetPrice = na
var float stopLossPrice = na
var float trailingStopPrice = na
if in_date_range
// Entry Logic: First green candle after a red candle touches the lower band
if close < lower and isRed
redTouchedLower := true
if redTouchedLower and isGreen
strategy.entry("Long", strategy.long)
targetPrice := close * 1.2 // Set the target price to 20% above the entry price
stopLossPrice := close * 0.88 // Set the stop loss to 12% below the entry price
trailingStopPrice := na // Reset trailing stop on entry
redTouchedLower := false
// Exit Logic: Trailing stop after 20% price increase
if strategy.position_size > 0 and not na(targetPrice) and close >= targetPrice
if na(trailingStopPrice)
trailingStopPrice := close - atr * atrMultiplier // Initialize trailing stop using ATR
trailingStopPrice := math.max(trailingStopPrice, close - atr * atrMultiplier) // Tighten dynamically based on ATR
// Exit if the price falls below the trailing stop after 20% increase
if strategy.position_size > 0 and not na(trailingStopPrice) and close < trailingStopPrice
strategy.close("Long", comment="Trailing Stop After 20% Increase")
targetPrice := na // Reset the target price
stopLossPrice := na // Reset the stop loss price
trailingStopPrice := na // Reset trailing stop
// Stop Loss: Exit if the price drops 12% below the entry price
if strategy.position_size > 0 and not na(stopLossPrice) and close <= stopLossPrice
strategy.close("Long", comment="Stop Loss Triggered")
targetPrice := na // Reset the target price
stopLossPrice := na // Reset the stop loss price
trailingStopPrice := na // Reset trailing stop
// Trailing Stop: Activate after touching the upper band
if strategy.position_size > 0 and close >= upper and isGreen
if na(trailingStopPrice)
trailingStopPrice := close - atr * atrMultiplier // Initialize trailing stop using ATR
trailingStopPrice := math.max(trailingStopPrice, close - atr * atrMultiplier) // Tighten dynamically based on ATR
// Exit if the price falls below the trailing stop
if strategy.position_size > 0 and not na(trailingStopPrice) and close < trailingStopPrice
strategy.close("Long", comment="Trailing Stop Triggered")
trailingStopPrice := na // Reset trailing stop
targetPrice := na // Reset the target price
stopLossPrice := na // Reset the stop loss price