meso.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. """
  2. 中观生态识别器
  3. 计算市场结构健康度评分(0-100)
  4. 基于五个维度:价格冲击、订单流平衡、流动性深度、波动率效率、信息冲击响应
  5. """
  6. from dataclasses import dataclass
  7. from typing import Dict, Optional
  8. from enum import Enum
  9. import numpy as np
  10. import pandas as pd
  11. class HealthLevel(Enum):
  12. """结构健康度分级"""
  13. HIGH = "high" # > 70
  14. MEDIUM = "medium" # 40-70
  15. LOW = "low" # < 40
  16. @dataclass
  17. class MesoEcosystem:
  18. """中观生态数据结构"""
  19. health_score: float # 总健康度评分 (0-100)
  20. health_level: HealthLevel
  21. price_impact: float # 价格冲击系数
  22. order_flow: float # 订单流平衡
  23. liquidity_depth: float # 流动性深度
  24. volatility_efficiency: float # 波动率效率
  25. info_response: float # 信息冲击响应
  26. components: Dict[str, float] # 各维度明细
  27. class MesoEcosystemIdentifier:
  28. """
  29. 中观生态识别器
  30. 计算市场微观结构健康度:
  31. 1. 价格冲击系数:大单对价格的影响程度
  32. 2. 订单流平衡:买卖盘力量对比
  33. 3. 流动性深度:买卖盘深度
  34. 4. 波动率效率:波动率与信息到达的关系
  35. 5. 信息冲击响应:价格对新信息的反应速度
  36. """
  37. def __init__(
  38. self,
  39. lookback_days: int = 60,
  40. weights: Optional[Dict[str, float]] = None,
  41. high_threshold: float = 70,
  42. medium_threshold: float = 40
  43. ):
  44. self.lookback_days = lookback_days
  45. self.weights = weights or {
  46. "price_impact": 0.30,
  47. "order_flow": 0.25,
  48. "liquidity_depth": 0.20,
  49. "volatility_efficiency": 0.15,
  50. "info_response": 0.10
  51. }
  52. self.high_threshold = high_threshold
  53. self.medium_threshold = medium_threshold
  54. def identify(
  55. self,
  56. price_data: pd.DataFrame,
  57. order_book_data: Optional[pd.DataFrame] = None,
  58. trade_data: Optional[pd.DataFrame] = None
  59. ) -> MesoEcosystem:
  60. """
  61. 识别中观生态结构健康度
  62. Args:
  63. price_data: 价格数据
  64. order_book_data: 订单簿数据(可选)
  65. trade_data: 成交数据(可选)
  66. Returns:
  67. MesoEcosystem: 中观生态识别结果
  68. """
  69. # 计算五个维度
  70. price_impact = self._calculate_price_impact(price_data, trade_data)
  71. order_flow = self._calculate_order_flow(price_data, order_book_data, trade_data)
  72. liquidity_depth = self._calculate_liquidity_depth(order_book_data, price_data)
  73. volatility_efficiency = self._calculate_volatility_efficiency(price_data)
  74. info_response = self._calculate_info_response(price_data)
  75. # 标准化到0-100分
  76. components = {
  77. "price_impact": self._normalize_price_impact(price_impact),
  78. "order_flow": self._normalize_order_flow(order_flow),
  79. "liquidity_depth": self._normalize_liquidity_depth(liquidity_depth),
  80. "volatility_efficiency": self._normalize_vol_efficiency(volatility_efficiency),
  81. "info_response": self._normalize_info_response(info_response)
  82. }
  83. # 计算加权总分
  84. health_score = sum(
  85. components[key] * self.weights[key]
  86. for key in components
  87. )
  88. # 确定健康等级
  89. if health_score >= self.high_threshold:
  90. health_level = HealthLevel.HIGH
  91. elif health_score >= self.medium_threshold:
  92. health_level = HealthLevel.MEDIUM
  93. else:
  94. health_level = HealthLevel.LOW
  95. return MesoEcosystem(
  96. health_score=health_score,
  97. health_level=health_level,
  98. price_impact=price_impact,
  99. order_flow=order_flow,
  100. liquidity_depth=liquidity_depth,
  101. volatility_efficiency=volatility_efficiency,
  102. info_response=info_response,
  103. components=components
  104. )
  105. def _calculate_price_impact(
  106. self,
  107. price_data: pd.DataFrame,
  108. trade_data: Optional[pd.DataFrame] = None
  109. ) -> float:
  110. """
  111. 计算价格冲击系数
  112. 基于Kyle的lambda:价格变化 / 交易量
  113. 值越小表示流动性越好
  114. """
  115. if trade_data is not None and not trade_data.empty:
  116. # 使用成交数据计算
  117. price_changes = price_data['close'].diff().abs()
  118. volumes = trade_data['volume'] if 'volume' in trade_data else price_data['volume']
  119. # 计算日内价格冲击
  120. impact = price_changes / np.sqrt(volumes)
  121. impact = impact.replace([np.inf, -np.inf], np.nan).dropna()
  122. if len(impact) > 0:
  123. return impact.iloc[-20:].mean()
  124. # 简化计算:使用价格波动率与成交量比率
  125. returns = price_data['close'].pct_change().abs()
  126. volume_ma = price_data['volume'].rolling(20).mean()
  127. impact = returns / np.log(volume_ma + 1)
  128. impact = impact.replace([np.inf, -np.inf], np.nan).dropna()
  129. return impact.iloc[-20:].mean() if len(impact) > 0 else 0.001
  130. def _calculate_order_flow(
  131. self,
  132. price_data: pd.DataFrame,
  133. order_book_data: Optional[pd.DataFrame] = None,
  134. trade_data: Optional[pd.DataFrame] = None
  135. ) -> float:
  136. """
  137. 计算订单流平衡
  138. 正值表示买盘占优,负值表示卖盘占优
  139. 使用买卖盘深度比或主动买卖比例
  140. """
  141. if order_book_data is not None and not order_book_data.empty:
  142. # 使用订单簿数据
  143. if 'bid_depth' in order_book_data and 'ask_depth' in order_book_data:
  144. bid_depth = order_book_data['bid_depth'].iloc[-1]
  145. ask_depth = order_book_data['ask_depth'].iloc[-1]
  146. if ask_depth > 0:
  147. return (bid_depth - ask_depth) / (bid_depth + ask_depth)
  148. # 使用价格-成交量关系估计
  149. # 上涨日成交量 vs 下跌日成交量
  150. returns = price_data['close'].pct_change()
  151. volumes = price_data['volume']
  152. up_volume = volumes[returns > 0].iloc[-20:].mean()
  153. down_volume = volumes[returns < 0].iloc[-20:].mean()
  154. if pd.isna(up_volume) or pd.isna(down_volume) or (up_volume + down_volume) == 0:
  155. return 0.0
  156. return (up_volume - down_volume) / (up_volume + down_volume)
  157. def _calculate_liquidity_depth(
  158. self,
  159. order_book_data: Optional[pd.DataFrame] = None,
  160. price_data: Optional[pd.DataFrame] = None
  161. ) -> float:
  162. """
  163. 计算流动性深度
  164. 单位价格变动所需的成交量
  165. """
  166. if order_book_data is not None and not order_book_data.empty:
  167. # 使用订单簿深度
  168. if 'bid_depth' in order_book_data:
  169. return order_book_data['bid_depth'].iloc[-20:].mean()
  170. # 使用Amihud非流动性指标倒数
  171. if price_data is not None:
  172. returns = price_data['close'].pct_change().abs()
  173. volumes = price_data['volume']
  174. illiquidity = returns / (volumes * price_data['close'])
  175. illiquidity = illiquidity.replace([np.inf, -np.inf], np.nan).dropna()
  176. if len(illiquidity) > 0:
  177. # 返回倒数,值越大流动性越好
  178. return 1.0 / (illiquidity.iloc[-20:].mean() + 1e-10)
  179. return 1.0
  180. def _calculate_volatility_efficiency(self, price_data: pd.DataFrame) -> float:
  181. """
  182. 计算波动率效率
  183. 实际波动率与信息到达率的比率
  184. 衡量波动率是否有效反映信息
  185. """
  186. # 计算已实现波动率
  187. returns = price_data['close'].pct_change().dropna()
  188. realized_vol = returns.iloc[-20:].std() * np.sqrt(252)
  189. # 计算信息到达率(使用成交量变化作为代理)
  190. volume_changes = price_data['volume'].pct_change().abs().iloc[-20:].mean()
  191. # 效率 = 波动率 / 信息到达率
  192. if volume_changes > 0:
  193. efficiency = realized_vol / (volume_changes + 0.01)
  194. else:
  195. efficiency = realized_vol
  196. return efficiency
  197. def _calculate_info_response(self, price_data: pd.DataFrame) -> float:
  198. """
  199. 计算信息冲击响应
  200. 价格对新信息的反应速度和程度
  201. 使用价格自相关性衡量
  202. """
  203. returns = price_data['close'].pct_change().dropna()
  204. if len(returns) < 20:
  205. return 0.5
  206. # 计算1阶和5阶自相关系数
  207. autocorr_1 = returns.iloc[-20:].autocorr(lag=1)
  208. autocorr_5 = returns.iloc[-20:].autocorr(lag=5)
  209. # 负自相关表示快速反转(信息快速被吸收)
  210. # 正自相关表示动量延续
  211. response = -(autocorr_1 + autocorr_5 / 5) / 2
  212. return response
  213. # 标准化方法
  214. def _normalize_price_impact(self, value: float) -> float:
  215. """标准化价格冲击系数 (0-100)"""
  216. # 值越小越好
  217. # 0.0001 = 优秀 (100分), 0.01 = 差 (0分)
  218. score = 100 * (1 - min(1, max(0, (value - 0.0001) / 0.0099)))
  219. return max(0, min(100, score))
  220. def _normalize_order_flow(self, value: float) -> float:
  221. """标准化订单流平衡 (0-100)"""
  222. # -1到1标准化到0-100,0为中性(50分)
  223. return 50 + value * 50
  224. def _normalize_liquidity_depth(self, value: float) -> float:
  225. """标准化流动性深度 (0-100)"""
  226. # 使用对数变换
  227. score = 50 + 50 * np.log(value + 1) / np.log(100)
  228. return max(0, min(100, score))
  229. def _normalize_vol_efficiency(self, value: float) -> float:
  230. """标准化波动率效率 (0-100)"""
  231. # 1-5之间为合理范围
  232. score = 100 - abs(value - 3) * 25
  233. return max(0, min(100, score))
  234. def _normalize_info_response(self, value: float) -> float:
  235. """标准化信息冲击响应 (0-100)"""
  236. # -0.5到0.5标准化到0-100
  237. score = 50 + value * 100
  238. return max(0, min(100, score))