3 分钟快速生成代码
输入想法,AI 即刻生成可运行代码
在 QMT 平台中,如果需要查询某个股票、期货或期权合约的详细属性(如流通股本、总股本、最小变价单位等),可以使用 ContextInfo.get_instrumentdetail(stockcode) 接口。该接口会返回一个包含合约所有静态属性的字典(dict)。
ContextInfo.get_instrumentdetail()ContextInfo.get_instrumentdetail(stockcode)stockcode (string),股票或合约代码,例如 '600000.SH'。对于您关心的三个指标,其对应的字典 Key 如下:
FloatVolumnTotalVolumnPriceTick您可以在策略的 handlebar 或 init 函数中直接调用此接口。以下是一个完整的查询示例:
#coding:gbk
def init(ContextInfo):
# 设定基础股票池
ContextInfo.set_universe(['600000.SH'])
def handlebar(ContextInfo):
# 仅在最后一根 K 线执行一次,避免重复打印
if not ContextInfo.is_last_bar():
return
stock = '600000.SH'
# 获取合约详细信息
detail = ContextInfo.get_instrumentdetail(stock)
if detail:
# 提取流通股本、总股本和最小变价单位
float_vol = detail.get('FloatVolumn', 0) # 流通股本
total_vol = detail.get('TotalVolumn', 0) # 总股本
price_tick = detail.get('PriceTick', 0) # 最小变价单位
name = detail.get('InstrumentName', '')
print(f"合约代码: {stock} ({name})")
print(f"流通股本: {float_vol} 股")
print(f"总 股 本: {total_vol} 股")
print(f"最小变价单位: {price_tick} 元")
else:
print(f"未能获取到 {stock} 的合约详细信息")
调用该接口返回的字典中包含丰富的合约属性,完整字段对照如下:
| 字段 Key | 含义 | 适用类型 |
|---|---|---|
| ExchangeID | 合约市场代码 | 通用 |
| InstrumentID | 合约代码 | 通用 |
| InstrumentName | 合约名称 | 通用 |
| ProductID | 合约的品种ID | 期货 |
| ProductName | 合约的品种名称 | 期货 |
| CreateDate | 上市日期 | 期货 |
| OpenDate | IPO日期 | 股票 |
| ExpireDate | 退市日或者到期日 | 通用 |
| PreClose | 前收盘价格 | 通用 |
| SettlementPrice | 前结算价格 | 通用 |
| UpStopPrice | 当日涨停价 | 通用 |
| DownStopPrice | 当日跌停价 | 通用 |
| FloatVolumn | 流通股本 | 股票 |
| TotalVolumn | 总股本 | 股票 |
| LongMarginRatio | 多头保证金率 | 期货 |
| ShortMarginRatio | 空头保证金率 | 期货 |
| PriceTick | 最小变价单位 | 通用 |
| VolumeMultiple | 合约乘数 (期货以外默认是1) | 通用 |
| MainContract | 主力合约标记 | 期货 |
| LastVolume | 昨日持仓量 | 期货 |
| InstrumentStatus | 合约停牌状态 | 通用 |
| IsTrading | 合约是否可交易 | 通用 |
| IsRecent | 是否是近月合约 | 期货 |