Home >  > VNPY学习记录13(部署到云服务器,实现自动交易)

VNPY学习记录13(部署到云服务器,实现自动交易)

蜗牛博客VNPY学习记录:

VN.PY 2.0学习记录一(如何回测)
VN.PY 2.0学习记录二(策略开发)
Vn.py学习记录三(米筐教程)
VN.PY 2.0学习记录四(多线程、多进程)
Vn.py学习记录五–交易时间段及Widgets
Vn.py学习记录六(无界面模拟盘)
Vn.py学习记录七(V2.0.5版本)
Vnpy学习记录八(R-Breaker及pickle)
Vn.py学习记录九(事件驱动引擎)
VN.PY学习记录十(源码概述)
VNPY学习记录11(微信+Vscode)
VNPY学习记录12(父子进程、回调函数)
VNPY学习记录13(部署到云服务器,实现自动交易)

一、服务器的选择
由于阿里云的windows服务器有点贵,所以我最后选择了腾讯云,到现在已经用了一年多的时间了,还是挺稳定的,购买链接

二、登陆服务器
(一)点击你购买的服务器最右端的“登陆”链接。

(二)在弹出的【登录Windows实例】窗口中,选择【使用 RDP 文件登录】,单击【下载RDP文件】,将 RDP 文件下载到本地。

(三)双击打开已下载到本地的 RDP 文件,输入密码,单击【确定】,即可远程连接到 Windows 云服务器。

二、部署代码到服务器
由于用的是的windows服务器,和在本地操作几乎是一样的,所以并没有什么难度。

三、代码自动运行
利用run.py来控制程序的运行,并且加了日期与时间表的判断,每周星期1-5程序自动运行,星期六、星期天则不会执行。
这样策略写好之后,平时基本上不用怎么管它了。

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.gateway.ctp import CtpGateway
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


ctp_setting = {
    "用户名": "00000000",
    "密码": "00000000",
    "经纪商代码": "00000000",
    "交易服务器": "100.100.100.50:00000",
    "行情服务器": "100.100.100.50:00000",
    "产品名称": "00000000",
    "授权编码": "00000000",
    "产品信息": ""
}

def run_child():
    """
    Running in the child process.
    """
    SETTINGS["log.file"] = True

    event_engine = EventEngine()
    main_engine = MainEngine(event_engine)
    main_engine.add_gateway(CtpGateway)
    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_engine.connect(ctp_setting, "CTP")
    main_engine.write_log("连接CTP接口")

    sleep(10)

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

    cta_engine.init_all_strategies()
    sleep(60)   # Leave enough time to complete strategy initialization
    main_engine.write_log("CTA策略全部初始化")

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

    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(15, 30)

    NIGHT_START = time(20, 45)
    NIGHT_END = time(2, 45)

    child_process = None

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

        # Get weekdays, Monday to Sunday is 0-6
        d=datetime.now()
        td = d.weekday()

        # Check whether in trading period
        if (
            current_time >= DAY_START and current_time <= DAY_END and td < 5
        ):
            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()

暧昧帖

本文暂无标签