程序是基于 example 里的 no_ui 修改而来
诡异的是大约有一半的时候下是运行到一半自行停止运行
下面是代码
判断是不是处于交易时间
from datetime import datetime, time
from config.common import log
from config.const import WEEK_DICT
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)
大于等于6 默认是周末
WEEKEND_START = 6
today = datetime.now().weekday() + 1
def is_trading_time():
""""""
current_time = datetime.now().time()
trading = False
if (today < WEEKEND_START):
if (
(current_time >= DAY_START and current_time <= DAY_END)
or (current_time >= NIGHT_START)
or (current_time <= NIGHT_END)
):
trading = True
time = current_time.strftime("%H:%M:%S")
log.info("time:" + time + ' trading:' + str(trading))
return trading
def run_child():
"""
Running in the child process.
"""
SETTINGS["log.file"] = True
SETTINGS.update()
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(60)
trading = time_util.is_trading_time()
if not trading:
cta_engine.stop_all_strategies()
log.info("关闭子进程")
sleep(60)
main_engine.close()
sleep(30)
sys.exit(0)
haha
if name == "main":
run_child()