def runChildProcess(): """子进程运行函数""" print '-'*20 # 创建日志引擎 le = LogEngine() le.setLogLevel(le.LEVEL_INFO) le.addConsoleHandler() le.info(u'启动行情记录运行子进程') ee = EventEngine2() le.info(u'事件引擎创建成功') me = MainEngine(ee) me.addGateway(ctpGateway) me.addApp(dataRecorder) le.info(u'主引擎创建成功') ee.register(EVENT_LOG, le.processLogEvent) le.info(u'注册日志事件监听') me.connect('CTP') le.info(u'连接CTP接口') while True: sleep(1)
def runChildProcess(): """子进程运行函数""" # 创建日志引擎 le = LogEngine() le.setLogLevel(le.LEVEL_INFO) le.addConsoleHandler() le.info(u'启动行情记录运行子进程') ee = EventEngine2() le.info(u'事件引擎创建成功') me = MainEngine(ee) me.addGateway(ctpGateway) me.addApp(dataRecorder) le.info(u'主引擎创建成功') ee.register(EVENT_LOG, le.processLogEvent) le.info(u'注册日志事件监听') me.connect('CTP') le.info(u'连接CTP接口') has_recorder_day = False while True: sleep(1) if has_recorder_day == False: time_now = datetime.now() if time_now.time().hour > 15 and time_now.time().minute > 5: event1 = Event(type_=EVENT_RECORDER_DAY) ee.put(event1) has_recorder_day = True
def main(): """主程序入口""" le = LogEngine() le.setLogLevel(le.LEVEL_INFO) le.addConsoleHandler() le.addFileHandler() le.info(u'启动JAQS服务进程') ee = EventEngine() le.info(u'事件引擎创建成功') me = MainEngine(ee) me.addGateway(ctpGateway) me.addApp(jaqsService) le.info(u'主引擎创建成功') ee.register(EVENT_LOG, le.processLogEvent) ee.register(EVENT_JS_LOG, le.processLogEvent) le.info(u'注册日志事件监听') me.connect('CTP') le.info(u'连接CTP接口') while True: sleep(1)
def runChildProcess(): """子进程运行函数""" print '-' * 20 # 创建日志引擎 le = LogEngine() le.setLogLevel(le.LEVEL_INFO) le.addConsoleHandler() le.info(u'启动行情记录运行子进程') ee = EventEngine2() le.info(u'事件引擎创建成功') me = MainEngine(ee) me.addGateway(okexGateway) me.addApp(dataRecorder) le.info(u'主引擎创建成功') ee.register(EVENT_LOG, le.processLogEvent) ee.register(EVENT_ERROR, processErrorEvent) le.info(u'注册日志事件监听') me.connect('OKEX') le.info(u'连接OKEX接口') while True: sleep(1)
def runChildProcess(): """子进程运行函数""" print('-' * 20) # 创建日志引擎 le = LogEngine() le.setLogLevel(le.LEVEL_INFO) le.addConsoleHandler() le.info('启动行情记录运行子进程') ee = EventEngine2() le.info('事件引擎创建成功') me = MainEngine(ee) me.addGateway(ctpGateway) me.addApp(dataRecorder) le.info('主引擎创建成功') ee.register(EVENT_LOG, le.processLogEvent) le.info('注册日志事件监听') me.connect('CTP') le.info('连接CTP接口') while True: sleep(1)
def runParentProcess(): """父进程运行函数""" # 创建日志引擎 le = LogEngine() le.setLogLevel(le.LEVEL_WARN) le.addConsoleHandler() le.info(u'启动CTA策略守护父进程') DAY_START1 = time(8, 58) # 日盘上午启动和停止时间 DAY_END1 = time(10, 17) DAY_START2 = time(10, 28) DAY_END2 = time(11, 32) DAY_START3 = time(13, 28) # 日盘下午启动和停止时间 DAY_END3 = time(15, 02) NIGHT_START = time(20, 58) # 夜盘启动和停止时间 NIGHT_END = time(2, 32) p = None # 子进程句柄 # scheduler = BackgroundScheduler() # scheduler = BlockingScheduler() while True: currentTime = datetime.now().time() # recording = False # 实际环境应该是这个 recording = True # 用于测试,开盘收盘时间之外也可以执行策略里面 # 判断当前处于的时间段 if (currentTime >= DAY_START1 and currentTime <= DAY_END1 ) or (currentTime >= DAY_START2 and currentTime <= DAY_END2) or ( currentTime >= DAY_START3 and currentTime <= DAY_END3) or ( currentTime >= NIGHT_START) or (currentTime <= NIGHT_END): recording = True # 记录时间则需要启动子进程 if recording and p is None: le.info(u'启动子进程') print 'before new' p = multiprocessing.Process(target=runChildProcess) print 'new mid' p.start() print 'new' le.info(u'子进程启动成功') # 非记录时间则退出子进程 if not recording and p is not None: le.info(u'关闭子进程') p.terminate() p.join() p = None le.info(u'子进程关闭成功') # sleep(5) can_done = Event() can_done.wait(timeout=5)
def runParentProcess(): """父进程运行函数""" # 创建日志引擎 le = LogEngine() le.setLogLevel(le.LEVEL_INFO) le.addConsoleHandler() le.addFileHandler() le.info(u'启动CTA策略守护父进程') DAY_START = time(8, 58) # 日盘启动和停止时间 DAY_END = time(15, 02) NIGHT_START = time(20, 58) # 夜盘启动和停止时间 NIGHT_END = time(23, 32) p = None # 子进程句柄 while True: currentTime = datetime.now().time() weekno = datetime.today().weekday() recording = False #recording = True if not itchat.instanceList[0].alive and globalSetting['web_chat']: try: itchat.auto_login(hotReload=True) except requests.exceptions.ConnectionError as e: print "ConnectionError happened {}".format(e) # 判断当前处于交易时段 if (((currentTime >= DAY_START and currentTime <= DAY_END) or (currentTime >= NIGHT_START and currentTime <= NIGHT_END))) and weekno < 5: recording = True # if(currentTime >= SHUT_START and currentTime <= SHUT_END) or weekno >= 5: #shut at weekend as well # recording = False # 记录时间则需要启动子进程 if recording and p is None: le.info(u'启动子进程') p = multiprocessing.Process(target=runChildProcess) p.start() le.info(u'子进程启动成功') # 非记录时间则退出子进程 if not recording and p is not None: le.info(u'关闭子进程') p.terminate() p.join() p = None le.info(u'子进程关闭成功') sleep(5)
def runParentProcess(): """父进程运行函数""" # 创建日志引擎 le = LogEngine() le.setLogLevel(le.LEVEL_INFO) le.addConsoleHandler() le.info('启动CTA策略守护父进程') # DAY_START = time(8, 59) # 日盘启动和停止时间1 # DAY_END = time(10, 16) # DAY_START2 = time(10, 29) # 日盘启动和停止时间2 # DAY_END2 = time(11, 31) # DAY_START3 = time(13, 29) # 日盘启动和停止时间3 # DAY_END3 = time(15, 1) # NIGHT_START = time(20, 59) # 夜盘启动和停止时间 # NIGHT_END = time(23, 31) p = None # 子进程句柄 while True: # currentTime = datetime.now().time() recording = True ##7天24小时无限制运行,False为限制运行条件 # 判断当前处于的时间段 # if ((currentTime >= DAY_START and currentTime <= DAY_END) or # (currentTime >= DAY_START2 and currentTime <= DAY_END2) or # (currentTime >= DAY_START3 and currentTime <= DAY_END3) or # (currentTime >= NIGHT_START and currentTime <= NIGHT_END)): # recording = True # if datetime.today().weekday() == 5 or datetime.today().weekday() == 6: # recording = False # 记录时间则需要启动子进程 if recording and p is None: le.info('启动子进程') p = multiprocessing.Process(target=runChildProcess) p.start() le.info('子进程启动成功') # 非记录时间则退出子进程 if not recording and p is not None: le.info('关闭子进程') p.terminate() p.join() p = None le.info('子进程关闭成功') sleep(1)
def main(): """主程序入口""" le = LogEngine() le.setLogLevel(le.LEVEL_INFO) le.addConsoleHandler() le.addFileHandler() le.info('服务器进程启动') # 创建事件引擎 ee = EventEngine2() le.info('事件引擎创建成功') # 创建主引擎 me = MainEngine(ee) # 添加交易接口 me.addGateway(ctpGateway) # 添加上层应用 me.addApp(ctaStrategy) me.addApp(rpcService) le.info('主引擎创建成功') # 阻塞运行 le.info('服务器启动成功') while 1: sleep(1)
def runParentProcess(): """父进程运行函数""" # 创建日志引擎 le = LogEngine() le.setLogLevel(le.LEVEL_INFO) #le.addConsoleHandler() le.info(u'启动CTA策略守护父进程') DAY_START = time(8, 45) # 日盘启动和停止时间 DAY_END = time(15, 30) NIGHT_START = time(20, 45) # 夜盘启动和停止时间 NIGHT_END = time(2, 45) p = None # 子进程句柄 while True: currentTime = datetime.now().time() recording = False weekend = False currentDay = datetime.now().weekday() if currentDay == 5 or currentDay == 6: ##周六 周日 不开盘 weekend = True # 判断当前处于的时间段 if ((currentTime >= DAY_START and currentTime <= DAY_END) or (currentTime >= NIGHT_START) or (currentTime <= NIGHT_END)): recording = True # 记录时间则需要启动子进程 if recording and p is None: if weekend == False: le.info(u'启动子进程') p = multiprocessing.Process(target=runChildProcess) p.start() le.info(u'子进程启动成功') # 非记录时间则退出子进程 if not recording and p is not None: le.info(u'关闭子进程') p.terminate() p.join() p = None le.info(u'子进程关闭成功') sleep(5)
def main(): """主程序入口""" le = LogEngine() le.setLogLevel(le.LEVEL_INFO) le.addConsoleHandler() le.addFileHandler() le.info(u'服务器进程启动') # 创建事件引擎 ee = EventEngine2() le.info(u'事件引擎创建成功') # 创建主引擎 me = MainEngine(ee) # 添加交易接口 me.addGateway(ctpGateway) # 添加上层应用 me.addApp(ctaStrategy) me.addApp(rpcService) le.info(u'主引擎创建成功') # 阻塞运行 le.info(u'服务器启动成功') while 1: sleep(1)
def runParentProcess(): """父进程运行函数,限制交易时间""" # 创建日志引擎 le = LogEngine() le.setLogLevel(le.LEVEL_INFO) le.addConsoleHandler() le.info('启动行情记录守护父进程') DAY_START = time(8, 59) # 日盘启动和停止时间1 DAY_END = time(10, 16) DAY_START2 = time(10, 29) # 日盘启动和停止时间2 DAY_END2 = time(11, 31) DAY_START3 = time(13, 29) # 日盘启动和停止时间3 DAY_END3 = time(15, 0o1) NIGHT_START = time(20, 59) # 夜盘启动和停止时间 NIGHT_END = time(23, 31) p = None # 子进程句柄 while True: currentTime = datetime.now().time() recording = False # 判断当前处于的时间段 if ((currentTime >= DAY_START and currentTime <= DAY_END) or (currentTime >= DAY_START2 and currentTime <= DAY_END2) or (currentTime >= DAY_START3 and currentTime <= DAY_END3) or (currentTime >= NIGHT_START and currentTime <= NIGHT_END)): recording = True if datetime.today().weekday() == 5 or datetime.today().weekday() == 6: recording = False # 记录时间则需要启动子进程 if recording and p is None: le.info('启动子进程') p = multiprocessing.Process(target=runChildProcess) p.start() le.info('子进程启动成功') # 非记录时间则退出子进程 if not recording and p is not None: le.info('关闭子进程') p.terminate() p.join() p = None le.info('子进程关闭成功') sleep(5)
def runParentProcess(): """父进程运行函数""" # 创建日志引擎 le = LogEngine() le.setLogLevel(le.LEVEL_INFO) le.addConsoleHandler() le.info(u'启动行情记录守护父进程') ''' DAY_START = time(8, 57) # 日盘启动和停止时间 DAY_END = time(15, 18) NIGHT_START = time(20, 57) # 夜盘启动和停止时间 NIGHT_END = time(2, 33) ''' p = None # 子进程句柄 while True: ''' currentTime = datetime.now().time() recording = False # 判断当前处于的时间段 if ((currentTime >= DAY_START and currentTime <= DAY_END) or (currentTime >= NIGHT_START) or (currentTime <= NIGHT_END)): recording = True # 过滤周末时间段:周六全天,周五夜盘,周日日盘 if ((datetime.today().weekday() == 6) or (datetime.today().weekday() == 5 and currentTime > NIGHT_END) or (datetime.today().weekday() == 0 and currentTime < DAY_START)): recording = False ''' recording = True # 记录时间则需要启动子进程 if recording and p is None: le.info(u'启动子进程') p = multiprocessing.Process(target=runChildProcess) p.start() le.info(u'子进程启动成功') # 非记录时间则退出子进程 if not recording and p is not None: le.info(u'关闭子进程') p.terminate() p.join() p = None le.info(u'子进程关闭成功') sleep(5)
def runParentProcess(): """父进程运行函数""" # 创建日志引擎 le = LogEngine() le.setLogLevel(le.LEVEL_INFO) le.addConsoleHandler() le.info(u'启动行情记录守护父进程') DAY_START = time(8, 57) # 日盘启动和停止时间 DAY_END = time(15, 18) NIGHT_START = time(20, 57) # 夜盘启动和停止时间 NIGHT_END = time(2, 33) p = None # 子进程句柄 while True: currentTime = datetime.now().time() recording = False # 判断当前处于的时间段 if ((currentTime >= DAY_START and currentTime <= DAY_END) or (currentTime >= NIGHT_START) or (currentTime <= NIGHT_END)): recording = True # 过滤周末时间段:周六全天,周五夜盘,周日日盘 if ((datetime.today().weekday() == 6) or (datetime.today().weekday() == 5 and currentTime > NIGHT_END) or (datetime.today().weekday() == 0 and currentTime < DAY_START)): recording = False # 记录时间则需要启动子进程 if recording and p is None: le.info(u'启动子进程') p = multiprocessing.Process(target=runChildProcess) p.start() le.info(u'子进程启动成功') # 非记录时间则退出子进程 if not recording and p is not None: le.info(u'关闭子进程') p.terminate() p.join() p = None le.info(u'子进程关闭成功') sleep(5)
def runParentProcess(): """父进程运行函数""" # 创建日志引擎 le = LogEngine() le.setLogLevel(le.LEVEL_WARN) le.addConsoleHandler() le.info(u'启动CTA策略守护父进程') DAY_START1 = time(9, 0) # 日盘上午启动和停止时间 DAY_END1 = time(11, 30) DAY_START2 = time(13, 30) # 日盘下午启动和停止时间 DAY_END2 = time(15, 00) NIGHT_START = time(21, 0) # 夜盘启动和停止时间 NIGHT_END = time(2, 30) p = None # 子进程句柄 while True: currentTime = datetime.now().time() # recording = False # 实际环境应该是这个 recording = True # 用于测试,开盘收盘时间之外也可以执行策略里面 # 判断当前处于的时间段 if ((currentTime >= DAY_START1 and currentTime <= DAY_END1) or (currentTime >= DAY_START2 and currentTime <= DAY_END2) or (currentTime >= NIGHT_START) or (currentTime <= NIGHT_END)): recording = True # 记录时间则需要启动子进程 if recording and p is None: le.info(u'启动子进程') p = multiprocessing.Process(target=runChildProcess) p.start() le.info(u'子进程启动成功') # 非记录时间则退出子进程 if not recording and p is not None: le.info(u'关闭子进程') p.terminate() p.join() p = None le.info(u'子进程关闭成功') sleep(5)
def runChildProcess(): """子进程运行函数""" print '*' * 50 # 创建日志引擎 le = LogEngine() le.setLogLevel(le.LEVEL_INFO) le.addConsoleHandler() le.addFileHandler() le.info(u'marketMaker childProcess is working') ee = EventEngine2() me = MainEngine(ee) le.info(u'MainEngine has been established') me.addGateway(huobiGateway) le.info(u'huobiGateway has been established') ee.register(EVENT_LOG, le.processLogEvent) ee.register(EVENT_MMK_LOG, le.processLogEvent) ee.register(EVENT_ERROR, processError) le.info(u'register log event monitor') me.connect('HUOBI') le.info(u'connect huobiGateway') sleep(5) # 等待CTP接口初始化 # for k,v in me.dataEngine.contractDict.items(): # print '%s:%s' % (k,v.__dict__) me.addApp(mmkStrategy) mmk = me.getApp(mmkStrategy.appName) mmk.loadSetting() # le.info(u'MMK策略载入成功') mmk.initAll() # le.info(u'MMK策略初始化成功') mmk.startAll() # le.info(u'MMK策略启动成功') while True: sleep(10)
def runParentProcess(): """父进程运行函数""" # 创建日志引擎 le = LogEngine() le.setLogLevel(le.LEVEL_INFO) le.addConsoleHandler() le.info(u'启动CTA策略守护父进程') day_start = time(8, 45) # 日盘启动和停止时间 day_end = time(15, 30) night_start = time(20, 45) # 夜盘启动和停止时间 night_end = time(2, 45) p = None # 子进程句柄 while True: currentTime = datetime.now().time() recording = False # 判断当前处于的时间段 if ((currentTime >= day_start and currentTime <= day_end) or (currentTime >= night_start) or (currentTime <= night_end)): recording = True # 记录时间则需要启动子进程 if recording and p is None: if util.isOpenFromJqdata() is False: sleep(3600) continue le.info(u'启动子进程') p = multiprocessing.Process(target=runChildProcess) p.start() le.info(u'子进程启动成功') # 非记录时间则退出子进程 if not recording and p is not None: le.info(u'关闭子进程') p.terminate() p.join() p = None le.info(u'子进程关闭成功') sleep(5)
def run(self): if self.running: return self.running = True le = LogEngine() self.le = le le.setLogLevel(le.LEVEL_INFO) le.addConsoleHandler() le.addFileHandler() le.info(u'启动CTA策略守护父进程') DAY_START = time(8, 45) # 日盘启动和停止时间 DAY_END = time(15, 30) NIGHT_START = time(20, 45) # 夜盘启动和停止时间 NIGHT_END = time(2, 45) self.process = None # 子进程句柄
def runParentProcess(): """父进程运行函数""" # 创建日志引擎 le = LogEngine() le.setLogLevel(le.LEVEL_INFO) le.addConsoleHandler() le.info(u'启动CTA策略守护父进程') DAY_START = time(19, 00) # 日盘启动和停止时间 DAY_END = time(19, 28) NIGHT_START = time(20, 45) # 夜盘启动和停止时间 NIGHT_END = time(2, 45) p = None # 子进程句柄 while True: currentTime = datetime.now().time() recording = False # 判断当前处于的时间段 if (currentTime >= DAY_START and currentTime <= DAY_END): recording = True # 记录时间则需要启动子进程 if recording and p is None: le.info(u'启动子进程') p = multiprocessing.Process(target=runChildProcess) p.start() le.info(u'子进程启动成功') # 非记录时间则退出子进程 if not recording and p is not None: le.info(u'关闭子进程') p.terminate() p.join() p = None le.info(u'子进程关闭成功') return sleep(5)
def runChildProcess(): """子进程运行函数""" print('-' * 20) # 创建日志引擎 le = LogEngine() le.setLogLevel(le.LEVEL_INFO) le.addConsoleHandler() le.info(u'启动行情记录运行子进程') ee = EventEngine2() le.info(u'事件引擎创建成功') me = MainEngine(ee) me.addGateway(ctpGateway) me.addApp(dataRecorder) le.info(u'主引擎创建成功') ee.register(EVENT_LOG, le.processLogEvent) ee.register(EVENT_ERROR, processErrorEvent) le.info(u'注册日志事件监听') me.connect('CTP') le.info(u'连接CTP接口') #获取全部合约信息并自动订阅 contracts = me.getAllContracts() while len(contracts) == 0: sleep(1) contracts = me.getAllContracts() print(u"共获取" + str(len(contracts)) + u"条合约信息") for contract in contracts: if contract.productClass == u"期货": req = VtSubscribeReq() req.symbol = contract.symbol me.subscribe(req, contract.gatewayName) me.getApp(dataRecorder.appName).tickSymbolSet.add(req.symbol) while True: sleep(1)
def runChildProcess(): """子进程运行函数""" print('-' * 30) # 创建日志引擎 le = LogEngine() le.setLogLevel(le.LEVEL_INFO) # le.addConsoleHandler() # le.addFileHandler() le.info(u'启动CTA策略运行子进程') ee = EventEngine2() le.info(u'事件引擎创建成功') me = MainEngine(ee) me.addGateway(okexGateway) me.addApp(ctaStrategy) le.info(u'主引擎创建成功') ee.register(EVENT_LOG, le.processLogEvent) ee.register(EVENT_CTA_LOG, le.processLogEvent) ee.register(EVENT_ERROR, processErrorEvent) le.info(u'注册日志事件监听') key = findConnectKey() me.connect(key) le.info(u'连接行情和交易接口') sleep(5) # 等待Gateway接口初始化 me.dataEngine.saveContracts() # 保存合约信息到文件 # 加载CTA策略 cta = me.getApp(ctaStrategy.appName) cta.loadSetting() cta.initAll() cta.startAll() while True: sleep(1)
def main(): """主程序入口""" le = LogEngine() le.setLogLevel(le.LEVEL_INFO) le.addConsoleHandler() le.addFileHandler() le.info(u'服务器进程启动') # 创建事件引擎 ee = EventEngine2() le.info(u'事件引擎创建成功') # 创建主引擎 me = MainEngine(ee) # 安全退出机制 def shutdown(signal, frame): le.info(u'安全关闭进程') me.exit() sys.exit() for sig in [signal.SIGINT, signal.SIGTERM]: signal.signal(sig, shutdown) # 添加交易接口 me.addGateway(ctpGateway) # 添加上层应用 me.addApp(ctaStrategy) me.addApp(rpcService) cta = me.getApp(ctaStrategy.appName) le.info(u'主引擎创建成功') cta.loadSetting() le.info(u'CTA策略载入成功') # 阻塞运行 le.info(u'服务器启动成功') while 1: sleep(1)
def main(): """主程序入口""" le = LogEngine() le.setLogLevel(le.LEVEL_INFO) le.addConsoleHandler() le.addFileHandler() le.info(u'服务器进程启动') # 创建事件引擎 ee = EventEngine2() le.info(u'事件引擎创建成功') # 创建主引擎 me = MainEngine(ee) # 安全退出机制 def shutdown(signal, frame): le.info(u'安全关闭进程') me.exit() sys.exit() for sig in [signal.SIGINT, signal.SIGTERM]: signal.signal(sig, shutdown) # 添加交易接口 me.addGateway(ctpGateway) # 添加上层应用 me.addApp(ctaStrategy) me.addApp(rpcService) le.info(u'主引擎创建成功') # 阻塞运行 le.info(u'服务器启动成功') while 1: sleep(1)
def runChildProcess(): """子进程运行函数""" print('-'*20) # 创建日志引擎,其实在MainEngine中会创建日志引擎的, # 不过为了能在这里就可以使用,就先创建了,反正是singlon模式 le = LogEngine() le.setLogLevel(le.LEVEL_INFO) le.addConsoleHandler() le.addFileHandler() le.info(u'启动CTA策略运行子进程') ee = EventEngine2() le.info(u'事件引擎创建成功') me = MainEngine(ee) me.addGateway(ctpGateway) me.addApp(ctaStrategy) le.info(u'主引擎创建成功') ee.register(EVENT_LOG, le.processLogEvent) ee.register(EVENT_CTA_LOG, le.processLogEvent) ee.register(EVENT_ERROR, processErrorEvent) le.info(u'注册日志事件监听') me.connect('CTP') le.info(u'连接CTP接口') sleep(10) # 等待CTP接口初始化 me.dataEngine.saveContracts() # 保存合约信息到文件 cta = me.getApp(ctaStrategy.appName) cta.loadSetting() le.info(u'CTA策略载入成功') cta.initAll() le.info(u'CTA策略初始化成功') cta.startAll() le.info(u'CTA策略启动成功') while True: sleep(1)
def runChildProcess(): """子进程运行函数""" print '-' * 20 # 创建日志引擎 le = LogEngine() le.setLogLevel(le.LEVEL_INFO) le.addConsoleHandler() le.addFileHandler() le.info(u'启动CTA策略运行子进程') ee = EventEngine2() le.info(u'事件引擎创建成功') me = MainEngine(ee) me.addGateway(futuGateway) me.addApp(ctaStrategy) le.info(u'主引擎创建成功') ee.register(EVENT_LOG, le.processLogEvent) ee.register(EVENT_CTA_LOG, le.processLogEvent) le.info(u'注册日志事件监听') me.connect('FUTU') le.info(u'连接CTP接口') sleep(5) # 等待CTP接口初始化 cta = me.getApp(ctaStrategy.appName) cta.loadSetting() le.info(u'CTA策略载入成功') cta.initAll() le.info(u'CTA策略初始化成功') cta.startAll() le.info(u'CTA策略启动成功') while True: sleep(1)
class SignalScaningEngine(object): """ 信号扫描回测引擎 函数接口和信号引擎保持一样, 从而实现同一套代码从回测到实盘。 """ TICK_MODE = 'tick' BAR_MODE = 'bar' DB_DATA_TYPE = 'DB' FILE_DATA_TYPE = 'FILE' #---------------------------------------------------------------------- def __init__(self): """Constructor""" self.dataStartDate = None # 回测数据开始日期,String self.dataEndDate = None # 回测数据结束日期,String self.dt = None # 最新的时间 self.logList = [] # 日志记录 self.datatype = self.DB_DATA_TYPE # 日志引擎实例 self.logEngine = None self.initLogEngine() #---------------------------------------------------------------------- def initLogEngine(self): """初始化日志引擎""" if not globalSetting["logActive"]: return # 创建引擎 self.logEngine = LogEngine() # 设置日志级别 levelDict = { "debug": LogEngine.LEVEL_DEBUG, "info": LogEngine.LEVEL_INFO, "warn": LogEngine.LEVEL_WARN, "error": LogEngine.LEVEL_ERROR, "critical": LogEngine.LEVEL_CRITICAL, } level = levelDict.get(globalSetting["logLevel"], LogEngine.LEVEL_CRITICAL) self.logEngine.setLogLevel(level) # 设置输出 if globalSetting['logConsole']: self.logEngine.addConsoleHandler() if globalSetting['logFile']: self.logEngine.addFileHandler() # 注册事件监听 #self.registerLogEvent(EVENT_LOG) #------------------------------------------------ # 通用功能 #------------------------------------------------ #---------------------------------------------------------------------- def roundToPriceTick(self, price): """取整价格到合约最小价格变动""" if not self.priceTick: return price newPrice = round(price / self.priceTick, 0) * self.priceTick return newPrice #---------------------------------------------------------------------- def output(self, content): """输出内容""" print(str(datetime.now()) + "\t" + content) #------------------------------------------------ # 参数设置相关 #------------------------------------------------ #---------------------------------------------------------------------- def setStartDate(self, startDate='20100416', initDays=0): """设置回测的启动日期""" self.dataStartDate = startDate # self.initDays = initDays # self.dataStartDate = datetime.strptime(startDate, '%Y%m%d') # initTimeDelta = timedelta(initDays) # self.strategyStartDate = self.dataStartDate + initTimeDelta #---------------------------------------------------------------------- def setEndDate(self, endDate='20100416'): self.dataEndDate = endDate #---------------------------------------------------------------------- def setBacktestingMode(self, mode): """设置回测模式""" self.mode = mode #---------------------------------------------------------------------- def setDatabase(self, dbName, symbol): """设置历史数据所用的数据库""" self.dbName = dbName self.symbol = symbol #---------------------------------------------------------------------- def setSize(self, size): """设置合约大小""" self.size = size #---------------------------------------------------------------------- def setPriceTick(self, priceTick): """设置价格最小变动""" self.priceTick = priceTick #------------------------------------------------ # 数据回放相关 #------------------------------------------------ #---------------------------------------------------------------------- def loadHistoryData(self): # 首先根据回测模式,确认要使用的数据类 if self.mode == self.BAR_MODE: dataClass = VtBarData func = self.newBar else: dataClass = VtTickData func = self.newTick if self.datatype == self.DB_DATA_TYPE: pass else: pass """载入历史数据""" self.dbClient = pymongo.MongoClient(globalSetting['mongoHost'], globalSetting['mongoPort']) collection = self.dbClient[self.dbName][self.symbol] self.output(u'开始载入数据') # 载入初始化需要用的数据 strStartDate = self.dataStartDate strEndDate = self.dataEndDate msg = u'data length:%s %s', strStartDate, strEndDate print(msg) flt = {'date': {'$gte': strStartDate, '$lt': strStartDate}} initCursor = collection.find(flt).sort('datetime') print('flt:', flt) print(self.dbName, self.symbol) # 将数据从查询指针中读取出,并生成列表 self.initData = [] # 清空initData列表 for d in initCursor: data = dataClass() data.__dict__ = d self.initData.append(data) # 载入回测数据 if not self.dataEndDate: flt = { 'date': { '$gte': strStartDate } } # 数据过滤条件 self.strategyStartDate else: flt = { 'date': { "$gte": strStartDate, "$lte": strEndDate } } # self.dataEndDate self.dbCursor = collection.find(flt).sort('datetime') print('flt:', flt) print(self.dbName, self.symbol) if isinstance(self.dbCursor, list): count = len(initCursor) + len(self.dbCursor) else: count = initCursor.count() + self.dbCursor.count() self.output(u'载入完成,数据量:%s' % count) #---------------------------------------------------------------------- def runBacktesting(self): """运行回测""" # 载入历史数据 self.loadHistoryData() # 首先根据回测模式,确认要使用的数据类 if self.mode == self.BAR_MODE: dataClass = VtBarData func = self.newBar else: dataClass = VtTickData func = self.newTick self.output(u'开始回测') #self.strategy.onInit() #self.strategy.inited = True self.output(u'策略初始化完成') self.strategy.trading = True self.strategy.onStart() self.output(u'策略启动完成') self.output(u'开始回放数据') for d in self.dbCursor: data = dataClass() data.__dict__ = d func(data) self.output(u'数据回放结束') #---------------------------------------------------------------------- def newBar(self, bar): """新的K线""" self.bar = bar self.dt = bar.datetime self.strategy.onBar(bar) # 推送K线到策略中 self.updateDailyClose(bar.datetime, bar.close) #---------------------------------------------------------------------- def newTick(self, tick): """新的Tick""" self.tick = tick self.dt = tick.datetime self.strategy.onTick(tick) self.updateDailyClose(tick.datetime, tick.lastPrice) #---------------------------------------------------------------------- def initStrategy(self, signalClass, setting=None): """ 初始化策略 setting是策略的参数设置,如果使用类中写好的默认设置则可以不传该参数 """ self.strategy = signalClass(self, setting, signalClass.templateName) self.strategy.name = self.strategy.className #------------------------------------------------ # 策略接口相关 #------------------------------------------------ #---------------------------------------------------------------------- def subscribe(self, call, vtSymbol): """回测中忽略""" pass def putStrategyEvent(self, name): """发送策略更新事件,回测中忽略""" pass #---------------------------------------------------------------------- def putVarEvent(self, call, d): """更新变量""" pass #---------------------------------------------------------------------- def putParamEvent(self, call, d): """发送策略更新事件,回测中忽略""" pass #---------------------------------------------------------------------- def insertData(self, dbName, collectionName, data): """考虑到回测中不允许向数据库插入数据,防止实盘交易中的一些代码出错""" pass #---------------------------------------------------------------------- def putSignalEvent(self, signal, d): """出现信号""" signalName = signal.signalName d['signalName'] = signalName # event = Event(EVENT_SIGNAL_PARAM) # event.dict_['data'] = d # self.eventEngine.put( event ) # self.mainEngine.dbInsert( SIGNALTRADING_DB_NAME, SIGNAL_COLLECTION_NAME, d) # print( d ) self.log('debug', d) def log(self, level, msg): if (level == 'debug'): self.logEngine.debug(msg) if (level == 'info'): self.logEngine.info(msg) if (level == 'warn'): self.logEngine.warn(msg) if (level == 'error'): self.logEngine.error(msg) pass #---------------------------------------------------------------------- def loadBar(self, dbName, collectionName, startDate): """直接返回初始化数据列表中的Bar""" return self.initData #---------------------------------------------------------------------- def loadTick(self, dbName, collectionName, startDate): """直接返回初始化数据列表中的Tick""" return self.initData #---------------------------------------------------------------------- def writeLog(self, content): """记录日志""" log = str(self.dt) + ' ' + content self.logList.append(log) #---------------------------------------------------------------------- def saveSyncData(self, strategy): """保存同步数据(无效)""" pass #---------------------------------------------------------------------- def getPriceTick(self, strategy): """获取最小价格变动""" return self.priceTick #---------------------------------------------------------------------- def sendSignal(self, signal): # VtSignal = type(signal) pass #---------------------------------------------------------------------- def updateDailyClose(self, datetime, lastPrice): pass
START_TIME = time(20, 45) CLOSE_TIME = time(15, 30) if __name__ == '__main__': # 创建日志引擎 le = LogEngine() le.setLogLevel(le.LEVEL_INFO) le.addConsoleHandler() # 进入循环 pWeb = None pTrading = None while True: le.info('-' * 30) dt = datetime.now() le.info(u'当前时间:%s' % dt) currentTime = dt.time() if currentTime >= START_TIME or currentTime <= CLOSE_TIME: le.info(u'当前处于交易时间段') if not pTrading: pTrading = Process(target=runTradingServer) pTrading.start() le.info(u'启动交易服务器进程') if not pWeb:
def runChildProcess(): """子进程运行函数""" print '*' * 50 # 创建日志引擎 le = LogEngine() le.setLogLevel(le.LEVEL_INFO) le.addConsoleHandler() le.addFileHandler() le.info(u'tab childProcess is working') ee = EventEngine2() me = MainEngine(ee) le.info(u'MainEngine has been established') me.addGateway(binanceGateway) le.info(u'binanceGateway has been established') ee.register(EVENT_LOG, le.processLogEvent) ee.register(EVENT_ERROR, processError) ee.register(EVENT_TABTRADING_LOG, le.processLogEvent) ee.register(EVENT_TABTRADING_ALGOLOG, le.processLogEvent) le.info(u'register log event monitor') me.connect('BINANCE') le.info(u'connect binanceGateway') sleep(5) # 等待CTP接口初始化 # for k,v in me.dataEngine.contractDict.items(): # print '%s:%s' % (k,v.__dict__) me.addApp(tabStrategy_binance) tab = me.getApp(tabStrategy_binance.appName) tab.init() le.info(u'tabEngine_binance has been inited') # tab.algoEngine.startAlgo() # le.info(u'tabAlgo已启动') # tab.startAll() # le.info(u'CTA策略启动成功') while True: sleep(10)
def runChildProcess(): """子进程运行函数""" print '-'*20 # 创建日志引擎 le = LogEngine() le.setLogLevel(le.LEVEL_INFO) le.addConsoleHandler() le.addFileHandler() le.info(u'启动CTA策略运行子进程') ee = EventEngine2() le.info(u'事件引擎创建成功') me = MainEngine(ee) me.addGateway(ctpGateway) me.addApp(dailyStrategy) #me.addApp(rpcService) ##先不加 le.info(u'主引擎创建成功') ee.register(EVENT_LOG, le.processLogEvent) ee.register(EVENT_DAILY_LOG, le.processLogEvent) ee.register(EVENT_ERROR,le.processErrorEvent) le.info(u'注册日志事件监听') me.connect('CTP') le.info(u'连接CTP接口') le.setMysqlClient(me.mysqlClient) ##日志写数据库 sleep(5) # 等待CTP接口初始化 cta = me.getApp(dailyStrategy.appName) cta.loadSetting() le.info(u'CTA策略载入成功') cta.initAll() le.info(u'CTA策略初始化成功') cta.startAll() le.info(u'CTA策略启动成功') while True: sleep(1)
def run(self): print('-' * 30) self.running = True # 创建日志引擎 le = LogEngine() self.le = le le.setLogLevel(le.LEVEL_INFO) # le.addConsoleHandler() # le.addFileHandler() le.info(u'启动CTA策略运行子进程') ee = EventEngine2() self.ee = ee le.info(u'事件引擎创建成功') me = MainEngine(ee) self.me = me me.addGateway(okexfGateway) me.addGateway(huobiGateway) me.addGateway(binanceGateway) me.addGateway(bitmexGateway) me.addGateway(oandaGateway) if is_windows: me.addGateway(ctpGateway) me.addApp(ctaStrategy) le.info(u'主引擎创建成功') ee.register(EVENT_LOG, le.processLogEvent) ee.register(EVENT_CTA_LOG, le.processLogEvent) ee.register(EVENT_ERROR, processErrorEvent) le.info(u'注册日志事件监听') for gw in self.get_gateways(me): le.info(u'连接Gateway[%s]的行情和交易接口' % gw) me.connect(gw) sleep(5) # 等待接口初始化 me.dataEngine.saveContracts() # 保存合约信息到文件 cta = me.getApp(ctaStrategy.appName) self.cta = cta le.info(u"读取策略配置") cta.loadSetting() le.info(u"初始化所有策略") cta.initAll() le.info(u"开始所有策略") cta.startAll()
def runChildProcess(): """子进程运行函数""" print '-'*20 # 创建日志引擎 le = LogEngine() le.setLogLevel(le.LEVEL_INFO) le.addConsoleHandler() le.addFileHandler() le.info(u'启动CTA策略运行子进程') ee = EventEngine2() le.info(u'事件引擎创建成功') me = MainEngine(ee) me.addGateway(ctpGateway) me.addApp(ctaStrategy) le.info(u'主引擎创建成功') ee.register(EVENT_LOG, le.processLogEvent) ee.register(EVENT_CTA_LOG, le.processLogEvent) le.info(u'注册日志事件监听') me.connect('CTP') le.info(u'连接CTP接口') sleep(10) # 等待CTP接口初始化 cta = me.getApp(ctaStrategy.appName) cta.loadSetting() le.info(u'CTA策略载入成功') cta.initAll() le.info(u'CTA策略初始化成功') cta.startAll() le.info(u'CTA策略启动成功') while True: sleep(1)
if __name__ == '__main__': # 创建日志引擎 le = LogEngine() le.setLogLevel(le.LEVEL_INFO) le.addConsoleHandler() # 进入循环 pWeb = None pTrading = None import os print(os.getpid()) while True: le.info('-'*30) dt = datetime.now() le.info(u'当前时间:%s' %dt) currentTime = dt.time() if currentTime >= START_TIME or currentTime <= CLOSE_TIME: le.info(u'当前处于交易时间段') if not pTrading: pTrading = Process(target=runTradingServer) pTrading.start() le.info(u'启动交易服务器进程') if not pWeb:
def runChildProcess(): """子进程运行函数""" print('-' * 20) # 创建日志引擎 le = LogEngine() le.setLogLevel(le.LEVEL_INFO) le.addConsoleHandler() le.addFileHandler() le.info(u'启动CTA策略运行子进程') DAY_START = time(19, 00) # 日盘启动和停止时间 DAY_END = time(19, 43) cta = None # 子进程句柄 while True: currentTime = datetime.now().time() recording = False # 判断当前处于的时间段 if (currentTime >= DAY_START and currentTime <= DAY_END): recording = True else: le.info(u'未在启动时间段') # 记录时间则需要启动子进程 if recording and cta is None: ee = EventEngine2() #le.info(u'事件引擎创建成功') me = MainEngine(ee) #连接数据库 me.dbConnect() le.info("连接数据库") me.addGateway(tushareGateway) me.addApp(dataRecorderCT) me.addApp(ctaStrategy) le.info(u'主引擎创建成功') ee.register(EVENT_LOG, le.processLogEvent) ee.register(EVENT_CTA_LOG, le.processLogEvent) ee.register(EVENT_ERROR, processErrorEvent) le.info(u'注册日志事件监听') #连接数据接口 me.connect(tushareGateway.gatewayName) le.info(u'连接TUSHARE接口') #sleep(10) # 等待CTP接口初始化 cta = me.getApp(ctaStrategy.appName) cta.loadSetting() le.info(u'CTA策略载入成功') cta.initAll() le.info(u'CTA策略初始化成功') cta.startAll() le.info(u'CTA策略启动成功') # 非记录时间则退出子进程 if not recording and cta is not None: cta.stopAll() ee.stop() le.info(u'关闭子进程') return sleep(5)
def runBacktestingProcess(): """子进程运行函数""" print('-' * 20) # 创建日志引擎 le = LogEngine() le.setLogLevel(le.LEVEL_INFO) le.addConsoleHandler() le.addFileHandler() le.info(u'启动CTA策略运行子进程') ee = EventEngine2() me = MainEngine(ee) me.addApp(ctaStrategy) le.info(u'主引擎创建成功') ee.register(EVENT_LOG, le.processLogEvent) ee.register(EVENT_CTA_LOG, le.processLogEvent) ee.register(EVENT_ERROR, processErrorEvent) le.info(u'注册日志事件监听') me.dbConnect() le.info("连接数据库") cta = me.getApp(ctaStrategy.appName) cta.loadSetting() le.info(u'CTA策略载入成功') cta.setBackTestingMode() le.info(u'运行CTA策略回测') cta.runBacktesting() cta.stopAll() ee.stop() while True: sleep(1)
def run(self, monitor=False): self.running = True # 创建日志引擎 le = LogEngine() self.le = le le.setLogLevel(le.LEVEL_INFO) # le.addConsoleHandler() # le.addFileHandler() le.info(u'启动CTA策略运行子进程') ee = EventEngine2() self.ee = ee le.info(u'事件引擎创建成功') self.handle_rs_setting() me = initialize_main_engine(ee) self.me = me le.info(u'主引擎创建成功') ee.register(EVENT_LOG, le.processLogEvent) ee.register(EVENT_CTA_LOG, le.processLogEvent) ee.register(EVENT_ERROR, processErrorEvent) le.info(u'注册日志事件监听') for gw in self.get_gateways(me): le.info(u'连接Gateway[%s]的行情和交易接口' % gw) me.connect(gw) sleep(5) # 等待接口初始化 me.dataEngine.saveContracts() # 保存合约信息到文件 cta = me.getApp(ctaStrategy.appName) self.cta = cta le.info(u"读取策略配置") cta.loadSetting() le.info(u"初始化所有策略") cta.initAll() if monitor: cta.enablePlugin(CtaStrategyInfoPlugin) le.info(u"开始所有策略") cta.startAll()
def runChildProcess(): """子进程运行函数""" # print '-'*20 # 创建日志引擎 le = LogEngine() le.setLogLevel(le.LEVEL_INFO) le.addConsoleHandler() le.addFileHandler() le.info(u'启动行情记录运行子进程') # 创建主引擎 me = MainEngine() ee = me.eventEngine ee.register(EVENT_LOG, le.processLogEvent) ee.register(EVENT_CTA_LOG, le.processLogEvent) ee.register(EVENT_ERROR, processErrorEvent) try: # 添加交易接口 try: me.addGateway(ctpGateway) except: pass # 添加上层应用 me.addApp(riskManager) me.addApp(dataRecorder) #fix: 当服务端初始化完毕后再开启rpcService #me.addApp(rpcService) me.addApp(ctaStrategy) me.addApp(spreadTrading) me.addApp(algoTrading) le.info(u'主引擎创建成功') # 自动建立MongoDB数据库 me.dbConnect() le.info(u'connect MongoDB') # 自动建立CTP链接 me.connect('CTP') le.info(u'连接CTP接口') # 取仓位信息 me.qryPosition("CTP") while not me.getAllContracts(): sleep(5) le.info(u'收集合约信息...') sleep(3) le.info(u'合约信息中数量: %s' % len(me.getAllContracts())) # 及时保存数据引擎里的合约数据到硬盘 me.dataEngine.saveContracts() #服务端初始化完成 #开启RPC me.addApp(rpcService) le.info(u'开启RPCService') ''' bug-fix: 休息,以便于客户端连接上来收CTP信息 ''' sleep(5.) #CTP连接完成,发送重启信号 event = Event(EVENT_CTP_RESTARTED) me.eventEngine.put(event) le.info(u'通知客户端CTP RESTART') while True: sleep(1) except KeyboardInterrupt: le.info(u"Keyboard interrupt in process") finally: le.info(u"cleaning up")
def runChildProcess(): """子进程运行函数""" print('-'*20) # 创建日志引擎 le = LogEngine() le.setLogLevel(le.LEVEL_INFO) le.addConsoleHandler() le.addFileHandler() le.info(u'启动CTA策略运行子进程') ee = EventEngine2() le.info(u'事件引擎创建成功') me = MainEngine(ee) le.info(u'bbbbbbbbbbbbbbbbbbbbbbbbbbbbb') me.addGateway(ctpGateway) sleep(10) # 等待CTP接口初始化 le.info(u'aaaaaaaaaaaaaaaaaaaaaaaaaaa') me.addApp(ctaStrategy) me.addApp(weixinRecorder) le.info(u'主引擎创建成功') ee.register(EVENT_LOG, le.processLogEvent) ee.register(EVENT_CTA_LOG, le.processLogEvent) ee.register(EVENT_ERROR, processErrorEvent) le.info(u'注册日志事件监听') me.connect('CTP') le.info(u'连接CTP接口') sleep(10) # 等待CTP接口初始化 me.dataEngine.saveContracts() # 保存合约信息到文件 cta = me.getApp(ctaStrategy.appName) cta.loadSetting() le.info(u'CTA策略载入成功') cta.initAll() le.info(u'CTA策略初始化成功') cta.startAll() le.info(u'CTA策略启动成功') while True: sleep(1)