vnpy/examples/WebTrader/run.py

75 lines
1.8 KiB
Python
Raw Normal View History

2018-01-21 07:36:48 +00:00
# encoding: UTF-8
"""
无人值守运行服务
"""
2018-05-07 16:37:09 +00:00
from __future__ import print_function
2018-01-21 07:36:48 +00:00
from time import sleep
from datetime import datetime, time
from multiprocessing import Process
2018-01-21 07:36:48 +00:00
2018-08-06 03:44:32 +00:00
import webbrowser
from webServer import run as runWebServer
from tradingServer import main as runTradingServer
from vnpy.trader.vtEngine import LogEngine
2018-01-21 07:36:48 +00:00
START_TIME = time(20, 45)
CLOSE_TIME = time(15, 30)
2018-01-23 13:16:02 +00:00
if __name__ == '__main__':
# 创建日志引擎
le = LogEngine()
le.setLogLevel(le.LEVEL_INFO)
le.addConsoleHandler()
2018-01-23 13:16:02 +00:00
# 进入循环
pWeb = None
pTrading = None
2018-01-23 13:16:02 +00:00
2018-08-06 03:44:32 +00:00
import os
print(os.getpid())
while True:
le.info('-'*30)
2018-01-23 13:16:02 +00:00
dt = datetime.now()
le.info(u'当前时间:%s' %dt)
currentTime = dt.time()
2018-01-23 13:16:02 +00:00
2018-05-21 04:06:12 +00:00
if currentTime >= START_TIME or currentTime <= CLOSE_TIME:
le.info(u'当前处于交易时间段')
if not pTrading:
pTrading = Process(target=runTradingServer)
pTrading.start()
le.info(u'启动交易服务器进程')
2018-01-23 13:16:02 +00:00
if not pWeb:
pWeb = Process(target=runWebServer)
pWeb.start()
le.info(u'启动WEB服务器进程')
2018-08-06 03:44:32 +00:00
webbrowser.open('http://127.0.0.1:5000')
2018-02-13 09:22:42 +00:00
else:
le.info(u'当前处于非交易时间段')
2018-02-13 09:22:42 +00:00
if pTrading:
pTrading.terminate()
pTrading.join()
pTrading = None
le.info(u'关闭交易服务器进程')
2018-02-13 09:22:42 +00:00
if pWeb:
pWeb.terminate()
pWeb.join()
pWeb = None
le.info(u'关闭WEB服务器进程')
sleep(60)