product_T_price.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import pandas as pd
  2. import numpy as np
  3. import talib as tb
  4. from pandas.plotting import table
  5. from jqdata import *
  6. # 期权合约信息
  7. class OptionContract:
  8. close: 0 #现价
  9. volume: 0 #成交量
  10. position: 0 #当前持仓量
  11. priceChangePct: 0 #收盘价涨跌幅
  12. intrinsic_value: 0 #内在价值
  13. time_value: 0 #时间价值
  14. def __init__(self,close, volume, position, priceChangePct, intrinsic_value=0, time_value=0):
  15. self.close = close
  16. self.volume = volume
  17. self.position = position
  18. self.priceChangePct = priceChangePct
  19. self.intrinsic_value = intrinsic_value
  20. self.time_value = time_value
  21. # 同一行权价的一组期权
  22. class OptionGroup:
  23. exercisePrice: 0 ##行权价
  24. call: None ##提醒的品种
  25. put: None ##提醒的类型
  26. def __init__(self, exercisePrice, call, put):
  27. self.exercisePrice = exercisePrice
  28. self.call = call
  29. self.put = put
  30. def __init__(self, exercisePrice):
  31. self.exercisePrice = exercisePrice
  32. #数据内容
  33. CONTENT = [
  34. 'close',
  35. 'volume',
  36. 'position',
  37. 'priceChangePct',
  38. ]
  39. ##数据域
  40. CONTENT_HEADER_MAP = {
  41. 'prePosition': '昨日持仓',
  42. 'close': '现价',
  43. 'volume': '成交量',
  44. 'position': '持仓量',
  45. 'priceChangePct': '涨跌幅'
  46. }
  47. # 豆粕期权
  48. symbol = 'AU'
  49. starttime = '2025-06-30'
  50. endtime = '2025-07-14'
  51. SUBJECT_MATTER = get_dominant_future(symbol,date = starttime)
  52. print(f"SUBJECT_MATTER: {SUBJECT_MATTER}")
  53. #m2407 匹配 2024-04-19
  54. EXPIRE_MONTH = '2025-09-24'
  55. ## 行权价间隔 比如3800 3750 每50元一档
  56. gap = 8
  57. # 获取期货价格
  58. try:
  59. # 获取期货价格数据
  60. future_price_data = get_price(SUBJECT_MATTER, end_date=endtime, count=1, fields=['close'])
  61. current_future_price = float(future_price_data['close'].iloc[-1])
  62. except Exception as e:
  63. print(f"获取期货价格失败: {e}")
  64. current_future_price = 781.4 # 使用模拟价格
  65. print(f"标的期货价格: {current_future_price}")
  66. #查询相关的合约,适用于商品
  67. qy = query(opt.OPT_CONTRACT_INFO).filter(
  68. opt.OPT_CONTRACT_INFO.underlying_symbol == SUBJECT_MATTER, ##期权标的物
  69. opt.OPT_CONTRACT_INFO.expire_date == EXPIRE_MONTH,##期权到期日
  70. ).order_by(opt.OPT_CONTRACT_INFO.exercise_price, opt.OPT_CONTRACT_INFO.contract_type)
  71. optList = opt.run_query(qy)
  72. optList
  73. code = SUBJECT_MATTER.split('.')[0]
  74. print(f"code: {code}")
  75. # optList.to_csv(f'price_list_{code}.csv')
  76. # 获取实时数据
  77. optionGroups = {}
  78. for index, row in optList.iterrows():
  79. code = row['code'] #key - 期权代码
  80. # print(f"检查{code}的数据")
  81. #查询具体合约的信息,商品需要去掉开盘前的静态信息
  82. realTimeQuery = query(opt.OPT_DAILY_PRICE).filter(
  83. opt.OPT_DAILY_PRICE.code==code,
  84. opt.OPT_DAILY_PRICE.date==endtime
  85. ).order_by(opt.OPT_DAILY_PRICE.date.desc()).limit(1)
  86. realTimeData = opt.run_query(realTimeQuery)
  87. # print(f"realTimeQuery: {realTimeQuery}")
  88. # print(f"realTimeData: {realTimeData}")
  89. #期权基本信息
  90. exercisePrice = row['exercise_price'] #行权价
  91. contractType = row['contract_type'] #合约类型。CO-认购期权,PO-认沽期权
  92. #实时表查询
  93. close = realTimeData.loc[0].at['close'] #现价
  94. volume = int(realTimeData.loc[0].at['volume']) #成交量
  95. position = realTimeData.loc[0].at['position'] #当前持仓量
  96. priceChangePct = str(round(realTimeData.loc[0].at['change_pct_close'], 2)) + '%' #收盘价涨跌幅
  97. #去除非标准的带A合约
  98. if(exercisePrice % gap != 0):
  99. continue
  100. # 计算内在价值和时间价值
  101. if contractType == 'CO': # 认购期权
  102. intrinsic_value = max(current_future_price - exercisePrice, 0)
  103. time_value = close - intrinsic_value
  104. print(f"处理认购期权, 期货价格: {current_future_price}, 行权价:{exercisePrice}")
  105. print(f"获得内在价值: {intrinsic_value}, 时间价值: {time_value}")
  106. else: # 认沽期权
  107. intrinsic_value = max(exercisePrice - current_future_price, 0)
  108. time_value = close - intrinsic_value
  109. print(f"处理认沽期权, 期货价格: {current_future_price}, 行权价:{exercisePrice}")
  110. print(f"获得内在价值: {intrinsic_value}, 时间价值: {time_value}")
  111. optionContract = OptionContract(close, volume, position, priceChangePct, intrinsic_value, time_value)
  112. if(exercisePrice in optionGroups):
  113. if(contractType == 'CO'):
  114. optionGroups[exercisePrice].call = optionContract
  115. else:
  116. optionGroups[exercisePrice].put = optionContract
  117. else:
  118. optionGroup = OptionGroup(exercisePrice)
  119. if(contractType == 'CO'):
  120. optionGroup.call = optionContract
  121. else:
  122. optionGroup.put = optionContract
  123. optionGroups[exercisePrice] = optionGroup
  124. # 使用optionGroups构造T型报价表
  125. data = []
  126. for exercisePrice in sorted(optionGroups.keys()):
  127. group = optionGroups[exercisePrice]
  128. # print(f"exercisePrice: {exercisePrice}, group: {group}")
  129. # 认购期权数据
  130. call_price = group.call.close if group.call else None
  131. call_volume = group.call.volume if group.call else None
  132. call_position = group.call.position if group.call else None
  133. call_change = group.call.priceChangePct if group.call else None
  134. call_intrinsic = group.call.intrinsic_value if group.call else None
  135. # print(f"exercisePrice: {exercisePrice}, call_intrinsic: {call_intrinsic}")
  136. call_time = group.call.time_value if group.call else None
  137. # 认沽期权数据
  138. put_price = group.put.close if group.put else None
  139. put_volume = group.put.volume if group.put else None
  140. put_position = group.put.position if group.put else None
  141. put_change = group.put.priceChangePct if group.put else None
  142. put_intrinsic = group.put.intrinsic_value if group.put else None
  143. put_time = group.put.time_value if group.put else None
  144. data.append({
  145. '认购涨跌幅': call_change,
  146. # '认购持仓量': call_position,
  147. # '认购成交量': call_volume,
  148. '认购时间价值': call_time,
  149. '认购内在价值': call_intrinsic,
  150. '认购价格': call_price,
  151. '行权价': exercisePrice,
  152. '认沽价格': put_price,
  153. '认沽内在价值': put_intrinsic,
  154. '认沽时间价值': put_time,
  155. # '认沽成交量': put_volume,
  156. # '认沽持仓量': put_position,
  157. '认沽涨跌幅': put_change
  158. })
  159. # 创建T型报价DataFrame
  160. df = pd.DataFrame(data)
  161. # 确保df的列顺序为:'认购涨跌幅','认购持仓量','认购成交量','认购时间价值','认购内在价值','认购价格','行权价','认沽价格','认沽内在价值','认沽时间价值','认沽成交量','认沽持仓量','认沽涨跌幅'
  162. df = df[['认购涨跌幅', '认购时间价值', '认购内在价值', '认购价格', '行权价', '认沽价格', '认沽内在价值', '认沽时间价值', '认沽涨跌幅']]
  163. print("\n" + "="*100)
  164. print("期权T型报价表(含内在价值和时间价值)")
  165. print("="*100)
  166. print(f"标的期货价格: {current_future_price}")
  167. print("="*100)
  168. # 显示T型报价
  169. print(df.to_string(index=False))