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

盈利目标达成后移动止损位及仓位平仓规则优化需求

实现移动止损与分批平仓的Pine Script策略

策略核心规则

  • 第一盈利目标(TP1)触发时,将止损位(SL)移动至TP1价位
  • 止损触发仅平仓50%仓位,剩余仓位需等待第二盈利目标(TP2)触发后再平仓

修改后的完整代码

//@version=5
strategy("移动止损分批平仓策略", overlay=true, default_qty_type=strategy.cash, default_qty_value=10000, initial_capital=10000)

// 自定义参数
pos_size = input.int(100, title="总仓位数量")
tp1_total_profit = input.int(400, title="第一盈利目标(总盈利)")
tp2_total_profit = input.int(800, title="第二盈利目标(总盈利)")
sl_total_loss = input.int(400, title="初始止损(总亏损)")
half_pos_size = pos_size / 2 // 半仓数量

// 示例入场条件:布林带突破(可替换为自定义逻辑)
upperBand = ta.sma(close, 20) + 2 * ta.stdev(close, 20)
lowerBand = ta.sma(close, 20) - 2 * ta.stdev(close, 20)

// 入场信号
shortCondition = close < lowerBand and close[1] > lowerBand
longCondition = close > upperBand and close[1] < upperBand

// 跟踪TP1触发状态的变量
var bool tp1_hit_long = false
var bool tp1_hit_short = false

// 多头交易逻辑
if (longCondition)
    strategy.entry("long", strategy.long, qty=pos_size)
    tp1_hit_long := false // 重置TP1触发标记

if strategy.position_size > 0
    entry_price = strategy.position_avg_price
    // 计算单位价位的盈利/止损
    tp1_price = entry_price + (tp1_total_profit / pos_size)
    tp2_price = entry_price + (tp2_total_profit / pos_size)
    // 动态切换止损位:TP1触发后用TP1价位作为新止损
    current_sl = tp1_hit_long ? tp1_price : entry_price - (sl_total_loss / pos_size)

    // 第一盈利目标:平半仓
    strategy.exit("Exit Long TP1", "long", qty=half_pos_size, limit=tp1_price, comment="平半仓(TP1)")
    // 第二盈利目标+动态止损:平剩余半仓
    strategy.exit("Exit Long TP2/SL", "long", qty=half_pos_size, limit=tp2_price, stop=current_sl, comment="平剩余仓位(TP2/SL)")

    // 检测TP1是否触发,更新状态标记
    if strategy.closedtrades and strategy.closedtrades.comment == "平半仓(TP1)"
        tp1_hit_long := true

// 空头交易逻辑
if (shortCondition)
    strategy.entry("short", strategy.short, qty=pos_size)
    tp1_hit_short := false // 重置TP1触发标记

if strategy.position_size < 0
    entry_price = strategy.position_avg_price
    // 空头盈利为价格下跌,止损为价格上涨
    tp1_price = entry_price - (tp1_total_profit / pos_size)
    tp2_price = entry_price - (tp2_total_profit / pos_size)
    // 动态切换止损位:TP1触发后用TP1价位作为新止损
    current_sl = tp1_hit_short ? tp1_price : entry_price + (sl_total_loss / pos_size)

    // 第一盈利目标:平半仓
    strategy.exit("Exit Short TP1", "short", qty=half_pos_size, limit=tp1_price, comment="平半仓(TP1)")
    // 第二盈利目标+动态止损:平剩余半仓
    strategy.exit("Exit Short TP2/SL", "short", qty=half_pos_size, limit=tp2_price, stop=current_sl, comment="平剩余仓位(TP2/SL)")

    // 检测TP1是否触发,更新状态标记
    if strategy.closedtrades and strategy.closedtrades.comment == "平半仓(TP1)"
        tp1_hit_short := true

关键改动说明

  • TP1触发状态跟踪:用var定义的布尔变量记录TP1是否触发,确保止损位只在TP1达成后更新
  • 动态止损计算:根据TP1触发状态切换止损价位,未触发时用初始止损,触发后锁定TP1的盈利
  • 分批平仓逻辑:拆分两个strategy.exit分别处理半仓的TP1平仓,以及剩余半仓的TP2+动态止损平仓,严格执行止损仅平半仓的规则
  • 多空方向适配:针对多头和空头的盈利/止损方向差异,分别计算对应价位,避免逻辑错误

内容的提问来源于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:40:24