Route business quotes and K-lines to CTP; keep Sina only for market chart page.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-02 15:14:13 +08:00
parent 98d63f38bf
commit 972ab5d08b
9 changed files with 52 additions and 101 deletions
+7 -54
View File
@@ -9,16 +9,13 @@ from __future__ import annotations
import logging
from typing import Callable, Optional
import requests
from modules.market.kline_chart import fetch_sina_klines, ths_to_sina_chart_symbol
from modules.market.kline_chart import fetch_market_klines
logger = logging.getLogger(__name__)
DAILY_LOOKBACK = 7
OVERLAP_WINDOW = 3
OVERLAP_RANGE_THRESHOLD = 0.70
KLINE_FETCH_TIMEOUT = 5
TREND_LONG = "long"
TREND_SHORT = "short"
@@ -178,48 +175,13 @@ def analyze_daily_trend(bars: list, *, overlap_threshold: float = OVERLAP_RANGE_
}
def _normalize_daily_bars(raw: list) -> list:
out = []
for row in raw:
if isinstance(row, list) and len(row) >= 5:
out.append({
"d": str(row[0]),
"o": float(row[1]),
"h": float(row[2]),
"l": float(row[3]),
"c": float(row[4]),
"v": float(row[5]) if len(row) > 5 and row[5] else 0.0,
})
elif isinstance(row, dict) and row.get("d"):
out.append({
"d": str(row["d"]),
"o": float(row.get("o", 0) or 0),
"h": float(row.get("h", 0) or 0),
"l": float(row.get("l", 0) or 0),
"c": float(row.get("c", 0) or 0),
"v": float(row.get("v", 0) or 0),
})
return out
def _fetch_sina_daily_quick(chart_sym: str) -> list:
url = (
"https://stock2.finance.sina.com.cn/futures/api/json.php/"
f"IndexService.getInnerFuturesDailyKLine?symbol={chart_sym}"
)
def _fetch_ctp_daily_bars(sym: str) -> list:
try:
resp = requests.get(
url, timeout=KLINE_FETCH_TIMEOUT,
headers={"Referer": "https://finance.sina.com.cn"},
)
raw = resp.json()
if raw and isinstance(raw, list):
bars = _normalize_daily_bars(raw)
if bars:
return bars
data = fetch_market_klines(sym, "d", prefer_ctp=True)
return data.get("bars") or []
except Exception as exc:
logger.debug("quick daily kline failed %s: %s", chart_sym, exc)
return []
logger.debug("ctp daily kline failed %s: %s", sym, exc)
return []
def fetch_week_daily_bars(
@@ -238,16 +200,7 @@ def fetch_week_daily_bars(
return []
return bars[-DAILY_LOOKBACK:] if bars else []
chart_sym = ths_to_sina_chart_symbol(sym)
if not chart_sym:
return []
bars = _fetch_sina_daily_quick(chart_sym)
if not bars:
try:
bars = fetch_sina_klines(sym, "d") or []
except Exception as exc:
logger.debug("fetch week daily fallback failed %s: %s", sym, exc)
return []
bars = _fetch_ctp_daily_bars(sym)
return bars[-DAILY_LOOKBACK:] if bars else []