diff --git a/vnpy/trader/app/ctaStrategy/ctaHistoryData.py b/vnpy/trader/app/ctaStrategy/ctaHistoryData.py index 7c892d29..ee5f0872 100644 --- a/vnpy/trader/app/ctaStrategy/ctaHistoryData.py +++ b/vnpy/trader/app/ctaStrategy/ctaHistoryData.py @@ -208,5 +208,41 @@ def loadTdxCsv(fileName, dbName, symbol): print bar.date, bar.time print u'插入完毕,耗时:%s' % (time()-start) +def loadOKEXCsv(fileName, dbName, symbol): + """将OKEX导出的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(open(fileName,"r")) + for d in reader: + if len(d[1]) > 10: + bar = VtBarData() + bar.vtSymbol = symbol + bar.symbol = symbol + + bar.datetime = datetime.strptime(d[1], '%Y-%m-%d %H:%M:%S') + bar.date = bar.datetime.date().strftime('%Y%m%d') + bar.time = bar.datetime.time().strftime('%H:%M:%S') + + bar.open = float(d[2]) + bar.high = float(d[3]) + bar.low = float(d[4]) + bar.close = float(d[5]) + + bar.volume = float(d[6]) + bar.tobtcvolume = float(d[7]) + + 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)