-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprices.py
More file actions
68 lines (56 loc) · 2.17 KB
/
prices.py
File metadata and controls
68 lines (56 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import time
import yfinance as yf
# Cache devise par ticker (USD, EUR, GBP…)
_currency_cache: dict[str, str] = {}
def _ticker_currency(ticker: str) -> str:
if ticker in _currency_cache:
return _currency_cache[ticker]
try:
fi = yf.Ticker(ticker).fast_info
currency = (getattr(fi, "currency", None) or "EUR").upper()
except Exception:
currency = "EUR"
_currency_cache[ticker] = currency
return currency
def currency_symbol(currency: str) -> str:
return {"USD": "$", "GBP": "£", "JPY": "¥"}.get(currency, "€")
def get_price(ticker: str) -> float | None:
try:
hist = yf.Ticker(ticker).history(period="1d")
if not hist.empty:
return round(float(hist["Close"].iloc[-1]), 4)
except Exception as e:
print(f"⚠️ Price error {ticker}: {e}")
return None
def get_quote(ticker: str) -> dict:
"""Retourne prix dans la devise native du ticker, avec devise détectée.
status : 'ok' | 'suspended' | 'error'
"""
try:
hist = yf.Ticker(ticker).history(period="2d")
if len(hist) >= 2:
prev = float(hist["Close"].iloc[-2])
current = float(hist["Close"].iloc[-1])
change_pct = ((current - prev) / prev) * 100
elif len(hist) == 1:
current = float(hist["Close"].iloc[-1])
prev = current
change_pct = 0.0
else:
long_hist = yf.Ticker(ticker).history(period="1mo")
status = "suspended" if long_hist.empty else "no_recent_data"
return {"ticker": ticker, "price": None, "currency": "EUR",
"change_pct": None, "status": status}
currency = _ticker_currency(ticker)
return {
"ticker": ticker,
"price": round(current, 4),
"currency": currency,
"prev_close": round(prev, 4),
"change_pct": round(change_pct, 2),
"status": "ok",
}
except Exception as e:
print(f"⚠️ Quote error {ticker}: {e}")
return {"ticker": ticker, "price": None, "currency": "EUR",
"change_pct": None, "status": "error"}