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

如何基于同一变量生成双向信号并优化Wundertrading策略平仓开仓逻辑

适配Wundertrading的交易策略反向开仓问题排查与修复

我正在搭建适配Wundertrading的交易策略,根据平台信号机制,需要发送重复信号先平仓原有持仓,再进行反向开仓。目前代码已实现平多+开空的双向信号逻辑,但平空+开多的信号完全无法生效,推测是逻辑层级问题,但找不到具体原因。

理想执行流程:

  • 信号出现的当前K线:完成平仓操作
  • 下一根K线:执行反向开仓

当前使用代码

/// Trade State Management
isInLongPosition = strategy.position_size > 0
isInShortPosition = strategy.position_size < 0


/// Trade Execution
longConditionCalc = (longCondition and isADXFilterEnabledAndAboveThreshold and TFSlong and RSIlong and ZFSlong)
shortConditionCalc = (shortCondition and isADXFilterEnabledAndAboveThreshold and TFSshort and RSIshort and ZFSshort)

closeLongConditionCalc= (closeLongCondition and TFSexitlong and ZFSshort) or shortConditionCalc
closeShortConditionCalc= (closeShortCondition and TFSexitshort and ZFSlong) or longConditionCalc


if isStartEndPeriodsAndTimeInRange
// Long Conditions
    if longConditionCalc and i_tradeDirection != 'Short Only' and isInLongPosition == false
        strategy.entry('Long', strategy.long, qty=contracts, comment = i_alertLongEntry)

    if closeLongConditionCalc and isInLongPosition == true
        strategy.close('Long', qty_percent=100, comment = i_alertLongExit)
    
    if slLongClose
        strategy.close('Long', qty_percent=100, when=slLongClose, comment = "")

// Short Conditions
    if shortConditionCalc and i_tradeDirection != 'Long Only' and isInShortPosition == false
        strategy.entry('Short', strategy.short, qty=contracts, comment = i_alertShortEntry)

    if closeShortConditionCalc and isInShortPosition == true
        strategy.close('Short', qty_percent=100, when=closeShortConditionCalc or longConditionCalc, comment = i_alertShortExit)

    if slShortClose
        strategy.close('Short', qty_percent=100, when=slShortClose, comment = "")

问题分析

  1. 平空逻辑冗余冲突:closeShortConditionCalc本身已包含longConditionCalc,但平仓判断又额外叠加when=closeShortConditionCalc or longConditionCalc,导致逻辑重复且可能触发状态判断混乱。
  2. 开仓时序错误:同一K线内完成平仓后,平台持仓状态更新存在延迟,此时判断isInShortPosition == false可能无法生效,导致开仓逻辑被跳过。
  3. 无状态标记控制时序:没有明确标记平仓操作的执行K线,无法确保反向开仓在下一根K线触发。

修复后的代码

/// Trade State Management
isInLongPosition = strategy.position_size > 0
isInShortPosition = strategy.position_size < 0

// 新增状态标记:记录上一根K线是否执行了平仓操作
var bool lastBarClosedLong = false
var bool lastBarClosedShort = false

/// Trade Execution
longConditionCalc = (longCondition and isADXFilterEnabledAndAboveThreshold and TFSlong and RSIlong and ZFSlong)
shortConditionCalc = (shortCondition and isADXFilterEnabledAndAboveThreshold and TFSshort and RSIshort and ZFSshort)

// 简化平仓条件,避免逻辑冗余
closeLongConditionCalc = (closeLongCondition and TFSexitlong and ZFSshort) or shortConditionCalc
closeShortConditionCalc = (closeShortCondition and TFSexitshort and ZFSlong) or longConditionCalc

if isStartEndPeriodsAndTimeInRange
    // 每次进入时间范围后重置平仓标记
    lastBarClosedLong := false
    lastBarClosedShort := false

    // Long Conditions
    // 开多:仅在上一根K线平空完成后触发,确保时序分离
    if longConditionCalc and i_tradeDirection != 'Short Only' and not isInLongPosition and lastBarClosedShort
        strategy.entry('Long', strategy.long, qty=contracts, comment = i_alertLongEntry)
        lastBarClosedShort := false  // 触发开仓后重置标记

    // 平多:当前有多头持仓且满足条件时执行
    if closeLongConditionCalc and isInLongPosition
        strategy.close('Long', qty_percent=100, comment = i_alertLongExit)
        lastBarClosedLong := true  // 标记当前K线完成平多

    if slLongClose
        strategy.close('Long', qty_percent=100, when=slLongClose, comment = "")
        lastBarClosedLong := true

    // Short Conditions
    // 开空:仅在上一根K线平多完成后触发,确保时序分离
    if shortConditionCalc and i_tradeDirection != 'Long Only' and not isInShortPosition and lastBarClosedLong
        strategy.entry('Short', strategy.short, qty=contracts, comment = i_alertShortEntry)
        lastBarClosedLong := false  // 触发开仓后重置标记

    // 平空:当前有空头持仓且满足条件时执行
    if closeShortConditionCalc and isInShortPosition
        strategy.close('Short', qty_percent=100, comment = i_alertShortExit)
        lastBarClosedShort := true  // 标记当前K线完成平空

    if slShortClose
        strategy.close('Short', qty_percent=100, when=slShortClose, comment = "")
        lastBarClosedShort := true

修复说明

  • 新增lastBarClosedLong/lastBarClosedShort状态变量,精准标记平仓操作的执行K线,确保反向开仓在下一根K线触发,符合理想流程。
  • 移除平空逻辑中冗余的when参数,避免条件冲突。
  • 开仓条件增加前一根K线平仓标记判断,解决平台持仓状态更新延迟导致的开仓失效问题。
  • 每次进入交易时间范围后重置平仓标记,避免跨周期状态干扰。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 13:30:52