2017-03-15 18:24:21 +00:00
|
|
|
|
# encoding: UTF-8
|
2017-05-03 08:07:27 +00:00
|
|
|
|
|
2017-05-03 10:40:17 +00:00
|
|
|
|
|
2017-03-15 18:24:21 +00:00
|
|
|
|
import os
|
2017-05-03 10:40:17 +00:00
|
|
|
|
import sys
|
2017-03-15 18:24:21 +00:00
|
|
|
|
import ctypes
|
2017-05-03 08:07:27 +00:00
|
|
|
|
from datetime import datetime, timedelta, date
|
|
|
|
|
from time import sleep
|
2017-03-15 18:24:21 +00:00
|
|
|
|
|
2017-04-20 12:29:24 +00:00
|
|
|
|
from eventType import *
|
2017-03-15 18:24:21 +00:00
|
|
|
|
from vtEngine import MainEngine
|
2017-05-03 10:03:17 +00:00
|
|
|
|
from ctaStrategy.strategy import STRATEGY_CLASS
|
2017-04-28 14:10:07 +00:00
|
|
|
|
from threading import Thread
|
|
|
|
|
|
2017-03-15 18:24:21 +00:00
|
|
|
|
from simple_monitor import *
|
|
|
|
|
from setup_logger import setup_logger
|
|
|
|
|
|
|
|
|
|
setup_logger(debug=True)
|
|
|
|
|
# ----------------------------------------------------------------------
|
2017-05-05 03:40:41 +00:00
|
|
|
|
"""
|
|
|
|
|
Linux 下:
|
|
|
|
|
启动:python noUiMain.py >>logs/mylog.log &
|
|
|
|
|
停止:ps -ef | grep python ,找到对应的进程PID, kill -9 PID
|
|
|
|
|
查看:tail -f logs/noUiMain_月日_小时分钟.log -f
|
|
|
|
|
Windows下:
|
|
|
|
|
启动:命令行窗口 python noUiMain.py
|
|
|
|
|
停止:关闭窗口
|
|
|
|
|
"""
|
2017-04-20 12:29:24 +00:00
|
|
|
|
class NoUiMain(object):
|
2017-03-15 18:24:21 +00:00
|
|
|
|
|
2017-04-20 12:29:24 +00:00
|
|
|
|
def __init__(self):
|
|
|
|
|
# gateway 是否连接
|
|
|
|
|
self.connected = False
|
2017-05-05 03:40:41 +00:00
|
|
|
|
# gateway 的连接名称,在vtEngine.initGateway()里面定义,对应的配置文件是 "连接名称_connect.json",
|
2017-05-11 03:39:38 +00:00
|
|
|
|
self.gateway_name = 'CTP'
|
2017-05-03 07:30:40 +00:00
|
|
|
|
# 启动的策略实例,须在catAlgo/CtaSetting.json 里面定义 [u'S28_RB1001', u'S28_TFT', u'S28_HCRB',u'atr_rsi']
|
2017-05-03 10:03:17 +00:00
|
|
|
|
self.strategies = [u'S28_HCRB']
|
2017-04-20 12:29:24 +00:00
|
|
|
|
|
|
|
|
|
self.g_count = 0
|
|
|
|
|
|
2017-05-11 03:39:38 +00:00
|
|
|
|
self.last_dt = datetime.now()
|
|
|
|
|
|
|
|
|
|
# 实例化 主引擎
|
2017-05-05 03:40:41 +00:00
|
|
|
|
print u'instance mainengine'
|
2017-04-20 12:29:24 +00:00
|
|
|
|
self.mainEngine = MainEngine()
|
|
|
|
|
|
|
|
|
|
def trade_off(self):
|
|
|
|
|
"""检查现在是否为非交易时间"""
|
|
|
|
|
now = datetime.now()
|
|
|
|
|
a = datetime.now().replace(hour=2, minute=35, second=0, microsecond=0)
|
|
|
|
|
b = datetime.now().replace(hour=8, minute=30, second=0, microsecond=0)
|
|
|
|
|
c = datetime.now().replace(hour=15, minute=30, second=0, microsecond=0)
|
|
|
|
|
d = datetime.now().replace(hour=20, minute=30, second=0, microsecond=0)
|
|
|
|
|
weekend = (now.isoweekday() == 6 and now >= a) or (now.isoweekday() == 7)
|
|
|
|
|
off = (a <= now <= b) or (c <= now <= d) or weekend
|
|
|
|
|
return off
|
|
|
|
|
|
|
|
|
|
def disconnect(self):
|
|
|
|
|
""""断开底层gateway的连接"""
|
|
|
|
|
if self.mainEngine:
|
|
|
|
|
self.mainEngine.disconnect(self.gateway_name)
|
|
|
|
|
self.connected = False
|
|
|
|
|
|
|
|
|
|
def onTimer(self, event):
|
|
|
|
|
"""定时器执行逻辑,每十秒执行一次"""
|
|
|
|
|
|
|
|
|
|
# 十秒才执行一次检查
|
|
|
|
|
self.g_count += 1
|
2017-06-03 12:15:13 +00:00
|
|
|
|
if self.g_count <= 30:
|
2017-04-20 12:29:24 +00:00
|
|
|
|
return
|
|
|
|
|
self.g_count = 0
|
2017-05-11 03:39:38 +00:00
|
|
|
|
dt = datetime.now()
|
|
|
|
|
if dt.hour != self.last_dt.hour:
|
|
|
|
|
self.last_dt = dt
|
2017-06-03 12:15:13 +00:00
|
|
|
|
print u'noUiMain.py checkpoint:{0}'.format(dt)
|
2017-05-11 03:39:38 +00:00
|
|
|
|
self.mainEngine.writeLog( u'noUiMain.py checkpoint:{0}'.format(dt))
|
2017-04-20 12:29:24 +00:00
|
|
|
|
|
|
|
|
|
# 定时断开
|
|
|
|
|
if self.trade_off():
|
2017-05-11 03:39:38 +00:00
|
|
|
|
"""非交易时间"""
|
2017-04-20 12:29:24 +00:00
|
|
|
|
if self.connected:
|
|
|
|
|
self.mainEngine.writeLog(u'断开连接{0}'.format(self.gateway_name))
|
2017-05-11 03:39:38 +00:00
|
|
|
|
self.disconnect()
|
2017-04-20 12:29:24 +00:00
|
|
|
|
self.mainEngine.writeLog(u'清空数据引擎')
|
|
|
|
|
self.mainEngine.clearData()
|
|
|
|
|
self.connected = False
|
|
|
|
|
return
|
|
|
|
|
|
2017-05-11 03:39:38 +00:00
|
|
|
|
# 交易时间内,定时重连和检查
|
2017-04-28 14:10:07 +00:00
|
|
|
|
if not self.connected:
|
2017-05-11 03:39:38 +00:00
|
|
|
|
self.mainEngine.writeLog(u'启动连接{0}'.format(self.gateway_name))
|
2017-04-20 12:29:24 +00:00
|
|
|
|
self.mainEngine.writeLog(u'清空数据引擎')
|
|
|
|
|
self.mainEngine.clearData()
|
|
|
|
|
self.mainEngine.writeLog(u'重新连接{0}'.format(self.gateway_name))
|
|
|
|
|
self.mainEngine.connect(self.gateway_name)
|
|
|
|
|
self.connected = True
|
2017-05-11 03:39:38 +00:00
|
|
|
|
return
|
|
|
|
|
else:
|
|
|
|
|
if not self.mainEngine.checkGatewayStatus(self.gateway_name):
|
|
|
|
|
self.mainEngine.writeLog(u'检查连接{0}异常,重新启动连接'.format(self.gateway_name))
|
|
|
|
|
self.mainEngine.writeLog(u'断开连接{0}'.format(self.gateway_name))
|
|
|
|
|
self.disconnect()
|
2017-06-03 12:15:13 +00:00
|
|
|
|
self.connected = False
|
|
|
|
|
#self.mainEngine.writeLog(u'清空数据引擎')
|
|
|
|
|
#self.mainEngine.clearData()
|
|
|
|
|
#self.mainEngine.writeLog(u'重新连接{0}'.format(self.gateway_name))
|
|
|
|
|
#self.mainEngine.connect(self.gateway_name)
|
|
|
|
|
#self.connected = True
|
|
|
|
|
#
|
2017-04-20 12:29:24 +00:00
|
|
|
|
def Start(self):
|
|
|
|
|
"""启动"""
|
|
|
|
|
|
|
|
|
|
# 若需要连接数据库,则启动
|
2017-04-28 14:10:07 +00:00
|
|
|
|
#self.mainEngine.dbConnect()
|
2017-04-20 12:29:24 +00:00
|
|
|
|
|
|
|
|
|
# 加载cta的配置
|
2017-05-05 03:40:41 +00:00
|
|
|
|
print u'load cta setting'
|
2017-04-20 12:29:24 +00:00
|
|
|
|
self.mainEngine.ctaEngine.loadSetting()
|
|
|
|
|
|
2017-05-05 03:40:41 +00:00
|
|
|
|
print u'initialize all strategies'
|
2017-04-20 12:29:24 +00:00
|
|
|
|
# 初始化策略,如果多个,则需要逐一初始化多个
|
|
|
|
|
for s in self.strategies:
|
2017-05-03 10:03:17 +00:00
|
|
|
|
print 'init trategy {0}'.format(s)
|
2017-04-20 12:29:24 +00:00
|
|
|
|
self.mainEngine.ctaEngine.initStrategy(s)
|
|
|
|
|
# 逐一启动策略
|
2017-05-03 10:03:17 +00:00
|
|
|
|
print 'start strategy {0}'.format(s)
|
2017-04-20 12:29:24 +00:00
|
|
|
|
self.mainEngine.ctaEngine.startStrategy(s)
|
|
|
|
|
|
|
|
|
|
# 指定的连接配置
|
2017-05-05 03:40:41 +00:00
|
|
|
|
print u'connect gateway:{0}'.format(self.gateway_name)
|
2017-04-20 12:29:24 +00:00
|
|
|
|
self.mainEngine.connect(self.gateway_name)
|
|
|
|
|
self.connected = True
|
|
|
|
|
|
|
|
|
|
# 注册定时器,用于判断重连
|
|
|
|
|
self.mainEngine.eventEngine.register(EVENT_TIMER, self.onTimer)
|
|
|
|
|
|
|
|
|
|
# 所有的日志监控
|
|
|
|
|
self.logM = LogMonitor(self.mainEngine.eventEngine)
|
|
|
|
|
self.errorM = ErrorMonitor(self.mainEngine.eventEngine)
|
|
|
|
|
self.tradeM = TradeMonitor(self.mainEngine.eventEngine)
|
|
|
|
|
self.orderM = OrderMonitor(self.mainEngine.eventEngine, self.mainEngine)
|
|
|
|
|
self.positionM = PositionMonitor(self.mainEngine.eventEngine)
|
|
|
|
|
self.accountM = AccountMonitor(self.mainEngine.eventEngine)
|
2017-03-15 18:24:21 +00:00
|
|
|
|
|
2017-04-28 14:10:07 +00:00
|
|
|
|
def run_noui():
|
|
|
|
|
|
2017-05-05 03:40:41 +00:00
|
|
|
|
try:
|
|
|
|
|
|
|
|
|
|
log_file_name = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)),
|
2017-04-28 14:10:07 +00:00
|
|
|
|
'logs',
|
|
|
|
|
u'noUiMain_{0}.log'.format(datetime.now().strftime('%m%d_%H%M'))))
|
2017-05-05 03:40:41 +00:00
|
|
|
|
except Exception as ex:
|
|
|
|
|
print u'Use local dict:{0}'.format(os.getcwd())
|
|
|
|
|
log_file_name = os.path.abspath(os.path.join(os.getcwd(),'logs',
|
|
|
|
|
u'noUiMain_{0}.log'.format(datetime.now().strftime('%m%d_%H%M'))))
|
2017-04-28 14:10:07 +00:00
|
|
|
|
|
|
|
|
|
setup_logger(filename=log_file_name, debug=False)
|
2017-04-20 12:29:24 +00:00
|
|
|
|
noUi = NoUiMain()
|
|
|
|
|
noUi.Start()
|
2017-04-28 14:10:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2017-05-03 08:07:27 +00:00
|
|
|
|
#from PyQt4 import QtGui
|
2017-04-28 14:10:07 +00:00
|
|
|
|
# 主程序
|
2017-05-03 08:07:27 +00:00
|
|
|
|
thread = Thread(target=run_noui, args=())
|
|
|
|
|
thread.start()
|
2017-05-03 07:30:40 +00:00
|
|
|
|
|
|
|
|
|
# 创建Qt应用对象,用于事件循环
|
2017-05-03 08:07:27 +00:00
|
|
|
|
#app = QtGui.QApplication(sys.argv)
|
|
|
|
|
#run_noui()
|
2017-05-03 07:30:40 +00:00
|
|
|
|
# 连续运行,用于输出行情
|
2017-05-03 08:07:27 +00:00
|
|
|
|
#app.exec_()
|
2017-05-03 07:30:40 +00:00
|
|
|
|
|