From d9401ba05540e5eb69d860c28bb7232a01939bb2 Mon Sep 17 00:00:00 2001 From: chenxy123 Date: Mon, 10 Apr 2017 21:40:18 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=BB=E5=BC=95?= =?UTF-8?q?=E6=93=8E=E5=86=85=E7=9A=84=E8=8B=B1=E6=96=87=E7=BF=BB=E8=AF=91?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E5=A4=8DctpGateway=E4=B8=ADonRspQryInvestorP?= =?UTF-8?q?osition=E7=9A=84zerodivision=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vn.trader/VT_setting.json | 2 +- vn.trader/ctaStrategy/language/__init__.py | 6 ++--- vn.trader/dataRecorder/language/__init__.py | 6 ++--- vn.trader/gateway/ctpGateway/CTP_connect.json | 6 ++--- vn.trader/gateway/ctpGateway/ctpGateway.py | 3 ++- .../gateway/ctpGateway/language/__init__.py | 8 +++---- .../gateway/ibGateway/language/__init__.py | 8 +++---- vn.trader/language/__init__.py | 6 ++--- vn.trader/language/chinese/text.py | 9 ++++++- vn.trader/language/english/text.py | 9 ++++++- vn.trader/riskManager/language/__init__.py | 6 ++--- vn.trader/vtEngine.py | 24 ++++++++++--------- 12 files changed, 55 insertions(+), 38 deletions(-) diff --git a/vn.trader/VT_setting.json b/vn.trader/VT_setting.json index 5f15b65d..776c8d36 100644 --- a/vn.trader/VT_setting.json +++ b/vn.trader/VT_setting.json @@ -7,5 +7,5 @@ "mongoLogging": true, "darkStyle": true, - "language": "English" + "language": "english" } \ No newline at end of file diff --git a/vn.trader/ctaStrategy/language/__init__.py b/vn.trader/ctaStrategy/language/__init__.py index 9b79651c..e42bd064 100644 --- a/vn.trader/ctaStrategy/language/__init__.py +++ b/vn.trader/ctaStrategy/language/__init__.py @@ -5,7 +5,7 @@ import os import traceback # 默认设置 -from Chinese import text +from chinese import text # 获取目录上级路径 path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')) @@ -16,8 +16,8 @@ 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 + if setting['language'] == 'english': + from english import text f.close() except: traceback.print_exc() diff --git a/vn.trader/dataRecorder/language/__init__.py b/vn.trader/dataRecorder/language/__init__.py index 9b79651c..e42bd064 100644 --- a/vn.trader/dataRecorder/language/__init__.py +++ b/vn.trader/dataRecorder/language/__init__.py @@ -5,7 +5,7 @@ import os import traceback # 默认设置 -from Chinese import text +from chinese import text # 获取目录上级路径 path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')) @@ -16,8 +16,8 @@ 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 + if setting['language'] == 'english': + from english import text f.close() except: traceback.print_exc() diff --git a/vn.trader/gateway/ctpGateway/CTP_connect.json b/vn.trader/gateway/ctpGateway/CTP_connect.json index fe1ffded..0f3215d3 100644 --- a/vn.trader/gateway/ctpGateway/CTP_connect.json +++ b/vn.trader/gateway/ctpGateway/CTP_connect.json @@ -1,7 +1,7 @@ { "brokerID": "9999", "tdAddress": "tcp://180.168.146.187:10000", - "password": "simnow申请", - "mdAddress": "tcp://180.168.212.228:41213", - "userID": "simnow申请" + "password": "19890624", + "mdAddress": "tcp://180.168.146.187:10010", + "userID": "000300" } \ No newline at end of file diff --git a/vn.trader/gateway/ctpGateway/ctpGateway.py b/vn.trader/gateway/ctpGateway/ctpGateway.py index 0d38e711..24d620b6 100644 --- a/vn.trader/gateway/ctpGateway/ctpGateway.py +++ b/vn.trader/gateway/ctpGateway/ctpGateway.py @@ -710,7 +710,8 @@ class CtpTdApi(TdApi): pos.positionProfit += data['PositionProfit'] # 计算持仓均价 - pos.price = (cost + data['PositionCost']) / pos.position + if pos.position: + pos.price = (cost + data['PositionCost']) / pos.position # 读取冻结 if pos.direction is DIRECTION_LONG: diff --git a/vn.trader/gateway/ctpGateway/language/__init__.py b/vn.trader/gateway/ctpGateway/language/__init__.py index 9b79651c..8325ae0c 100644 --- a/vn.trader/gateway/ctpGateway/language/__init__.py +++ b/vn.trader/gateway/ctpGateway/language/__init__.py @@ -5,10 +5,10 @@ import os import traceback # 默认设置 -from Chinese import text +from chinese import text # 获取目录上级路径 -path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')) +path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..')) SETTING_FILENAME = 'VT_setting.json' SETTING_FILENAME = os.path.join(path, SETTING_FILENAME) @@ -16,8 +16,8 @@ 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 + if setting['language'] == 'english': + from english import text f.close() except: traceback.print_exc() diff --git a/vn.trader/gateway/ibGateway/language/__init__.py b/vn.trader/gateway/ibGateway/language/__init__.py index 9b79651c..8325ae0c 100644 --- a/vn.trader/gateway/ibGateway/language/__init__.py +++ b/vn.trader/gateway/ibGateway/language/__init__.py @@ -5,10 +5,10 @@ import os import traceback # 默认设置 -from Chinese import text +from chinese import text # 获取目录上级路径 -path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')) +path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..')) SETTING_FILENAME = 'VT_setting.json' SETTING_FILENAME = os.path.join(path, SETTING_FILENAME) @@ -16,8 +16,8 @@ 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 + if setting['language'] == 'english': + from english import text f.close() except: traceback.print_exc() diff --git a/vn.trader/language/__init__.py b/vn.trader/language/__init__.py index 7c23b182..696e9d4e 100644 --- a/vn.trader/language/__init__.py +++ b/vn.trader/language/__init__.py @@ -4,7 +4,7 @@ import json import os # 默认设置 -from Chinese import text, constant +from chinese import text, constant # 获取目录上级路径 path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) @@ -15,8 +15,8 @@ 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, constant + if setting['language'] == 'english': + from english import text, constant f.close() except: pass diff --git a/vn.trader/language/chinese/text.py b/vn.trader/language/chinese/text.py index 995b3c72..dfad30fb 100644 --- a/vn.trader/language/chinese/text.py +++ b/vn.trader/language/chinese/text.py @@ -123,4 +123,11 @@ TEST = u'测试' CONNECT = u'连接' CPU_MEMORY_INFO = u'CPU使用率:{cpu}% 内存使用率:{memory}%' -CONFIRM_EXIT = u'确认退出?' \ No newline at end of file +CONFIRM_EXIT = u'确认退出?' + +GATEWAY_NOT_EXIST = u'接口不存在:{gateway}' +DATABASE_CONNECTING_COMPLETED = u'MongoDB连接成功' +DATABASE_CONNECTING_FAILED = u'MongoDB连接失败' +DATA_INSERT_FAILED = u'数据插入失败,MongoDB没有连接' +DATA_QUERY_FAILED = u'数据查询失败,MongoDB没有连接' +DATA_UPDATE_FAILED = u'数据更新失败,MongoDB没有连接' diff --git a/vn.trader/language/english/text.py b/vn.trader/language/english/text.py index d68cc67d..daaf8ac9 100644 --- a/vn.trader/language/english/text.py +++ b/vn.trader/language/english/text.py @@ -123,4 +123,11 @@ TEST = u'Test' CONNECT = u'Connect ' CPU_MEMORY_INFO = u'CPU Usage:{cpu}% Memory Usage:{memory}%' -CONFIRM_EXIT = u'Confirm Exit?' \ No newline at end of file +CONFIRM_EXIT = u'Confirm Exit?' + +GATEWAY_NOT_EXIST = u"Can't find the gateway:{gateway}" +DATABASE_CONNECTING_COMPLETED = u'MongoDB is connected.' +DATABASE_CONNECTING_FAILED = u'Failed to connect to MongoDB.' +DATA_INSERT_FAILED = u'Data insert failed,please connect MongoDB first.' +DATA_QUERY_FAILED = u'Data query failed, please connect MongoDB first.' +DATA_UPDATE_FAILED = u'Data update failed, please connect MongoDB first.' \ No newline at end of file diff --git a/vn.trader/riskManager/language/__init__.py b/vn.trader/riskManager/language/__init__.py index 9b79651c..e42bd064 100644 --- a/vn.trader/riskManager/language/__init__.py +++ b/vn.trader/riskManager/language/__init__.py @@ -5,7 +5,7 @@ import os import traceback # 默认设置 -from Chinese import text +from chinese import text # 获取目录上级路径 path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')) @@ -16,8 +16,8 @@ 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 + if setting['language'] == 'english': + from english import text f.close() except: traceback.print_exc() diff --git a/vn.trader/vtEngine.py b/vn.trader/vtEngine.py index c4c6864a..ec9c796a 100644 --- a/vn.trader/vtEngine.py +++ b/vn.trader/vtEngine.py @@ -10,6 +10,7 @@ from pymongo.errors import ConnectionFailure from eventEngine import * from vtGateway import * from vtFunction import loadMongoSetting +from language import text from gateway import GATEWAY_DICT from ctaStrategy.ctaEngine import CtaEngine @@ -75,7 +76,7 @@ class MainEngine(object): # 接口连接后自动执行数据库连接的任务 self.dbConnect() else: - self.writeLog(u'接口不存在:%s' %gatewayName) + self.writeLog(text.GATEWAY_NOT_EXIST.format(gateway=gatewayName)) #---------------------------------------------------------------------- def subscribe(self, subscribeReq, gatewayName): @@ -84,7 +85,7 @@ class MainEngine(object): gateway = self.gatewayDict[gatewayName] gateway.subscribe(subscribeReq) else: - self.writeLog(u'接口不存在:%s' %gatewayName) + self.writeLog(text.GATEWAY_NOT_EXIST.format(gateway=gatewayName)) #---------------------------------------------------------------------- def sendOrder(self, orderReq, gatewayName): @@ -97,7 +98,7 @@ class MainEngine(object): gateway = self.gatewayDict[gatewayName] return gateway.sendOrder(orderReq) else: - self.writeLog(u'接口不存在:%s' %gatewayName) + self.writeLog(text.GATEWAY_NOT_EXIST.format(gateway=gatewayName)) #---------------------------------------------------------------------- def cancelOrder(self, cancelOrderReq, gatewayName): @@ -106,7 +107,8 @@ class MainEngine(object): gateway = self.gatewayDict[gatewayName] gateway.cancelOrder(cancelOrderReq) else: - self.writeLog(u'接口不存在:%s' %gatewayName) + self.writeLog(text.GATEWAY_NOT_EXIST.format(gateway=gatewayName)) + #---------------------------------------------------------------------- def qryAccount(self, gatewayName): @@ -115,7 +117,7 @@ class MainEngine(object): gateway = self.gatewayDict[gatewayName] gateway.qryAccount() else: - self.writeLog(u'接口不存在:%s' %gatewayName) + self.writeLog(text.GATEWAY_NOT_EXIST.format(gateway=gatewayName)) #---------------------------------------------------------------------- def qryPosition(self, gatewayName): @@ -124,7 +126,7 @@ class MainEngine(object): gateway = self.gatewayDict[gatewayName] gateway.qryPosition() else: - self.writeLog(u'接口不存在:%s' %gatewayName) + self.writeLog(text.GATEWAY_NOT_EXIST.format(gateway=gatewayName)) #---------------------------------------------------------------------- def exit(self): @@ -165,14 +167,14 @@ class MainEngine(object): # 调用server_info查询服务器状态,防止服务器异常并未连接成功 self.dbClient.server_info() - self.writeLog(u'MongoDB连接成功') + self.writeLog(text.DATABASE_CONNECTING_COMPLETED) # 如果启动日志记录,则注册日志事件监听函数 if logging: self.eventEngine.register(EVENT_LOG, self.dbLogging) except ConnectionFailure: - self.writeLog(u'MongoDB连接失败') + self.writeLog(text.DATABASE_CONNECTING_FAILED) #---------------------------------------------------------------------- def dbInsert(self, dbName, collectionName, d): @@ -182,7 +184,7 @@ class MainEngine(object): collection = db[collectionName] collection.insert_one(d) else: - self.writeLog(u'数据插入失败,MongoDB没有连接') + self.writeLog(text.DATA_INSERT_FAILED) #---------------------------------------------------------------------- def dbQuery(self, dbName, collectionName, d): @@ -196,7 +198,7 @@ class MainEngine(object): else: return [] else: - self.writeLog(u'数据查询失败,MongoDB没有连接') + self.writeLog(text.DATA_QUERY_FAILED) return [] #---------------------------------------------------------------------- @@ -207,7 +209,7 @@ class MainEngine(object): collection = db[collectionName] collection.replace_one(flt, d, upsert) else: - self.writeLog(u'数据更新失败,MongoDB没有连接') + self.writeLog(text.DATA_UPDATE_FAILED) #---------------------------------------------------------------------- def dbLogging(self, event): From 126431c7b0cd4f9fa7fcf205325b5b92eae7e1c3 Mon Sep 17 00:00:00 2001 From: cooooo Date: Mon, 10 Apr 2017 23:12:39 +0800 Subject: [PATCH 2/3] typo --- vn.trader/language/english/text.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/vn.trader/language/english/text.py b/vn.trader/language/english/text.py index daaf8ac9..3864191d 100644 --- a/vn.trader/language/english/text.py +++ b/vn.trader/language/english/text.py @@ -87,11 +87,11 @@ ASK_PRICE_3 = u'Ask Price 3' ASK_PRICE_4 = u'Ask Price 4' ASK_PRICE_5 = u'Ask Price 5' -BID_VOLUME_1 = u'Bid Price 1' -BID_VOLUME_2 = u'Bid Price 2' -BID_VOLUME_3 = u'Bid Price 3' -BID_VOLUME_4 = u'Bid Price 4' -BID_VOLUME_5 = u'Bid Price 5' +BID_VOLUME_1 = u'Bid Volume 1' +BID_VOLUME_2 = u'Bid Volume 2' +BID_VOLUME_3 = u'Bid Volume 3' +BID_VOLUME_4 = u'Bid Volume 4' +BID_VOLUME_5 = u'Bid Volume 5' ASK_VOLUME_1 = u'Ask Volume 1' ASK_VOLUME_2 = u'Ask Volume 2' ASK_VOLUME_3 = u'Ask Volume 3' From adb146ac2001be09586ca15ca8cfe1e452f119a8 Mon Sep 17 00:00:00 2001 From: cooooo Date: Wed, 12 Apr 2017 16:49:14 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AF=BC=E5=85=A5TradeBl?= =?UTF-8?q?azer=E5=AF=BC=E5=87=BA=E5=88=B0csv=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 5 +-- vn.trader/ctaStrategy/ctaHistoryData.py | 46 +++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 45f47ea9..54c74033 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -# C++源代码 +# C++源代码 ipch/ Release/ @@ -49,4 +49,5 @@ vn.lts/build/* vn.trader/ctaAlgo/data/* vn.trader/build/* -vn.trader/dist/* \ No newline at end of file +vn.trader/dist/* +*.bak diff --git a/vn.trader/ctaStrategy/ctaHistoryData.py b/vn.trader/ctaStrategy/ctaHistoryData.py index b6fb211e..0ddc0d7a 100644 --- a/vn.trader/ctaStrategy/ctaHistoryData.py +++ b/vn.trader/ctaStrategy/ctaHistoryData.py @@ -436,6 +436,52 @@ def loadTdxCsv(fileName, dbName, symbol): print u'插入完毕,耗时:%s' % (time()-start) +#---------------------------------------------------------------------- +def loadTBCsv(fileName, dbName, symbol): + """将TradeBlazer导出的csv格式的历史数据插入到Mongo数据库中 + 数据样本: + //时间,开盘价,最高价,最低价,收盘价,成交量,持仓量 + 2017/04/05 09:00,3200,3240,3173,3187,312690,2453850 + """ + import csv + + start = time() + print u'开始读取CSV文件%s中的数据插入到%s的%s中' %(fileName, dbName, symbol) + + # 锁定集合,并创建索引 + host, port, logging = loadMongoSetting() + + client = pymongo.MongoClient(host, port) + collection = client[dbName][symbol] + collection.ensure_index([('datetime', pymongo.ASCENDING)], unique=True) + + # 读取数据和插入到数据库 + reader = csv.reader(file(fileName, 'r')) + for d in reader: + if len(d[0]) > 10: + bar = CtaBarData() + bar.vtSymbol = symbol + bar.symbol = symbol + + bar.datetime = datetime.strptime(d[0], '%Y/%m/%d %H:%M') + bar.date = bar.datetime.date().strftime('%Y%m%d') + bar.time = bar.datetime.time().strftime('%H:%M:%S') + + bar.open = float(d[1]) + bar.high = float(d[2]) + bar.low = float(d[3]) + bar.close = float(d[4]) + + bar.volume = float(d[5]) + bar.openInterest = float(d[6]) + + flt = {'datetime': bar.datetime} + collection.update_one(flt, {'$set':bar.__dict__}, upsert=True) + print '%s \t %s' % (bar.date, bar.time) + + print u'插入完毕,耗时:%s' % (time()-start) + + if __name__ == '__main__': ## 简单的测试脚本可以写在这里 #from time import sleep