vn.py量化社区
By Traders, For Traders.
Member
加入于:
帖子: 58
声望: 9

现在基于网络量化平台做的都很专业,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.在聚宽平台注册试用数据,链接: 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
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.vtSymbol = symbol
    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)
    getfuture.to_excel(
                    exportpath + "Mapping" + str(date.today())  + ".xls",
                    index=True, header=True)
if __name__ == '__main__':
    jqdownloadMappingExcel()
    #下载主力合约
    jqdownloadMinuteBarBySymbol('rb0000', '2018-1-1', '2019-5-1')
    #下载单个品种
    jqdownloadMinuteBarBySymbol('zn1807','2018-6-2','2018-6-8')
Member
加入于:
帖子: 58
声望: 9

可以在我的github的 JDDataService文件夹下载。

https://github.com/BillyZhangGuoping/MarketDataAnaylzerbyDataFrame

Member
加入于:
帖子: 22
声望: 0

谢谢分享

Member
avatar
加入于:
帖子: 1
声望: 0

谢谢分享

Member
加入于:
帖子: 22
声望: 0

可惜聚宽分钟数据信息太少没有持仓量

Member
avatar
加入于:
帖子: 2
声望: 0

谢谢

Member
avatar
加入于:
帖子: 14
声望: 0

很赞,感谢分享

© 2015-2019 上海韦纳软件科技有限公司
备案服务号:沪ICP备18006526号-3