From e4b9b25fc05fb501f36fc7ace4bc6273fe392a6b Mon Sep 17 00:00:00 2001 From: Hunggin <516672315@qq.com> Date: Tue, 12 Sep 2017 23:02:15 +0800 Subject: [PATCH] loadTbCsv Add a function to load data from csv file from TB --- vnpy/trader/app/ctaStrategy/ctaHistoryData.py | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/vnpy/trader/app/ctaStrategy/ctaHistoryData.py b/vnpy/trader/app/ctaStrategy/ctaHistoryData.py index 08c25b47..dbe0394a 100644 --- a/vnpy/trader/app/ctaStrategy/ctaHistoryData.py +++ b/vnpy/trader/app/ctaStrategy/ctaHistoryData.py @@ -17,7 +17,7 @@ from vnpy.data.datayes import DatayesApi from vnpy.trader.vtGlobal import globalSetting from vnpy.trader.vtConstant import * from vnpy.trader.vtObject import VtBarData -from .ctaBase import SETTING_DB_NAME, TICK_DB_NAME, MINUTE_DB_NAME, DAILY_DB_NAME +from .ctaBase import SETTING_DB_NAME, TICK_DB_NAME, MINUTE_DB_NAME # 以下为vn.trader和通联数据规定的交易所代码映射 @@ -396,6 +396,41 @@ def loadMcCsv(fileName, dbName, symbol): print u'插入完毕,耗时:%s' % (time()-start) +#---------------------------------------------------------------------- +def loadTbCsv(fileName, dbName, symbol): + """将TradeBlazer导出的csv格式的历史分钟数据插入到Mongo数据库中""" + import csv + + start = time() + print u'开始读取CSV文件%s中的数据插入到%s的%s中' %(fileName, dbName, symbol) + + # 锁定集合,并创建索引 + client = pymongo.MongoClient(globalSetting['mongoHost'], globalSetting['mongoPort']) + collection = client[dbName][symbol] + collection.ensure_index([('datetime', pymongo.ASCENDING)], unique=True) + + # 读取数据和插入到数据库 + reader = csv.reader(file(fileName, 'r')) + for d in reader: + bar = VtBarData() + bar.vtSymbol = symbol + bar.symbol = symbol + bar.open = float(d[1]) + bar.high = float(d[2]) + bar.low = float(d[3]) + bar.close = float(d[4]) + bar.date = datetime.strptime(d[0].split(' ')[0], '%Y/%m/%d').strftime('%Y%m%d') + bar.time = d[0].split(' ')[1]+":00" + bar.datetime = datetime.strptime(bar.date + ' ' + bar.time, '%Y%m%d %H:%M:%S') + bar.volume = d[5] + bar.openInterest = d[6] + + flt = {'datetime': bar.datetime} + collection.update_one(flt, {'$set':bar.__dict__}, upsert=True) + print bar.date, bar.time + + print u'插入完毕,耗时:%s' % (time()-start) + #---------------------------------------------------------------------- def loadTdxCsv(fileName, dbName, symbol): """将通达信导出的csv格式的历史分钟数据插入到Mongo数据库中"""