vn.py量化社区
By Traders, For Traders.
Member
avatar
加入于:
帖子: 17
声望: 1

通常开发策略的时候总想通过开发工具的debug模式来走读代码或review代码检查逻辑的正确性,但在vnpy项目一直没有实现这个愿望。。例如,我想回测一个策略,通过在app\cta_backtester\engine.py下写一个入口函数据来调用回测,代码如下:
if name == 'main':
engine = BacktesterEngine()
engine.start_backtesting(
class_name='AtrRsiStrategy',
vt_symbol='RB2005.SHFE',
interval='1h',
start=datetime(2017, 3, 16),
end=datetime(2020, 3, 15),
rate=2.5e-05,
slippage=0.2,
size=300,
pricetick=0.2,
capital=1000000,
inverse=False,
setting={'atr_length': 22, 'atr_ma_length': 10, 'rsi_length': 5, 'rsi_entry': 16, 'trailing_percent': 0.8, 'fixed_size': 1})

可是点击debug运行,就报错了,错误提示如下:

Connected to pydev debugger (build 191.7479.30)
vnstudio is useing of rqdata....
F:\vnstudio\lib\site-packages\pymysql\cursors.py:170: Warning: (3090, "Changing sql mode 'NO_AUTO_CREATE_USER' is deprecated. It will be removed in a future release.")
result = self._query(query)
Traceback (most recent call last):
File "D:\Program Files\JetBrains\PyCharm 2019.1.3\helpers\pydev\pydevd.py", line 1758, in <module>
main()
File "D:\Program Files\JetBrains\PyCharm 2019.1.3\helpers\pydev\pydevd.py", line 1752, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "D:\Program Files\JetBrains\PyCharm 2019.1.3\helpers\pydev\pydevd.py", line 1147, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "D:\Program Files\JetBrains\PyCharm 2019.1.3\helpers\pydev_pydev_imps_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "F:/vnstudio/Lib/site-packages/vnpy/app/cta_backtester/engine.py", line 451, in <module>
engine = BacktesterEngine()
TypeError: init() missing 2 required positional arguments: 'main_engine' and 'event_engine'

说没有加载main_engine和event_engine,据我了解,这两个引擎是在启动pyqt5界面时加载的,想请老师我如何在debug模式下加载这两个引擎呢。。。利用vnpy搭建一个高效的开发环境怎么弄才好呢,我是新手,问题较多哈。。。

Administrator
avatar
加入于:
帖子: 4028
声望: 220
  1. 用vscode,装好Python插件
  2. 在你要断点的地方打好断点(不会的搜索下)
  3. 然后运行如下主入口:
from vnpy.event import EventEngine
from vnpy.trader.engine import MainEngine
from vnpy.trader.ui import MainWindow, create_qapp
from vnpy.gateway.ctp import CtpGateway
from vnpy.app.cta_strategy import CtaStrategyApp
from vnpy.app.cta_backtester import CtaBacktesterApp

def main():
    """Start VN Trader"""
    qapp = create_qapp()

    event_engine = EventEngine()
    main_engine = MainEngine(event_engine)

    main_engine.add_gateway(CtpGateway)
    main_engine.add_app(CtaStrategyApp)
    main_engine.add_app(CtaBacktesterApp)

    main_window = MainWindow(main_engine, event_engine)
    main_window.showMaximized()

    qapp.exec()

if __name__ == "__main__":
    main()
Member
avatar
加入于:
帖子: 17
声望: 1

谢谢老师指导,我已经能debug成功了。

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

沙漠小草 wrote:

谢谢老师指导,我已经能debug成功了。

你好,我也在debug vnpy,不过一直报错未成功。很希望可以详细请教您一下。
如果方便可以加下weixin请教您么?十分感谢!!!

Member
avatar
加入于:
帖子: 17
声望: 1

下载vnpy后,用vsstudio或pycharm打开项目,在vnpy.trader下新建一个pythoh文件,复制老师提供的代码,直接debug运行就可以了。

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