环境:
ubuntu20.04
anaconda
python3.7.3
64bit
版本:2.1.8
期望效果:在策略代码中:on_bar: print(bar.datetime)或者self.write_log(bar.datetime)能够在回测后log或者console显示
测试步骤:
1>配置中设置debug等级改10无效果;改0无效果
2>print(bar.datetime)无效果
3>self.write_log(bar.datetime)无效果
4>~/.vntrader/log和终端均没有看到bar.datetime显示
代码:
1>策略部分:加打印
def on_bar(self, bar: BarData):
"""
Callback of new bar data update.
"""
self.bg.update_bar(bar)
print(bar.datetime)
self.write_log(bar.datetime)
2>回测部分
from vnpy.app.cta_strategy.backtesting import BacktestingEngine, OptimizationSetting
from vnpy.app.cta_strategy.strategies.boll_channel_strategy import (
BollChannelStrategy,
)
from datetime import datetime
engine = BacktestingEngine()
engine.set_parameters(
vt_symbol="au888.SHFE",
interval="1m",
start=datetime(2020, 12, 1),
end=datetime(2021, 2, 1),
rate=0.3/10000,
slippage=0.2,
size=300,
pricetick=0.2,
capital=1_000_000,
)
engine.add_strategy(BollChannelStrategy, {})
engine.load_data()
engine.run_backtesting()
df = engine.calculate_result()
engine.calculate_statistics()
显示log:
2021-01-07 17:48:52.365784 开始加载历史数据
2021-01-07 17:48:52.365843 加载进度:# [0%]
2021-01-07 17:48:52.549528 加载进度:# [10%]
2021-01-07 17:48:52.753696 加载进度:## [19%]
2021-01-07 17:48:52.949280 加载进度:### [29%]
2021-01-07 17:48:53.123592 加载进度:#### [39%]
2021-01-07 17:48:53.316483 加载进度:##### [48%]
2021-01-07 17:48:53.412940 加载进度:###### [58%]
2021-01-07 17:48:53.413635 加载进度:####### [68%]
2021-01-07 17:48:53.414179 加载进度:######## [77%]
2021-01-07 17:48:53.414713 加载进度:######### [87%]
2021-01-07 17:48:53.415245 加载进度:########## [97%]
2021-01-07 17:48:53.415777 历史数据加载完成,数据量:13365
2021-01-07 17:48:53.431238 策略初始化完成
2021-01-07 17:48:53.431283 开始回放历史数据
2021-01-07 17:48:53.437451 回放进度:= [0%]
2021-01-07 17:48:53.451508 回放进度:== [10%]
2021-01-07 17:48:53.461197 回放进度:=== [20%]
2021-01-07 17:48:53.470675 回放进度:==== [30%]
2021-01-07 17:48:53.480084 回放进度:===== [40%]
2021-01-07 17:48:53.486435 回放进度:====== [50%]
2021-01-07 17:48:53.499963 回放进度:======= [60%]
2021-01-07 17:48:53.506256 回放进度:======== [70%]
2021-01-07 17:48:53.513867 回放进度:========= [80%]
2021-01-07 17:48:53.525373 回放进度:========== [90%]
2021-01-07 17:48:53.525497 回放进度:=========== [100%]
2021-01-07 17:48:53.525526 历史数据回放结束
2021-01-07 17:48:53.525627 开始计算逐日盯市盈亏
2021-01-07 17:48:53.527545 逐日盯市盈亏计算完成
2021-01-07 17:48:53.527567 开始计算策略统计指标
2021-01-07 17:48:53.557790 ------------------------------
2021-01-07 17:48:53.557828 首个交易日: 2020-12-11
2021-01-07 17:48:53.557837 最后交易日: 2021-01-05
2021-01-07 17:48:53.557843 总交易日: 20
2021-01-07 17:48:53.557849 盈利交易日: 8
2021-01-07 17:48:53.557857 亏损交易日: 12
2021-01-07 17:48:53.557874 起始资金: 1,000,000.00
2021-01-07 17:48:53.557889 结束资金: 998,066.50
2021-01-07 17:48:53.557901 总收益率: -0.19%
2021-01-07 17:48:53.557911 年化收益: -2.32%
2021-01-07 17:48:53.557918 最大回撤: -4,401.36
2021-01-07 17:48:53.557932 百分比最大回撤: -0.44%
2021-01-07 17:48:53.557942 最长回撤天数: 9
2021-01-07 17:48:53.557953 总盈亏: -1,933.50
2021-01-07 17:48:53.557961 总手续费: 67.50
2021-01-07 17:48:53.557968 总滑点: 1,140.00
2021-01-07 17:48:53.557974 总成交金额: 2,250,054.00
2021-01-07 17:48:53.557983 总成交笔数: 19
2021-01-07 17:48:53.557992 日均盈亏: -96.68
2021-01-07 17:48:53.558004 日均手续费: 3.38
2021-01-07 17:48:53.558015 日均滑点: 57.00
2021-01-07 17:48:53.558023 日均成交金额: 112,502.70
2021-01-07 17:48:53.558031 日均成交笔数: 0.95
2021-01-07 17:48:53.558037 日均收益率: -0.01%
2021-01-07 17:48:53.558044 收益标准差: 0.10%
2021-01-07 17:48:53.558056 Sharpe Ratio: -1.65
2021-01-07 17:48:53.558064 收益回撤比: -0.44
2021-01-07 17:48:53.558600 策略统计指标计算完成