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:
69
test_polygon.py
Normal file
69
test_polygon.py
Normal file
@@ -0,0 +1,69 @@
|
||||
"""Test script for Polygon.io API integration."""
|
||||
import logging
|
||||
from polygon_api import PolygonAPI
|
||||
from chart_generator import ChartGenerator
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
||||
)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def test_polygon_integration():
|
||||
"""Test Polygon.io API and chart generation."""
|
||||
|
||||
api_key = os.getenv('POLYGON_API_KEY')
|
||||
if not api_key:
|
||||
logger.error("POLYGON_API_KEY not set in .env file")
|
||||
return False
|
||||
|
||||
logger.info("Initializing Polygon API")
|
||||
api = PolygonAPI(api_key)
|
||||
|
||||
# Test tickers
|
||||
tickers = ["AAPL", "TSLA"]
|
||||
|
||||
for ticker in tickers:
|
||||
logger.info(f"\nTesting {ticker}...")
|
||||
|
||||
# Fetch candle data
|
||||
logger.info(f"Fetching candle data for {ticker}")
|
||||
candle_data = api.get_candles(ticker, days=30)
|
||||
|
||||
if not candle_data:
|
||||
logger.error(f"Failed to fetch candle data for {ticker}")
|
||||
continue
|
||||
|
||||
logger.info(f"Successfully fetched {len(candle_data['timestamps'])} candles")
|
||||
logger.info(f"Price range: ${min(candle_data['low']):.2f} - ${max(candle_data['high']):.2f}")
|
||||
|
||||
# Generate chart
|
||||
logger.info(f"Generating chart for {ticker}")
|
||||
chart = ChartGenerator.create_price_chart(ticker, candle_data, f"{ticker} Test")
|
||||
|
||||
if not chart:
|
||||
logger.error(f"Failed to generate chart for {ticker}")
|
||||
continue
|
||||
|
||||
# Save chart
|
||||
filename = f"/tmp/{ticker}_polygon_chart.png"
|
||||
with open(filename, "wb") as f:
|
||||
f.write(chart.getvalue())
|
||||
logger.info(f"Chart saved: {filename} ({len(chart.getvalue())} bytes)")
|
||||
|
||||
logger.info("\nPolygon.io integration test completed successfully!")
|
||||
return True
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
success = test_polygon_integration()
|
||||
exit(0 if success else 1)
|
||||
except Exception as e:
|
||||
logger.error(f"Test failed: {e}", exc_info=True)
|
||||
exit(1)
|
||||
Reference in New Issue
Block a user