🚀 AI 一键生成 qmt 策略代码

使用 get_instrumentdetail 获取 PriceTick 实现合约筛选

qmt | 发布时间: 2025/12/29
以下内容由 EasyQuant 生成。

问题描述

怎么获取合约的最小变动单位 PriceTick?

解决方案

在 QMT 量化交易平台中,获取合约最小变动单位(PriceTick)可以通过调用 ContextInfo.get_instrumentdetail(stockcode) 接口来实现。

该接口返回一个包含合约详细信息的字典,其中键 'PriceTick' 对应的值即为最小变动单位。

获取 PriceTick 的方法

  1. 调用接口:使用 ContextInfo.get_instrumentdetail(合约代码)
  2. 解析返回值:该函数返回一个字典(dict),访问 ['PriceTick'] 字段即可。

代码示例

以下是一个完整的策略代码示例,展示如何在 handlebar 函数中获取并打印指定合约的最小变动单位。

# -*- coding: gbk -*-

def init(ContextInfo):
    # 在初始化中可以设置需要查询的股票或期货代码
    # 这里以浦发银行(股票)和沪深300股指期货(期货)为例
    ContextInfo.stock_list = ['600000.SH', 'IF2306.IF']

def handlebar(ContextInfo):
    # 仅在最后一根K线执行,避免历史回测时重复打印
    if not ContextInfo.is_last_bar():
        return

    for code in ContextInfo.stock_list:
        # 获取合约详细信息字典
        detail_dict = ContextInfo.get_instrumentdetail(code)
        
        # 判断是否成功获取到数据
        if detail_dict:
            # 提取 PriceTick 字段
            price_tick = detail_dict.get('PriceTick')
            instrument_name = detail_dict.get('InstrumentName')
            
            print(f"合约: {code} ({instrument_name})")
            print(f"最小变动单位 (PriceTick): {price_tick}")
            print("-" * 30)
        else:
            print(f"未找到合约 {code} 的详细信息")

返回字段说明

ContextInfo.get_instrumentdetail(stockcode) 返回的字典中,除了 PriceTick,还包含以下常用字段:

  • InstrumentName: 合约名称
  • ExchangeID: 市场代码
  • VolumeMultiple: 合约乘数
  • UpStopPrice: 涨停价
  • DownStopPrice: 跌停价
  • LongMarginRatio: 多头保证金率
  • ShortMarginRatio: 空头保证金率

Q&A

Q: get_instrumentdetail 接口支持哪些品种?
A: 该接口支持股票、期货、期权等多种证券品种,只要是 QMT 系统中存在的合约代码均可查询。

Q: 如果传入错误的合约代码会发生什么?
A: 如果传入的 stockcode 不存在或格式错误,函数通常会返回 None 或空字典。因此在代码中建议先判断返回值是否有效(如示例中的 if detail_dict:)。

Q: 获取到的 PriceTick 是什么类型的数值?
A: 返回的 PriceTick 是一个浮点数(float),例如股票通常返回 0.01,某些期货品种可能返回 0.25.0 等。