小編給大家分享一下如何利用Joinquant數(shù)據(jù)源為vnpy添加期貨行情數(shù)據(jù),希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
創(chuàng)新互聯(lián)于2013年開(kāi)始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元額濟(jì)納做網(wǎng)站,已為上家服務(wù),為額濟(jì)納各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18980820575
使用聚寬(Joinquant)數(shù)據(jù)源JQData為vnpy添加期貨行情數(shù)據(jù)
代碼就兩個(gè)文件,一個(gè)config.json主要放登陸用戶名和期貨品種名稱對(duì)應(yīng)表,因?yàn)槠谪浧贩N在聚寬的名稱是不一樣的。
JQDataload.py是提供分鐘級(jí)別下載方法,和一個(gè)期貨品種名稱對(duì)照列表從聚寬下載。
聚寬一天提供100萬(wàn)條數(shù)據(jù)下載,所以每次下載結(jié)束會(huì)有倒計(jì)時(shí)提供。其實(shí)代碼很簡(jiǎn)單,唯一難點(diǎn)就是就是vnpy是每天9點(diǎn)00開(kāi)始點(diǎn),而聚寬是每天9點(diǎn)01為開(kāi)始點(diǎn);就抄抄之前vnpy已有代碼解決。
結(jié)構(gòu)如下
-JDDataService
|--config.json
|--JQDataload.py
其實(shí)使用很簡(jiǎn)單,步驟如下:
1.運(yùn)行 pip install jqdatasdk , 安裝jqdata的sdk
2.在聚寬平臺(tái)注冊(cè)試用數(shù)據(jù),鏈接: https://www.joinquant.com/default/index/sdk
3.在config.json中維護(hù)登錄名和密碼,
4.運(yùn)行JQDataload.py
config.json代碼如下:
{ "Username": "聚寬申請(qǐng)", "Password": "聚寬申請(qǐng)", "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] # 數(shù)據(jù)庫(kù) # 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的時(shí)間改成提前一分鐘 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): """下載某一合約的分鐘線數(shù)據(jù)""" 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線數(shù)據(jù)下載完成%s - %s,耗時(shí)%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: 用來(lái)過(guò)濾securities的類型, list元素可選: ‘stock’, ‘fund’, ‘index’, ‘futures’, ‘etf’, ‘lof’, ‘fja’, ‘fjb’.types為空時(shí)返回所有股票, 不包括基金, 指數(shù)和期貨 getfuture.to_excel( exportpath + "Mapping" + str(date.today()) + "futures.xls", index=True, header=True) # ---------------------------------------------------------------------- def downloadAllMinuteBar(days=10): """下載所有配置中的合約的分鐘線數(shù)據(jù)""" print('-' * 50) print(u'開(kāi)始下載合約分鐘線數(shù)據(jù)') print('-' * 50) startDt = datetime.today() - days * timedelta(1) startDate = startDt.strftime('%Y-%m-%d') # 添加下載任務(wù) enddt = datetime.today() endDate = enddt.strftime('%Y-%m-%d %H:%M:%S') jqdownloadMinuteBarBySymbol('rb1910', startDate, endDate) print('-' * 50) print u'合約分鐘線數(shù)據(jù)下載完成' print('-' * 50) if __name__ == '__main__': # jqdownloadMappingExcel() #下載主力合約 downloadAllMinuteBar(days=10) #下載單個(gè)品種 # jqdownloadMinuteBarBySymbol('510050.XSHG',startDate,endDate)
看完了這篇文章,相信你對(duì)“如何利用Joinquant數(shù)據(jù)源為vnpy添加期貨行情數(shù)據(jù)”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!