Fix positions page hang by moving recommend refresh to background
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+66
-8
@@ -4,13 +4,16 @@ from __future__ import annotations
|
||||
import logging
|
||||
from typing import Callable, Optional
|
||||
|
||||
from kline_chart import fetch_sina_klines
|
||||
import requests
|
||||
|
||||
from kline_chart import fetch_sina_klines, ths_to_sina_chart_symbol
|
||||
|
||||
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"
|
||||
@@ -107,20 +110,75 @@ 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]),
|
||||
})
|
||||
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),
|
||||
})
|
||||
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}"
|
||||
)
|
||||
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
|
||||
except Exception as exc:
|
||||
logger.debug("quick daily kline failed %s: %s", chart_sym, exc)
|
||||
return []
|
||||
|
||||
|
||||
def fetch_week_daily_bars(
|
||||
symbol: str,
|
||||
*,
|
||||
fetch_fn: Callable[[str, str], list] | None = None,
|
||||
) -> list:
|
||||
fn = fetch_fn or fetch_sina_klines
|
||||
try:
|
||||
bars = fn(symbol, "d") or []
|
||||
except Exception as exc:
|
||||
logger.debug("fetch week daily failed %s: %s", symbol, exc)
|
||||
sym = (symbol or "").strip()
|
||||
if not sym:
|
||||
return []
|
||||
if fetch_fn:
|
||||
try:
|
||||
bars = fetch_fn(sym, "d") or []
|
||||
except Exception as exc:
|
||||
logger.debug("fetch week daily failed %s: %s", sym, exc)
|
||||
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:
|
||||
return []
|
||||
return bars[-DAILY_LOOKBACK:]
|
||||
try:
|
||||
bars = fetch_sina_klines(sym, "d") or []
|
||||
except Exception as exc:
|
||||
logger.debug("fetch week daily fallback failed %s: %s", sym, exc)
|
||||
return []
|
||||
return bars[-DAILY_LOOKBACK:] if bars else []
|
||||
|
||||
|
||||
def analyze_product_trend(
|
||||
|
||||
Reference in New Issue
Block a user