Add matplotlib-based chart generation with Yahoo Finance data

- Add chart_generator.py with price and candlestick chart support
- Implement Yahoo Finance candle data fetching for free historical data
- Update bot to generate and attach charts to stock embeds
- Add matplotlib dependency to requirements.txt
- Configure dual API approach: Finnhub for quotes, Yahoo for charts

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Michael Simard
2025-12-25 00:25:00 -06:00
parent 3040ab3cc1
commit 71e70b77b0
5 changed files with 290 additions and 8 deletions

View File

@@ -1,6 +1,6 @@
import yfinance as yf
import logging
from typing import Optional, Dict, Any
from typing import Optional, Dict, Any, List
from .base import StockAPIBase
@@ -83,3 +83,46 @@ class YahooFinanceAPI(StockAPIBase):
except Exception as e:
logger.error(f"Yahoo Finance API unavailable: {e}")
return False
def get_candles(self, ticker: str, days: int = 30, resolution: str = 'D') -> Optional[Dict[str, List]]:
"""
Retrieve historical candlestick data from Yahoo Finance.
Args:
ticker: Stock ticker symbol
days: Number of days of historical data to fetch
resolution: Candle resolution (ignored, always daily for Yahoo Finance)
Returns:
Dictionary with OHLCV data or None if unavailable
"""
try:
stock = yf.Ticker(ticker)
# Fetch historical data
hist = stock.history(period=f"{days}d")
if hist.empty:
logger.warning(f"No candle data available for ticker: {ticker}")
return None
# Convert to the format expected by chart generator
timestamps = [int(date.timestamp()) for date in hist.index]
opens = hist['Open'].tolist()
highs = hist['High'].tolist()
lows = hist['Low'].tolist()
closes = hist['Close'].tolist()
volumes = hist['Volume'].tolist()
return {
'timestamps': timestamps,
'open': opens,
'high': highs,
'low': lows,
'close': closes,
'volume': volumes
}
except Exception as e:
logger.error(f"Error fetching candle data for {ticker}: {e}")
return None