From 1339c819cfe5405b6cb3d5f9a64f999fb2eb5b47 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Mon, 24 Dec 2018 13:30:20 +0800 Subject: [PATCH] =?UTF-8?q?[Add]=E5=A2=9E=E5=8A=A0CccDataService=E7=9A=84?= =?UTF-8?q?=E6=97=A5=E7=BA=BF=E6=95=B0=E6=8D=AE=E4=B8=8B=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataService/CccDataService/dataService.py | 47 ++++++++++++++++++- .../CccDataService/downloadData.py | 10 ++-- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/examples/DataService/CccDataService/dataService.py b/examples/DataService/CccDataService/dataService.py index e7ffc88a..c7a0d1b0 100644 --- a/examples/DataService/CccDataService/dataService.py +++ b/examples/DataService/CccDataService/dataService.py @@ -10,7 +10,7 @@ import requests from pymongo import MongoClient, ASCENDING from vnpy.trader.vtObject import VtBarData -from vnpy.trader.app.ctaStrategy.ctaBase import MINUTE_DB_NAME +from vnpy.trader.app.ctaStrategy.ctaBase import MINUTE_DB_NAME, DAILY_DB_NAME # 加载配置 @@ -23,7 +23,7 @@ SYMBOLS = setting['SYMBOLS'] mc = MongoClient(MONGO_HOST, MONGO_PORT) # Mongo连接 db = mc[MINUTE_DB_NAME] # 数据库 - +dbDaily = mc[DAILY_DB_NAME] #---------------------------------------------------------------------- def generateVtBar(vtSymbol, d): @@ -87,6 +87,49 @@ def downloadMinuteBarBySymbol(vtSymbol, end): datetime.datetime.fromtimestamp(l[-1]['time']), cost)) +#---------------------------------------------------------------------- +def downloadDailyBarBySymbol(vtSymbol): + """下载某一合约的分钟线数据""" + startTime = time.time() + + cl = dbDaily[vtSymbol] + cl.ensure_index([('datetime', ASCENDING)], unique=True) + + symbol, exchange = vtSymbol.split('.') + fsym, tsym = symbol.split('/') + + url = 'https://min-api.cryptocompare.com/data/histoday' + params = { + 'fsym': fsym, + 'tsym': tsym, + 'e': exchange, + 'limit': 2000 + } + resp = requests.get(url, headers={}, params=params) + + if resp.status_code != 200: + print(u'%s数据下载失败' %vtSymbol) + return + + j = resp.json() + l = j['Data'] + + for d in l: + bar = generateVtBar(vtSymbol, d) + d = bar.__dict__ + flt = {'datetime': bar.datetime} + cl.replace_one(flt, d, True) + + endTime = time.time() + cost = (endTime - startTime) * 1000 + + + print(u'合约%s数据下载完成%s - %s,耗时%s毫秒' %(vtSymbol, + datetime.datetime.fromtimestamp(l[0]['time']), + datetime.datetime.fromtimestamp(l[-1]['time']), + cost)) + + #---------------------------------------------------------------------- def downloadAllMinuteBar(end): """下载所有配置中的合约的分钟线数据""" diff --git a/examples/DataService/CccDataService/downloadData.py b/examples/DataService/CccDataService/downloadData.py index 1da0bc31..7c7d9cc9 100644 --- a/examples/DataService/CccDataService/downloadData.py +++ b/examples/DataService/CccDataService/downloadData.py @@ -8,7 +8,9 @@ from dataService import * if __name__ == '__main__': - #downMinuteBarBySymbol('BTC/USDT.OKEX', '20181012') - #downMinuteBarBySymbol('BTC/USDT.HUOBIPRO', '20181012') - #downMinuteBarBySymbol('BTC/USDT.BINANCE', '20181012') - downloadAllMinuteBar('20181012') \ No newline at end of file + #downloadMinuteBarBySymbol('BTC/USDT.OKEX', '20181012') + #downloadMinuteBarBySymbol('BTC/USDT.HUOBIPRO', '20181012') + #downloadMinuteBarBySymbol('BTC/USDT.BINANCE', '20181012') + #downloadAllMinuteBar('20181012') + + downloadDailyBarBySymbol('BTC/USDT.BINANCE') \ No newline at end of file