feat: saxo price
This commit is contained in:
@@ -66,26 +66,36 @@ def watchlist_quotes():
|
||||
|
||||
@router.post("/{ticker}")
|
||||
def add_ticker(ticker: str):
|
||||
"""Adds a tracked instrument. yfinance validation is best-effort, not a gate — an
|
||||
instrument can be entirely Saxo-sourced (both saxo_option_symbol and
|
||||
saxo_quote_symbol linked, see the two PUT .../saxo-*-link endpoints below) with no
|
||||
yfinance equivalent at all, so a ticker yfinance doesn't recognize is still added,
|
||||
just without a name/asset_class lookup or an initial price to report back."""
|
||||
from services.data_fetcher import get_quote
|
||||
from services.database import get_instruments_watchlist, add_instrument_watchlist
|
||||
import yfinance as yf
|
||||
ticker = ticker.strip().upper()
|
||||
if any(row["ticker"] == ticker for row in get_instruments_watchlist()):
|
||||
raise HTTPException(409, f"'{ticker}' is already in the watchlist")
|
||||
|
||||
q = get_quote(ticker)
|
||||
if not q or not q.get("price"):
|
||||
raise HTTPException(400, f"Ticker '{ticker}' not found on yfinance")
|
||||
name = ticker
|
||||
asset_class = "unknown"
|
||||
try:
|
||||
info = yf.Ticker(ticker).fast_info
|
||||
name = getattr(info, "long_name", None) or getattr(info, "short_name", None) or ticker
|
||||
quote_type = getattr(info, "quote_type", "") or ""
|
||||
asset_class = _QUOTE_TYPE_TO_ASSET_CLASS.get(quote_type.upper(), "unknown")
|
||||
except Exception:
|
||||
pass
|
||||
if q and q.get("price"):
|
||||
try:
|
||||
info = yf.Ticker(ticker).fast_info
|
||||
name = getattr(info, "long_name", None) or getattr(info, "short_name", None) or ticker
|
||||
quote_type = getattr(info, "quote_type", "") or ""
|
||||
asset_class = _QUOTE_TYPE_TO_ASSET_CLASS.get(quote_type.upper(), "unknown")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
add_instrument_watchlist(ticker, name, asset_class)
|
||||
return {"ticker": ticker, "name": name, "asset_class": asset_class, "price": q["price"]}
|
||||
return {
|
||||
"ticker": ticker, "name": name, "asset_class": asset_class,
|
||||
"price": q.get("price") if q else None,
|
||||
"yfinance_recognized": bool(q and q.get("price")),
|
||||
}
|
||||
|
||||
|
||||
@router.delete("/{ticker}")
|
||||
|
||||
Reference in New Issue
Block a user