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

Backtrader追踪止损订单成交价格异常问题及修复方案

Backtrader追踪止损订单成交价格异常问题及修复方案
  • 测试策略:13:10建立空头仓位,设置0.19美元的追踪止损买单
  • 交易结果异常:策略在13:10正确开空,14:19追踪止损触发,成交价格为37.31,但实际应为37.28(14:17最低价37.09,加上0.19的追踪幅度)
  • 问题根源:Backtrader仅使用K线收盘价更新追踪止损,未正确利用K线的高低价进行计算
  • 修复方案:修改Backtrader的order.py文件,在execute方法中添加以下代码:
def execute(self, dt, size, price,
            closed, closedvalue, closedcomm,
            opened, openedvalue, openedcomm,
            margin, pnl,
            psize, pprice):

    # 添加以下四行代码
    if self.issell():
        price = self.data.high[0]
    if self.isbuy():
        price = self.data.low[0]

    super(Order, self).execute(dt, size, price,
                               closed, closedvalue, closedcomm,
                               opened, openedvalue, openedcomm,
                               margin, pnl, psize, pprice)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 09:57:33