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

Pinescript V5多空持仓切换时止损离场策略失效问题

Pine Script V5 策略:固定入场TP/SL与旧持仓止损失效问题

问题描述

  • 基于ATR编写的Pine Script V5策略存在以下问题:
    1. 新开多单持仓时,旧空单持仓无法触发止损离场
    2. 新开空单持仓时,旧多单持仓无法触发止损离场
  • 示例情况:ID为578206的空单持仓触及止损点位时未被平仓

核心代码

alen = input(14, 'ATR Length (default 14)', group = 'Take Profit & Stoploss')
smooth = input(3, 'Smoothing', group = 'Take Profit & Stoploss')
TP_ATR_input = input(4.5, 'TP Multiplier', group = 'Take Profit & Stoploss')
SL_ATR_input = input(1.5, 'SL Multiplier', group = 'Take Profit & Stoploss')

datr = ta.atr(alen)
sclose = ta.sma(close, smooth)

SL_ATR_L = sclose - datr * SL_ATR_input
TP_ATR_L = sclose + datr * TP_ATR_input
SL_ATR_S = sclose + datr * SL_ATR_input
TP_ATR_S = sclose - datr * TP_ATR_input

// TP and SL Plots
plot(TP_ATR_L, 'TPLong', color=color.new(#1d8121, 0))
plot(SL_ATR_L, 'SLLong', color=color.new(color.maroon, 0))
plot(TP_ATR_S, 'TPShort', color=color.new(#1d8121, 0))
plot(SL_ATR_S, 'SLShort', color=color.new(color.maroon, 0))


longCondition = L_above_3ema and s3 ? high + 2 * atr : na
shortCondition = S_below_3ema and l3 ? low - 2 * atr : na

if (longCondition)
    strategy.entry("enter long", strategy.long)
    strategy.exit("Exit long", from_entry = "enter long", limit = TP_ATR_L, stop = SL_ATR_L) 

if (shortCondition)
    strategy.entry("enter short", strategy.short)
    strategy.exit("Exit short", from_entry = "enter short", limit = TP_ATR_S, stop = SL_ATR_S)

尝试过的无效修复代码

if (strategy.position_size < 0)
    strategy.exit("TP short", from_entry = "enter short", limit = TP_ATR_S)
    strategy.exit("SL short", from_entry = "enter short", stop = SL_ATR_S)

需求与疑问

  • 当前已实现多空切换时自动取消旧订单,但不符合需求
  • 需求:每笔多/空订单拥有入场时固定的TP/SL点位
  • 疑问:Pine Script是否支持该功能?

解决方案

Pine Script V5完全支持为每笔订单设置入场时固定的TP/SL,问题出在策略默认的单一持仓模式,以及strategy.exit的绑定逻辑未针对独立订单处理。

关键问题原因

  1. 默认模式下,strategy.entry会自动平掉反向持仓,导致旧订单被直接平仓而非触发止损/止盈
  2. 固定名称的入场订单会导致旧的strategy.exit指令被覆盖,无法持续监控旧持仓

修复步骤

  1. 启用多持仓模式:在策略开头添加pyramiding参数,允许同时持有多笔多/空订单
  2. 生成唯一订单ID:为每笔入场订单分配唯一名称,确保strategy.exit与对应订单一一绑定
  3. 固定入场时的TP/SL值:入场时直接计算并传入TP/SL数值,避免后续K线更新导致数值变化

修复后的示例代码

//@version=5
strategy("固定TP/SL多订单策略", overlay=true, pyramiding=100, default_qty_type=strategy.cash, default_qty_value=10000)

alen = input(14, 'ATR Length (default 14)', group = 'Take Profit & Stoploss')
smooth = input(3, 'Smoothing', group = 'Take Profit & Stoploss')
TP_ATR_input = input(4.5, 'TP Multiplier', group = 'Take Profit & Stoploss')
SL_ATR_input = input(1.5, 'SL Multiplier', group = 'Take Profit & Stoploss')

datr = ta.atr(alen)
sclose = ta.sma(close, smooth)

// 订单计数器,生成唯一ID
var int orderCounter = 0

// 替换为你的实际多空条件
longCondition = ta.crossover(ta.sma(close, 5), ta.sma(close, 20))
shortCondition = ta.crossunder(ta.sma(close, 5), ta.sma(close, 20))

if (longCondition)
    orderCounter += 1
    longId = "Long #" + str.tostring(orderCounter)
    // 计算入场时的固定TP/SL
    entrySL = sclose - datr * SL_ATR_input
    entryTP = sclose + datr * TP_ATR_input
    strategy.entry(longId, strategy.long)
    strategy.exit("Exit " + longId, from_entry=longId, limit=entryTP, stop=entrySL)
    // 绘制该订单的TP/SL标记
    plotshape(entrySL, title=longId+" SL", location=location.belowbar, color=color.maroon, style=shape.labelup, text="SL")
    plotshape(entryTP, title=longId+" TP", location=location.abovebar, color=color.green, style=shape.labeldown, text="TP")

if (shortCondition)
    orderCounter += 1
    shortId = "Short #" + str.tostring(orderCounter)
    // 计算入场时的固定TP/SL
    entrySL = sclose + datr * SL_ATR_input
    entryTP = sclose - datr * TP_ATR_input
    strategy.entry(shortId, strategy.short)
    strategy.exit("Exit " + shortId, from_entry=shortId, limit=entryTP, stop=entrySL)
    // 绘制该订单的TP/SL标记
    plotshape(entrySL, title=shortId+" SL", location=location.abovebar, color=color.maroon, style=shape.labeldown, text="SL")
    plotshape(entryTP, title=shortId+" TP", location=location.belowbar, color=color.green, style=shape.labelup, text="TP")

代码说明

  • pyramiding=100:允许同时持有多笔订单,不会自动平反向持仓
  • 唯一订单ID:确保每笔入场订单的止损/止盈指令独立绑定,不会被覆盖
  • 固定TP/SL:入场时计算的数值会被策略固化,后续K线变化不会影响该订单的离场条件

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 19:07:42