小編給大家分享一下如何利用Joinquant數(shù)據(jù)源為vnpy添加期貨行情數(shù)據(jù),希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供臨清企業(yè)網(wǎng)站建設(shè),專注與成都做網(wǎng)站、成都網(wǎng)站建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、H5響應(yīng)式網(wǎng)站、小程序制作等業(yè)務(wù)。10年已為臨清眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進(jìn)行中。使用聚寬(Joinquant)數(shù)據(jù)源JQData為vnpy添加期貨行情數(shù)據(jù)
代碼就兩個(gè)文件,一個(gè)config.json主要放登陸用戶名和期貨品種名稱對(duì)應(yīng)表,因?yàn)槠谪浧贩N在聚寬的名稱是不一樣的。
JQDataload.py是提供分鐘級(jí)別下載方法,和一個(gè)期貨品種名稱對(duì)照列表從聚寬下載。
聚寬一天提供100萬條數(shù)據(jù)下載,所以每次下載結(jié)束會(huì)有倒計(jì)時(shí)提供。其實(shí)代碼很簡(jiǎn)單,唯一難點(diǎn)就是就是vnpy是每天9點(diǎn)00開始點(diǎn),而聚寬是每天9點(diǎn)01為開始點(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ù),鏈接: /tupian/20230522/404 "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: 用來過濾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'開始下載合約分鐘線數(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)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道,感謝各位的閱讀!
網(wǎng)頁名稱:如何利用Joinquant數(shù)據(jù)源為vnpy添加期貨行情數(shù)據(jù)-創(chuàng)新互聯(lián)
文章源于:http://www.rwnh.cn/article48/ccgohp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、建站公司、微信小程序、App設(shè)計(jì)、網(wǎng)站導(dǎo)航、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容