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

同样的前置ip等登录信息,通过VNTrader脚本策略模板加载启动可以正常获取到tick行情,为何通过VScode(如下图),运行脚本策略时get_tick("")获取不到tick行情数据,是不是脚本策略只能通过Jupyter 或是 VNTrader的脚本策略模块启动运行才能获取到tick行情呢?请您指教。

description

Member
avatar
加入于:
帖子: 38
声望: 6

我这样写是可以直接命令行运行的

from time import sleep

from vnpy.app.script_trader import init_cli_trading
from vnpy.gateway.ctp import CtpGateway
from vnpy.gateway.xtp import XtpGateway

# CTP接口连接设置
ctp_setting = {

}

# XTP接口连接设置
xtp_setting = {

}

engine = init_cli_trading([CtpGateway, XtpGateway])
engine.connect_gateway(ctp_setting, "CTP")
engine.connect_gateway(xtp_setting, "XTP")

sleep(10)

vt_symbols = ["IC2012.CFFEX", "rb2009.SHFE", "601990.SSE"]
engine.subscribe(vt_symbols)

# 获取合约信息
for vt_symbol in vt_symbols:
    contract = engine.get_contract(vt_symbol)
    msg = f"合约信息,{contract}"
    engine.write_log(msg)

# 持续运行,使用strategy_active来判断是否要退出程序
engine.strategy_active = True
while engine.strategy_active:
    # 轮询获取行情
    for vt_symbol in vt_symbols:
        tick = engine.get_tick(vt_symbol, use_df=True)
        msg = f"最新行情, {tick}"
        engine.write_log(msg)

    # 等待3秒进入下一轮
    sleep(3)

但这个代码通过VNTrader加载就不行,命令行可以运行。后来发现是 def run(engine: ScriptEngine)函数已经集成了底层的CTP和XTP的接口,所以需要在VNTrader里面登录CTP和XTP的接口才能正常运行,如果没有登录CTP和XTP接口,是无法获取Tick数据的。
我也希望能够有一个详细的Demo来说明脚本策略模式的编写格式和要求。

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