"""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)