From 67227beaa9d0f882717f913ea311e136e4978420 Mon Sep 17 00:00:00 2001 From: xldistance <819873074@qq.com> Date: Sat, 3 Feb 2018 09:05:27 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=B0=86OKEX=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E7=9A=84=E6=95=B0=E6=8D=AE=E5=AF=BC=E5=85=A5=E5=88=B0?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnpy/trader/app/ctaStrategy/ctaHistoryData.py | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/vnpy/trader/app/ctaStrategy/ctaHistoryData.py b/vnpy/trader/app/ctaStrategy/ctaHistoryData.py index 7c892d29..4ec8b159 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') + 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) From c016de4f6f8ef3c0c888f4bf97ae057866450658 Mon Sep 17 00:00:00 2001 From: xldistance <819873074@qq.com> Date: Sun, 4 Feb 2018 10:28:04 +0800 Subject: [PATCH 2/2] Update ctaHistoryData.py --- vnpy/trader/app/ctaStrategy/ctaHistoryData.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vnpy/trader/app/ctaStrategy/ctaHistoryData.py b/vnpy/trader/app/ctaStrategy/ctaHistoryData.py index 4ec8b159..ee5f0872 100644 --- a/vnpy/trader/app/ctaStrategy/ctaHistoryData.py +++ b/vnpy/trader/app/ctaStrategy/ctaHistoryData.py @@ -228,7 +228,7 @@ def loadOKEXCsv(fileName, dbName, symbol): bar.vtSymbol = symbol bar.symbol = symbol - bar.datetime = datetime.strptime(d[1], '%Y-%m-%d %H:%M') + 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')