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

新人问大家一个小白问题。写了jupter模式的代码,主要部分如下:
engine = init_cli_trading([CtpGateway])
engine.connect_gateway(setting,"CTP")
engine.subscribe(["zn2011.SHFE"])
tick_zn = engine.get_tick(vt_symbol="zn2011.SHFE", use_df=True)
tick_zn
print(type(tick_zn))

在vscode里打印tick_zn输出none,打印tick_zn类型发现是NoneType
在jupter里如果把整段代码一次输入运行,tick_zn的输出也是none,但如果把查询行情语句tick_zn = engine.get_tick(vt_symbol="zn2011.SHFE", use_df=True)再运行一次,再输出tick_zn就是DataFrame格式的,似乎代码一句一句的输入执行可以正常查询到值,全部代码一次运行就无法查询,请问是什么原因呢

Administrator
avatar
加入于:
帖子: 4122
声望: 229

connect后需要一段时间的等待,才能完成连接、登录、查询合约的流程,简单就加个sleep

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

用Python的交易员 wrote:

connect后需要一段时间的等待,才能完成连接、登录、查询合约的流程,简单就加个sleep

感谢老师回复,我在vscode里加入了以下代码:

engine = init_cli_trading([CtpGateway])
engine.connect_gateway(setting,"CTP")

tick_zn = engine.get_tick(vt_symbol="zn2011.SHFE", use_df=True)
print(type(tick_zn))
time.sleep(10)
print(type(tick_zn))

for i in range(3):
    time.sleep(10)
    tick_zn = engine.get_tick(vt_symbol="zn2011.SHFE", use_df=True)
    print(type(tick_zn))
    print(tick_zn)
for i in range(3):
    time.sleep(10)
    tick_zn2 = engine.get_tick(vt_symbol="zn2011.SHFE", use_df=True)
    print(type(tick_zn2))
    print(tick_zn2)

第二个for循环用新变量赋值,输出结果如下:

<class 'NoneType'>
2020-09-28 22:28:04.985832 交易服务器连接成功
2020-09-28 22:28:04.986830 行情服务器连接成功
2020-09-28 22:28:05.088557 交易服务器授权验证成功
2020-09-28 22:28:05.510443 行情服务器登录成功
2020-09-28 22:28:05.525390 交易服务器登录成功
2020-09-28 22:28:05.580259 结算信息确认成功
2020-09-28 22:28:05.937288 合约信息查询成功

<class 'NoneType'>

<class 'NoneType'>
None

<class 'NoneType'>
None

<class 'NoneType'>
None

<class 'NoneType'>
None

<class 'NoneType'>
None

<class 'NoneType'>
None
通过sleep10秒以及for循环赋值,输出结果依然是none,目前想不出什么原因,老师麻烦帮测试一下

Member
avatar
加入于:
帖子: 733
声望: 35

请问sleep时间调长也是这样吗?

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

xiaohe wrote:

请问sleep时间调长也是这样吗?
是的,您方便的话可以用vscode运行下代码,只打印tick行情验证下,我把get_tick语句放在while循环里,打印输出的一直是none

Member
avatar
加入于:
帖子: 733
声望: 35

请试试看这样行不行

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

sleep(10)

vt_symbols = ["zn2011.SHFE"]
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)
Member
avatar
加入于:
帖子: 6
声望: 0

xiaohe wrote:

请试试看这样行不行

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

sleep(10)

vt_symbols = ["zn2011.SHFE"]
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)

感谢老师,这段代码有效,我测试后发现connect登录后先sleep10秒和100秒,while循环第一次输出的还是none,之后才能输出正确的行情,但在订阅合约语句之后也sleep10秒,while循环便能正确输出,似乎订阅合约也需要一定时间才能完成,完成后才能查询行情信息,这是作为新手学到的一个经验。

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