chore: enforce linting as part of build
This commit is contained in:
@@ -81,6 +81,7 @@ def get_cache() -> CacheService:
|
||||
global _cache_instance
|
||||
if _cache_instance is None:
|
||||
import os
|
||||
|
||||
redis_url = os.environ.get("REDIS_URL")
|
||||
_cache_instance = CacheService(redis_url)
|
||||
return _cache_instance
|
||||
|
||||
@@ -110,7 +110,9 @@ class DataService:
|
||||
await self.cache.set_json(cache_key, payload)
|
||||
return payload
|
||||
|
||||
async def get_options_chain_for_expiry(self, symbol: str | None = None, expiry: str | None = None) -> dict[str, Any]:
|
||||
async def get_options_chain_for_expiry(
|
||||
self, symbol: str | None = None, expiry: str | None = None
|
||||
) -> dict[str, Any]:
|
||||
ticker_symbol = (symbol or self.default_symbol).upper()
|
||||
expirations_data = await self.get_option_expirations(ticker_symbol)
|
||||
expirations = list(expirations_data.get("expirations") or [])
|
||||
@@ -176,7 +178,9 @@ class DataService:
|
||||
await self.cache.set_json(cache_key, payload)
|
||||
return payload
|
||||
except Exception as exc: # pragma: no cover - network dependent
|
||||
logger.warning("Failed to fetch options chain for %s %s from yfinance: %s", ticker_symbol, target_expiry, exc)
|
||||
logger.warning(
|
||||
"Failed to fetch options chain for %s %s from yfinance: %s", ticker_symbol, target_expiry, exc
|
||||
)
|
||||
payload = self._fallback_options_chain(
|
||||
ticker_symbol,
|
||||
quote,
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
import asyncio
|
||||
import logging
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime, timedelta
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
import yfinance as yf
|
||||
@@ -16,6 +16,7 @@ logger = logging.getLogger(__name__)
|
||||
@dataclass
|
||||
class PriceData:
|
||||
"""Price data for a symbol."""
|
||||
|
||||
symbol: str
|
||||
price: float
|
||||
currency: str
|
||||
@@ -25,19 +26,19 @@ class PriceData:
|
||||
|
||||
class PriceFeed:
|
||||
"""Live price feed service using yfinance with Redis caching."""
|
||||
|
||||
|
||||
CACHE_TTL_SECONDS = 60
|
||||
DEFAULT_SYMBOLS = ["GLD", "TLT", "BTC-USD"]
|
||||
|
||||
|
||||
def __init__(self):
|
||||
self._cache = get_cache()
|
||||
|
||||
|
||||
async def get_price(self, symbol: str) -> Optional[PriceData]:
|
||||
"""Get current price for a symbol, with caching.
|
||||
|
||||
|
||||
Args:
|
||||
symbol: Yahoo Finance symbol (e.g., "GLD", "BTC-USD")
|
||||
|
||||
|
||||
Returns:
|
||||
PriceData or None if fetch fails
|
||||
"""
|
||||
@@ -47,7 +48,7 @@ class PriceFeed:
|
||||
cached = await self._cache.get(cache_key)
|
||||
if cached:
|
||||
return PriceData(**cached)
|
||||
|
||||
|
||||
# Fetch from yfinance
|
||||
try:
|
||||
data = await self._fetch_yfinance(symbol)
|
||||
@@ -55,44 +56,39 @@ class PriceFeed:
|
||||
# Cache the result
|
||||
if self._cache.enabled:
|
||||
await self._cache.set(
|
||||
cache_key,
|
||||
cache_key,
|
||||
{
|
||||
"symbol": data.symbol,
|
||||
"price": data.price,
|
||||
"currency": data.currency,
|
||||
"timestamp": data.timestamp.isoformat(),
|
||||
"source": data.source
|
||||
"source": data.source,
|
||||
},
|
||||
ttl=self.CACHE_TTL_SECONDS
|
||||
ttl=self.CACHE_TTL_SECONDS,
|
||||
)
|
||||
return data
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to fetch price for {symbol}: {e}")
|
||||
|
||||
|
||||
return None
|
||||
|
||||
|
||||
async def _fetch_yfinance(self, symbol: str) -> Optional[PriceData]:
|
||||
"""Fetch price from yfinance (run in thread pool to avoid blocking)."""
|
||||
loop = asyncio.get_event_loop()
|
||||
return await loop.run_in_executor(None, self._sync_fetch_yfinance, symbol)
|
||||
|
||||
|
||||
def _sync_fetch_yfinance(self, symbol: str) -> Optional[PriceData]:
|
||||
"""Synchronous yfinance fetch."""
|
||||
ticker = yf.Ticker(symbol)
|
||||
hist = ticker.history(period="1d", interval="1m")
|
||||
|
||||
|
||||
if not hist.empty:
|
||||
last_price = hist["Close"].iloc[-1]
|
||||
currency = ticker.info.get("currency", "USD")
|
||||
|
||||
return PriceData(
|
||||
symbol=symbol,
|
||||
price=float(last_price),
|
||||
currency=currency,
|
||||
timestamp=datetime.utcnow()
|
||||
)
|
||||
|
||||
return PriceData(symbol=symbol, price=float(last_price), currency=currency, timestamp=datetime.utcnow())
|
||||
return None
|
||||
|
||||
|
||||
async def get_prices(self, symbols: list[str]) -> dict[str, Optional[PriceData]]:
|
||||
"""Get prices for multiple symbols concurrently."""
|
||||
tasks = [self.get_price(s) for s in symbols]
|
||||
|
||||
Reference in New Issue
Block a user