VeighNa量化社区
你的开源社区量化交易平台 | vn.py | vnpy
Member
avatar
加入于:
帖子: 2
声望: 0

同样的回测程序用现成的双ma策略就没问题,用我写的这个策略就没有交易记录,搞两天也没整明白咋回事。

from datetime import datetime
from vnpy_ctastrategy import (
    CtaTemplate,
    BarData,
    TickData,
    OrderData,
    StopOrder,
    TradeData,
    BarGenerator,
    ArrayManager,
)



class MyStrategy(CtaTemplate):


    author = "Trader"

    def on_init(self) -> None:

        # 记录当前K线索引
        self.current_bar_index = 0
        self.last_bar_index = 0


        # 独立的K线合成器
        self.bg_1min = BarGenerator(self.on_bar, window=1)  # 1分钟K线

        # 数据容器
        self.am_1min = ArrayManager(size=100)
        self.load_bar(100)  # 预加载100根K线 
        self.write_log("策略初始化")


    def on_start(self):
        self.write_log("策略启动")
        self.put_event()


    def on_stop(self):
        self.write_log("策略停止")
        self.put_event()

    def on_stop_order(self, stop_order: StopOrder):
        pass

    def on_tick(self, tick: TickData):
        """处理Tick数据"""
        self.bg_1min.update_tick(tick)

    def on_order(self, order: OrderData):
        pass

    def on_trade(self, trade: TradeData):
        print(f"交易: {trade.vt_tradeid}, 方向: {trade.direction}, 开平: {trade.offset}, 价格: {trade.price}, 数量: {trade.volume}")
        self.put_event()

    def on_bar(self, bar: BarData):
        """处理K线数据(保留原始回调)"""
        self.cancel_all()
        self.bg_1min.update_bar(bar)
        self.am_1min.update_bar(bar)
        if not self.am_1min.inited:
            return

        self.current_bar_index += 1
        if self.current_bar_index < 100:
            return
        elif self.current_bar_index < 120:
            self.buy(bar.close_price, 1)
            print('买入')
            self.put_event()

==== 终端输出 ===

2025-07-20 21:06:06.661633 Start loading historical data
2025-07-20 21:06:06.661838 Loading progress: # [0%]
2025-07-20 21:06:06.722401 Loading progress: ########### [100%]
2025-07-20 21:06:06.729123 Historical data loading completed, data count: 690
买入
买入
买入
买入
买入
买入
买入
买入
买入
买入
买入
买入
买入
买入
买入
买入
买入
买入
买入
买入
2025-07-20 21:06:06.848437 Strategy initialization complete
2025-07-20 21:06:06.848662 Start replaying historical data
2025-07-20 21:06:06.849117 Backtesting progress: = [0%]
2025-07-20 21:06:06.849742 Backtesting progress: == [10%]
2025-07-20 21:06:06.850479 Backtesting progress: === [20%]
2025-07-20 21:06:06.850882 Backtesting progress: ==== [30%]
2025-07-20 21:06:06.851292 Backtesting progress: ===== [40%]
2025-07-20 21:06:06.851687 Backtesting progress: ====== [50%]
2025-07-20 21:06:06.852231 Backtesting progress: ======= [60%]
2025-07-20 21:06:06.852682 Backtesting progress: ======== [70%]
2025-07-20 21:06:06.853130 Backtesting progress: ========= [80%]
2025-07-20 21:06:06.853594 Backtesting progress: ========== [90%]
2025-07-20 21:06:06.853694 Historical backtest complete
2025-07-20 21:06:06.853747 Start calculating daily mark-to-market profit and loss
2025-07-20 21:06:06.853823 The backtest trade record is empty
2025-07-20 21:06:06.854865 The daily mark-to-market profit and loss calculation is complete
close_price pre_close trades trade_count start_pos end_pos turnover commission slippage trading_pnl holding_pnl total_pnl net_pnl
date
2025-07-01 3014.0 1.0 [] 0 0 0 0 0 0 0 0.0 0.0 0.0
2025-07-02 3063.0 3014.0 [] 0 0 0 0 0 0 0 0.0 0.0 0.0
2025-07-20 21:06:06.861326 Start calculating strategy statistical indicators
2025-07-20 21:06:06.864186 ------------------------------
2025-07-20 21:06:06.864250 Start Date: 2025-07-01
2025-07-20 21:06:06.864290 End Date: 2025-07-02
2025-07-20 21:06:06.864325 Total Days: 2
2025-07-20 21:06:06.864370 Profit Days: 0
2025-07-20 21:06:06.864411 Loss Days: 0
2025-07-20 21:06:06.864451 Capital: 1,000,000.00
2025-07-20 21:06:06.864483 End Balance: 1,000,000.00
2025-07-20 21:06:06.864530 Total Return: 0.00%
2025-07-20 21:06:06.864597 Annual Return: 0.00%
2025-07-20 21:06:06.864655 Max Drawdown: 0.00
2025-07-20 21:06:06.864708 Max Ddpercent: 0.00%
2025-07-20 21:06:06.864753 Max Drawdown Duration: 0
2025-07-20 21:06:06.864807 Total Det Pnl: 0.00
2025-07-20 21:06:06.864849 Total Commission: 0.00
2025-07-20 21:06:06.864904 Total Slippage: 0.00
2025-07-20 21:06:06.864945 Total Turnover: 0.00
2025-07-20 21:06:06.864981 Total Trade Count: 0
2025-07-20 21:06:06.865033 Daily Net P&L: 0.00
2025-07-20 21:06:06.865079 Daily Commission: 0.00
2025-07-20 21:06:06.865124 Daily Slippage: 0.00
2025-07-20 21:06:06.865170 Daily Turnover: 0.00
2025-07-20 21:06:06.865217 Daily Trade Count: 0.0
2025-07-20 21:06:06.865267 Daily Return: 0.00%
2025-07-20 21:06:06.865314 Return Std: 0.00%
2025-07-20 21:06:06.865409 Sharpe Ratio: 0.00
2025-07-20 21:06:06.865461 EWM Sharpe: 0.00
2025-07-20 21:06:06.865511 Return Drawdown Ratio: 0.00
2025-07-20 21:06:06.865807 Strategy statistical indicators calculation completed

Member
avatar
加入于:
帖子: 5567
声望: 335

没成交,可以自己去backtesting.py里面打印排查

Member
avatar
加入于:
帖子: 2
声望: 0

我发现回测数据只加载了600多个分钟数据,self.current_bar_index能累计到5000多,on_bar有两个调用者,一个load_bar,一个new_bar。
虽然load_bar会调用on_bar,但交易不会被执行,有个strategy.trading开关只有执行run_backtesting时才设为True执行交易。
load_bar(100)原来真是以天为单位的,并不是加载100根分钟线。

© 2015-2022 上海韦纳软件科技有限公司
备案服务号:沪ICP备18006526号

沪公网安备 31011502017034号

【用户协议】
【隐私政策】
【免责条款】