Files
OpenFin/backend/routers/market_data.py
OpenSquared 6cca7f66b6 feat: ticker validation + SLV/USO/WEAT/CORN/TUR added to ETFs watchlist
Backend:
- GET /api/market/validate?symbol= — validates ticker against yfinance,
  returns {valid, name, price} or {valid: false, reason: 'helpful message'}
- Added SLV, USO, WEAT, CORN, TUR to ETFs WATCHLIST category

Frontend:
- validateTicker() async helper exported from useApi.ts
- InstrumentPicker (PatternLab): custom ticker field now validates before selecting
  Shows spinner while checking, red error message if not found on yfinance
- InstrumentLens (PatternExplorer): same validation on Go button + Enter key

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-22 20:15:26 +02:00

96 lines
3.3 KiB
Python

from fastapi import APIRouter, Query
from typing import Optional, Dict, Any
from datetime import datetime
from services.data_fetcher import get_all_quotes, get_historical, compute_historical_iv, WATCHLIST
_macro_cache: Dict[str, Any] = {}
router = APIRouter(prefix="/api/market", tags=["market"])
@router.get("/quotes")
def quotes_all():
return get_all_quotes()
@router.get("/quote/{symbol}")
def quote_single(symbol: str):
from services.data_fetcher import get_quote
return get_quote(symbol)
@router.get("/history/{symbol}")
def history(
symbol: str,
period: str = Query("1y", description="1d,5d,1mo,3mo,6mo,1y,2y,5y"),
interval: str = Query("1d", description="1m,5m,15m,1h,1d,1wk,1mo"),
):
return get_historical(symbol, period, interval)
@router.get("/iv/{symbol}")
def implied_vol(symbol: str, window: int = 30):
iv = compute_historical_iv(symbol, window)
return {"symbol": symbol, "iv": iv, "window": window}
@router.get("/watchlist")
def watchlist():
return WATCHLIST
@router.get("/validate")
def validate_ticker(symbol: str = Query(..., description="Ticker to validate against yfinance")):
"""Check if a ticker is fetchable. Returns valid=True + live price/name, or valid=False + reason."""
from services.data_fetcher import get_quote
import yfinance as yf
symbol = symbol.strip().upper()
q = get_quote(symbol)
if q and q.get("price"):
# Try to get a display name
try:
info = yf.Ticker(symbol).fast_info
name = getattr(info, "long_name", None) or getattr(info, "short_name", None) or symbol
except Exception:
name = symbol
return {"valid": True, "symbol": symbol, "name": name, "price": q["price"], "change_pct": q.get("change_pct")}
return {"valid": False, "symbol": symbol, "reason": f"Ticker '{symbol}' not found on yfinance — check the symbol (e.g. GC=F for Gold, ^GSPC for S&P 500)"}
@router.get("/macro-regime")
def macro_regime(force: bool = False):
"""Macro gauge values + 5-scenario scoring. Cached 15 min."""
from services.data_fetcher import get_macro_gauges, score_macro_scenarios
now = datetime.utcnow()
if not force and _macro_cache.get("data") and _macro_cache.get("ts"):
age = (now - _macro_cache["ts"]).total_seconds()
if age < 900:
return {**_macro_cache["data"], "cached": True, "cache_age_sec": int(age)}
gauges = get_macro_gauges()
scenarios = score_macro_scenarios(gauges)
result: Dict[str, Any] = {
"gauges": gauges,
"scenarios": scenarios,
"fetched_at": now.isoformat(),
"cached": False,
}
_macro_cache["data"] = result
_macro_cache["ts"] = now
if force:
# Build a compact gauge summary (key → value + change_pct) for the journal
gauges_summary = {
k: {"value": v.get("value"), "change_pct": v.get("change_pct"), "label": v.get("label")}
for k, v in gauges.items()
if v.get("value") is not None or v.get("change_pct") is not None
}
from services.database import log_macro_regime
log_macro_regime(
dominant=scenarios.get("dominant", "incertain"),
scores=scenarios.get("scores", {}),
reasons=scenarios.get("reasons", {}),
gauges_summary=gauges_summary,
)
return result