行情区图表:成交量、十字线 OHLCV、可视高低点、日线满 500 根

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-02 11:12:10 +08:00
parent ba681c7a58
commit 113d8c1669
7 changed files with 223 additions and 84 deletions
+6 -9
View File
@@ -11,6 +11,7 @@ from typing import Any, Callable, Optional
from hub_ohlcv_lib import (
TIMEFRAME_MS,
bar_limit_for_timeframe,
chart_fetch_start_ms,
format_price_by_tick,
last_closed_bar_open_ms,
normalize_chart_timeframe,
@@ -233,14 +234,15 @@ def resolve_chart_bars(
need = bar_limit_for_timeframe(tf)
now_ms = int(time.time() * 1000)
start_ms = window_start_ms(tf, need, retention_days(), now_ms)
fetch_start_ms = chart_fetch_start_ms(tf, need, now_ms)
db_read_start_ms = window_start_ms(tf, need, retention_days(), now_ms)
last_closed = last_closed_bar_open_ms(tf, now_ms)
db_rows: list[dict[str, Any]] = []
if not force_refresh:
period_ms = TIMEFRAME_MS[tf]
db_rows = load_bars_range(
ex_k, sym, tf, max(0, start_ms - period_ms), now_ms + period_ms, db_path
ex_k, sym, tf, max(0, db_read_start_ms - period_ms), now_ms + period_ms, db_path
)
newest_db = db_rows[-1]["open_time_ms"] if db_rows else None
@@ -253,7 +255,7 @@ def resolve_chart_bars(
remote_err: Optional[str] = None
if need_fetch:
since = start_ms
since = fetch_start_ms
if db_rows and not force_refresh:
since = min(since, int(db_rows[0]["open_time_ms"]))
remote = remote_fetch(
@@ -265,7 +267,7 @@ def resolve_chart_bars(
if remote.get("ok") and remote.get("bars"):
fetched = upsert_bars(ex_k, sym, tf, remote["bars"], db_path)
price_tick = remote.get("price_tick")
db_rows = load_bars_range(ex_k, sym, tf, start_ms, now_ms, db_path)
db_rows = load_bars_range(ex_k, sym, tf, fetch_start_ms, now_ms, db_path)
else:
remote_err = remote.get("msg") or remote.get("error") or "实例拉取 K 线失败"
if not db_rows:
@@ -282,9 +284,6 @@ def resolve_chart_bars(
if fetched:
from_cache = max(0, len(candles) - min(fetched, len(candles)))
hi = max(candles, key=lambda x: x["high"])
lo = min(candles, key=lambda x: x["low"])
return {
"ok": True,
"symbol": sym,
@@ -297,8 +296,6 @@ def resolve_chart_bars(
"fetched": fetched,
"purged": purged,
"price_tick": price_tick,
"range_high": {"time": hi["time"], "price": hi["high"]},
"range_low": {"time": lo["time"], "price": lo["low"]},
"stale": bool(remote_err),
"stale_message": remote_err if remote_err else None,
"updated_at": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),