增加K线

This commit is contained in:
dekun
2026-05-30 10:35:25 +08:00
parent 74bd241579
commit 53959a9008
8 changed files with 683 additions and 134 deletions
+56 -2
View File
@@ -8,7 +8,8 @@ from fastapi.staticfiles import StaticFiles
from .config import ROOT_DIR, settings
from .funding_store import get_funding_bundle
from .kline_store import get_daily_candles, sync_daily_klines
from .kline_store import get_candles, get_daily_candles, sync_daily_klines, sync_klines
from .chart_intervals import CHART_INTERVALS, limit_for_interval, validate_interval
from .db import get_latest_snapshot, init_db, log_push, save_snapshot
from .exceptions import BinanceRateLimitedError
from .period_api import get_period_top30
@@ -144,9 +145,44 @@ async def api_refresh_daybefore():
return get_latest_snapshot("daybefore") or {"message": "done"}
@app.get("/api/chart/{symbol}")
async def api_chart(
symbol: str,
interval: str = "1d",
limit: int | None = None,
refresh: bool = False,
):
"""合约 K 线:优先读本地 SQLite,过期再拉币安入库。"""
sym = symbol.upper().strip()
if not sym.endswith("USDT"):
raise HTTPException(400, "invalid symbol")
try:
iv = validate_interval(interval)
except ValueError as e:
raise HTTPException(400, str(e)) from e
default_limit = limit_for_interval(iv)
try:
candles, source = await get_candles(
sym, iv, limit or default_limit, force_refresh=refresh
)
return {
"symbol": sym,
"interval": iv,
"limit": len(candles),
"candles": candles,
"source": source,
"intervals": list(CHART_INTERVALS),
}
except BinanceRateLimitedError as e:
raise HTTPException(503, f"币安限流,请 {e.retry_after_sec} 秒后再试") from e
except Exception as e:
logger.error("chart %s %s failed: %s", sym, iv, e)
raise HTTPException(502, "K线获取失败") from e
@app.get("/api/chart/{symbol}/daily")
async def api_chart_daily(symbol: str, limit: int | None = None, refresh: bool = False):
"""合约日 K 线:优先读本地 SQLite,过期再拉币安入库"""
"""合约日 K 线(兼容旧路径)"""
sym = symbol.upper().strip()
if not sym.endswith("USDT"):
raise HTTPException(400, "invalid symbol")
@@ -158,6 +194,7 @@ async def api_chart_daily(symbol: str, limit: int | None = None, refresh: bool =
"limit": len(candles),
"candles": candles,
"source": source,
"intervals": list(CHART_INTERVALS),
}
except BinanceRateLimitedError as e:
raise HTTPException(503, f"币安限流,请 {e.retry_after_sec} 秒后再试") from e
@@ -180,6 +217,23 @@ async def api_funding_history(symbol: str, limit: int | None = None, refresh: bo
raise HTTPException(502, "资金费率获取失败") from e
@app.post("/api/chart/{symbol}/refresh")
async def api_chart_refresh(
symbol: str, interval: str = "1d", limit: int | None = None
):
"""强制从币安同步 K 线到本地库。"""
sym = symbol.upper().strip()
try:
iv = validate_interval(interval)
except ValueError as e:
raise HTTPException(400, str(e)) from e
try:
candles = await sync_klines(sym, iv, limit)
return {"symbol": sym, "interval": iv, "saved": len(candles), "source": "binance"}
except BinanceRateLimitedError as e:
raise HTTPException(503, f"币安限流,请 {e.retry_after_sec} 秒后再试") from e
@app.post("/api/chart/{symbol}/daily/refresh")
async def api_chart_daily_refresh(symbol: str, limit: int | None = None):
"""强制从币安同步日 K 到本地库。"""