Qionk/a-share-quant
0
1"""2A股涨跌停限制检测与预测修正3"""4 5import numpy as np6 7 8def detect_price_limit_pct(stock_code: str, stock_name: str = "") -> float:9 """10 根据股票代码和名称检测涨跌停比例。11 返回: 涨跌幅限制百分比(如 0.10 = 10%),无法识别返回 None12 """13 code = str(stock_code).strip()14 15 # ST / *ST 股票 (5%)16 if stock_name and ("ST" in stock_name.upper()):17 return 0.0518 19 if code.startswith("688"):20 return 0.20 # 科创板 STAR Market21 if code.startswith("300") or code.startswith("301") or code.startswith("302"):22 return 0.20 # 创业板 ChiNext/GEM23 if code.startswith("60") or code.startswith("00"):24 return 0.10 # 沪深主板25 if code.startswith("83") or code.startswith("87") or code.startswith("43"):26 return 0.30 # 北交所/新三板 BSE/NEEQ27 28 return None29 30 31def get_board_name(stock_code: str) -> str:32 """获取板块名称(含涨跌幅信息)"""33 code = str(stock_code).strip()34 if code.startswith("688"):35 return "科创板 (±20%)"36 if code.startswith("300") or code.startswith("301") or code.startswith("302"):37 return "创业板/GEM (±20%)"38 if code.startswith("60"):39 return "沪市主板 (±10%)"40 if code.startswith("00"):41 return "深市主板 (±10%)"42 if code.startswith("83") or code.startswith("87") or code.startswith("43"):43 return "北交所/新三板 (±30%)"44 return "未知板块"45 46 47def apply_price_limits(predictions, last_close: float, limit_pct: float):48 """49 将预测价格裁剪到涨跌停范围内。50 predictions: ensemble_predict() 返回的 dict51 返回: 裁剪后的 dict52 """53 if limit_pct is None:54 return predictions55 56 upper = last_close * (1 + limit_pct)57 lower = last_close * (1 - limit_pct)58 59 result = predictions.copy()60 for key in ["predicted_close", "confidence_lower", "confidence_upper"]:61 if key in result and result[key] is not None and len(result[key]) > 0:62 arr = result[key] if isinstance(result[key], np.ndarray) else np.array(result[key])63 result[key] = np.clip(arr, lower, upper)64 65 if "model_predictions" in result and result["model_predictions"]:66 for k, v in result["model_predictions"].items():67 arr = v if isinstance(v, np.ndarray) else np.array(v)68 result["model_predictions"][k] = np.clip(arr, lower, upper)69 70 return result