From 7f823cab9f8e643468da45551279975315ab9628 Mon Sep 17 00:00:00 2001 From: "Aaron.G" Date: Sun, 12 Feb 2017 22:47:26 +0800 Subject: [PATCH 1/2] =?UTF-8?q?tussore=E4=B8=8B=E8=BD=BD=E6=B2=AA=E6=B7=B1?= =?UTF-8?q?=E8=82=A1=E7=A5=A8=E6=97=A5=E8=A1=8C=E6=83=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加tushare下载沪深股票日行情插入MongoDB。 --- vn.trader/ctaAlgo/ctaHistoryData.py | 50 +++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/vn.trader/ctaAlgo/ctaHistoryData.py b/vn.trader/ctaAlgo/ctaHistoryData.py index d2d7b76c..aea28949 100644 --- a/vn.trader/ctaAlgo/ctaHistoryData.py +++ b/vn.trader/ctaAlgo/ctaHistoryData.py @@ -314,6 +314,55 @@ class HistoryDataEngine(object): +#---------------------------------------------------------------------- +def downloadEquityDailyBarts(self, symbol): + """ + 下载股票的日行情,symbol是股票代码 + """ + print u'开始下载%s日行情' %symbol + + # 查询数据库中已有数据的最后日期 + cl = self.dbClient[DAILY_DB_NAME][symbol] + cx = cl.find(sort=[('datetime', pymongo.DESCENDING)]) + if cx.count(): + last = cx[0] + else: + last = '' + # 开始下载数据 + import tushare as ts + + if last: + start = last['date'][:4]+'-'+last['date'][4:6]+'-'+last['date'][6:] + + data = ts.get_k_data(symbol,start) + + if not data.empty: + # 创建datetime索引 + self.dbClient[DAILY_DB_NAME][symbol].ensure_index([('datetime', pymongo.ASCENDING)], + unique=True) + + for index, d in data.iterrows(): + bar = CtaBarData() + bar.vtSymbol = symbol + bar.symbol = symbol + try: + bar.open = d.get('open') + bar.high = d.get('high') + bar.low = d.get('low') + bar.close = d.get('close') + bar.date = d.get('date').replace('-', '') + bar.time = '' + bar.datetime = datetime.strptime(bar.date, '%Y%m%d') + bar.volume = d.get('volume') + except KeyError: + print d + + flt = {'datetime': bar.datetime} + self.dbClient[DAILY_DB_NAME][symbol].update_one(flt, {'$set':bar.__dict__}, upsert=True) + + print u'%s下载完成' %symbol + else: + print u'找不到合约%s' %symbol #---------------------------------------------------------------------- def loadMcCsv(fileName, dbName, symbol): """将Multicharts导出的csv格式的历史数据插入到Mongo数据库中""" @@ -393,6 +442,7 @@ if __name__ == '__main__': #e = HistoryDataEngine() #sleep(1) #e.downloadEquityDailyBar('000001') + #e.downloadEquityDailyBarts('000001') # 这里将项目中包含的股指日内分钟线csv导入MongoDB,作者电脑耗时大约3分钟 loadMcCsv('IF0000_1min.csv', MINUTE_DB_NAME, 'IF0000') From 95a4d10b99d5843f7be1073c606085be12912304 Mon Sep 17 00:00:00 2001 From: recotra Date: Mon, 13 Feb 2017 10:17:10 +0800 Subject: [PATCH 2/2] Update vtEngine.py --- vn.trader/vtEngine.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vn.trader/vtEngine.py b/vn.trader/vtEngine.py index 103317b0..08ece81a 100644 --- a/vn.trader/vtEngine.py +++ b/vn.trader/vtEngine.py @@ -193,7 +193,7 @@ class MainEngine(object): self.writeLog(u'接口不存在:%s' %gatewayName) #---------------------------------------------------------------------- - def qryAccont(self, gatewayName): + def qryAccount(self, gatewayName): """查询特定接口的账户""" if gatewayName in self.gatewayDict: gateway = self.gatewayDict[gatewayName]