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

TradingView策略测试器Short空头策略止损功能失效问题咨询

问题根源

  • 空头仓位状态判断错误:Pine Script 中空头仓位的strategy.position_size返回值为负数,原有代码bool ShortIsActive = startShortDeal or strategy.position_size > 0中>0的判断仅适用于多头仓位,所以开空之后ShortIsActive直接变为false,导致strategy.exit的when条件不满足,止损、止盈订单都会被系统撤销。
  • 止盈止损订单提交逻辑错误:原有代码每根K线都重复执行strategy.exit,配合错误的状态判断,会导致订单刚挂上就被取消,无法持续跟踪仓位生效。

修复方案

1. 修正仓位状态判断

// 替换原有错误的ShortIsActive判断代码
bool ShortIsActive = startShortDeal or strategy.position_size < 0

2. 调整订单提交逻辑

仅在开空信号触发时同步提交止盈止损订单,同时增加from_entry参数和对应入场单绑定,避免订单匹配错误:

if (isWithinBacktestPeriod())
    // 仅开仓时一次性提交订单,无需每根K线重复提交
    if startShortDeal
        strategy.entry("Short Entry", strategy.short, alert_message = "short("  + syminfo.ticker + "): Started")
        strategy.exit(id ="Short exit", from_entry="Short Entry", stop=ShortStopPrice, limit = enableTrailing ? na : ShortTakeProfitPrice, trail_price = enableTrailing ? ShortTakeProfitPrice : na, trail_offset = enableTrailing ? ShortTrailingTakeProfitStepTicks : na, alert_message = "Short(" + syminfo.ticker + "): Take Profit activated")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 00:06:01