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
+32 -26
View File
@@ -63,7 +63,12 @@ from stats_engine import (
from kline_store import ensure_kline_tables
from kline_stream import kline_hub, sse_format
from kline_chart import generate_review_kline_chart, fetch_market_klines, MARKET_PERIODS
from market import get_price as market_get_price, set_ths_refresh_token, get_quote_source_label
from market import (
fetch_raw_for_volume,
get_price as market_get_price,
set_ths_refresh_token,
get_quote_source_label,
)
from db_conn import OperationalError, connect_db, database_label, is_benign_migration_error, is_db_contention_error, is_schema_migration_error, rollback_if_postgres
from admin_settings import save_admin_credentials
from db_backup import (
@@ -583,10 +588,19 @@ def build_market_quote_payload(
if codes:
market_code = codes.get("market_code", "") or market_code
sina_code = codes.get("sina_code", "") or sina_code
quote_source = "sina"
quote_source = "none"
price = None
prev_close = None
if not prefer_sina:
if prefer_sina:
mc, sc = resolve_market_codes(symbol, market_code, sina_code)
if mc or sc:
price = market_get_price(mc, sc)
quote_source = "sina"
if prev_close is None and sc:
raw = fetch_raw_for_volume(sc)
if raw and raw.get("prev_close") is not None:
prev_close = raw["prev_close"]
else:
try:
from vnpy_bridge import ctp_status, ctp_get_tick_detail
from trading_context import get_trading_mode
@@ -601,17 +615,10 @@ def build_market_quote_payload(
prev_close = detail["pre_close"]
except Exception:
pass
if price is None:
price = fetch_price(symbol, market_code, sina_code)
name = symbol
codes = ths_to_codes(symbol)
if codes:
name = codes.get("name", symbol)
if prev_close is None and sina_code:
from market import fetch_raw_for_volume
raw = fetch_raw_for_volume(sina_code)
if raw and raw.get("prev_close") is not None:
prev_close = raw["prev_close"]
return {
"symbol": symbol,
"name": name,
@@ -651,23 +658,22 @@ def resolve_market_codes(ths_code: str, market_code: str = "", sina_code: str =
def fetch_price(ths_code: str, market_code: str = "", sina_code: str = "") -> Optional[float]:
"""业务现价:仅 CTP 柜台 tick,不回退新浪。"""
sym = (ths_code or "").strip()
if sym:
try:
from vnpy_bridge import ctp_status, ctp_get_tick_price
from trading_context import get_trading_mode
mode = get_trading_mode(get_setting)
if ctp_status(mode).get("connected"):
p = ctp_get_tick_price(mode, sym)
if p and p > 0:
return p
except Exception:
pass
mc, sc = resolve_market_codes(sym, market_code, sina_code)
if not mc and not sc:
if not sym:
return None
return market_get_price(mc, sc)
try:
from vnpy_bridge import ctp_status, ctp_get_tick_price
from trading_context import get_trading_mode
mode = get_trading_mode(get_setting)
if ctp_status(mode).get("connected"):
p = ctp_get_tick_price(mode, sym)
if p and p > 0:
return p
except Exception:
pass
return None
# —————————————— 监控逻辑 ——————————————
@@ -799,7 +805,7 @@ def start_background_threads():
target=lambda: kline_hub.worker_loop(
DB_PATH,
lambda sym, mc, sc: build_market_quote_payload(
sym, mc, sc, prefer_sina=True,
sym, mc, sc, prefer_sina=False,
),
get_mode_fn=lambda: get_trading_mode(get_setting),
),