You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TradingView Pine脚本Trailing STOPLOSS不触发问题求助

Pine脚本策略异常修复方案

问题根因

  • SuperTrend离场指令仅单次触发:你将strategy.exit("x2")包裹在条件判断语句中,只有仓位刚好为50且SuperTrend发生交叉的单根K线会提交离场订单,后续即使SuperTrend方向持续反转,也不会产生新的离场指令,导致剩余仓位无法平仓。
  • 硬编码仓位校验容错性极低:abs(strategy.position_size)==50的判断完全依赖默认开仓100、首次止盈平50的固定逻辑,开仓量、止盈比例调整或滑点、手续费带来的仓位微小偏差都会导致判断失效。
  • 初始止损逻辑不符合需求:你将止损参数绑定在仅平50%仓位的x1离场指令上,初始止损触发时只会平50%仓位,无法实现「初始止损触发全仓离场」的要求。

修复方案

  1. 拆分初始止损与部分止盈逻辑:单独设置全仓生效的初始止损指令,部分止盈指令仅绑定止盈条件,不绑定止损。
  2. SuperTrend离场改用持续生效规则:移除strategy.exit("x2")外层的条件包裹,直接在SuperTrend方向反转时调用strategy.close平仓对应仓位。
  3. 移除硬编码的仓位数值判断,改用仓位方向判断触发对应离场规则。

修正后完整代码

//@version=4
strategy("SLtrail", overlay = true,calc_on_every_tick=true,default_qty_value=100)

quant=input(title="Trade Quantity",defval=50)
// === 指标逻辑 ===
length = input(type=input.integer,defval=20,minval=1,title="Length")
ratio = input(type=input.integer,defval=3,title="Multiplier (3x length, 4x length, etc)",options=[3,4,5,6,7,8,9,10])
longOnly = input(type=input.bool,defval=false,title="Long Only")
fast = ema(hl2,length)
slow = ema(hl2,length * ratio)
plot(fast,linewidth=2,color=color.orange,title="Fast")
plot(slow,linewidth=2,color=color.blue,title="Slow")

longCond = crossover(fast,slow)
shortCond = crossunder(fast,slow)

src = input(close, "Source")
factor = input(3, "Multiplier", minval = 0.25, step = 0.25)
st(src, atrlen, atrfactor) =>
    var max     = src
    var min     = src
    var uptrend = true
    var stop    = 0.0
    atrM        = nz(atr(atrlen) * atrfactor, tr)
    max         := max(max, src)
    min         := min(min, src)
    stop        := nz(uptrend ? max(stop, max - atrM) : min(stop, min + atrM), src)
    uptrend     := src - stop >= 0.0
    if uptrend != nz(uptrend[1], true)
        max    := src
        min    := src
        stop   := uptrend ? max - atrM : min + atrM
    [stop, uptrend]

[atrStop, uptrend] = st(src, length, factor)

plot(atrStop, "Volatility Stop", style=plot.style_stepline, color= uptrend ? #009688 : #F44336)
bool downtrend = (uptrend != true)

// 开仓逻辑
if (longCond )
    strategy.entry("BUY",strategy.long)
    
if (shortCond )
    strategy.entry("SELL",strategy.short)

// 止盈止损参数
tar=input(defval=30.0,title="TARGET IN POINTS")
sl=input(defval=30.0,title="STOP LOSS IN POINTS")
tar:=tar/syminfo.mintick
sl:=sl/syminfo.mintick

// 全仓初始止损
strategy.exit("full_sl", from_entry="BUY", qty_percent=100, loss = sl)
strategy.exit("full_sl", from_entry="SELL", qty_percent=100, loss = sl)

// 50%仓位第一目标位止盈
strategy.exit("x1", from_entry="BUY", qty_percent = 50, profit = tar)
strategy.exit("x1", from_entry="SELL", qty_percent = 50, profit = tar)

// 剩余仓位SuperTrend跟踪止损
if strategy.position_size>0 and not uptrend
    strategy.close("BUY", comment="SuperTrend多单离场")
if strategy.position_size<0 and uptrend
    strategy.close("SELL", comment="SuperTrend空单离场")

内容的提问来源于stack exchange,提问作者Prashant Dada

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 15:36:02