| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- 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 #收盘价涨跌幅
- intrinsic_value: 0 #内在价值
- time_value: 0 #时间价值
-
-
- def __init__(self,close, volume, position, priceChangePct, intrinsic_value=0, time_value=0):
- self.close = close
- self.volume = volume
- self.position = position
- self.priceChangePct = priceChangePct
- self.intrinsic_value = intrinsic_value
- self.time_value = time_value
-
- # 同一行权价的一组期权
- 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': '涨跌幅'
- }
- # 豆粕期权
- symbol = 'AU'
- starttime = '2025-06-30'
- endtime = '2025-07-14'
- SUBJECT_MATTER = get_dominant_future(symbol,date = starttime)
- print(f"SUBJECT_MATTER: {SUBJECT_MATTER}")
- #m2407 匹配 2024-04-19
- EXPIRE_MONTH = '2025-09-24'
- ## 行权价间隔 比如3800 3750 每50元一档
- gap = 8
- # 获取期货价格
- try:
- # 获取期货价格数据
- future_price_data = get_price(SUBJECT_MATTER, end_date=endtime, count=1, fields=['close'])
- current_future_price = float(future_price_data['close'].iloc[-1])
- except Exception as e:
- print(f"获取期货价格失败: {e}")
- current_future_price = 781.4 # 使用模拟价格
- print(f"标的期货价格: {current_future_price}")
- #查询相关的合约,适用于商品
- 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
- code = SUBJECT_MATTER.split('.')[0]
- print(f"code: {code}")
- # optList.to_csv(f'price_list_{code}.csv')
- # 获取实时数据
- optionGroups = {}
- for index, row in optList.iterrows():
-
- code = row['code'] #key - 期权代码
- # print(f"检查{code}的数据")
- #查询具体合约的信息,商品需要去掉开盘前的静态信息
- realTimeQuery = query(opt.OPT_DAILY_PRICE).filter(
- opt.OPT_DAILY_PRICE.code==code,
- opt.OPT_DAILY_PRICE.date==endtime
- ).order_by(opt.OPT_DAILY_PRICE.date.desc()).limit(1)
- realTimeData = opt.run_query(realTimeQuery)
- # print(f"realTimeQuery: {realTimeQuery}")
- # print(f"realTimeData: {realTimeData}")
-
- #期权基本信息
- 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
-
- # 计算内在价值和时间价值
- if contractType == 'CO': # 认购期权
- intrinsic_value = max(current_future_price - exercisePrice, 0)
- time_value = close - intrinsic_value
- print(f"处理认购期权, 期货价格: {current_future_price}, 行权价:{exercisePrice}")
- print(f"获得内在价值: {intrinsic_value}, 时间价值: {time_value}")
- else: # 认沽期权
- intrinsic_value = max(exercisePrice - current_future_price, 0)
- time_value = close - intrinsic_value
- print(f"处理认沽期权, 期货价格: {current_future_price}, 行权价:{exercisePrice}")
- print(f"获得内在价值: {intrinsic_value}, 时间价值: {time_value}")
-
- optionContract = OptionContract(close, volume, position, priceChangePct, intrinsic_value, time_value)
-
- 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
- # 使用optionGroups构造T型报价表
- data = []
- for exercisePrice in sorted(optionGroups.keys()):
- group = optionGroups[exercisePrice]
- # print(f"exercisePrice: {exercisePrice}, group: {group}")
-
- # 认购期权数据
- call_price = group.call.close if group.call else None
- call_volume = group.call.volume if group.call else None
- call_position = group.call.position if group.call else None
- call_change = group.call.priceChangePct if group.call else None
- call_intrinsic = group.call.intrinsic_value if group.call else None
- # print(f"exercisePrice: {exercisePrice}, call_intrinsic: {call_intrinsic}")
- call_time = group.call.time_value if group.call else None
-
- # 认沽期权数据
- put_price = group.put.close if group.put else None
- put_volume = group.put.volume if group.put else None
- put_position = group.put.position if group.put else None
- put_change = group.put.priceChangePct if group.put else None
- put_intrinsic = group.put.intrinsic_value if group.put else None
- put_time = group.put.time_value if group.put else None
-
- data.append({
- '认购涨跌幅': call_change,
- # '认购持仓量': call_position,
- # '认购成交量': call_volume,
- '认购时间价值': call_time,
- '认购内在价值': call_intrinsic,
- '认购价格': call_price,
- '行权价': exercisePrice,
- '认沽价格': put_price,
- '认沽内在价值': put_intrinsic,
- '认沽时间价值': put_time,
- # '认沽成交量': put_volume,
- # '认沽持仓量': put_position,
- '认沽涨跌幅': put_change
- })
- # 创建T型报价DataFrame
- df = pd.DataFrame(data)
- # 确保df的列顺序为:'认购涨跌幅','认购持仓量','认购成交量','认购时间价值','认购内在价值','认购价格','行权价','认沽价格','认沽内在价值','认沽时间价值','认沽成交量','认沽持仓量','认沽涨跌幅'
- df = df[['认购涨跌幅', '认购时间价值', '认购内在价值', '认购价格', '行权价', '认沽价格', '认沽内在价值', '认沽时间价值', '认沽涨跌幅']]
- print("\n" + "="*100)
- print("期权T型报价表(含内在价值和时间价值)")
- print("="*100)
- print(f"标的期货价格: {current_future_price}")
- print("="*100)
- # 显示T型报价
- print(df.to_string(index=False))
|