From c030a5daf6a8a7c227a297bedbc9557406cb4616 Mon Sep 17 00:00:00 2001 From: msincenselee Date: Wed, 1 Jan 2020 22:26:32 +0800 Subject: [PATCH] =?UTF-8?q?[=E6=96=B0=E5=8A=9F=E8=83=BD]=20Tick=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E8=AE=B0=E5=BD=95=E8=BF=90=E8=A1=8C=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- prod/linux/tick_record/run.py | 102 ++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 prod/linux/tick_record/run.py diff --git a/prod/linux/tick_record/run.py b/prod/linux/tick_record/run.py new file mode 100644 index 00000000..723407e0 --- /dev/null +++ b/prod/linux/tick_record/run.py @@ -0,0 +1,102 @@ +import os +import sys +import multiprocessing +from time import sleep +from datetime import datetime, time +from logging import INFO + + +# 将repostory的目录i,作为根目录,添加到系统环境中。 +ROOT_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..')) +sys.path.append(ROOT_PATH) +print(f'append {ROOT_PATH} into sys.path') + +from vnpy.event import EventEngine +from vnpy.trader.setting import SETTINGS +from vnpy.trader.engine import MainEngine +from vnpy.trader.utility import load_json +from vnpy.gateway.ctp import CtpGateway +from vnpy.app.tick_recorder import TickRecorderApp +from vnpy.app.cta_strategy.base import EVENT_CTA_LOG + + +SETTINGS["log.active"] = True +SETTINGS["log.level"] = INFO +SETTINGS["log.console"] = True + + +ctp_setting = load_json('connect_ctp.json') + + +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) + record_engine = main_engine.add_app(TickRecorderApp) + 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) + + 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 + + # 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()