您的位置:首页 > 文旅 > 美景 > qmt量化交易策略小白学习笔记第62期【qmt编程之获取过期期权合约代码--get_stock_list_in_sector函数--内置Python】

qmt量化交易策略小白学习笔记第62期【qmt编程之获取过期期权合约代码--get_stock_list_in_sector函数--内置Python】

2024/10/6 4:01:07 来源:https://blog.csdn.net/fanglue3705/article/details/142132291  浏览:    关键词:qmt量化交易策略小白学习笔记第62期【qmt编程之获取过期期权合约代码--get_stock_list_in_sector函数--内置Python】

qmt编程之获取期权数据

qmt更加详细的教程方法,会持续慢慢梳理。

也可找寻博主的历史文章,搜索关键词查看解决方案 !

获取过期期权合约代码

注意

获取过期期权合约代码本质上是通过get_stock_list_in_sector()获取到过期板块内容,所以在使用前,请务必确保已经下载过历史合约信息

  1. 原生python:调用xtdata.download_history_contracts()进行下载

  2. 内置python:在界面端数据管理 - 过期合约数据 - 过期合约列表勾选下载,下载后需要重启客户端生效

内置python
调用方法

内置python

C.get_stock_list_in_sector(sector_name)

参数

字段类型说明
sector_namestring板块名称
返回值
  • list:内含成份股代码,代码形式为 'stockcode.market',如 '000002.SZ'
示例
#encoding:gbkimport redef init(C):option_code_list1 = get_option_code(C,"IF",data_type = 0) # 获取中金所当前可交易期权合约option_code_list2 = get_option_code(C,"SHO",data_type = 1) # 获取上交所已退市可交易期权合约option_code_list3 = get_option_code(C,"IF",data_type = 2) # 获取 中金所 所有期权(包含历史)合约print(option_code_list1[:5])print("="*20)print(option_code_list2[:5])print("="*20)print(option_code_list3[:5])# 可通过C.get_option_detail_data()查看合约具体信息def hanldbar(C):returndef get_option_code(C,market,data_type = 0):'''ToDo:取出指定market的期权合约Args:market: 目标市场,比如中金所填 IF data_type: 返回数据范围,可返回已退市合约,默认仅返回当前0: 仅当前1: 仅历史2: 历史 + 当前'''_history_sector_dict = {"IF":"过期中金所","SF":"过期上期所","DF":"过期大商所","ZF":"过期郑商所","INE":"过期能源中心","SHO":"过期上证期权","SZO":"过期深证期权",}# _now_secotr_dict = {#     "IF":"中金所",#     "SF":"上期所",#     "DF":"大商所",#     "ZF":"郑商所",#     "INE":"能源中心",#     "SHO":"上证期权",#     "SZO":"深证期权",# }_sector = _history_sector_dict.get(market)# _now_sector = _now_secotr_dict.get(market)if _sector == None:raise KeyError(f"不存在该市场:{market}")_now_sector = _sector[2:]# 过期上证和过期深证有专门的板块,不需要处理if market == "SHO" or market == "SZO":if data_type == 0:_list = C.get_stock_list_in_sector(_now_sector)elif data_type == 1:_list = C.get_stock_list_in_sector(_sector)elif data_type == 2:_list = C.get_stock_list_in_sector(_sector) + C.get_stock_list_in_sector(_now_sector)else:raise KeyError(f"data_type参数错误:{data_type}")return _list# 期货期权需要额外处理if data_type == 0:all_list = C.get_stock_list_in_sector(_now_sector)elif data_type == 1:all_list = C.get_stock_list_in_sector(_sector)elif data_type == 2:all_list = C.get_stock_list_in_sector(_sector) + C.get_stock_list_in_sector(_now_sector)else:raise KeyError(f"data_type参数错误:{data_type}")_list = []pattern1 = r'^[A-Z]{2}\d{4}-[A-Z]-\d{4}\.[A-Z]+$'pattern2 = r'^[a-zA-Z]+\d+[a-zA-Z]\d+\.[A-Z]+$'pattern3 = r'^[a-zA-Z]+\d+-[a-zA-Z]-\d+\.[A-Z]+$'for i in all_list:if re.match(pattern1,i):_list.append(i)elif re.match(pattern2,i):_list.append(i)elif re.match(pattern3,i):_list.append(i)# _list =[i for i in all_list if re.match(pattern, i)]return _list
返回值 
['HO2312-C-2100.IF', 'HO2312-C-2100.IF', 'HO2312-C-2125.IF', 'HO2312-C-2125.IF', 'HO2312-C-2150.IF']
====================
['10000001.SHO', '10000002.SHO', '10000003.SHO', '10000004.SHO', '10000005.SHO']
====================
['HO2301-C-2325.IF', 'HO2301-C-2325.IF', 'HO2301-C-2350.IF', 'HO2301-C-2350.IF', 'HO2301-C-2375.IF']

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com