3 分钟快速生成代码
输入想法,AI 即刻生成可运行代码
在 QMT 中,要获取某个 ETF 对应的所有看涨期权合约列表,可以使用 ContextInfo.get_option_list 函数。
ContextInfo.get_option_list(undl_code, dedate, opttype, isavailable)
'510050.SH' (上证50ETF) 或 '510300.SH' (沪深300ETF)。dedate 为具体日期时,True 表示仅返回当日可交易的合约,False 可能包含已退市或未上市的合约(通常填 True)。以下代码展示了如何在策略运行过程中(回测或实盘),获取当前日期对应的某 ETF 所有看涨期权合约列表:
# -*- coding: gbk -*-
def init(ContextInfo):
# 设置标的ETF,例如华夏上证50ETF
ContextInfo.etf_code = '510050.SH'
def handlebar(ContextInfo):
# 获取当前K线的时间戳
timetag = ContextInfo.get_bar_timetag(ContextInfo.barpos)
# 将时间戳转换为 YYYYMMDD 格式的字符串
current_date = timetag_to_datetime(timetag, '%Y%m%d')
# 获取该ETF在当前日期可交易的所有看涨期权(CALL)
# 参数说明:标的代码, 当前日期, 期权类型"CALL", 是否可用True
call_options = ContextInfo.get_option_list(ContextInfo.etf_code, current_date, "CALL", True)
# 打印结果
print(f"日期: {current_date}, 标的: {ContextInfo.etf_code}")
print(f"看涨期权数量: {len(call_options)}")
print(f"合约列表样例: {call_options[:5]}") # 仅打印前5个作为示例
# 如果需要获取特定月份到期的看涨期权(例如2023年12月到期)
# specific_month_calls = ContextInfo.get_option_list(ContextInfo.etf_code, "202312", "CALL", True)
current_date 是动态获取的,因此既适用于历史回测(获取历史某天的期权链),也适用于实盘(获取当天的期权链)。