大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
小编给大家分享一下如何利用Joinquant数据源为vnpy添加期货行情数据,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
创新互联于2013年开始,是专业互联网技术服务公司,拥有项目成都网站建设、成都网站设计网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元额济纳做网站,已为上家服务,为额济纳各地企业和个人服务,联系电话:18980820575
使用聚宽(Joinquant)数据源JQData为vnpy添加期货行情数据
代码就两个文件,一个config.json主要放登陆用户名和期货品种名称对应表,因为期货品种在聚宽的名称是不一样的。
JQDataload.py是提供分钟级别下载方法,和一个期货品种名称对照列表从聚宽下载。
聚宽一天提供100万条数据下载,所以每次下载结束会有倒计时提供。其实代码很简单,唯一难点就是就是vnpy是每天9点00开始点,而聚宽是每天9点01为开始点;就抄抄之前vnpy已有代码解决。
结构如下
-JDDataService
|--config.json
|--JQDataload.py
其实使用很简单,步骤如下:
1.运行 pip install jqdatasdk , 安装jqdata的sdk
2.在聚宽平台注册试用数据,链接: https://www.joinquant.com/default/index/sdk
3.在config.json中维护登录名和密码,
4.运行JQDataload.py
config.json代码如下:
{ "Username": "聚宽申请", "Password": "聚宽申请", "rb1910":"RB1910.XSGE", "zn1807": "ZN1807.XSGE", "rb0000": "RB9999.XSGE" }
JQDataload.py代码如下:
# encoding: UTF-8 from __future__ import print_function import sys import json from datetime import datetime,date,timedelta from time import time, sleep from pymongo import MongoClient, ASCENDING import pandas as pd from vnpy.trader.vtObject import VtBarData, VtTickData from vnpy.trader.app.ctaStrategy.ctaBase import (MINUTE_DB_NAME, DAILY_DB_NAME, TICK_DB_NAME) import jqdatasdk as jq # 加载配置 config = open('config.json') setting = json.load(config) mc = MongoClient() # Mongo连接 dbMinute = mc[MINUTE_DB_NAME] # 数据库 # dbDaily = mc[DAILY_DB_NAME] # dbTick = mc[TICK_DB_NAME] USERNAME = setting['Username'] PASSWORD = setting['Password'] jq.auth(USERNAME, PASSWORD) FIELDS = ['open', 'high', 'low', 'close', 'volume'] # ---------------------------------------------------------------------- def generateVtBar(row, symbol): """生成K线""" bar = VtBarData() bar.symbol = symbol bar.exchange = "SHFE" bar.vtSymbol = bar.vtSymbol = '.'.join([bar.symbol, bar.exchange]) bar.open = row['open'] bar.high = row['high'] bar.low = row['low'] bar.close = row['close'] bar.volume = row['volume'] bardatetime = row.name bar.date = bardatetime.strftime("%Y%m%d") bar.time = bardatetime.strftime("%H%M%S") # 将bar的时间改成提前一分钟 hour = bar.time[0:2] minute = bar.time[2:4] sec = bar.time[4:6] if minute == "00": minute = "59" h = int(hour) if h == 0: h = 24 hour = str(h - 1).rjust(2, '0') else: minute = str(int(minute) - 1).rjust(2, '0') bar.time = hour + minute + sec bar.datetime = datetime.strptime(' '.join([bar.date, bar.time]), '%Y%m%d %H%M%S') return bar # ---------------------------------------------------------------------- def jqdownloadMinuteBarBySymbol(symbol,startDate,endDate): """下载某一合约的分钟线数据""" start = time() cl = dbMinute[symbol] cl.ensure_index([('datetime', ASCENDING)], unique=True) # 添加索引 df = jq.get_price(setting[symbol],start_date = startDate,end_date = endDate, frequency='1m', fields=FIELDS,skip_paused = True) for ix, row in df.iterrows(): bar = generateVtBar(row, symbol) d = bar.__dict__ flt = {'datetime': bar.datetime} cl.replace_one(flt, d, True) end = time() cost = (end - start) * 1000 print(u'合约%s的分钟K线数据下载完成%s - %s,耗时%s毫秒' % (symbol, df.index[0], df.index[-1], cost)) print(jq.get_query_count()) def jqdownloadMappingExcel(exportpath = "C:\Project\\"): getfuture = jq.get_all_securities(types=['futures'], date=None) # list: 用来过滤securities的类型, list元素可选: ‘stock’, ‘fund’, ‘index’, ‘futures’, ‘etf’, ‘lof’, ‘fja’, ‘fjb’.types为空时返回所有股票, 不包括基金, 指数和期货 getfuture.to_excel( exportpath + "Mapping" + str(date.today()) + "futures.xls", index=True, header=True) # ---------------------------------------------------------------------- def downloadAllMinuteBar(days=10): """下载所有配置中的合约的分钟线数据""" print('-' * 50) print(u'开始下载合约分钟线数据') print('-' * 50) startDt = datetime.today() - days * timedelta(1) startDate = startDt.strftime('%Y-%m-%d') # 添加下载任务 enddt = datetime.today() endDate = enddt.strftime('%Y-%m-%d %H:%M:%S') jqdownloadMinuteBarBySymbol('rb1910', startDate, endDate) print('-' * 50) print u'合约分钟线数据下载完成' print('-' * 50) if __name__ == '__main__': # jqdownloadMappingExcel() #下载主力合约 downloadAllMinuteBar(days=10) #下载单个品种 # jqdownloadMinuteBarBySymbol('510050.XSHG',startDate,endDate)
看完了这篇文章,相信你对“如何利用Joinquant数据源为vnpy添加期货行情数据”有了一定的了解,如果想了解更多相关知识,欢迎关注创新互联行业资讯频道,感谢各位的阅读!