Replace Yahoo Finance with Polygon.io for chart data
- Add Polygon.io API client for reliable historical data access - Update bot to use Polygon instead of Yahoo Finance for charts - Add POLYGON_API_KEY to config and environment example - Polygon free tier: 5 API calls/minute, more reliable than Yahoo - Fallback to FinViz chart URL when Polygon unavailable 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
53
bot.py
53
bot.py
@@ -11,6 +11,7 @@ from stock_api import YahooFinanceAPI, FinnhubAPI
|
||||
from market_hours import MarketHours
|
||||
from crypto_api import CoinGeckoAPI
|
||||
from chart_generator import ChartGenerator
|
||||
from polygon_api import PolygonAPI
|
||||
|
||||
|
||||
# Configure logging
|
||||
@@ -37,9 +38,13 @@ class StockBot(commands.Bot):
|
||||
self.stock_api = YahooFinanceAPI()
|
||||
logger.info("Using Yahoo Finance API for stock data")
|
||||
|
||||
# Always initialize Yahoo Finance for chart data (free historical data)
|
||||
self.yahoo_api = YahooFinanceAPI()
|
||||
logger.info("Using Yahoo Finance API for chart data")
|
||||
# Initialize Polygon.io for chart data (free historical data)
|
||||
if Config.POLYGON_API_KEY:
|
||||
self.chart_api = PolygonAPI(Config.POLYGON_API_KEY)
|
||||
logger.info("Using Polygon.io API for chart data")
|
||||
else:
|
||||
logger.warning("POLYGON_API_KEY not set, charts will use FinViz fallback")
|
||||
self.chart_api = None
|
||||
|
||||
self.scheduler = AsyncIOScheduler(timezone=pytz.timezone('America/New_York'))
|
||||
self.target_channel_ids = [int(id) for id in Config.CHANNEL_IDS]
|
||||
@@ -258,17 +263,18 @@ class StockBot(commands.Bot):
|
||||
await channel.send(f"Unable to retrieve data for ticker: {ticker}")
|
||||
return
|
||||
|
||||
# Generate chart using Yahoo Finance historical data
|
||||
# Generate chart using Polygon.io historical data
|
||||
chart_file = None
|
||||
candle_data = self.yahoo_api.get_candles(ticker, days=30)
|
||||
if candle_data:
|
||||
chart_buffer = ChartGenerator.create_price_chart(
|
||||
ticker,
|
||||
candle_data,
|
||||
stock_data.get('company_name')
|
||||
)
|
||||
if chart_buffer:
|
||||
chart_file = discord.File(chart_buffer, filename="chart.png")
|
||||
if self.chart_api:
|
||||
candle_data = self.chart_api.get_candles(ticker, days=30)
|
||||
if candle_data:
|
||||
chart_buffer = ChartGenerator.create_price_chart(
|
||||
ticker,
|
||||
candle_data,
|
||||
stock_data.get('company_name')
|
||||
)
|
||||
if chart_buffer:
|
||||
chart_file = discord.File(chart_buffer, filename="chart.png")
|
||||
|
||||
# Create embed with appropriate chart reference
|
||||
embed = self.create_stock_embed(stock_data, has_chart_attachment=bool(chart_file))
|
||||
@@ -553,17 +559,18 @@ async def get_stock_price(ctx, ticker: str = None):
|
||||
await ctx.send(f"Unable to retrieve data for ticker: {ticker}")
|
||||
return
|
||||
|
||||
# Generate chart using Yahoo Finance historical data
|
||||
# Generate chart using Polygon.io historical data
|
||||
chart_file = None
|
||||
candle_data = bot.yahoo_api.get_candles(ticker, days=30)
|
||||
if candle_data:
|
||||
chart_buffer = ChartGenerator.create_price_chart(
|
||||
ticker,
|
||||
candle_data,
|
||||
stock_data.get('company_name')
|
||||
)
|
||||
if chart_buffer:
|
||||
chart_file = discord.File(chart_buffer, filename="chart.png")
|
||||
if bot.chart_api:
|
||||
candle_data = bot.chart_api.get_candles(ticker, days=30)
|
||||
if candle_data:
|
||||
chart_buffer = ChartGenerator.create_price_chart(
|
||||
ticker,
|
||||
candle_data,
|
||||
stock_data.get('company_name')
|
||||
)
|
||||
if chart_buffer:
|
||||
chart_file = discord.File(chart_buffer, filename="chart.png")
|
||||
|
||||
# Create embed with appropriate chart reference
|
||||
embed = bot.create_stock_embed(stock_data, has_chart_attachment=bool(chart_file))
|
||||
|
||||
Reference in New Issue
Block a user