From 2189cdef62e67b04c3146832177e5909fe6256b5 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Fri, 7 Jul 2017 17:59:31 +0800 Subject: [PATCH] =?UTF-8?q?Examples=E4=B8=AD=E6=96=B0=E5=A2=9E=E4=B8=8A?= =?UTF-8?q?=E6=B5=B7=E4=B8=AD=E6=9C=9F=E5=8E=86=E5=8F=B2=E8=A1=8C=E6=83=85?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/CtaBacktesting/backtesting.ipynb | 14 ++-- examples/ShcifcoDataService/config.json | 11 +++ examples/ShcifcoDataService/dataService.py | 91 +++++++++++++++++++++ examples/ShcifcoDataService/downloadData.py | 11 +++ examples/ShcifcoDataService/runService.py | 30 +++++++ vnpy/data/shcifco/test.py | 8 +- 6 files changed, 154 insertions(+), 11 deletions(-) create mode 100644 examples/ShcifcoDataService/config.json create mode 100644 examples/ShcifcoDataService/dataService.py create mode 100644 examples/ShcifcoDataService/downloadData.py create mode 100644 examples/ShcifcoDataService/runService.py diff --git a/examples/CtaBacktesting/backtesting.ipynb b/examples/CtaBacktesting/backtesting.ipynb index 1de18e1d..0540c100 100644 --- a/examples/CtaBacktesting/backtesting.ipynb +++ b/examples/CtaBacktesting/backtesting.ipynb @@ -79,13 +79,13 @@ "name": "stdout", "output_type": "stream", "text": [ - "2017-06-02 16:07:31.265000\t开始载入数据\n", - "2017-06-02 16:07:31.423000\t载入完成,数据量:331890\n", - "2017-06-02 16:07:31.423000\t开始回测\n", - "2017-06-02 16:07:31.441000\t策略初始化完成\n", - "2017-06-02 16:07:31.441000\t策略启动完成\n", - "2017-06-02 16:07:31.442000\t开始回放数据\n", - "2017-06-02 16:07:54.738000\t数据回放结束\n" + "2017-07-06 23:15:17.471000\t开始载入数据\n", + "2017-07-06 23:15:17.485000\t载入完成,数据量:0\n", + "2017-07-06 23:15:17.485000\t开始回测\n", + "2017-07-06 23:15:17.485000\t策略初始化完成\n", + "2017-07-06 23:15:17.485000\t策略启动完成\n", + "2017-07-06 23:15:17.485000\t开始回放数据\n", + "2017-07-06 23:15:17.486000\t数据回放结束\n" ] } ], diff --git a/examples/ShcifcoDataService/config.json b/examples/ShcifcoDataService/config.json new file mode 100644 index 00000000..5d7715c9 --- /dev/null +++ b/examples/ShcifcoDataService/config.json @@ -0,0 +1,11 @@ +{ + "MONGO_HOST": "localhost", + "MONGO_PORT": 27017, + + "SHCIFCO_IP": "180.169.126.123", + "SHCIFCO_PORT": "45065", + "SHCIFCO_TOKEN": "请联系上海中期申请", + + "SYMBOLS": ["cu1707", "cu1708", "cu1709", "cu1712", + "m1707", "m1708", "m1709", "m1712"] +} \ No newline at end of file diff --git a/examples/ShcifcoDataService/dataService.py b/examples/ShcifcoDataService/dataService.py new file mode 100644 index 00000000..270cbde9 --- /dev/null +++ b/examples/ShcifcoDataService/dataService.py @@ -0,0 +1,91 @@ +# encoding: UTF-8 + +import json +import time +import datetime +import random + +from pymongo import MongoClient, ASCENDING + +from vnpy.data.shcifco.vnshcifco import ShcifcoApi, PERIOD_1MIN +from vnpy.trader.vtObject import VtBarData +from vnpy.trader.app.ctaStrategy.ctaBase import MINUTE_DB_NAME + + +# 加载配置 +config = open('config.json') +setting = json.load(config) + +MONGO_HOST = setting['MONGO_HOST'] +MONGO_PORT = setting['MONGO_PORT'] +SHCIFCO_IP = setting['SHCIFCO_IP'] +SHCIFCO_PORT = setting['SHCIFCO_PORT'] +SHCIFCO_TOKEN = setting['SHCIFCO_TOKEN'] +SYMBOLS = setting['SYMBOLS'] + +api = ShcifcoApi(SHCIFCO_IP, SHCIFCO_PORT, SHCIFCO_TOKEN) # 历史行情服务API对象 +mc = MongoClient(MONGO_HOST, MONGO_PORT) # Mongo连接 +db = mc[MINUTE_DB_NAME] # 数据库 + + +#---------------------------------------------------------------------- +def generateVtBar(d): + """生成K线""" + bar = VtBarData() + + bar.symbol = d['symbol'] + bar.vtSymbol = d['symbol'] + bar.date = d['date'] + bar.time = ':'.join([d['time'][:2], d['time'][2:]]) + bar.open = d['open'] + bar.high = d['high'] + bar.low = d['low'] + bar.close = d['close'] + bar.volume = d['volume'] + bar.openInterest = d['openInterest'] + bar.datetime = datetime.datetime.strptime(' '.join([bar.date, bar.time]), '%Y%m%d %H:%M') + + return bar + +#---------------------------------------------------------------------- +def downMinuteBarBySymbol(symbol, num): + """下载某一合约的分钟线数据""" + start = time.time() + + cl = db[symbol] # 集合 + cl.ensure_index([('datetime', ASCENDING)], unique=True) # 添加索引 + + l = api.getHisBar(symbol, num, period=PERIOD_1MIN) + if not l: + print u'%s数据下载失败' %symbol + return + + for d in l: + bar = generateVtBar(d) + d = bar.__dict__ + flt = {'datetime': bar.datetime} + cl.replace_one(flt, d, True) + + end = time.time() + cost = (end - start) * 1000 + + print u'合约%s数据下载完成%s - %s,耗时%s毫秒' %(symbol, generateVtBar(l[0]).datetime, + generateVtBar(l[-1]).datetime, cost) + +#---------------------------------------------------------------------- +def downloadAllMinuteBar(num): + """下载所有配置中的合约的分钟线数据""" + print '-' * 50 + print u'开始下载合约分钟线数据' + print '-' * 50 + + for symbol in SYMBOLS: + downMinuteBarBySymbol(symbol, num) + time.sleep(1) + + print '-' * 50 + print u'合约分钟线数据下载完成' + print '-' * 50 + + + \ No newline at end of file diff --git a/examples/ShcifcoDataService/downloadData.py b/examples/ShcifcoDataService/downloadData.py new file mode 100644 index 00000000..3cf6f3cc --- /dev/null +++ b/examples/ShcifcoDataService/downloadData.py @@ -0,0 +1,11 @@ +# encoding: UTF-8 + +""" +立即下载数据到数据库中,用于手动执行更新操作。 +""" + +from dataService import * + + +if __name__ == '__main__': + downloadAllMinuteBar(1000) \ No newline at end of file diff --git a/examples/ShcifcoDataService/runService.py b/examples/ShcifcoDataService/runService.py new file mode 100644 index 00000000..2b5ac936 --- /dev/null +++ b/examples/ShcifcoDataService/runService.py @@ -0,0 +1,30 @@ +# encoding: UTF-8 + +""" +定时服务,可无人值守运行,实现每日自动下载更新历史行情数据到数据库中。 +""" + +from dataService import * + + +if __name__ == '__main__': + taskCompletedDate = None + + # 生成一个随机的任务下载时间,用于避免所有用户在同一时间访问数据服务器 + taskTime = datetime.time(hour=17, minute=random.randint(1,59)) + + # 进入主循环 + while True: + t = datetime.datetime.now() + + # 每天到达任务下载时间后,执行数据下载的操作 + if t.time() > taskTime and (taskCompletedDate is None or t.date() != taskCompletedDate): + # 下载1000根分钟线数据,足以覆盖过去两天的行情 + downloadAllMinuteBar(1000) + + # 更新任务完成的日期 + taskCompletedDate = t.date() + else: + print u'当前时间%s,任务定时%s' %(t, taskTime) + + time.sleep(60) \ No newline at end of file diff --git a/vnpy/data/shcifco/test.py b/vnpy/data/shcifco/test.py index 15d8e192..eb2f88d8 100644 --- a/vnpy/data/shcifco/test.py +++ b/vnpy/data/shcifco/test.py @@ -4,9 +4,9 @@ from vnshcifco import ShcifcoApi, PERIOD_1MIN if __name__ == "__main__": - ip = '101.231.179.199' - port = '10102' - token = 'testd2cda34b2d317779e812eb84ee4224a6_qpweqf1' + ip = '180.169.126.123' + port = '45065' + token = '请联系上海中期申请' symbol = 'cu1709' # 创建API对象 @@ -22,6 +22,6 @@ if __name__ == "__main__": print api.getLastBar(symbol) # 获取历史分钟线 - print api.getHisBar(symbol, 502, period=PERIOD_1MIN) + print api.getHisBar(symbol, 500, period=PERIOD_1MIN) \ No newline at end of file