Examples中新增上海中期历史行情下载服务

This commit is contained in:
vn.py 2017-07-07 17:59:31 +08:00
parent 62b2565d7f
commit 2189cdef62
6 changed files with 154 additions and 11 deletions

View File

@ -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"
]
}
],

View File

@ -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"]
}

View File

@ -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

View File

@ -0,0 +1,11 @@
# encoding: UTF-8
"""
立即下载数据到数据库中用于手动执行更新操作
"""
from dataService import *
if __name__ == '__main__':
downloadAllMinuteBar(1000)

View File

@ -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)

View File

@ -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)