# 商品期权T型报价代码 # 参考资料: # - 原始策略来源: https://www.joinquant.com/view/community/detail/625edad0050315dcc2df540cd462df60 # - 研究网址: https://www.joinquant.com/research?target=research&url=/user/75474983526/notebooks/Options/%E5%95%86%E5%93%81%E6%9C%9F%E6%9D%83T%E5%9E%8B%E6%8A%A5%E4%BB%B7.ipynb # TODO: 添加商品期权T型报价相关代码 import pandas as pd import numpy as np import talib as tb from pandas.plotting import table from jqdata import * # 期权合约信息 class OptionContract: close: 0 #现价 volume: 0 #成交量 position: 0 #当前持仓量 priceChangePct: 0 #收盘价涨跌幅 def __init__(self,close, volume, position, priceChangePct): self.close = close self.volume = volume self.position = position self.priceChangePct = priceChangePct # 同一行权价的一组期权 class OptionGroup: exercisePrice: 0 ##行权价 call: None ##提醒的品种 put: None ##提醒的类型 def __init__(self, exercisePrice, call, put): self.exercisePrice = exercisePrice self.call = call self.put = put def __init__(self, exercisePrice): self.exercisePrice = exercisePrice #数据内容 CONTENT = [ 'close', 'volume', 'position', 'priceChangePct', ] ##数据域 CONTENT_HEADER_MAP = { 'prePosition': '昨日持仓', 'close': '现价', 'volume': '成交量', 'position': '持仓量', 'priceChangePct': '涨跌幅' } # 豆粕期权 SUBJECT_MATTER = 'M2407.XDCE' ##查询数据日期,以2024-03-22为例 DATA_DATE = '2024-03-22' EXPIRE_MONTH = '2024-03-27' #m2407 匹配 2024-04-19 EXPIRE_MONTH = '2024-06-07' ## 行权价间隔 比如3800 3750 每50元一档 gap = 50 #查询相关的合约,适用于商品 qy = query(opt.OPT_CONTRACT_INFO).filter( opt.OPT_CONTRACT_INFO.underlying_symbol == SUBJECT_MATTER, ##期权标的物 opt.OPT_CONTRACT_INFO.expire_date == EXPIRE_MONTH,##期权到期日 ).order_by(opt.OPT_CONTRACT_INFO.exercise_price, opt.OPT_CONTRACT_INFO.contract_type) optList = opt.run_query(qy) optList optionGroups = {} for index, row in optList.iterrows(): code = row['code'] #key - 期权代码 #查询具体合约的信息,商品需要去掉开盘前的静态信息 realTimeQuery = query(opt.OPT_DAILY_PRICE).filter(opt.OPT_DAILY_PRICE.code==code, opt.OPT_DAILY_PRICE.date==DATA_DATE).order_by(opt.OPT_DAILY_PRICE.date.desc()).limit(1) realTimeData = opt.run_query(realTimeQuery) #期权基本信息 exercisePrice = row['exercise_price'] #行权价 contractType = row['contract_type'] #合约类型。CO-认购期权,PO-认沽期权 #实时表查询 close = realTimeData.loc[0].at['close'] #现价 volume = int(realTimeData.loc[0].at['volume']) #成交量 position = realTimeData.loc[0].at['position'] #当前持仓量 priceChangePct = str(round(realTimeData.loc[0].at['change_pct_close'], 2)) + '%' #收盘价涨跌幅 #去除非标准的带A合约 if(exercisePrice % gap != 0): continue optionContract = OptionContract(close, volume, position, priceChangePct) if(exercisePrice in optionGroups): if(contractType == 'CO'): optionGroups[exercisePrice].call = optionContract else: optionGroups[exercisePrice].put = optionContract else: optionGroup = OptionGroup(exercisePrice) if(contractType == 'CO'): optionGroup.call = optionContract else: optionGroup.put = optionContract optionGroups[exercisePrice] = optionGroup list = optionGroups.values() rtitle = CONTENT.copy() rtitle.reverse() data = {} for key in rtitle: data['C-'+CONTENT_HEADER_MAP[key]] = [] data['行权价'] = [] for key in CONTENT: data['P-'+CONTENT_HEADER_MAP[key]] = [] for option in list: for key in CONTENT: data['C-'+CONTENT_HEADER_MAP[key]].append(getattr(option.call, key)) data['行权价'].append(option.exercisePrice) for key in rtitle: data['P-'+CONTENT_HEADER_MAP[key]].append(getattr(option.put, key)) df = pd.DataFrame(data) df