K线本地缓存、图表交互优化与交易记录表格修复
新增 kline_store 优先读本地库;修复加载中遮挡、支持缩放与交易时段刷新;修复交易记录操作列被裁切。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+52
-2
@@ -3,6 +3,7 @@ import json
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import sqlite3
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
from zoneinfo import ZoneInfo
|
||||
@@ -10,6 +11,7 @@ from zoneinfo import ZoneInfo
|
||||
import requests
|
||||
|
||||
from symbols import ths_to_codes
|
||||
from kline_store import ensure_kline_tables, get_cached_entry, save_bars
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
TZ = ZoneInfo("Asia/Shanghai")
|
||||
@@ -213,14 +215,60 @@ def bars_to_api(bars: list) -> list[dict]:
|
||||
return result
|
||||
|
||||
|
||||
def fetch_market_klines(symbol: str, period: str) -> dict:
|
||||
def fetch_market_klines(symbol: str, period: str, db_path: Optional[str] = None) -> dict:
|
||||
chart_sym = ths_to_sina_chart_symbol(symbol)
|
||||
p = (period or "15m").lower()
|
||||
if p == "timeshare":
|
||||
chart_type = "line"
|
||||
else:
|
||||
chart_type = "candle"
|
||||
bars = fetch_sina_klines(symbol, p)
|
||||
|
||||
bars: list = []
|
||||
source = "remote"
|
||||
cached_at = None
|
||||
|
||||
if db_path and chart_sym:
|
||||
try:
|
||||
conn = sqlite3.connect(db_path)
|
||||
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:
|
||||
remote_bars = fetch_sina_klines(symbol, p)
|
||||
if remote_bars:
|
||||
bars = remote_bars
|
||||
source = "remote"
|
||||
if db_path and chart_sym:
|
||||
try:
|
||||
conn = sqlite3.connect(db_path)
|
||||
ensure_kline_tables(conn)
|
||||
save_bars(conn, chart_sym, p, remote_bars)
|
||||
meta = conn.execute(
|
||||
"SELECT updated_at FROM kline_meta WHERE chart_symbol=? AND period=?",
|
||||
(chart_sym, p),
|
||||
).fetchone()
|
||||
conn.close()
|
||||
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 db_path and chart_sym:
|
||||
try:
|
||||
conn = sqlite3.connect(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)
|
||||
|
||||
return {
|
||||
"symbol": symbol,
|
||||
"chart_symbol": chart_sym,
|
||||
@@ -228,6 +276,8 @@ def fetch_market_klines(symbol: str, period: str) -> dict:
|
||||
"chart_type": chart_type,
|
||||
"count": len(bars),
|
||||
"bars": bars_to_api(bars),
|
||||
"source": source,
|
||||
"cached_at": cached_at,
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user