Pine Script SuperTrend策略:实现订单提前平仓而非同K线平仓
解决SuperTrend策略新信号触发前提前平仓旧订单的问题
问题说明
使用「SuperTrend Cryptobase」Pine Script策略通过webhook自动化交易时,新买卖信号触发时旧订单无法在新信号执行前完成平仓——现有代码中strategy.close与新订单strategy.entry在同一K线执行,需要调整为在新信号发出的前一根K线就提前平仓旧订单。
解决方案
核心思路是提前识别趋势反转的前兆,在正式触发买卖信号的前一根K线就平仓对应旧仓位:
- SuperTrend的趋势反转逻辑是:当价格突破下方的dn线时转多,跌破上方的up线时转空
- 在趋势即将反转的K线(尚未正式触发buy/sellSignal),就提前平仓反向仓位,确保新信号触发时旧仓位已完全平仓
具体修改步骤:
- 新增趋势反转的前置判断条件,捕捉即将转多/转空的信号
- 在前置条件触发时平仓旧仓位,与正式买卖信号的执行分开
- 保留原有的信号触发与新订单执行逻辑,确保策略逻辑一致性
修改后的完整代码
//@version=4 strategy("SuperTrend Cryptobase", overlay=true) Periods = input(title="ATR Period", type=input.integer, defval=10) src = input(hl2, title="Source") Multiplier = input(title="ATR Multiplier", type=input.float, step=0.1, defval=3.0) changeATR= input(title="Change ATR Calculation Method ?", type=input.bool, defval=true) showsignals = input(title="Show Buy/Sell Signals ?", type=input.bool, defval=false) highlighting = input(title="Highlighter On/Off ?", type=input.bool, defval=true) barcoloring = input(title="Bar Coloring On/Off ?", type=input.bool, defval=true) atr2 = sma(tr, Periods) atr= changeATR ? atr(Periods) : atr2 up=src-(Multiplier*atr) up1 = nz(up[1],up) up := close[1] > up1 ? max(up,up1) : up dn=src+(Multiplier*atr) dn1 = nz(dn[1], dn) dn := close[1] < dn1 ? min(dn, dn1) : dn trend = 1 trend := nz(trend[1], trend) trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend // 新增趋势反转前置条件:提前识别即将转多/转空的信号 preBuyCondition = trend == -1 and close > dn1 // 即将转多,下一根K线会触发buySignal preSellCondition = trend == 1 and close < up1 // 即将转空,下一根K线会触发sellSignal upPlot = plot(trend == 1 ? up : na, title="Up Trend", style=plot.style_linebr, linewidth=2, color=color.rgb(5, 102, 86)) buySignal = trend == 1 and trend[1] == -1 plotshape(buySignal ? up : na, title="UpTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.rgb(5, 102, 86), transp=0) plotshape(buySignal and showsignals ? up : na, title="Comprar", text="Comprar", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.rgb(5, 102, 86), textcolor=color.white, transp=0) dnPlot = plot(trend == 1 ? na : dn, title="Down Trend", style=plot.style_linebr, linewidth=2, color=color.rgb(178, 40, 51)) sellSignal = trend == -1 and trend[1] == 1 plotshape(sellSignal ? dn : na, title="DownTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.rgb(178, 40, 51), transp=0) plotshape(sellSignal and showsignals ? dn : na, title="Vender", text="Vender", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.rgb(178, 40, 51), textcolor=color.white, transp=0) mPlot = plot(ohlc4, title="", style=plot.style_circles, linewidth=0) longFillColor = highlighting ? (trend == 1 ? color.rgb(5, 102, 86, 95) : color.white) : color.white shortFillColor = highlighting ? (trend == -1 ? color.rgb(178, 40, 51, 95) : color.white) : color.white fill(mPlot, upPlot, title="UpTrend Highligter", color=longFillColor) fill(mPlot, dnPlot, title="DownTrend Highligter", color=shortFillColor) FromMonth = input(defval = 9, title = "From Month", minval = 1, maxval = 12) FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31) FromYear = input(defval = 2018, title = "From Year", minval = 999) ToMonth = input(defval = 1, title = "To Month", minval = 1, maxval = 12) ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31) ToYear = input(defval = 9999, title = "To Year", minval = 999) start = timestamp(FromYear, FromMonth, FromDay, 00, 00) finish = timestamp(ToYear, ToMonth, ToDay, 23, 59) window() => time >= start and time <= finish ? true : false //Alerts alertcondition(buySignal, title="SuperTrend Compra", message="SuperTrend Compra!") alertcondition(sellSignal, title="SuperTrend Venda", message="SuperTrend Venda!") // 提前平仓旧仓位:在趋势即将反转的K线执行 if (preBuyCondition and window()) strategy.close("short", comment = "Short提前平仓") if (preSellCondition and window()) strategy.close("long", comment = "Long提前平仓") // 原有信号触发与新订单执行逻辑 longCondition = buySignal if (longCondition and window()) strategy.entry("long", strategy.long, comment="long") shortCondition = sellSignal if (shortCondition and window()) strategy.entry("short", strategy.short, comment="short") buy1= barssince(buySignal) sell1 = barssince(sellSignal) color1 = buy1[1] < sell1[1] ? color.rgb(5, 102, 86) : buy1[1] > sell1[1] ? color.rgb(178, 40, 51) : na barcolor(barcoloring ? color1 : na)
代码说明
preBuyCondition和preSellCondition捕捉趋势即将反转的节点,此时提前平仓旧仓位- 平仓逻辑与新订单执行逻辑分开,确保旧仓位在新信号K线之前就完成平仓
- 保留了原策略的所有可视化、时间窗口、alert等功能,仅调整仓位平仓时机
内容的提问来源于stack exchange,提问作者Guilomaker
相关产品推荐
相关产品推荐

