vn.py官网
开源量化社区
Member
avatar
加入于:
帖子: 154
声望: 0

1:老师好,如下面代码所示,在no_ui脚本模式基础上添加了图形界面,成功连接到行情和初始化了策略,并且界面也启动成功,但是为什么启动后图形界面什么都没有更新呢?CTA策略图形界面也没有打开,该如何改进,望老师指导,万分感激!

import multiprocessing
from time import sleep
from datetime import datetime, time
from logging import INFO

from vnpy.event import EventEngine
from vnpy.trader.setting import SETTINGS
from vnpy.trader.engine import MainEngine
from vnpy.trader.ui import MainWindow, create_qapp


from vnpy.gateway.ib import IbGateway
from vnpy.app.cta_strategy import CtaStrategyApp
from vnpy.app.cta_strategy.base import EVENT_CTA_LOG


SETTINGS["log.active"] = True
SETTINGS["log.level"] = INFO
SETTINGS["log.console"] = True
SETTINGS["log.file"] = True

ib_setting = {
    "TWS地址": "127.0.0.1",
    "TWS端口": 4002,
    "客户号": 1,
    "交易账户": ""
}

def run_child():
    """
    Running in the child process.
    """
    qapp = create_qapp()

    event_engine = EventEngine()
    main_engine = MainEngine(event_engine)





    main_engine.add_gateway(IbGateway)
    cta_engine = main_engine.add_app(CtaStrategyApp)
    main_engine.write_log("主引擎创建成功")


    log_engine = main_engine.get_engine("log")
    event_engine.register(EVENT_CTA_LOG, log_engine.process_log_event)
    main_engine.write_log("注册日志事件监听")



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




    main_engine.connect(ib_setting, "IB")
    sleep(3)
    main_engine.write_log("已连接到盈透API")



    cta_engine.init_engine()
    main_engine.write_log("CTA策略开始初始化")

    cta_engine.init_all_strategies()

    sleep(10)   # Leave enough time to complete strategy initialization


    main_engine.write_log("CTA策略已全部初始化")

    cta_engine.start_all_strategies()
    main_engine.write_log("CTA策略已全部启动")


    sleep(10)

    while True:

        sleep(1)


def run_parent():
    """
    Running in the parent process.
    """
    print("启动CTA策略守护父进程")

    # Chinese futures market trading period (day/night)
    DAY_START = time(8, 45)
    DAY_END = time(16, 40)

    NIGHT_START = time(16, 55)
    NIGHT_END = time(5, 20)



    child_process = None

    while True:
        current_time = datetime.now().time()
        trading = False

        # Check whether in trading period
        if (
            (current_time >= DAY_START and current_time <= DAY_END)
            or (current_time >= NIGHT_START)
            or (current_time <= NIGHT_END)
        ):
            trading = True

        # Start child process in trading period
        if trading and child_process is None:
            print("启动子进程")
            child_process = multiprocessing.Process(target=run_child)
            child_process.start()
            print("子进程启动成功")

        # 非记录时间则退出子进程
        if not trading and child_process is not None:
            print("关闭子进程")
            child_process.terminate()
            child_process.join()
            child_process = None
            print("子进程关闭成功")

        sleep(5)


if __name__ == "__main__":
    run_parent()
Administrator
avatar
加入于:
帖子: 4985
声望: 291

先创建MainWindow,再去调用初始化函数操作

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

用Python的交易员 wrote:

先创建MainWindow,再去调用初始化函数操作

1:老师您好,以上源码已修改,现在是先创建MainWindow,再去调用api和初始化函数操作,但是为什么没有连接到api和初始化策略呢?需要使用put_evet吗?望老师指导,万分感激

Administrator
avatar
加入于:
帖子: 4985
声望: 291

修改后的代码贴出来看看?

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

用Python的交易员 wrote:

修改后的代码贴出来看看?
1:老师您好,已在一楼的问题源码中修改,实盘交易时,图形界面的vntrader定时重启和api定时重连这个功能非常实在,图形界面更直观跟踪策略运行状态,望老师指导,万分感激!

2:老师您好,想咨询一下,为什么代码执行到下面这段代码后就不执行后面的初始化策略部分的代码了呢? 需要怎么修改呢? 望老师指导,万分感激!

    main_window = MainWindow(main_engine, event_engine)
    main_window.showMaximized()
    qapp.exec()
Administrator
avatar
加入于:
帖子: 4985
声望: 291

qapp.exec会将主线程进入无限循环,因此后续代码都不会执行了

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

用Python的交易员 wrote:

qapp.exec会将主线程进入无限循环,因此后续代码都不会执行了

1:老师您好,可以指点一下,上面的这个no_ui脚本需要如何修改才能在在界面启动后去自动连接api,自动启动CTA策略并初始化及全部启动策略吗?望回复,万分感激!

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

沪公网安备 31011502017034号