From 581294d07936a7b6f57ec0295b0cdcaa10c7dd5e Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Fri, 21 Dec 2018 22:08:54 +0800 Subject: [PATCH] =?UTF-8?q?[Add]=E6=96=B0=E5=A2=9ERQData=E7=9A=84Tick?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/RQData/dataService.py | 120 +++++++++++++++++++++----------- examples/RQData/downloadData.py | 9 +-- examples/RQData/runService.py | 33 --------- 3 files changed, 83 insertions(+), 79 deletions(-) delete mode 100644 examples/RQData/runService.py diff --git a/examples/RQData/dataService.py b/examples/RQData/dataService.py index b6736e6d..33a1e9f4 100644 --- a/examples/RQData/dataService.py +++ b/examples/RQData/dataService.py @@ -7,9 +7,12 @@ from datetime import datetime from time import time, sleep from pymongo import MongoClient, ASCENDING +import pandas as pd -from vnpy.trader.vtObject import VtBarData -from vnpy.trader.app.ctaStrategy.ctaBase import MINUTE_DB_NAME, DAILY_DB_NAME +from vnpy.trader.vtObject import VtBarData, VtTickData +from vnpy.trader.app.ctaStrategy.ctaBase import (MINUTE_DB_NAME, + DAILY_DB_NAME, + TICK_DB_NAME) import rqdatac as rq @@ -17,16 +20,13 @@ import rqdatac as rq config = open('config.json') setting = json.load(config) -MONGO_HOST = setting['MONGO_HOST'] -MONGO_PORT = setting['MONGO_PORT'] -SYMBOLS = setting['SYMBOLS'] +mc = MongoClient() # Mongo连接 +dbMinute = mc[MINUTE_DB_NAME] # 数据库 +dbDaily = mc[DAILY_DB_NAME] +dbTick = mc[TICK_DB_NAME] -mc = MongoClient(MONGO_HOST, MONGO_PORT) # Mongo连接 -db = mc[MINUTE_DB_NAME] # 数据库 -db2 = mc[DAILY_DB_NAME] - -USERNAME = setting['USERNAME'] -PASSWORD = setting['PASSWORD'] +USERNAME = setting['rqUsername'] +PASSWORD = setting['rqPassword'] rq.init(USERNAME, PASSWORD) FIELDS = ['open', 'high', 'low', 'close', 'volume'] @@ -49,12 +49,56 @@ def generateVtBar(row, symbol): return bar +#---------------------------------------------------------------------- +def generateVtTick(row, symbol): + """生成K线""" + tick = VtTickData() + tick.symbol = symbol + tick.vtSymbol = symbol + + tick.lastPrice = row['last'] + tick.volume = row['volume'] + tick.openInterest = row['open_interest'] + tick.datetime = row.name + tick.openPrice = row['open'] + tick.highPrice = row['high'] + tick.lowPrice = row['low'] + tick.preClosePrice = row['prev_close'] + tick.upperLimit = row['limit_up'] + tick.lowerLimit = row['limit_down'] + + tick.bidPrice1 = row['b1'] + tick.bidPrice2 = row['b2'] + tick.bidPrice3 = row['b3'] + tick.bidPrice4 = row['b4'] + tick.bidPrice5 = row['b5'] + + tick.bidVolume1 = row['b1_v'] + tick.bidVolume2 = row['b2_v'] + tick.bidVolume3 = row['b3_v'] + tick.bidVolume4 = row['b4_v'] + tick.bidVolume5 = row['b5_v'] + + tick.askPrice1 = row['a1'] + tick.askPrice2 = row['a2'] + tick.askPrice3 = row['a3'] + tick.askPrice4 = row['a4'] + tick.askPrice5 = row['a5'] + + tick.askVolume1 = row['a1_v'] + tick.askVolume2 = row['a2_v'] + tick.askVolume3 = row['a3_v'] + tick.askVolume4 = row['a4_v'] + tick.askVolume5 = row['a5_v'] + + return tick + #---------------------------------------------------------------------- def downloadMinuteBarBySymbol(symbol): """下载某一合约的分钟线数据""" start = time() - cl = db[symbol] + cl = dbMinute[symbol] cl.ensure_index([('datetime', ASCENDING)], unique=True) # 添加索引 df = rq.get_price(symbol, frequency='1m', fields=FIELDS) @@ -68,14 +112,14 @@ def downloadMinuteBarBySymbol(symbol): end = time() cost = (end - start) * 1000 - print(u'合约%s数据下载完成%s - %s,耗时%s毫秒' %(symbol, df.index[0], df.index[-1], cost)) + print(u'合约%s的分钟K线数据下载完成%s - %s,耗时%s毫秒' %(symbol, df.index[0], df.index[-1], cost)) #---------------------------------------------------------------------- def downloadDailyBarBySymbol(symbol): """下载某一合约日线数据""" start = time() - cl = db2[symbol] + cl = dbDaily[symbol] cl.ensure_index([('datetime', ASCENDING)], unique=True) # 添加索引 df = rq.get_price(symbol, frequency='1d', fields=FIELDS, end_date=datetime.now().strftime('%Y%m%d')) @@ -89,36 +133,28 @@ def downloadDailyBarBySymbol(symbol): end = time() cost = (end - start) * 1000 - print(u'合约%s数据下载完成%s - %s,耗时%s毫秒' %(symbol, df.index[0], df.index[-1], cost)) - + print(u'合约%s的日K线数据下载完成%s - %s,耗时%s毫秒' %(symbol, df.index[0], df.index[-1], cost)) #---------------------------------------------------------------------- -def downloadAllMinuteBar(): - """下载所有配置中的合约的分钟线数据""" - print('-' * 50) - print(u'开始下载合约分钟线数据') - print('-' * 50) - - # 添加下载任务 - for symbol in SYMBOLS: - downloadMinuteBarBySymbol(str(symbol)) - - print('-' * 50) - print(u'合约分钟线数据下载完成') - print('-' * 50) +def downloadTickBySymbol(symbol, date): + """下载某一合约日线数据""" + start = time() -#---------------------------------------------------------------------- -def downloadAllDailyBar(): - """下载所有配置中的合约的日数据""" - print('-' * 50) - print(u'开始下载合约日线数据') - print('-' * 50) + cl = dbTick[symbol] + cl.ensure_index([('datetime', ASCENDING)], unique=True) # 添加索引 - # 添加下载任务 - for symbol in SYMBOLS: - downloadDailyBarBySymbol(str(symbol)) + df = rq.get_price(symbol, + frequency='tick', + start_date=date, + end_date=date) - print('-' * 50) - print(u'合约日线数据下载完成') - print('-' * 50) - \ No newline at end of file + for ix, row in df.iterrows(): + tick = generateVtTick(row, symbol) + d = tick.__dict__ + flt = {'datetime': tick.datetime} + cl.replace_one(flt, d, True) + + end = time() + cost = (end - start) * 1000 + + print(u'合约%sTick数据下载完成%s - %s,耗时%s毫秒' %(symbol, df.index[0], df.index[-1], cost)) diff --git a/examples/RQData/downloadData.py b/examples/RQData/downloadData.py index f0263ffc..19cc7c95 100644 --- a/examples/RQData/downloadData.py +++ b/examples/RQData/downloadData.py @@ -8,7 +8,8 @@ from dataService import * if __name__ == '__main__': - downloadMinuteBarBySymbol('CU99') - downloadDailyBarBySymbol('IF99') - downloadDailyBarBySymbol('TA99') - downloadDailyBarBySymbol('I99') \ No newline at end of file + #downloadMinuteBarBySymbol('CU99') + #downloadDailyBarBySymbol('IF99') + #downloadDailyBarBySymbol('TA99') + #downloadDailyBarBySymbol('I99') + downloadTickBySymbol('IF1901', '2018-12-21') \ No newline at end of file diff --git a/examples/RQData/runService.py b/examples/RQData/runService.py deleted file mode 100644 index 58c79e30..00000000 --- a/examples/RQData/runService.py +++ /dev/null @@ -1,33 +0,0 @@ -# encoding: UTF-8 - -""" -定时服务,可无人值守运行,实现每日自动下载更新历史行情数据到数据库中。 -""" -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) - - # 进入主循环 - 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