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

做空场景下的追踪止损与止盈策略适配技术咨询

适配做空策略的追踪止损与止盈实现

问题背景

已完成多头头寸的追踪止损与止盈功能开发,需将该逻辑适配到做空策略中,以下为当前代码:

bullish = tk_cross_bull and cs_cross_bull and price_above_kumo and ta.crossover(lower, close)
bearish = tk_cross_bear and cs_cross_bear and price_below_kumo and ta.crossover(close,lower)

// Configure trail stop level with input options
longTrailPerc = input.float(title='Trail Long Loss (%)', minval=0.0, step=0.1, defval=5) * 0.01
shortTrailPerc = input.float(title='Trail Short Loss (%)', minval=0.0, step=0.1, defval=5) * 0.01

// Determine trail stop loss prices
longStopPrice = 0.0
shortStopPrice = 0.0

longStopPrice := if strategy.position_size > 0
    stopValue = close * (1 - longTrailPerc)
    math.max(stopValue, longStopPrice[1])
else
    0

shortStopPrice := if strategy.position_size < 0
    stopValue = close * (1 + shortTrailPerc)
    math.min(stopValue, shortStopPrice[1])
else
    999999

strategy.entry('Short', strategy.short, when=bearish and short_entry and timePeriod)
//strategy.exit('Exit', stop = longStopPrice, limit = shortStopPrice)
//strategy.close('Short', when=bullish and not long_entry)
if (bullish and timePeriod)
    strategy.exit(id='Exit', limit = shortStopPrice)

适配方案

  1. 补充做空止盈参数与计算逻辑
    • 添加做空止盈比例输入项,做空止盈价逻辑为:价格下跌至目标值时平仓,因此用当前价格乘以(1 - 止盈比例)计算目标价位。
  2. 绑定空单的止损与止盈
    • 用strategy.exit将空单的追踪止损价、止盈价与做空开仓指令绑定,确保持仓期间止损价动态更新。
  3. 区分多空平仓逻辑
    • 针对多头、空头分别设置独立的平仓触发条件,避免逻辑混淆。

修改后的完整代码

//@version=5
strategy("多空追踪止损止盈策略", overlay=true)

// 假设你已定义以下变量:tk_cross_bull, cs_cross_bull, price_above_kumo, lower, tk_cross_bear, cs_cross_bear, price_below_kumo, short_entry, timePeriod, long_entry
bullish = tk_cross_bull and cs_cross_bull and price_above_kumo and ta.crossover(lower, close)
bearish = tk_cross_bear and cs_cross_bear and price_below_kumo and ta.crossover(close, lower)

// 输入参数:止损与止盈比例
longTrailPerc = input.float(title='多头追踪止损比例(%)', minval=0.0, step=0.1, defval=5) * 0.01
shortTrailPerc = input.float(title='空头追踪止损比例(%)', minval=0.0, step=0.1, defval=5) * 0.01
longTakeProfitPerc = input.float(title='多头止盈比例(%)', minval=0.0, step=0.1, defval=10) * 0.01
shortTakeProfitPerc = input.float(title='空头止盈比例(%)', minval=0.0, step=0.1, defval=10) * 0.01

// 计算多头追踪止损价
longStopPrice = 0.0
longStopPrice := if strategy.position_size > 0
    stopValue = close * (1 - longTrailPerc)
    math.max(stopValue, longStopPrice[1])
else
    0

// 计算空头追踪止损价
shortStopPrice = 999999.0
shortStopPrice := if strategy.position_size < 0
    stopValue = close * (1 + shortTrailPerc)
    math.min(stopValue, shortStopPrice[1])
else
    999999.0

// 计算多头止盈价
longTakeProfitPrice = 0.0
longTakeProfitPrice := if strategy.position_size > 0
    close * (1 + longTakeProfitPerc)
else
    0

// 计算空头止盈价
shortTakeProfitPrice = 999999.0
shortTakeProfitPrice := if strategy.position_size < 0
    close * (1 - shortTakeProfitPerc)
else
    999999.0

// 多头开仓与平仓
if bullish and timePeriod and long_entry
    strategy.entry('Long', strategy.long)
    strategy.exit('Exit Long', 'Long', stop=longStopPrice, limit=longTakeProfitPrice)

// 空头开仓与平仓
if bearish and timePeriod and short_entry
    strategy.entry('Short', strategy.short)
    strategy.exit('Exit Short', 'Short', stop=shortStopPrice, limit=shortTakeProfitPrice)

// 额外平仓条件:多头信号触发时平空单
if bullish and timePeriod and strategy.position_size < 0
    strategy.close('Short')

关键说明

  • 空头追踪止损逻辑:空单持仓时,止损价随价格下跌持续下移(通过math.min确保止损价只降不升),锁定已有利润,避免价格反弹回吐收益。
  • 空头止盈逻辑:空单盈利目标为价格下跌至开仓价的(1 - 止盈比例)位置,触发后自动平仓。
  • strategy.exit绑定对应开仓指令,确保止损价在持仓期间动态更新,无需复杂的独立条件判断。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 23:06:26