解决simnow经常出现的未初始化异常。交易时间定时判断MD/TD是否连接。

This commit is contained in:
msincenselee 2017-05-11 11:39:38 +08:00
parent fdea5a35af
commit 4cc6497560
4 changed files with 59 additions and 13 deletions

View File

@ -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:
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'主动断开连接')
@ -561,6 +574,7 @@ class CtpTdApi(TdApi):
self.writeLog(text.TRADING_SERVER_LOGIN)
# 确认结算信息
req = {}
req['BrokerID'] = self.brokerID

View File

@ -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,23 +66,39 @@ 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))

View File

@ -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):
"""订阅特定接口的行情"""

View File

@ -146,6 +146,10 @@ class VtGateway(object):
"""查询持仓"""
pass
def checkStatus(self):
"""查询状态"""
return True
# ----------------------------------------------------------------------
def close(self):
"""关闭"""