Per-timeframe cache TTLs — D1 cached 4h, H1 30m, M15 5m

A single 5-minute refresh_interval applied to every timeframe wasted
API credits: a daily candle was being refetched every 5 minutes even
though it only changes once per day. Now each TF caches for roughly
its bar interval, so repeat scans within an hour skip D1 entirely
(saves ~60s of throttle wait) and within 30 min also skip H1.

config.py: new cache_ttl_per_tf dict on PlatformConfig with sensible
defaults for every timeframe; refresh_interval kept as fallback.

data_provider.py: _cache_ttl() helper used by both fetch_ohlcv and
prefetch_batch instead of the global refresh_interval.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Harwinder
2026-05-05 14:55:05 -04:00
parent 8a1e57f682
commit 4c1d839a1a
2 changed files with 26 additions and 4 deletions
+17 -1
View File
@@ -196,9 +196,25 @@ class PlatformConfig:
# Twelve Data API key (overridden by TWELVE_DATA_API_KEY env var)
twelve_data_api_key: str = "f563b22ffdfd4924955decbe1a8c8701"
# Dashboard refresh interval (seconds)
# Dashboard refresh interval (seconds) — used as a fallback when a
# timeframe isn't covered by cache_ttl_per_tf below.
refresh_interval: int = 300 # 5 minutes
# Per-timeframe cache TTL (seconds). A 1-week candle changes less
# often than a 15-minute candle, so caching them all for the same
# 5 minutes wastes API credits. Tuned to roughly match each TF's
# bar interval so we re-fetch about once per new bar.
cache_ttl_per_tf: Dict[Timeframe, int] = field(default_factory=lambda: {
Timeframe.M1: 60, # 1 minute
Timeframe.M5: 150, # 2.5 minutes
Timeframe.M15: 300, # 5 minutes
Timeframe.M30: 600, # 10 minutes
Timeframe.H1: 1800, # 30 minutes
Timeframe.H4: 7200, # 2 hours
Timeframe.D1: 14400, # 4 hours
Timeframe.W1: 86400, # 24 hours
})
# Logging
log_level: str = "INFO"
log_signals_to_file: bool = True