import multiprocessing
import sys
import os
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_ctp import CtpGateway

from vnpy_ctastrategy import CtaStrategyApp

from vnpy_ctastrategy.base import EVENT_CTA_LOG

from vnpy_datarecorder import DataRecorderApp
from vnpy_datarecorder.engine import EVENT_RECORDER_LOG

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

ctp_setting = {
"用户名": "000000",
"密码": "000000",
"经纪商代码": "9999",
"交易服务器": "180.168.146.187:10101",
"行情服务器": "180.168.146.187:10111",
"产品名称": "",
"授权编码": "",
"产品信息": ""
}

Chinese futures market trading period (day/night)

DAY_START = time(8, 45)
DAY_END = time(15, 15)

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

def check_trading_period():
""""""
current_time = datetime.now().time()

trading = False
if (
    (current_time >= DAY_START and current_time <= DAY_END)
    or (current_time >= NIGHT_START)
    or (current_time <= NIGHT_END)
):
    trading = True

return trading


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

event_engine = EventEngine()
main_engine = MainEngine(event_engine)
main_engine.add_app(DataRecorderApp)
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_RECORDER_LOG, log_engine.process_log_event)
main_engine.write_log("注册日志事件监听")

main_engine.connect(ctp_setting, "CTP")
main_engine.write_log("连接CTP接口")
main_engine.write_log(os.getcwd())

sleep(10)

# cta_engine.init_engine()
# data_recorder_engine.init_engine()
# main_engine.write_log("DATARecorder初始化完成")


# data_recorder_engine.run()
# print('1111111111111111\n')
# sleep(10)   # Leave enough time to complete strategy initialization
# main_engine.write_log("DATArecorder 启动")

print('222222222222222\n')
# data_recorder_engine.start()
main_engine.write_log("DATArecorder 开始记录")

while True:
    sleep(10)

    trading = check_trading_period()
    if not trading:
        print("关闭子进程")
        main_engine.close()
        sys.exit(0)


def run_parent():
"""
Running in the parent process.
"""
print("启动数据记录守护父进程")

child_process = None

while True:
    trading = check_trading_period()

    # Start child process in trading period
    # trading = False
    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:
        if not child_process.is_alive():
            child_process = None
            print("子进程关闭成功")

    sleep(5)


if name == "main":
run_parent()

说明下,录制的配置和数据默认是在用户空间下,例如我的是 在 C:\Users\Administrator.vntrader 这个下面。
编辑格式没弄明白怎么搞,管理员看到话,麻烦编辑成代码模式