Keeping a Futures Trading Process Active During Session Hours
Summary
This Python example describes a process manager for a CTA futures strategy using vn.py and the CTP gateway. A parent process checks the clock and launches a child process during configured daytime and overnight trading windows. The child creates the event and main engines, connects to CTP, initializes and starts strategies, then polls the session schedule and closes the main engine when the configured window ends.
The code includes logging setup and delays intended to allow connection and strategy initialization to complete. It demonstrates session-based process lifecycle management, but it does not implement a trading signal, order logic, or recovery after an unexpected child-process failure. The schedule is hard-coded, and the time check treats the late-night window as active across midnight. No tests, operational results, or discussion of exchange-specific holiday calendars and session differences are provided, so the example requires adaptation before practical deployment.
Key ideas
- A parent process can launch a strategy process only during configured futures trading sessions.
- The child process connects to CTP, initializes the CTA engine, and starts all strategies.
- The example closes the trading engine when its clock check detects that the session has ended.
- The code does not describe trading logic or restart handling after an unexpected failure.
Tags
Full text
# run.py
```py
"""
Keep a CTA strategy process alive during futures trading hours.
"""
import multiprocessing
import sys
from time import sleep
from datetime import datetime, time
from vnpy.event import EventEngine
from vnpy.trader.setting import SETTINGS
from vnpy.trader.engine import MainEngine, LogEngine
from vnpy.trader.logger import INFO, logger
from vnpy_ctp import CtpGateway
from vnpy_ctastrategy import CtaStrategyApp, CtaEngine
from vnpy_ctastrategy.base import EVENT_CTA_LOG
SETTINGS["log.active"] = True
SETTINGS["log.level"] = INFO
SETTINGS["log.console"] = True
ctp_setting = {
"用户名": "",
"密码": "",
"经纪商代码": "",
"交易服务器": "",
"行情服务器": "",
"产品名称": "",
"授权编码": "",
"产品信息": ""
}
# Chinese futures market trading period (day/night)
DAY_START = time(8, 45)
DAY_END = time(15, 0)
NIGHT_START = time(20, 45)
NIGHT_END = time(2, 45)
def check_trading_period() -> bool:
"""
Return whether the clock is inside the day session or the overnight night session.
"""
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() -> None:
"""
Running in the child process.
"""
SETTINGS["log.file"] = True
event_engine: EventEngine = EventEngine()
main_engine: MainEngine = MainEngine(event_engine)
main_engine.add_gateway(CtpGateway)
cta_engine: CtaEngine = main_engine.add_app(CtaStrategyApp)
logger.info("主引擎创建成功")
log_engine: LogEngine = main_engine.get_engine("log") # type: ignore
event_engine.register(EVENT_CTA_LOG, log_engine.process_log_event)
logger.info("注册日志事件监听")
main_engine.connect(ctp_setting, "CTP")
logger.info("连接CTP接口")
sleep(10)
cta_engine.init_engine()
logger.info("CTA策略初始化完成")
cta_engine.init_all_strategies()
sleep(60) # Leave enough time to complete strategy initialization
logger.info("CTA策略全部初始化")
cta_engine.start_all_strategies()
logger.info("CTA策略全部启动")
while True:
sleep(10)
trading = check_trading_period()
if not trading:
logger.info("关闭子进程")
main_engine.close()
sys.exit(0)
def run_parent() -> None:
"""
Running in the parent process.
"""
print("启动CTA策略守护父进程")
child_process = None
while True:
trading = check_trading_period()
# 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:
if not child_process.is_alive():
child_process = None
print("子进程关闭成功")
sleep(5)
if __name__ == "__main__":
run_parent()
```Shown in full with attribution under the source's licence. Licence: MIT
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.