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

移动止损与条件离场设置及VWAP策略回测无交易问题求解

问题原因分析
  • 入场条件逻辑写反:你的交易规则要求close > VWAP时做多,但代码中LongCondition01 = vwap > close,完全与规则相反,不会触发任何开仓订单,因此没有交易记录。
  • strategy.exit的when参数使用错误:你将close < vwap的离场条件放在when参数中,逻辑变为「只有当close < vwap成立时,才会提交8%止损的离场订单」,既不符合你「两个离场条件满足其一即可出场」的规则,也会导致止损订单无法在持仓全程生效。
  • 时间过滤条件未生效:你定义了TimePeriod来限定回测起始时间,但入场判断逻辑中没有加入该条件,时间限制不生效。
解决方法
  1. 修正入场条件为LongCondition01 = close > vwap,符合做多规则。
  2. 拆分离场逻辑:将close < vwap的主动离场条件单独通过strategy.close实现,止损订单不需要加when参数,保证持仓期间止损一直有效。
  3. 入场判断中加入TimePeriod条件,确保回测仅在指定时间范围内运行。
持仓最高浮盈回落10%移动止损实现逻辑

跟踪持仓以来的最高收盘价,计算回落10%的止损价位,与固定8%止损价位取最大值作为实际止损价,即可实现「未盈利时触发8%固定止损,盈利后触发最高浮盈回落10%的移动止损」的效果。

修正后完整代码
//@version=5
strategy("Long VWAP", overlay=true)

// 变量定义
vwapValue = ta.vwap(close, volume)
OutTrade = strategy.position_size <= 0
TimePeriod = time > timestamp(syminfo.timezone, 2010, 01, 01, 0, 0)

// 入场条件
LongCondition01 = close > vwapValue
if(LongCondition01 and OutTrade and TimePeriod)
    strategy.entry("Long", strategy.long)

// 离场逻辑
if strategy.position_size != 0
    // 固定8%止损
    fixedStopLoss = strategy.position_avg_price * 0.92
    // 持仓以来最高收盘价
    highestCloseInPosition = ta.highest(close, strategy.barssinceentry + 1)
    // 最高浮盈回落10%的移动止损
    trailingStopLoss = highestCloseInPosition * 0.9
    // 实际止损取两个止损价的最大值
    finalStop = math.max(fixedStopLoss, trailingStopLoss)
    // 提交止损订单
    strategy.exit("Exit", "Long", stop=finalStop)
    // close < vwap时主动离场
    if close < vwapValue
        strategy.close("Long")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 21:57:03