🚀 AI 一键生成 qmt 策略代码

QMT API 历史数据下载卡死?详解 start_time 参数正确格式

qmt | 发布时间: 2026/5/25
以下内容由 EasyQuant 生成。

问题描述

反馈一个download_history_data2的BUG,如果start_time参数格式不对,函数就是卡死。

反馈一个 download_history_data2 的 BUG,如果 start_time 参数格式不对,函数就是卡死。

start_time=2025-02-25 如果输入这个格式的时间,函数就会卡死。

解决方案

问题分析

在 QMT 平台中,底层数据接口通常由 C++ 实现。当 Python 层传入的时间参数格式不符合底层预期时,底层解析可能会失败,从而导致死循环或程序直接卡死。您遇到的 download_history_data2(以及类似的 get_market_dataget_history_data 等接口)因传入 2025-02-25 导致卡死,正是因为触发了这一格式校验缺陷。

正确的时间格式规范

根据 QMT API 官方文档规范,所有涉及时间范围的参数(如 start_timeend_time),其标准字符串格式必须是以下两种之一,不能包含横杠(-)、空格或冒号(:)

  1. 年月日格式'YYYYMMDD'(例如:'20250225'
  2. 年月日时分秒格式'YYYYMMDDHHMMSS'(例如:'20250225093000'

解决方案与代码建议

为了避免在策略编写或数据下载过程中因时间格式不规范导致 QMT 客户端卡死,建议在调用 API 前对时间字符串进行统一的清洗和格式化。

1. 错误与正确写法对比

# 错误写法(会导致卡死)
# start_time = '2025-02-25'

# 正确写法
start_time = '20250225'

2. 封装时间清洗工具函数

在实际开发中,建议编写一个工具函数,自动将常规的日期格式转换为 QMT 兼容的格式:

def format_qmt_time(date_str):
    """
    将常规时间字符串转换为 QMT 支持的格式
    例如: '2025-02-25' -> '20250225'
          '2025-02-25 09:30:00' -> '20250225093000'
    """
    if not date_str:
        return ''
    return str(date_str).replace('-', '').replace(' ', '').replace(':', '')

# 在策略或下载脚本中使用
def handlebar(ContextInfo):
    raw_start_time = '2025-02-25'
    safe_start_time = format_qmt_time(raw_start_time)
    
    # 安全调用数据接口
    # ContextInfo.download_history_data2(..., start_time=safe_start_time, ...)

通过在入参前进行 replace('-', '') 处理,可以有效规避此类底层 BUG,保证量化程序的稳定性。