From 4cc6497560ef5f39834cdb053647dddab5b9392c Mon Sep 17 00:00:00 2001 From: msincenselee Date: Thu, 11 May 2017 11:39:38 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3simnow=E7=BB=8F=E5=B8=B8?= =?UTF-8?q?=E5=87=BA=E7=8E=B0=E7=9A=84=E6=9C=AA=E5=88=9D=E5=A7=8B=E5=8C=96?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E3=80=82=E4=BA=A4=E6=98=93=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E5=AE=9A=E6=97=B6=E5=88=A4=E6=96=ADMD/TD=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vn.trader/gateway/ctpGateway/ctpGateway.py | 26 ++++++++++++++----- vn.trader/noUiMain.py | 30 +++++++++++++++++----- vn.trader/vtEngine.py | 10 ++++++++ vn.trader/vtGateway.py | 6 ++++- 4 files changed, 59 insertions(+), 13 deletions(-) diff --git a/vn.trader/gateway/ctpGateway/ctpGateway.py b/vn.trader/gateway/ctpGateway/ctpGateway.py index 542d13bc..10f303ee 100644 --- a/vn.trader/gateway/ctpGateway/ctpGateway.py +++ b/vn.trader/gateway/ctpGateway/ctpGateway.py @@ -117,7 +117,6 @@ class CtpGateway(VtGateway): f = file(fileName) except IOError: self.writeLog(text.LOADING_ERROR) - return # 解析json文件 @@ -173,16 +172,28 @@ class CtpGateway(VtGateway): #---------------------------------------------------------------------- def qryAccount(self): """查询账户资金""" - if self.tdApi is not None: - self.tdApi.qryAccount() - + if self.tdApi is None: + self.tdConnected = False + return + self.tdApi.qryAccount() + #---------------------------------------------------------------------- def qryPosition(self): """查询持仓""" if self.tdApi is None: + self.mdConnected = False return self.tdApi.qryPosition() - + + def checkStatus(self): + """查询md/td的状态""" + if self.tdApi is None or self.mdApi is None: + return False + + if not self.tdConnected or not self.mdConnected: + return False + + return True #---------------------------------------------------------------------- def close(self): """关闭""" @@ -190,11 +201,13 @@ class CtpGateway(VtGateway): tmp1 = self.mdApi self.mdApi = None tmp1.close() + self.mdConnected = False if self.tdConnected and self.tdApi is not None: tmp2 = self.tdApi self.tdApi = None tmp2.close() + self.tdConnected = False self.writeLog(u'主动断开连接') @@ -560,7 +573,8 @@ class CtpTdApi(TdApi): self.gateway.tdConnected = True self.writeLog(text.TRADING_SERVER_LOGIN) - + + # 确认结算信息 req = {} req['BrokerID'] = self.brokerID diff --git a/vn.trader/noUiMain.py b/vn.trader/noUiMain.py index 6ff23c92..85ceb0fa 100644 --- a/vn.trader/noUiMain.py +++ b/vn.trader/noUiMain.py @@ -32,13 +32,15 @@ class NoUiMain(object): # gateway 是否连接 self.connected = False # gateway 的连接名称,在vtEngine.initGateway()里面定义,对应的配置文件是 "连接名称_connect.json", - self.gateway_name = 'CTP_EBF' + self.gateway_name = 'CTP' # 启动的策略实例,须在catAlgo/CtaSetting.json 里面定义 [u'S28_RB1001', u'S28_TFT', u'S28_HCRB',u'atr_rsi'] self.strategies = [u'S28_HCRB'] self.g_count = 0 - # 实例化主引擎 + self.last_dt = datetime.now() + + # 实例化 主引擎 print u'instance mainengine' self.mainEngine = MainEngine() @@ -64,28 +66,44 @@ class NoUiMain(object): # 十秒才执行一次检查 self.g_count += 1 - if self.g_count <= 10: + if self.g_count <= 20: return self.g_count = 0 - print u'noUiMain.py checkpoint:{0}'.format(datetime.now()) + dt = datetime.now() + if dt.hour != self.last_dt.hour: + self.last_dt = dt + self.mainEngine.writeLog( u'noUiMain.py checkpoint:{0}'.format(dt)) # 定时断开 if self.trade_off(): + """非交易时间""" if self.connected: - self.disconnect() self.mainEngine.writeLog(u'断开连接{0}'.format(self.gateway_name)) + self.disconnect() self.mainEngine.writeLog(u'清空数据引擎') self.mainEngine.clearData() self.connected = False return - # 定时重连 + # 交易时间内,定时重连和检查 if not self.connected: + self.mainEngine.writeLog(u'启动连接{0}'.format(self.gateway_name)) 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 + 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() + 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 def Start(self): """启动""" diff --git a/vn.trader/vtEngine.py b/vn.trader/vtEngine.py index c81103dc..a28281fc 100644 --- a/vn.trader/vtEngine.py +++ b/vn.trader/vtEngine.py @@ -86,6 +86,16 @@ class MainEngine(object): else: self.writeLog(text.GATEWAY_NOT_EXIST.format(gateway=gatewayName)) + def checkGatewayStatus(self,gatewayName): + """check gateway connect status""" + if gatewayName in self.gatewayDict: + gateway = self.gatewayDict[gatewayName] + return gateway.checkStatus() + + else: + self.writeLog(text.GATEWAY_NOT_EXIST.format(gateway=gatewayName)) + return False + # ---------------------------------------------------------------------- def subscribe(self, subscribeReq, gatewayName): """订阅特定接口的行情""" diff --git a/vn.trader/vtGateway.py b/vn.trader/vtGateway.py index 68abd967..da9c7e69 100644 --- a/vn.trader/vtGateway.py +++ b/vn.trader/vtGateway.py @@ -145,7 +145,11 @@ class VtGateway(object): def qryPosition(self): """查询持仓""" pass - + + def checkStatus(self): + """查询状态""" + return True + # ---------------------------------------------------------------------- def close(self): """关闭"""