这篇文章主要为大家展示了“怎么使用Joinquant做实盘行情数据”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“怎么使用Joinquant做实盘行情数据”这篇文章吧。
如下图,如果运行行情数据下载时候,是在交易日中的话比如2点半或者上午8点,如果填写的endDate是当天或者之后的日期,那么返回数据会自动填充到下午3点交易时候。此时之后的数据都是交易量为0,价格就是2点半时候价格。
所以在用作实盘数据分析时候,必须要填入endDate的分钟时间为当前时间,才可以确保不会出现控制。整体代码更新如下:
# encoding: UTF-8 from __future__ import print_functionimport sysimport jsonfrom datetime import datetime,date,timedeltafrom time import time, sleep from pymongo import MongoClient, ASCENDINGimport pandas as pd from vnpy.trader.vtObject import VtBarData, VtTickDatafrom 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做实盘行情数据”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网行业资讯频道!