diff --git a/vn.trader/gateway/ctpGateway/ctpGateway.py b/vn.trader/gateway/ctpGateway/ctpGateway.py index e4e6cbd1..0d38e711 100644 --- a/vn.trader/gateway/ctpGateway/ctpGateway.py +++ b/vn.trader/gateway/ctpGateway/ctpGateway.py @@ -17,6 +17,7 @@ from vnctpmd import MdApi from vnctptd import TdApi from ctpDataType import * from vtGateway import * +from language import text # 以下为一些VT类型和CTP类型的映射字典 @@ -105,7 +106,7 @@ class CtpGateway(VtGateway): except IOError: log = VtLogData() log.gatewayName = self.gatewayName - log.logContent = u'读取连接配置出错,请检查' + log.logContent = text.LOADING_ERROR self.onLog(log) return @@ -117,7 +118,9 @@ class CtpGateway(VtGateway): brokerID = str(setting['brokerID']) tdAddress = str(setting['tdAddress']) mdAddress = str(setting['mdAddress']) - if 'authCode' in setting: #如果json文件提供了验证码 + + # 如果json文件提供了验证码 + if 'authCode' in setting: authCode = str(setting['authCode']) userProductInfo = str(setting['userProductInfo']) self.tdApi.requireAuthentication = True @@ -128,7 +131,7 @@ class CtpGateway(VtGateway): except KeyError: log = VtLogData() log.gatewayName = self.gatewayName - log.logContent = u'连接配置缺少字段,请检查' + log.logContent = text.CONFIG_KEY_MISSING self.onLog(log) return @@ -244,10 +247,8 @@ class CtpMdApi(MdApi): """服务器连接""" self.connectionStatus = True - log = VtLogData() - log.gatewayName = self.gatewayName - log.logContent = u'行情服务器连接成功' - self.gateway.onLog(log) + self.writeLog(text.DATA_SERVER_CONNECTED) + self.login() #---------------------------------------------------------------------- @@ -257,10 +258,7 @@ class CtpMdApi(MdApi): self.loginStatus = False self.gateway.mdConnected = False - log = VtLogData() - log.gatewayName = self.gatewayName - log.logContent = u'行情服务器连接断开' - self.gateway.onLog(log) + self.writeLog(text.DATA_SERVER_DISCONNECTED) #---------------------------------------------------------------------- def onHeartBeatWarning(self, n): @@ -285,10 +283,7 @@ class CtpMdApi(MdApi): self.loginStatus = True self.gateway.mdConnected = True - log = VtLogData() - log.gatewayName = self.gatewayName - log.logContent = u'行情服务器登录完成' - self.gateway.onLog(log) + self.writeLog(text.DATA_SERVER_LOGIN) # 重新订阅之前订阅的合约 for subscribeReq in self.subscribedSymbols: @@ -310,10 +305,7 @@ class CtpMdApi(MdApi): self.loginStatus = False self.gateway.mdConnected = False - log = VtLogData() - log.gatewayName = self.gatewayName - log.logContent = u'行情服务器登出完成' - self.gateway.onLog(log) + self.writeLog(text.DATA_SERVER_LOGOUT) # 否则,推送错误信息 else: @@ -442,6 +434,14 @@ class CtpMdApi(MdApi): def close(self): """关闭""" self.exit() + + #---------------------------------------------------------------------- + def writeLog(self, content): + """发出日志""" + log = VtLogData() + log.gatewayName = self.gatewayName + log.logContent = content + self.gateway.onLog(log) ######################################################################## @@ -482,10 +482,7 @@ class CtpTdApi(TdApi): """服务器连接""" self.connectionStatus = True - log = VtLogData() - log.gatewayName = self.gatewayName - log.logContent = u'交易服务器连接成功' - self.gateway.onLog(log) + self.writeLog(text.TRADING_SERVER_CONNECTED) if self.requireAuthentication: self.authenticate() @@ -499,10 +496,7 @@ class CtpTdApi(TdApi): self.loginStatus = False self.gateway.tdConnected = False - log = VtLogData() - log.gatewayName = self.gatewayName - log.logContent = u'交易服务器连接断开' - self.gateway.onLog(log) + self.writeLog(text.TRADING_SERVER_DISCONNECTED) #---------------------------------------------------------------------- def onHeartBeatWarning(self, n): @@ -515,10 +509,7 @@ class CtpTdApi(TdApi): if error['ErrorID'] == 0: self.authStatus = True - log = VtLogData() - log.gatewayName = self.gatewayName - log.logContent = u'交易服务器验证成功' - self.gateway.onLog(log) + self.writeLog(text.TRADING_SERVER_AUTHENTICATED) self.login() @@ -532,10 +523,7 @@ class CtpTdApi(TdApi): self.loginStatus = True self.gateway.tdConnected = True - log = VtLogData() - log.gatewayName = self.gatewayName - log.logContent = u'交易服务器登录完成' - self.gateway.onLog(log) + self.writeLog(text.TRADING_SERVER_LOGIN) # 确认结算信息 req = {} @@ -560,10 +548,7 @@ class CtpTdApi(TdApi): self.loginStatus = False self.gateway.tdConnected = False - log = VtLogData() - log.gatewayName = self.gatewayName - log.logContent = u'交易服务器登出完成' - self.gateway.onLog(log) + self.writeLog(text.TRADING_SERVER_LOGOUT) # 否则,推送错误信息 else: @@ -635,11 +620,8 @@ class CtpTdApi(TdApi): #---------------------------------------------------------------------- def onRspSettlementInfoConfirm(self, data, error, n, last): """确认结算信息回报""" - log = VtLogData() - log.gatewayName = self.gatewayName - log.logContent = u'结算信息确认完成' - self.gateway.onLog(log) - + self.writeLog(text.SETTLEMENT_INFO_CONFIRMED) + # 查询合约代码 self.reqID += 1 self.reqQryInstrument({}, self.reqID) @@ -835,10 +817,7 @@ class CtpTdApi(TdApi): self.gateway.onContract(contract) if last: - log = VtLogData() - log.gatewayName = self.gatewayName - log.logContent = u'交易合约信息获取完成' - self.gateway.onLog(log) + self.writeLog(text.CONTRACT_DATA_RECEIVED) #---------------------------------------------------------------------- def onRspQryDepthMarketData(self, data, error, n, last): @@ -1468,6 +1447,13 @@ class CtpTdApi(TdApi): """关闭""" self.exit() + #---------------------------------------------------------------------- + def writeLog(self, content): + """发出日志""" + log = VtLogData() + log.gatewayName = self.gatewayName + log.logContent = content + self.gateway.onLog(log) #---------------------------------------------------------------------- diff --git a/vn.trader/gateway/ctpGateway/language/Chinese/__init__.py b/vn.trader/gateway/ctpGateway/language/Chinese/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/vn.trader/gateway/ctpGateway/language/Chinese/text.py b/vn.trader/gateway/ctpGateway/language/Chinese/text.py new file mode 100644 index 00000000..217fff5d --- /dev/null +++ b/vn.trader/gateway/ctpGateway/language/Chinese/text.py @@ -0,0 +1,18 @@ +# encoding: UTF-8 + +LOADING_ERROR = u'读取连接配置出错,请检查' +CONFIG_KEY_MISSING = u'连接配置缺少字段,请检查' + +DATA_SERVER_CONNECTED = u'行情服务器连接成功' +DATA_SERVER_DISCONNECTED = u'行情服务器连接断开' +DATA_SERVER_LOGIN = u'行情服务器登录完成' +DATA_SERVER_LOGOUT = u'行情服务器登出完成' + +TRADING_SERVER_CONNECTED = u'交易服务器连接成功' +TRADING_SERVER_DISCONNECTED = u'交易服务器连接断开' +TRADING_SERVER_AUTHENTICATED = u'交易服务器验证成功' +TRADING_SERVER_LOGIN = u'交易服务器登录完成' +TRADING_SERVER_LOGOUT = u'交易服务器登出完成' + +SETTLEMENT_INFO_CONFIRMED = u'结算信息确认完成' +CONTRACT_DATA_RECEIVED = u'交易合约信息获取完成' \ No newline at end of file diff --git a/vn.trader/gateway/ctpGateway/language/English/__init__.py b/vn.trader/gateway/ctpGateway/language/English/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/vn.trader/gateway/ctpGateway/language/English/text.py b/vn.trader/gateway/ctpGateway/language/English/text.py new file mode 100644 index 00000000..743167f4 --- /dev/null +++ b/vn.trader/gateway/ctpGateway/language/English/text.py @@ -0,0 +1,18 @@ +# encoding: UTF-8 + +LOADING_ERROR = u'Error occurred when loading the config file, please check.' +CONFIG_KEY_MISSING = u'Key missing in the config file, please check.' + +DATA_SERVER_CONNECTED = u'Data server connected.' +DATA_SERVER_DISCONNECTED = u'Data server disconnected' +DATA_SERVER_LOGIN = u'Data server login completed.' +DATA_SERVER_LOGOUT = u'Data server logout completed.' + +TRADING_SERVER_CONNECTED = u'Trading server connected.' +TRADING_SERVER_DISCONNECTED = u'Trading server disconnected.' +TRADING_SERVER_AUTHENTICATED = u'Trading server authenticated.' +TRADING_SERVER_LOGIN = u'Trading server login completed.' +TRADING_SERVER_LOGOUT = u'Trading server logout completed.' + +SETTLEMENT_INFO_CONFIRMED = u'Settlement info confirmed.' +CONTRACT_DATA_RECEIVED = u'Contract data received.' \ No newline at end of file diff --git a/vn.trader/gateway/ctpGateway/language/__init__.py b/vn.trader/gateway/ctpGateway/language/__init__.py new file mode 100644 index 00000000..9b79651c --- /dev/null +++ b/vn.trader/gateway/ctpGateway/language/__init__.py @@ -0,0 +1,23 @@ +# encoding: UTF-8 + +import json +import os +import traceback + +# 默认设置 +from Chinese import text + +# 获取目录上级路径 +path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')) +SETTING_FILENAME = 'VT_setting.json' +SETTING_FILENAME = os.path.join(path, SETTING_FILENAME) + +# 打开配置文件,读取语言配置 +try: + f = file(SETTING_FILENAME) + setting = json.load(f) + if setting['language'] == 'English': + from English import text + f.close() +except: + traceback.print_exc() diff --git a/vn.trader/gateway/ibGateway/ibGateway.py b/vn.trader/gateway/ibGateway/ibGateway.py index 15a8438c..6c98bbfd 100644 --- a/vn.trader/gateway/ibGateway/ibGateway.py +++ b/vn.trader/gateway/ibGateway/ibGateway.py @@ -17,10 +17,9 @@ import calendar from datetime import datetime, timedelta from copy import copy -from PyQt4 import QtGui, QtCore - from vnib import * from vtGateway import * +from language import text # 以下为一些VT类型和CTP类型的映射字典 @@ -151,7 +150,7 @@ class IbGateway(VtGateway): except IOError: log = VtLogData() log.gatewayName = self.gatewayName - log.logContent = u'读取连接配置出错,请检查' + log.logContent = text.LOADING_ERROR self.onLog(log) return @@ -165,7 +164,7 @@ class IbGateway(VtGateway): except KeyError: log = VtLogData() log.gatewayName = self.gatewayName - log.logContent = u'连接配置缺少字段,请检查' + log.logContent = text.CONFIG_KEY_MISSING self.onLog(log) return @@ -266,7 +265,7 @@ class IbGateway(VtGateway): """查询账户资金""" log = VtLogData() log.gatewayName = self.gatewayName - log.logContent = u'IB接口账户信息提供主推更新,无需查询' + log.logContent = text.NONEED_TO_QRYACCOUNT self.onLog(log) #---------------------------------------------------------------------- @@ -274,7 +273,7 @@ class IbGateway(VtGateway): """查询持仓""" log = VtLogData() log.gatewayName = self.gatewayName - log.logContent = u'IB接口持仓信息提供主推更新,无需查询' + log.logContent = text.NONEED_TO_QRYPOSITION self.onLog(log) #---------------------------------------------------------------------- @@ -320,7 +319,7 @@ class IbWrapper(IbApi): log = VtLogData() log.gatewayName = self.gatewayName - log.logContent = (u'IB接口连接成功,当前服务器时间 %s' %t) + log.logContent = text.API_CONNECTED.format(time=t) self.gateway.onLog(log) for symbol, req in self.subscribeReqDict.items(): @@ -476,7 +475,7 @@ class IbWrapper(IbApi): log = VtLogData() log.gatewayName = self.gatewayName - log.logContent = (u'IB接口连接断开') + log.logContent = text.API_DISCONNECTED self.gateway.onLog(log) #---------------------------------------------------------------------- diff --git a/vn.trader/gateway/ibGateway/language/Chinese/__init__.py b/vn.trader/gateway/ibGateway/language/Chinese/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/vn.trader/gateway/ibGateway/language/Chinese/text.py b/vn.trader/gateway/ibGateway/language/Chinese/text.py new file mode 100644 index 00000000..38a64374 --- /dev/null +++ b/vn.trader/gateway/ibGateway/language/Chinese/text.py @@ -0,0 +1,9 @@ +# encoding: UTF-8 + +LOADING_ERROR = u'读取连接配置出错,请检查' +CONFIG_KEY_MISSING = u'连接配置缺少字段,请检查' +NONEED_TO_QRYACCOUNT = u'IB接口账户信息提供主推更新,无需查询' +NONEED_TO_QRYPOSITION = u'IB接口持仓信息提供主推更新,无需查询' + +API_CONNECTED = u'IB接口连接成功,当前服务器时间{time}' +API_DISCONNECTED = u'IB接口连接断开' \ No newline at end of file diff --git a/vn.trader/gateway/ibGateway/language/English/__init__.py b/vn.trader/gateway/ibGateway/language/English/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/vn.trader/gateway/ibGateway/language/English/text.py b/vn.trader/gateway/ibGateway/language/English/text.py new file mode 100644 index 00000000..ffc76d88 --- /dev/null +++ b/vn.trader/gateway/ibGateway/language/English/text.py @@ -0,0 +1,9 @@ +# encoding: UTF-8 + +LOADING_ERROR = u'Error occurred when loading the config file, please check.' +CONFIG_KEY_MISSING = u'Key missing in the config file, please check.' +NONEED_TO_QRYACCOUNT = u'IB account data is automatically updated.' +NONEED_TO_QRYPOSITION = u'IB position data is automatically updated.' + +API_CONNECTED = u'IB gateway connected,current server time is {time}.' +API_DISCONNECTED = u'IB gateway disconnected.' \ No newline at end of file diff --git a/vn.trader/gateway/ibGateway/language/__init__.py b/vn.trader/gateway/ibGateway/language/__init__.py new file mode 100644 index 00000000..9b79651c --- /dev/null +++ b/vn.trader/gateway/ibGateway/language/__init__.py @@ -0,0 +1,23 @@ +# encoding: UTF-8 + +import json +import os +import traceback + +# 默认设置 +from Chinese import text + +# 获取目录上级路径 +path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')) +SETTING_FILENAME = 'VT_setting.json' +SETTING_FILENAME = os.path.join(path, SETTING_FILENAME) + +# 打开配置文件,读取语言配置 +try: + f = file(SETTING_FILENAME) + setting = json.load(f) + if setting['language'] == 'English': + from English import text + f.close() +except: + traceback.print_exc()