From 2f546fbf0d552df1f9f2f8c1fe277170b7cd45fe Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Tue, 26 Sep 2017 16:53:43 +0800 Subject: [PATCH] =?UTF-8?q?[Add]=E6=96=B0=E5=A2=9ETuShare=E5=8E=86?= =?UTF-8?q?=E5=8F=B2=E6=95=B0=E6=8D=AE=E6=9C=8D=E5=8A=A1=EF=BC=8C=E9=92=88?= =?UTF-8?q?=E5=AF=B9=E8=82=A1=E7=A5=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/README.md | 6 +- examples/TushareDataService/config.json | 6 ++ examples/TushareDataService/dataService.py | 95 +++++++++++++++++++++ examples/TushareDataService/downloadData.py | 11 +++ examples/TushareDataService/runService.py | 32 +++++++ 5 files changed, 148 insertions(+), 2 deletions(-) create mode 100644 examples/TushareDataService/config.json create mode 100644 examples/TushareDataService/dataService.py create mode 100644 examples/TushareDataService/downloadData.py create mode 100644 examples/TushareDataService/runService.py diff --git a/examples/README.md b/examples/README.md index 5b786215..41fa0f31 100644 --- a/examples/README.md +++ b/examples/README.md @@ -12,6 +12,8 @@ * ServerClient:服务端(业务逻辑)和客户端(GUI界面)分离的VnTrader -* ShcifcoDataService:上海中期历史行情服务 +* ShcifcoDataService:上海中期历史行情服务(期货) -* TqDataService:天勤历史行情服务 \ No newline at end of file +* TqDataService:天勤历史行情服务(期货) + +* TushareDataService:TuShare历史行情服务(股票) \ No newline at end of file diff --git a/examples/TushareDataService/config.json b/examples/TushareDataService/config.json new file mode 100644 index 00000000..2da1f0b4 --- /dev/null +++ b/examples/TushareDataService/config.json @@ -0,0 +1,6 @@ +{ + "MONGO_HOST": "localhost", + "MONGO_PORT": 27017, + + "SYMBOLS": ["510050", "510300"] +} \ No newline at end of file diff --git a/examples/TushareDataService/dataService.py b/examples/TushareDataService/dataService.py new file mode 100644 index 00000000..2e5e511b --- /dev/null +++ b/examples/TushareDataService/dataService.py @@ -0,0 +1,95 @@ +# encoding: UTF-8 + +import sys +import json +from datetime import datetime +from time import time, sleep + +from pymongo import MongoClient, ASCENDING + +from vnpy.trader.vtObject import VtBarData +from vnpy.trader.app.ctaStrategy.ctaBase import MINUTE_DB_NAME + +import tushare as ts + +# 加载配置 +config = open('config.json') +setting = json.load(config) + +MONGO_HOST = setting['MONGO_HOST'] +MONGO_PORT = setting['MONGO_PORT'] +SYMBOLS = setting['SYMBOLS'] + +mc = MongoClient(MONGO_HOST, MONGO_PORT) # Mongo连接 +db = mc[MINUTE_DB_NAME] # 数据库 + + +#---------------------------------------------------------------------- +def generateExchange(symbol): + """生成VT合约代码""" + if symbol[0:2] in ['60', '51']: + exchange = 'SSE' + elif symbol[0:2] in ['00', '15', '30']: + exchange = 'SZSE' + return exchange + +#---------------------------------------------------------------------- +def generateVtBar(row): + """生成K线""" + bar = VtBarData() + + bar.symbol = row['code'] + bar.exchange = generateExchange(bar.symbol) + bar.vtSymbol = '.'.join([bar.symbol, bar.exchange]) + bar.open = row['open'] + bar.high = row['high'] + bar.low = row['low'] + bar.close = row['close'] + bar.volume = row['vol'] + bar.datetime = row.name + bar.date = bar.datetime.strftime("%Y%m%d") + bar.time = bar.datetime.strftime("%H:%M:%S") + + return bar + +#---------------------------------------------------------------------- +def downMinuteBarBySymbol(symbol): + """下载某一合约的分钟线数据""" + start = time() + + cl = db[symbol] + cl.ensure_index([('datetime', ASCENDING)], unique=True) # 添加索引 + + df = ts.bar(symbol, ktype='1min') + df = df.sort_index() + + for ix, row in df.iterrows(): + bar = generateVtBar(row) + d = bar.__dict__ + flt = {'datetime': bar.datetime} + cl.replace_one(flt, d, True) + + end = time() + cost = (end - start) * 1000 + + print u'合约%s数据下载完成%s - %s,耗时%s毫秒' %(symbol, df.index[0], df.index[-1], cost) + + +#---------------------------------------------------------------------- +def downloadAllMinuteBar(): + """下载所有配置中的合约的分钟线数据""" + print '-' * 50 + print u'开始下载合约分钟线数据' + print '-' * 50 + + # 添加下载任务 + for symbol in SYMBOLS: + downMinuteBarBySymbol(str(symbol)) + + print '-' * 50 + print u'合约分钟线数据下载完成' + print '-' * 50 + + + + \ No newline at end of file diff --git a/examples/TushareDataService/downloadData.py b/examples/TushareDataService/downloadData.py new file mode 100644 index 00000000..115fdb31 --- /dev/null +++ b/examples/TushareDataService/downloadData.py @@ -0,0 +1,11 @@ +# encoding: UTF-8 + +""" +立即下载数据到数据库中,用于手动执行更新操作。 +""" + +from dataService import * + + +if __name__ == '__main__': + downloadAllMinuteBar() \ No newline at end of file diff --git a/examples/TushareDataService/runService.py b/examples/TushareDataService/runService.py new file mode 100644 index 00000000..4d3234bc --- /dev/null +++ b/examples/TushareDataService/runService.py @@ -0,0 +1,32 @@ +# encoding: UTF-8 + +""" +定时服务,可无人值守运行,实现每日自动下载更新历史行情数据到数据库中。 +""" + +import time +import datetime + +from dataService import downloadAllMinuteBar + + +if __name__ == '__main__': + taskCompletedDate = None + + # 生成一个随机的任务下载时间,用于避免所有用户在同一时间访问数据服务器 + taskTime = datetime.time(hour=17, minute=0) + + # 进入主循环 + while True: + t = datetime.datetime.now() + + # 每天到达任务下载时间后,执行数据下载的操作 + if t.time() > taskTime and (taskCompletedDate is None or t.date() != taskCompletedDate): + downloadAllMinuteBar() + + # 更新任务完成的日期 + taskCompletedDate = t.date() + else: + print u'当前时间%s,任务定时%s' %(t, taskTime) + + time.sleep(60) \ No newline at end of file