Add separate kline.db and pre-seed small-account four-product K-lines on startup.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -17,7 +17,12 @@ import requests
|
||||
|
||||
from modules.core.symbols import ths_to_codes
|
||||
from modules.core.db_conn import connect_db
|
||||
from modules.market.kline_store import ensure_kline_tables, get_cached_entry, save_bars
|
||||
from modules.market.kline_store import (
|
||||
connect_kline_db,
|
||||
ensure_kline_tables,
|
||||
get_cached_entry,
|
||||
save_bars,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
TZ = ZoneInfo("Asia/Shanghai")
|
||||
@@ -261,6 +266,14 @@ def bars_to_api(bars: list) -> list[dict]:
|
||||
return result
|
||||
|
||||
|
||||
def _resolve_kline_db_path(db_path: Optional[str]) -> str:
|
||||
if db_path:
|
||||
return db_path
|
||||
from modules.core.paths import KLINE_DB_PATH
|
||||
|
||||
return KLINE_DB_PATH
|
||||
|
||||
|
||||
def fetch_market_klines(
|
||||
symbol: str,
|
||||
period: str,
|
||||
@@ -270,6 +283,7 @@ def fetch_market_klines(
|
||||
trading_mode: Optional[str] = None,
|
||||
prefer_ctp: bool = False,
|
||||
) -> dict:
|
||||
db_path = _resolve_kline_db_path(db_path)
|
||||
chart_sym = ths_to_sina_chart_symbol(symbol)
|
||||
p = (period or "15m").lower()
|
||||
if p == "timeshare":
|
||||
@@ -308,18 +322,21 @@ def fetch_market_klines(
|
||||
bars = ctp_bars
|
||||
source = "ctp"
|
||||
|
||||
if not bars and db_path and chart_sym and not force_remote and need_sina:
|
||||
local_cached: Optional[dict] = None
|
||||
if db_path and chart_sym and not force_remote:
|
||||
try:
|
||||
conn = connect_db(db_path)
|
||||
cached = get_cached_entry(conn, chart_sym, p)
|
||||
conn = connect_kline_db(db_path)
|
||||
local_cached = get_cached_entry(conn, chart_sym, p)
|
||||
conn.close()
|
||||
if cached and cached.get("fresh"):
|
||||
bars = cached["bars"]
|
||||
source = "local"
|
||||
cached_at = cached.get("updated_at")
|
||||
except Exception as exc:
|
||||
logger.warning("kline cache read failed %s %s: %s", chart_sym, p, exc)
|
||||
|
||||
if not bars and local_cached and local_cached.get("bars") and need_sina:
|
||||
if local_cached.get("fresh"):
|
||||
bars = local_cached["bars"]
|
||||
source = "local"
|
||||
cached_at = local_cached.get("updated_at")
|
||||
|
||||
if need_sina and (not bars or len(ctp_bars) < MIN_CTP_KLINE_BARS or not prefer_ctp):
|
||||
remote_bars = fetch_sina_klines(symbol, p)
|
||||
if remote_bars:
|
||||
@@ -331,7 +348,7 @@ def fetch_market_klines(
|
||||
source = "remote"
|
||||
if db_path and chart_sym and not ctp_connected:
|
||||
try:
|
||||
conn = connect_db(db_path)
|
||||
conn = connect_kline_db(db_path)
|
||||
ensure_kline_tables(conn)
|
||||
save_bars(conn, chart_sym, p, remote_bars)
|
||||
meta = conn.execute(
|
||||
@@ -342,17 +359,10 @@ def fetch_market_klines(
|
||||
cached_at = meta[0] if meta else None
|
||||
except Exception as exc:
|
||||
logger.warning("kline cache write failed %s %s: %s", chart_sym, p, exc)
|
||||
elif not bars and db_path and chart_sym:
|
||||
try:
|
||||
conn = connect_db(db_path)
|
||||
cached = get_cached_entry(conn, chart_sym, p)
|
||||
conn.close()
|
||||
if cached and cached.get("bars"):
|
||||
bars = cached["bars"]
|
||||
source = "local"
|
||||
cached_at = cached.get("updated_at")
|
||||
except Exception as exc:
|
||||
logger.warning("kline cache fallback failed %s %s: %s", chart_sym, p, exc)
|
||||
elif not bars and local_cached and local_cached.get("bars"):
|
||||
bars = local_cached["bars"]
|
||||
source = "local_seed"
|
||||
cached_at = local_cached.get("updated_at")
|
||||
|
||||
api_bars = bars_to_api(bars)
|
||||
prev_close = None
|
||||
|
||||
Reference in New Issue
Block a user