Fix empty recommend list when CTP ticks unavailable off-hours.

Fall back to daily prev_close for margin estimates; keep previous cache when refresh gets all no_price; stop re-fetching only for missing turnover.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-03 06:43:20 +08:00
parent 125cc60a3d
commit 16d90814a1
2 changed files with 40 additions and 4 deletions
+23 -2
View File
@@ -99,8 +99,6 @@ def recommend_cache_needs_refresh(
return True
if rows_missing_category(rows):
return True
if rows_missing_turnover(rows):
return True
if rows_missing_contract_spec(rows):
return True
if float(capital or 0) > 0 and not rows:
@@ -294,6 +292,29 @@ def refresh_recommend_cache(
margin_used=used,
)
rows = filter_affordable_recommendations(all_rows)
prev_rows: list[dict] = []
try:
prev_row = conn.execute(
"SELECT rows_json FROM product_recommend_cache WHERE id=1",
).fetchone()
if prev_row:
prev_rows = json.loads(prev_row["rows_json"] or "[]")
if not isinstance(prev_rows, list):
prev_rows = []
except Exception:
prev_rows = []
if not rows and prev_rows:
no_price = sum(1 for r in all_rows if r.get("status") == "no_price")
if not all_rows or no_price >= max(1, len(all_rows) // 2):
logger.warning(
"recommend refresh: keeping previous %d rows "
"(affordable=0, no_price=%d/%d, capital=%.2f)",
len(prev_rows),
no_price,
len(all_rows),
float(capital or 0),
)
rows = prev_rows
if not rows and float(capital or 0) > 0:
logger.warning(
"recommend refresh: 0 affordable rows capital=%.2f total=%d no_price=%d blocked=%d",