Refactor market K-line storage with tiered retention and chunked loading.
Store 1m/5m/1h/12h/1d/1w with per-timeframe policies, aggregate 15m and 2h/4h on read, and support left-pan history fetches via before_ms. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+109
-3
@@ -3,6 +3,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
import os
|
||||
import time
|
||||
from typing import Any, Callable, Optional
|
||||
|
||||
@@ -32,7 +33,26 @@ CHART_TIMEFRAME_ORDER = (
|
||||
)
|
||||
DAILY_PLUS_TIMEFRAMES = frozenset({"1d", "1w"})
|
||||
|
||||
# 部分交易所 ccxt 无原生 12h,或原生 K 线间隔异常时从 1h 聚合
|
||||
# 入库 / 同步真源(交易所拉取)
|
||||
STORED_TIMEFRAMES = frozenset({"1m", "5m", "1h", "12h", "1d", "1w"})
|
||||
PERMANENT_STORED_TIMEFRAMES = frozenset({"12h", "1d", "1w"})
|
||||
YEAR_ROLLING_STORED = frozenset({"5m", "1h"})
|
||||
|
||||
# 展示周期 → 本地聚合源(不落库)
|
||||
CHART_DISPLAY_AGGREGATE_FROM: dict[str, str] = {
|
||||
"15m": "5m",
|
||||
"2h": "1h",
|
||||
"4h": "1h",
|
||||
}
|
||||
|
||||
SMALL_DISPLAY_TFS = frozenset({"1m", "5m", "15m"})
|
||||
MID_DISPLAY_TFS = frozenset({"1h", "2h", "4h"})
|
||||
|
||||
HUB_KLINE_1M_MAX_BARS = max(1000, int(os.getenv("HUB_KLINE_1M_MAX_BARS", "10000")))
|
||||
HUB_KLINE_5M_1H_RETENTION_DAYS = max(30, int(os.getenv("HUB_KLINE_5M_1H_RETENTION_DAYS", "365")))
|
||||
HUB_KLINE_SEED_BARS = max(100, int(os.getenv("HUB_KLINE_SEED_BARS", "500")))
|
||||
|
||||
# 部分交易所 ccxt 无原生 12h,或原生 K 线间隔异常时从 1h 聚合(仅远程拉取 fallback)
|
||||
OHLCV_AGGREGATE_FROM: dict[str, str] = {
|
||||
"12h": "1h",
|
||||
}
|
||||
@@ -55,9 +75,95 @@ def normalize_chart_timeframe(raw: str | None, default: str = "5m") -> str:
|
||||
return tf if tf in CHART_TIMEFRAMES else default
|
||||
|
||||
|
||||
def bar_limit_for_timeframe(timeframe: str) -> int:
|
||||
def sync_timeframe_for_display(timeframe: str) -> str:
|
||||
"""展示周期对应的入库 / 同步周期。"""
|
||||
tf = normalize_chart_timeframe(timeframe)
|
||||
return 500 if tf in DAILY_PLUS_TIMEFRAMES else 1000
|
||||
return CHART_DISPLAY_AGGREGATE_FROM.get(tf, tf)
|
||||
|
||||
|
||||
def aggregation_source_for_display(timeframe: str) -> str | None:
|
||||
tf = normalize_chart_timeframe(timeframe)
|
||||
return CHART_DISPLAY_AGGREGATE_FROM.get(tf)
|
||||
|
||||
|
||||
def aggregate_ratio(display_tf: str, source_tf: str) -> int:
|
||||
d = normalize_chart_timeframe(display_tf)
|
||||
s = normalize_chart_timeframe(source_tf)
|
||||
return max(1, int(TIMEFRAME_MS[d] // TIMEFRAME_MS[s]))
|
||||
|
||||
|
||||
def chart_initial_limit(timeframe: str) -> int:
|
||||
tf = normalize_chart_timeframe(timeframe)
|
||||
if tf in SMALL_DISPLAY_TFS:
|
||||
return 300
|
||||
if tf == "1w":
|
||||
return 150
|
||||
return 200
|
||||
|
||||
|
||||
def chart_chunk_limit(timeframe: str) -> int:
|
||||
tf = normalize_chart_timeframe(timeframe)
|
||||
if tf in SMALL_DISPLAY_TFS:
|
||||
return 500
|
||||
if tf == "1w":
|
||||
return 150
|
||||
if tf in MID_DISPLAY_TFS:
|
||||
return 300
|
||||
return 200
|
||||
|
||||
|
||||
def chart_memory_cap(timeframe: str) -> int:
|
||||
tf = normalize_chart_timeframe(timeframe)
|
||||
if tf in SMALL_DISPLAY_TFS:
|
||||
return 5000
|
||||
if tf == "1w":
|
||||
return 500
|
||||
return 1000
|
||||
|
||||
|
||||
def bar_limit_for_timeframe(timeframe: str) -> int:
|
||||
return chart_memory_cap(timeframe)
|
||||
|
||||
|
||||
def storage_retention_days(storage_tf: str) -> int | None:
|
||||
"""None 表示不按天截断(1m 按根数;12h/1d/1w 永久)。"""
|
||||
tf = normalize_chart_timeframe(storage_tf)
|
||||
if tf in YEAR_ROLLING_STORED:
|
||||
return HUB_KLINE_5M_1H_RETENTION_DAYS
|
||||
return None
|
||||
|
||||
|
||||
def history_cutoff_ms_for_storage(storage_tf: str, now_ms: int | None = None) -> int:
|
||||
days = storage_retention_days(storage_tf)
|
||||
if days is None:
|
||||
return 0
|
||||
now = int(now_ms if now_ms is not None else time.time() * 1000)
|
||||
return max(0, now - int(days) * 86400000)
|
||||
|
||||
|
||||
def seed_bar_target(storage_tf: str) -> int:
|
||||
tf = normalize_chart_timeframe(storage_tf)
|
||||
if tf == "1m":
|
||||
return HUB_KLINE_1M_MAX_BARS
|
||||
if tf in YEAR_ROLLING_STORED:
|
||||
period = TIMEFRAME_MS[tf]
|
||||
return min(
|
||||
int(86400000 * HUB_KLINE_5M_1H_RETENTION_DAYS / period) + 20,
|
||||
150000,
|
||||
)
|
||||
return HUB_KLINE_SEED_BARS
|
||||
|
||||
|
||||
def retention_policy_meta() -> dict[str, Any]:
|
||||
return {
|
||||
"1m": {"mode": "bars", "max_bars": HUB_KLINE_1M_MAX_BARS},
|
||||
"5m": {"mode": "days", "days": HUB_KLINE_5M_1H_RETENTION_DAYS},
|
||||
"1h": {"mode": "days", "days": HUB_KLINE_5M_1H_RETENTION_DAYS},
|
||||
"12h": {"mode": "permanent"},
|
||||
"1d": {"mode": "permanent"},
|
||||
"1w": {"mode": "permanent"},
|
||||
"aggregate_from": dict(CHART_DISPLAY_AGGREGATE_FROM),
|
||||
}
|
||||
|
||||
|
||||
def last_closed_bar_open_ms(timeframe: str, now_ms: int | None = None) -> int:
|
||||
|
||||
Reference in New Issue
Block a user