|
|
| import yfinance as yf
|
| import pandas as pd
|
| from typing import List, Dict, Union
|
|
|
|
|
| try:
|
| from ..schemas import OHLC, StockData
|
| except (ImportError, ValueError):
|
| try:
|
| from schemas import OHLC, StockData
|
| except (ImportError, ValueError):
|
|
|
|
|
| pass
|
|
|
| def get_market_data(symbol: str, period: str = "1mo", interval: str = "1d") -> List[Dict]:
|
| """
|
| Fetch historical data for a symbol.
|
| """
|
| try:
|
| ticker = yf.Ticker(symbol)
|
| history = ticker.history(period=period, interval=interval)
|
|
|
| if history.empty:
|
| return []
|
|
|
| data = []
|
| for index, row in history.iterrows():
|
| data.append({
|
| "date": index.strftime("%Y-%m-%d"),
|
| "open": float(row["Open"]),
|
| "high": float(row["High"]),
|
| "low": float(row["Low"]),
|
| "close": float(row["Close"]),
|
| "volume": int(row["Volume"])
|
| })
|
| return data
|
| except Exception as e:
|
| print(f"Error fetching data for {symbol}: {e}")
|
| return []
|
|
|
| def get_current_price(symbol: str) -> float:
|
| """Get the latest price."""
|
| try:
|
| ticker = yf.Ticker(symbol)
|
| info = ticker.history(period="1d")
|
| if not info.empty:
|
| return float(info["Close"].iloc[-1])
|
| return 0.0
|
| except Exception:
|
| return 0.0
|
|
|