[Del]移除天勤数据服务
This commit is contained in:
parent
97fcd7f19a
commit
243c0b1ac5
@ -1,3 +0,0 @@
|
||||
# 天勤历史行情服务
|
||||
|
||||
请在[www.tq18.cn](www.tq18.cn)下载天勤行情终端,安装运行后,即可使用该服务。
|
@ -1,4 +0,0 @@
|
||||
{
|
||||
"MONGO_HOST": "localhost",
|
||||
"MONGO_PORT": 27017
|
||||
}
|
@ -1,107 +0,0 @@
|
||||
# encoding: UTF-8
|
||||
|
||||
from __future__ import print_function
|
||||
import sys
|
||||
import json
|
||||
from datetime import datetime
|
||||
from time import sleep
|
||||
|
||||
from pymongo import MongoClient, ASCENDING
|
||||
|
||||
from vnpy.data.tq.vntq import TqApi
|
||||
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']
|
||||
|
||||
mc = MongoClient(MONGO_HOST, MONGO_PORT) # Mongo连接
|
||||
db = mc[MINUTE_DB_NAME] # 数据库
|
||||
|
||||
api = TqApi() # 历史行情服务API对象
|
||||
api.connect() # 连接
|
||||
taskList = [] # 下载任务列表
|
||||
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def generateVtBar(symbol, d):
|
||||
"""生成K线"""
|
||||
bar = VtBarData()
|
||||
|
||||
bar.symbol = symbol
|
||||
bar.vtSymbol = symbol
|
||||
bar.open = d['open']
|
||||
bar.high = d['high']
|
||||
bar.low = d['low']
|
||||
bar.close = d['close']
|
||||
bar.volume = d['volume']
|
||||
bar.openInterest = d['open_oi']
|
||||
bar.datetime = datetime.fromtimestamp(d['datetime']/1000000000)
|
||||
bar.date = bar.datetime.strftime("%Y%m%d")
|
||||
bar.time = bar.datetime.strftime("%H:%M:%S")
|
||||
|
||||
return bar
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def onChart(symbol, seconds):
|
||||
"""K线更新处理函数"""
|
||||
# 避免重复记录已经完成的任务
|
||||
if symbol not in taskList:
|
||||
return
|
||||
|
||||
serial = api.get_kline_serial(symbol, seconds)
|
||||
|
||||
cl = db[symbol] # 集合
|
||||
cl.ensure_index([('datetime', ASCENDING)], unique=True) # 添加索引
|
||||
|
||||
l = serial.values()
|
||||
for d in l:
|
||||
bar = generateVtBar(symbol, d)
|
||||
d = bar.__dict__
|
||||
flt = {'datetime': bar.datetime}
|
||||
cl.replace_one(flt, d, True)
|
||||
|
||||
start = datetime.fromtimestamp(l[0]['datetime']/1000000000)
|
||||
end = datetime.fromtimestamp(l[-1]['datetime']/1000000000)
|
||||
print(u'合约%s下载完成%s - %s' %(symbol, start, end))
|
||||
|
||||
# 移除已经完成的任务
|
||||
if symbol in taskList:
|
||||
taskList.remove(symbol)
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def downMinuteBarBySymbol(symbol, num):
|
||||
"""下载某一合约的分钟线数据"""
|
||||
api.subscribe_chart(symbol, 60, num, onChart)
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def downloadAllMinuteBar(num, symbols):
|
||||
"""下载所有配置中的合约的分钟线数据"""
|
||||
print('-' * 50)
|
||||
print(u'开始下载合约分钟线数据')
|
||||
print('-' * 50)
|
||||
|
||||
# 添加下载任务
|
||||
taskList.extend(symbols)
|
||||
|
||||
for symbol in symbols:
|
||||
downMinuteBarBySymbol(str(symbol), num)
|
||||
|
||||
while True:
|
||||
sleep(2)
|
||||
|
||||
# 如果任务列表为空,则说明数据已经全部下载完成
|
||||
if not taskList:
|
||||
print('-' * 50)
|
||||
print(u'合约分钟线数据下载完成')
|
||||
print('-' * 50)
|
||||
return
|
||||
|
||||
|
||||
|
||||
|
@ -1,17 +0,0 @@
|
||||
# encoding: UTF-8
|
||||
|
||||
"""
|
||||
立即下载数据到数据库中,用于手动执行更新操作。
|
||||
|
||||
注意: 请先在本机启动天勤终端 (0.8.0 以上版本) 并保持运行, 再执行本程序
|
||||
"""
|
||||
|
||||
from dataService import *
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
symbols = ["CFFEX.IF1710", "CFFEX.IF1711", "CFFEX.IF1712", "CFFEX.IF1803",
|
||||
"CFFEX.IH1710", "CFFEX.IH1711", "CFFEX.IH1712", "CFFEX.IH1803",
|
||||
"CFFEX.IC1710", "CFFEX.IC1711", "CFFEX.IC1712", "CFFEX.IC1803"]
|
||||
downloadAllMinuteBar(1000, symbols)
|
@ -1,40 +0,0 @@
|
||||
# encoding: UTF-8
|
||||
|
||||
"""
|
||||
定时服务,可无人值守运行,实现每日自动下载更新历史行情数据到数据库中。
|
||||
|
||||
注意: 请确保本程序运行时, 本机天勤终端 (0.8.0 以上版本)正在运行中
|
||||
"""
|
||||
from __future__ import print_function
|
||||
|
||||
import time
|
||||
import datetime
|
||||
|
||||
from dataService import downloadAllMinuteBar
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
taskCompletedDate = None
|
||||
|
||||
# 生成一个随机的任务下载时间,用于避免所有用户在同一时间访问数据服务器
|
||||
taskTime = datetime.time(hour=17, minute=0)
|
||||
|
||||
symbols = ["CFFEX.IF1710", "CFFEX.IF1711", "CFFEX.IF1712", "CFFEX.IF1803",
|
||||
"CFFEX.IH1710", "CFFEX.IH1711", "CFFEX.IH1712", "CFFEX.IH1803",
|
||||
"CFFEX.IC1710", "CFFEX.IC1711", "CFFEX.IC1712", "CFFEX.IC1803"]
|
||||
|
||||
# 进入主循环
|
||||
while True:
|
||||
t = datetime.datetime.now()
|
||||
|
||||
# 每天到达任务下载时间后,执行数据下载的操作
|
||||
if t.time() > taskTime and (taskCompletedDate is None or t.date() != taskCompletedDate):
|
||||
# 下载1000根分钟线数据,足以覆盖过去两天的行情
|
||||
downloadAllMinuteBar(1000, symbols)
|
||||
|
||||
# 更新任务完成的日期
|
||||
taskCompletedDate = t.date()
|
||||
else:
|
||||
print(u'当前时间%s,任务定时%s' %(t, taskTime))
|
||||
|
||||
time.sleep(60)
|
Loading…
Reference in New Issue
Block a user