fix: improve XAU price fallback with multiple symbol attempts
This commit is contained in:
+36
-16
@@ -43,25 +43,30 @@ class XAUBriefAgent:
|
||||
return self._get_gold_fallback()
|
||||
|
||||
def _get_gold_fallback(self):
|
||||
"""Fallback: dados da Binance (XAU/USDT)."""
|
||||
"""Fallback: dados da Binance via par alternativo (GOLD/USDT)."""
|
||||
result = {}
|
||||
try:
|
||||
r = requests.get(
|
||||
"https://api.binance.com/api/v3/ticker/24hr?symbol=XAUUSDT",
|
||||
timeout=8
|
||||
)
|
||||
if r.status_code == 200:
|
||||
data = r.json()
|
||||
result["xau_usd"] = float(data.get("lastPrice", 0))
|
||||
result["chp"] = float(data.get("priceChangePercent", 0))
|
||||
result["high"] = float(data.get("highPrice", 0))
|
||||
result["low"] = float(data.get("lowPrice", 0))
|
||||
result["volume"] = float(data.get("quoteVolume", 0))
|
||||
except:
|
||||
pass
|
||||
# Tenta XAU/BRL ou XAU/USDT na Binance
|
||||
symbols = ["XAUUSDT", "GLDUSDT", "PAXGUSDT"]
|
||||
for sym in symbols:
|
||||
try:
|
||||
r = requests.get(
|
||||
f"https://api.binance.com/api/v3/ticker/24hr?symbol={sym}",
|
||||
timeout=8
|
||||
)
|
||||
if r.status_code == 200:
|
||||
data = r.json()
|
||||
result["xau_usd"] = float(data.get("lastPrice", 0))
|
||||
result["chp"] = float(data.get("priceChangePercent", 0))
|
||||
result["high"] = float(data.get("highPrice", 0))
|
||||
result["low"] = float(data.get("lowPrice", 0))
|
||||
result["volume"] = float(data.get("quoteVolume", 0))
|
||||
if result["xau_usd"] > 100: # preço válido de ouro
|
||||
break
|
||||
except:
|
||||
continue
|
||||
|
||||
# Segundo fallback: exchangerate
|
||||
if not result.get("xau_usd"):
|
||||
if not result.get("xau_usd") or result["xau_usd"] < 100:
|
||||
try:
|
||||
r = requests.get(
|
||||
"https://api.exchangerate-api.com/v4/latest/XAU",
|
||||
@@ -74,6 +79,21 @@ class XAUBriefAgent:
|
||||
except:
|
||||
pass
|
||||
|
||||
# Terceiro fallback: Metal-price.org API pública
|
||||
if not result.get("xau_usd") or result["xau_usd"] < 100:
|
||||
try:
|
||||
r = requests.get(
|
||||
"https://api.metals.live/v1/spot/gold",
|
||||
timeout=8
|
||||
)
|
||||
if r.status_code == 200:
|
||||
data = r.json()
|
||||
if isinstance(data, list) and len(data) > 0:
|
||||
result["xau_usd"] = data[0].get("gold", 0)
|
||||
result["chp"] = 0
|
||||
except:
|
||||
pass
|
||||
|
||||
return result
|
||||
|
||||
# ── Treasury Yields (US 10Y) ───────────────────────────────────────────────
|
||||
|
||||
Reference in New Issue
Block a user