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

如何在Pine Script中按点差设置止盈止损,拆分仓位50%止盈50%持有?

问题解答

你的代码不可行,核心问题有两个

  • 开仓条件触发的同一根K线里,仓位还未实际建立,直接下达的平仓订单会因无对应持仓而无法执行。
  • 代码里没有设置基于点差的止盈/止损触发逻辑,只是单纯下达平仓指令,根本达不到“到目标点差才平仓50%”的效果。

正确实现拆分仓位+点差止盈止损的思路

方法1:用strategy.exit实现(推荐,更稳定)

针对开仓订单,关联两个strategy.exit指令,各自对应50%仓位,用点差计算目标价格:

// 定义点差参数
profit_points = 50  // 止盈点差
stop_points = 30    // 止损点差

// 多头逻辑
longCondition = close > upperBand and close[1] < upperBand
if (longCondition)
    pos_size = 1000  // 总仓位
    strategy.entry("long", strategy.long, qty=pos_size)
    // 第一个50%仓位的止盈止损(基于点差)
    strategy.exit("exit_long_half1", "long", qty=pos_size/2, 
                  profit=close + profit_points*syminfo.mintick, 
                  stop=close - stop_points*syminfo.mintick)
    // 第二个50%仓位可设置不同点差,或单独持有
    strategy.exit("exit_long_half2", "long", qty=pos_size/2, 
                  profit=close + profit_points*2*syminfo.mintick, 
                  stop=close - stop_points*syminfo.mintick)

// 空头逻辑同理
shortCondition = close < lowerBand and close[1] > lowerBand
if (shortCondition)
    pos_size = 1000
    strategy.entry("short", strategy.short, qty=pos_size)
    strategy.exit("exit_short_half1", "short", qty=pos_size/2, 
                  profit=close - profit_points*syminfo.mintick, 
                  stop=close + stop_points*syminfo.mintick)
    strategy.exit("exit_short_half2", "short", qty=pos_size/2, 
                  profit=close - profit_points*2*syminfo.mintick, 
                  stop=close + stop_points*syminfo.mintick)

方法2:用strategy.order配合价格条件判断

先完成开仓,之后在每根K线判断价格是否达到点差目标,再下达对应仓位的平仓指令:

profit_points = 50
stop_points = 30
pos_size = 1000
size_trim = pos_size / 2

// 标记持仓状态
var bool long_hold = false
var bool short_hold = false

// 多头开仓
longCondition = close > upperBand and close[1] < upperBand
if (longCondition)
    strategy.entry("long", strategy.long, qty=pos_size)
    long_hold := true
    short_hold := false

// 多头拆分平仓逻辑
if (long_hold)
    // 第一个50%止盈
    if (close >= strategy.entry_price("long") + profit_points*syminfo.mintick)
        strategy.order("long1", strategy.short, qty=size_trim, comment="closefirst50%")
    // 第一个50%止损
    if (close <= strategy.entry_price("long") - stop_points*syminfo.mintick)
        strategy.order("long1", strategy.short, qty=size_trim, comment="closefirst50%")
    // 第二个50%的止盈(可单独设置条件)
    if (close >= strategy.entry_price("long") + profit_points*2*syminfo.mintick)
        strategy.order("long2", strategy.short, qty=size_trim, comment="closesecond50%")

// 空头逻辑同理
shortCondition = close < lowerBand and close[1] > lowerBand
if (shortCondition)
    strategy.entry("short", strategy.short, qty=pos_size)
    short_hold := true
    long_hold := false

if (short_hold)
    if (close <= strategy.entry_price("short") - profit_points*syminfo.mintick)
        strategy.order("short1", strategy.long, qty=size_trim, comment="closefirst50%")
    if (close >= strategy.entry_price("short") + stop_points*syminfo.mintick)
        strategy.order("short1", strategy.long, qty=size_trim, comment="closefirst50%")
    if (close <= strategy.entry_price("short") - profit_points*2*syminfo.mintick)
        strategy.order("short2", strategy.long, qty=size_trim, comment="closesecond50%")

关键注意事项

  • 用syminfo.mintick把点差转换为实际价格,适配不同品种的最小变动单位
  • 拆分仓位时要确保qty参数准确,避免超平或平仓不足
  • strategy.exit会自动关联开仓订单,比手动用strategy.order更可靠,不容易出现持仓混乱

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 01:25:31