From 29ac15b396f94e1d02664877ff4db49f31bc6787 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Tue, 24 Oct 2017 08:02:40 +0800 Subject: [PATCH] =?UTF-8?q?[Add]=E6=96=B0=E5=A2=9E=E5=AF=8C=E9=80=94?= =?UTF-8?q?=E8=AF=81=E5=88=B8=E5=8E=86=E5=8F=B2=E6=95=B0=E6=8D=AE=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=EF=BC=88=E6=B8=AF=E8=82=A1=E3=80=81=E7=BE=8E=E8=82=A1?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/FutuDataService/config.json | 6 ++ examples/FutuDataService/dataService.py | 94 +++++++++++++++++++ examples/FutuDataService/downloadData.py | 11 +++ examples/FutuDataService/runService.py | 32 +++++++ .../trader/gateway/futuGateway/futuGateway.py | 4 +- 5 files changed, 145 insertions(+), 2 deletions(-) create mode 100644 examples/FutuDataService/config.json create mode 100644 examples/FutuDataService/dataService.py create mode 100644 examples/FutuDataService/downloadData.py create mode 100644 examples/FutuDataService/runService.py diff --git a/examples/FutuDataService/config.json b/examples/FutuDataService/config.json new file mode 100644 index 00000000..004da532 --- /dev/null +++ b/examples/FutuDataService/config.json @@ -0,0 +1,6 @@ +{ + "MONGO_HOST": "localhost", + "MONGO_PORT": 27017, + + "SYMBOLS": ["HK.00001", "HK.00002"] +} \ No newline at end of file diff --git a/examples/FutuDataService/dataService.py b/examples/FutuDataService/dataService.py new file mode 100644 index 00000000..71c6cb40 --- /dev/null +++ b/examples/FutuDataService/dataService.py @@ -0,0 +1,94 @@ +# encoding: UTF-8 + +import sys +import json +from datetime import datetime, timedelta +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 futuquant as ft + +# 加载配置 +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] # 数据库 +quote = ft.OpenQuoteContext() # 富途行情接口 +today = datetime.now().date() +startDate = (today - timedelta(10)).strftime('%Y-%m-%d') # 数据下载起始日期 + + +#---------------------------------------------------------------------- +def generateVtBar(row): + """生成K线""" + bar = VtBarData() + + bar.symbol = row['code'] + bar.exchange = '' + bar.vtSymbol = bar.symbol + bar.open = row['open'] + bar.high = row['high'] + bar.low = row['low'] + bar.close = row['close'] + bar.volume = row['volume'] + bar.datetime = datetime.strptime(row['time_key'], '%Y-%m-%d %H:%M:%S') + 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) # 添加索引 + + code, data = quote.get_history_kline(symbol, start=startDate, ktype='K_1M') + if code: + print u'合约%s数据下载失败:%s' %(symbol, data) + return + + data = data.sort_index() + + for ix, row in data.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, data.iloc[0]['time_key'], + data.iloc[-1]['time_key'], 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/FutuDataService/downloadData.py b/examples/FutuDataService/downloadData.py new file mode 100644 index 00000000..115fdb31 --- /dev/null +++ b/examples/FutuDataService/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/FutuDataService/runService.py b/examples/FutuDataService/runService.py new file mode 100644 index 00000000..aba424b2 --- /dev/null +++ b/examples/FutuDataService/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 diff --git a/vnpy/trader/gateway/futuGateway/futuGateway.py b/vnpy/trader/gateway/futuGateway/futuGateway.py index ab672443..86318aab 100644 --- a/vnpy/trader/gateway/futuGateway/futuGateway.py +++ b/vnpy/trader/gateway/futuGateway/futuGateway.py @@ -354,9 +354,9 @@ class FutuGateway(VtGateway): def close(self): """关闭""" if self.quoteCtx: - self.quoteCtx.stop() + self.quoteCtx.close() if self.tradeCtx: - self.tradeCtx.stop() + self.tradeCtx.close() #---------------------------------------------------------------------- def initQuery(self):