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,8 +1,8 @@
import finnhub
import logging
from typing import Optional, Dict, Any
from typing import Optional, Dict, Any, List
from .base import StockAPIBase
from datetime import datetime
from datetime import datetime, timedelta
import pytz
@@ -170,3 +170,40 @@ class FinnhubAPI(StockAPIBase):
except Exception as e:
logger.error(f"Error fetching earnings calendar: {e}")
return None
def get_candles(self, ticker: str, days: int = 30, resolution: str = 'D') -> Optional[Dict[str, List]]:
"""
Retrieve historical candlestick data from Finnhub.
Args:
ticker: Stock ticker symbol
days: Number of days of historical data to fetch
resolution: Candle resolution (1, 5, 15, 30, 60, D, W, M)
Returns:
Dictionary with OHLCV data or None if unavailable
"""
try:
# Calculate timestamps
end_time = int(datetime.now().timestamp())
start_time = int((datetime.now() - timedelta(days=days)).timestamp())
# Fetch candle data
candles = self.client.stock_candles(ticker, resolution, start_time, end_time)
if not candles or candles.get('s') != 'ok':
logger.warning(f"No candle data available for ticker: {ticker}")
return None
return {
'timestamps': candles['t'],
'open': candles['o'],
'high': candles['h'],
'low': candles['l'],
'close': candles['c'],
'volume': candles['v']
}
except Exception as e:
logger.error(f"Error fetching candle data for {ticker}: {e}")
return None