增加k线图

This commit is contained in:
dekun
2026-05-22 13:47:27 +08:00
parent ee621976db
commit 74f98af40d
13 changed files with 543 additions and 8 deletions
+35
View File
@@ -8,7 +8,9 @@ from fastapi.staticfiles import StaticFiles
from .aggregator import aggregate_period, enrich_snapshot_meta
from .config import ROOT_DIR, settings
from .kline_store import get_daily_candles, sync_daily_klines
from .db import get_latest_snapshot, init_db, log_push
from .exceptions import BinanceRateLimitedError
from .periods import get_today_period, get_yesterday_period
from .scheduler import job_finalize_yesterday, job_push_wecom, job_refresh_today, start_scheduler, startup_tasks, stop_scheduler
from .state import get_today_cache
@@ -128,3 +130,36 @@ async def api_refresh_yesterday():
async def api_refresh_today():
await job_refresh_today()
return get_today_cache() or {"message": "done"}
@app.get("/api/chart/{symbol}/daily")
async def api_chart_daily(symbol: str, limit: int | None = None, refresh: bool = False):
"""合约日 K 线:优先读本地 SQLite,过期再拉币安入库。"""
sym = symbol.upper().strip()
if not sym.endswith("USDT"):
raise HTTPException(400, "invalid symbol")
try:
candles, source = await get_daily_candles(sym, limit, force_refresh=refresh)
return {
"symbol": sym,
"interval": "1d",
"limit": len(candles),
"candles": candles,
"source": source,
}
except BinanceRateLimitedError as e:
raise HTTPException(503, f"币安限流,请 {e.retry_after_sec} 秒后再试") from e
except Exception as e:
logger.error("chart %s failed: %s", sym, e)
raise HTTPException(502, "K线获取失败") from e
@app.post("/api/chart/{symbol}/daily/refresh")
async def api_chart_daily_refresh(symbol: str, limit: int | None = None):
"""强制从币安同步日 K 到本地库。"""
sym = symbol.upper().strip()
try:
candles = await sync_daily_klines(sym, limit)
return {"symbol": sym, "saved": len(candles), "source": "binance"}
except BinanceRateLimitedError as e:
raise HTTPException(503, f"币安限流,请 {e.retry_after_sec} 秒后再试") from e