现在基于网络量化平台做的都很专业,richquant也是vnpy数据源的提供方;不过richquant的试用版只有30天,而聚宽 (Joinquant)有一年试用期,而且也提供连续主力数据。
我就东抄西抄,利用以后logic,做了使用 聚宽(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.在聚宽平台注册试用数据,链接:
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)
可以在我的github的 JDDataService文件夹下载。