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:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user