fix: preserve ATH/Mayer/200D SMA when CoinGecko rate-limits
- ATH: fall back to cached value when fetch fails - 200D SMA: compute from history.json when CoinGecko blocks us - Mayer Multiple: derived from 200D SMA fallback - Drawdown: preserve cached value on ATH fetch failure - Fixes N/A Drawdown and -- header stats after quick refresh
This commit is contained in:
+27
-4
@@ -126,18 +126,41 @@ def run_scrape(force_full=False):
|
||||
|
||||
log.info("Fetching BTC ATH...")
|
||||
ath_data = price.fetch_ath()
|
||||
if price_current.get("price") and ath_data.get("ath"):
|
||||
drawdown = price.calculate_drawdown(price_current["price"], ath_data["ath"])
|
||||
metrics["drawdown"] = {"value": drawdown, "ath": ath_data["ath"]}
|
||||
ath_val = ath_data.get("ath") or existing_cache.get("drawdown", {}).get("ath")
|
||||
if price_current.get("price") and ath_val:
|
||||
drawdown = price.calculate_drawdown(price_current["price"], ath_val)
|
||||
metrics["drawdown"] = {"value": drawdown, "ath": ath_val}
|
||||
elif existing_cache.get("drawdown", {}).get("value") is not None:
|
||||
log.info("ATH fetch failed — reusing cached drawdown")
|
||||
metrics["drawdown"] = existing_cache["drawdown"]
|
||||
else:
|
||||
metrics["drawdown"] = {"value": None}
|
||||
|
||||
log.info("Fetching historical prices...")
|
||||
log.info("Fetching historical prices for 200D SMA / Mayer...")
|
||||
hist = price.fetch_historical()
|
||||
if hist:
|
||||
sma_200d = price.calculate_200d_sma(hist)
|
||||
mayer = price.calculate_mayer_multiple(price_current.get("price"), sma_200d)
|
||||
metrics["price_extras"] = {"sma_200d": sma_200d, "mayer_multiple": mayer}
|
||||
else:
|
||||
# CoinGecko rate-limited — compute from history.json instead
|
||||
try:
|
||||
hist_path = os.path.join(DATA_DIR, "history.json")
|
||||
with open(hist_path) as f:
|
||||
hdata = json.load(f)
|
||||
btc_vals = hdata.get("btc_price", {}).get("values", [])
|
||||
if len(btc_vals) >= 200:
|
||||
sma_200d = sum(btc_vals[-200:]) / 200
|
||||
cur_p = price_current.get("price") or btc_vals[-1]
|
||||
mayer = cur_p / sma_200d if sma_200d else None
|
||||
metrics["price_extras"] = {"sma_200d": sma_200d, "mayer_multiple": round(mayer, 4) if mayer else None}
|
||||
log.info("Computed 200D SMA from history.json (CoinGecko rate-limited)")
|
||||
elif existing_cache.get("price_extras"):
|
||||
metrics["price_extras"] = existing_cache["price_extras"]
|
||||
except Exception:
|
||||
if existing_cache.get("price_extras"):
|
||||
metrics["price_extras"] = existing_cache["price_extras"]
|
||||
log.info("Reusing cached price_extras")
|
||||
|
||||
# 3. On-chain metrics — use cached values (historical data is permanent)
|
||||
onchain_keys = ["puell_multiple", "mvrv_zscore", "reserve_risk", "rhodl_ratio",
|
||||
|
||||
Reference in New Issue
Block a user