You need to enable JavaScript to run this app. Pine Script触发strategy.exit止损后重复同向开仓问题求解
优惠活动
大模型
产品
解决方案
定价
更多

Pine Script触发strategy.exit止损后重复同向开仓问题求解

PineScript策略重复开仓问题修复方案

核心错误原因

  • 止损状态变量初始化错误:stoppedOutLong和stoppedOutShort初始值设为na,在PineScript的布尔判断中not na返回值仍为na,导致开仓条件里的状态校验直接失效。
  • 止损状态逻辑被错误重置:当前的stoppedOut变量只有在止损判定成立的那根K线会被设为true,下一根K线只要不满足止损判定条件,就会进入else分支被重置为false,止损锁定逻辑完全不生效。
  • 平仓接口调用错误:strategy.close的第一个参数需要传入开仓时定义的仓位ID,你传入的是止损退出单的ID,导致反向开仓前的平仓逻辑不生效。

具体修复步骤

  1. 将两个止损状态变量的初始值改为false,保证布尔判断逻辑正常运行。
  2. 调整止损状态的赋值逻辑:仅在止损平仓触发时设置对应止损状态为true,仅在反向开仓成功时重置对应止损状态为false,删除其他无关节点的状态重置逻辑。
  3. 修正strategy.close的仓位ID参数,保证反向开仓前可以正常平掉当前仓位。
  4. 止损退出单无需每次K线重复提交,改为开仓时同步提交止损止盈单,避免逻辑冗余。

修正后完整代码

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © CryptoEatsTheWorld

//@version=4
strategy("15-Minute MACD Support/Resistance", overlay=true)

// 修正止损状态变量初始化
var bool stoppedOutLong = false
var bool stoppedOutShort = false

ema = ema(close, 65)

plot(ema, "EMA 65", #00CCFF, linewidth=2)

//functions
xrf(values, length) =>
    r_val = float(na)
    if length >= 1
        for i = 0 to length by 1
            if na(r_val) or not na(values[i])
                r_val  :=  values[i]
                r_val

[diff, dea, macdhisto] = macd(close, 8, 17, 9)

a1 = barssince(crossover(diff,dea)[1])
oversold = (xrf(close,a1+1)>close) and (diff>xrf(diff,a1+1)) and crossover(diff,dea)
a2 = barssince(crossunder(dea,diff)[1])
overbought = (xrf(close,a2+1)<close) and (xrf(diff,a2+1)>diff) and crossunder(dea,diff)

plotshape(oversold, style=shape.triangleup, color=color.green, size=size.small, location=location.belowbar)
plotshape(overbought, style=shape.triangledown, color=color.red, size=size.small, location=location.abovebar)
barColour = (overbought or oversold) ? #FFFFFF : na
barcolor(color=barColour)

OBcheck = 0
OScheck = 0

if (overbought)
    OBcheck := 1
else
    OBcheck := 0
if (oversold)
    OScheck := 1
else
    OScheck := 0

OStimelapse = barssince(OScheck > 0)
OBtimelapse = barssince(OBcheck > 0)

whichOver = OStimelapse > OBtimelapse
//if whichOver = TRUE then OVERBOUGHT, if FALSE then OVERSOLD

longcondition = not whichOver and close > ema and not stoppedOutLong
shortcondition = whichOver and close < ema and not stoppedOutShort

// 止盈止损参数
GainPerc = 0.00668
LossPerc = 0.00668

starttime = timestamp(2021,1,1,0,0)

// 开仓逻辑:开仓时同步提交止损单,同时重置对应止损状态
if (longcondition and time >= starttime)
    strategy.entry("Long", strategy.long, 5, alert_message = "Long")
    // 开多单时解除空单止损锁定
    stoppedOutShort := false
    // 同步提交多单止损止盈
    strategy.exit("Close Long", "Long", stop = strategy.position_avg_price * (1 - LossPerc), limit = strategy.position_avg_price * (1 + GainPerc), alert_message = "Long Stopped Out")

if (shortcondition and time >= starttime)
    strategy.entry("Short", strategy.short, 5, alert_message = "Short")
    // 开空单时解除多单止损锁定
    stoppedOutLong := false
    // 同步提交空单止损止盈
    strategy.exit("Close Short", "Short", stop = strategy.position_avg_price * (1 + LossPerc), limit = strategy.position_avg_price * (1 + GainPerc), alert_message = "Short Stopped Out")

// 监听止损平仓事件,设置对应锁定状态
if (strategy.closedtrades.exit_comment(strategy.closedtrades - 1) == "Long Stopped Out" and strategy.closedtrades.size(strategy.closedtrades - 1) > 0)
    stoppedOutLong := true

if (strategy.closedtrades.exit_comment(strategy.closedtrades - 1) == "Short Stopped Out" and strategy.closedtrades.size(strategy.closedtrades - 1) < 0)
    stoppedOutShort := true

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.08 02:39:03