VeighNa量化社区
你的开源社区量化交易平台
Member
加入于:
帖子: 8
声望: 0

环境:
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 策略统计指标计算完成

Member
avatar
加入于:
帖子: 5018
声望: 303

print只会在终端输出而不会写入日志。前面做的关于日志的配置,都是通过事件引擎,注册日志事件监听写入日志文件的那种,包括在策略里write_log的代码。但是策略继承的是ctatemplate,ctatemplate里的write_log函数是创造了一个cta engine log event,要通过cta_engine的,而回测是通过backtesting_engine去append logs,不会写进日志文件里,不是同一种日志。

description

description

感觉回测调试notebook_trading可能更合适你https://github.com/vnpy/vnpy/tree/master/examples/notebook_trading

Member
加入于:
帖子: 8
声望: 0

只要能输出都行,不论在log文件还是终端,目前两种都不行,
问题是print也没在终端输出
print在回测函数可以打印到终端,但是在策略def on_bar(self, bar: BarData):添加的打印也显示不了,每日数据的处理调试很必要的

self.bg.update_bar(bar)
print(bar.datetime)
self.write_log(bar.datetime)
Member
avatar
加入于:
帖子: 5018
声望: 303

我之前理解错了,我以为你的拿的vn.py的no_ui模板打开的,之前那些日志调试都是针对no_ui模板,里面有注册日志事件监听的逻辑,包括策略里write_log的内容。你回测脚本没用到cta_engine, 策略write_log当然没反应。我拿了你回测脚本的代码,在同样的策略下加了print(bar),脚本运行能打印。这个可能需要自己排查了。

description

description

Member
加入于:
帖子: 8
声望: 0

多谢,犯了低级错误,需要在安装目录里改就好了,因为代码都从系统库导入
~/anaconda_python37/lib/python3.7/site-packages/vnpy

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

沪公网安备 31011502017034号

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