返回列表 发布新帖
查看: 63|回复: 0

《极速回测利器:使用Feather加速行情数据读取,速度提升800倍!》

29

主题

70

回帖

257

积分

管理员

积分
257
发表于 2025-5-15 11:16:25 | 查看全部 |阅读模式
前言
在进行策略回测时,经常需要使用历史 K 线数据。尤其是在股票池较多、回测时间比较长的情况下,传统的数据读取方式往往效率低下,尤其对于配置较低的硬件来说,用户体验更是不佳。
为了提高 K 线数据的读取速度并改善用户体验,我们可以结合迅投的行情接口,采用更高效的数据结构——Feather。
如何使用?三大操作步骤如下:
  • 1.生成 Feather 数据文件——极速准备行情数据
  • 2.修改策略,将数据读取代码替换为 Feather 格式
  • 3.进行回测

详细指引:1.生成 Feather 数据:操作轻松,速度提升近 800 倍
以下是已准备好的数据,您只需根据实际情况修改参数即可:
  1. import os

  2. # 参数设置
  3. data_dir_path = r'E:\tokendatadir'
  4. start_time      ='19990101'
  5. end_time        ='20240227'
  6. period          ='1d'
  7. dividend_type   ='front_ratio'  # 等比前复权
  8. token           ='' # 填自己的实际token,可以加群或者https://dict.thinktrader.net/dictionary/?id=I3DJ97#%E5%A6%82%E4%BD%95%E6%88%90%E4%B8%BA-vip-%E8%A1%8C%E6%83%85%E7%94%A8%E6%88%B7  了解详细信息


  9. output_dir = os.path.join(data_dir_path, r'feather')
  10. if not os.path.exists(output_dir):
  11.     os.makedirs(output_dir)

  12. if 1:
  13.     # 导入 xtdatacenter 模块

  14.     from xtquant import xtdatacenter as xtdc  
  15.     xtdc.set_token(token)  
  16.     xtdc.set_data_home_dir(data_dir_path)
  17.     print(help(xtdc.set_data_home_dir))  
  18.     xtdc.init()
  19.   

  20. from xtquant import xtdata
  21. #下载

  22. xtdata.download_sector_data()
  23. stock_lists = xtdata.get_stock_list_in_sector('沪深A股')


  24. def on_download(info):
  25.     print(info)


  26. if 1:
  27.     xtdata.download_history_data2(stock_lists, start_time=start_time, end_time=end_time, period=period,callback=on_download, incrementally=False)


  28. ex_price = xtdata.get_market_data([],stock_lists,period=period,start_time=start_time,end_time=end_time,fill_data=False,dividend_type=dividend_type)
  29. for col in ex_price:
  30.   
  31.     output = os.path.join(output_dir, col+period+'.feather')
  32.     ex_price[col].T.to_feather(output)
  33. print('完成')

复制代码
注意:实际应用中,该脚本只需每天运行一次以更新数据。若仅需回测特定时间段的行情,则无需更新数据。
2.Feather 与 QMT 速度对比:秒速读取,轻松体验
从下图可以看出,Feather 的数据读取速度快了近 800 倍!而且几乎是秒速读取,完全没有卡顿感。
3.修改策略,使用 Feather 进行数据读取:极速回测,一键体验
最后,在您的策略中,将数据读取代码修改为使用 Feather 格式,快来体验极速回测吧!


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

投诉/建议联系

admin@discuz.vip

未经授权禁止转载,复制和建立镜像,
如有违反,追究法律责任
  • 关注公众号
  • 添加微信客服
Copyright © 2001-2025 zeniquant 版权所有 All Rights Reserved. 粤ICP备2025409975号-1
关灯 在本版发帖
扫一扫添加微信客服
QQ客服返回顶部
快速回复 返回顶部 返回列表