//@version=6
strategy("复合波段交易策略[优化版]",
overlay=true,
pyramiding=1,
initial_capital=100000,
currency=currency.USD,
calc_on_every_tick=false) // 防止重复计算
// ======= 输入参数模块 =======
fastLength = input.int(10, "短期均线周期", minval=1, group="趋势参数")
slowLength = input.int(20, "长期均线周期", minval=1, group="趋势参数")
rsiLength = input.int(14, "RSI周期", minval=1, maxval=100, group="震荡指标")
bbLength = input.int(20, "布林带周期", minval=1, group="波动通道")
mult = input.float(2.0, "标准差倍数", step=0.1, minval=1.5, maxval=3.0, group="波动通道")
stopLossPct = input.float(1.5, "止损比例 (%)", step=0.1, minval=0.5, maxval=5)/100
takeProfitPct = input.float(3.5, "止盈比例 (%)", step=0.1, minval=1.0, maxval=10)/100
tradeSizePct = input.float(15, "单笔风险比例 (%)", step=0.5, minval=5, maxval=30)/100
// ======= 核心指标计算 =======
// EMA双线系统
fastMA = ta.ema(close, fastLength)
slowMA = ta.ema(close, slowLength)
// RSI动量指标
rsi = ta.rsi(math.min(close, high), rsiLength) // 防止极端值
// 布林带通道(使用标准函数)
[bbUpper, bbMiddle, bbLower] = ta.bollinger(close, bbLength, mult)
// ======= 信号生成系统 =======
bullishCross = ta.crossover(fastMA, slowMA)
bearishCross = ta.crossunder(fastMA, slowMA)
// 增强型过滤条件
oversoldCondition = rsi <= 35 and rsi > rsi[1] // RSI回升中
overboughtCondition = rsi >= 65 and rsi < rsi[1] // RSI回落中
priceBelowBB = low < bbLower and close > bbLower // 假突破过滤
priceAboveBB = high > bbUpper and close < bbUpper
longCondition = bullishCross and (oversoldCondition or priceBelowBB)
shortCondition = bearishCross and (overboughtCondition or priceAboveBB)
// ======= 风险管理模块 =======
var float positionSize = na
positionSize := strategy.equity * tradeSizePct / close
// 止损止盈计算(入场时锁定)
var float stopLossLevel = na
var float takeProfitLevel = na
if (longCondition or shortCondition)
stopLossLevel := close * (1 - (strategy.position_size > 0 ? stopLossPct : -stopLossPct))
takeProfitLevel := close * (1 + (strategy.position_size > 0 ? takeProfitPct : -takeProfitPct))
// ======= 交易执行系统 =======
// 多单逻辑
if (longCondition and strategy.position_size <= 0)
strategy.entry("Long", strategy.long, qty=math.floor(positionSize))
strategy.exit("Exit Long", "Long",
stop=stopLossLevel,
limit=takeProfitLevel,
comment="多头平仓")
// 空单逻辑
if (shortCondition and strategy.position_size >= 0)
strategy.entry("Short", strategy.short, qty=math.floor(positionSize))
strategy.exit("Exit Short", "Short",
stop=stopLossLevel,
limit=takeProfitLevel,
comment="空头平仓")
// ======= 可视化增强系统 =======
// 均线系统
plot(fastMA, "快线", #00BCD4, 2, plot.style_linebr)
plot(slowMA, "慢线", #FF9800, 2, plot.style_linebr)
// 布林通道填充
bbFill = plot(bbUpper, "布林上轨", #2196F3, 1, transp=70, display=display.none)
plot(bbLower, "布林下轨", #4CAF50, 1, transp=70, display=display.none)
fill(bbFill, bbFill, color=color.new(#2196F3, 90), title="通道填充")
// 信号标记
plotshape(longCondition, "买入信号", shape.triangleup, location.belowbar, #4CAF50, 0, "", text="B")
plotshape(shortCondition, "卖出信号", shape.triangledown, location.abovebar, #F44336, 0, "", text="S")
// ======= 动态监控面板 =======
var table infoTable = table.new(position.top_right, 3, 1, bgcolor=color.rgb(33, 33, 33))
if barstate.islast
table.cell(infoTable, 0, 0, "策略状态:" +
(strategy.position_size > 0 ? "▶ 持多单" :
strategy.position_size < 0 ? "▶ 持空单" : "▷ 中立"),
text_color=strategy.position_size != 0 ? color.yellow : color.white)
table.cell(infoTable, 1, 0, "RSI:" + str.tostring(rsi, "#.##") +
" | 通道偏离:" + str.tostring((close-bbMiddle)/bbMiddle*100, "+#.##") + "%",
text_color=color.silver)
table.cell(infoTable, 2, 0, "止损:" + str.tostring(stopLossLevel, "#.##") +
" | 止盈:" + str.tostring(takeProfitLevel, "#.##"),
text_color=color.gray) 2:Syntax error at input "end of line without line continuation"