#!/usr/bin/env python3 """ Time Series Cache Demo for TradingAgents This script demonstrates the intelligent time series caching system that optimizes financial API calls by caching data locally. Features demonstrated: 1. OHLCV data caching with YFinance 2. News data caching 3. Technical indicators caching 4. Cache performance monitoring 5. Cache management operations """ import os import sys from datetime import datetime, timedelta # Add the project root to Python path sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from tradingagents.dataflows import ( get_YFin_data_cached, get_YFin_data_window_cached, get_finnhub_news_cached, get_google_news_cached, get_technical_indicators_cached, get_cache_statistics, clear_cache_data ) def demo_ohlcv_caching(): """Demonstrate OHLCV data caching""" print("๐Ÿฆ OHLCV Data Caching Demo") print("=" * 50) symbol = "AAPL" end_date = "2024-01-15" start_date = "2024-01-01" print(f"๐Ÿ“Š Fetching {symbol} data from {start_date} to {end_date}") print("First call (will fetch from API and cache)...") # First call - should fetch from API start_time = datetime.now() data1 = get_YFin_data_cached(symbol, start_date, end_date) time1 = (datetime.now() - start_time).total_seconds() print(f"โฑ๏ธ First call took: {time1:.2f} seconds") print(f"๐Ÿ“„ Data length: {len(data1.split('\\n'))} lines") print("\\nSecond call (should use cache)...") # Second call - should use cache start_time = datetime.now() data2 = get_YFin_data_cached(symbol, start_date, end_date) time2 = (datetime.now() - start_time).total_seconds() print(f"โฑ๏ธ Second call took: {time2:.2f} seconds") print(f"๐Ÿš€ Speed improvement: {time1/max(time2, 0.001):.1f}x faster") print(f"โœ… Data identical: {data1 == data2}") print() def demo_window_caching(): """Demonstrate window-based data caching""" print("๐ŸชŸ Window-Based Caching Demo") print("=" * 50) symbol = "TSLA" curr_date = "2024-01-15" look_back_days = 30 print(f"๐Ÿ“Š Fetching {symbol} data: {look_back_days} days before {curr_date}") # Fetch data with windowing data = get_YFin_data_window_cached(symbol, curr_date, look_back_days) print(f"๐Ÿ“„ Retrieved data length: {len(data.split('\\n'))} lines") print() def demo_news_caching(): """Demonstrate news data caching""" print("๐Ÿ“ฐ News Data Caching Demo") print("=" * 50) symbol = "AAPL" curr_date = "2024-01-15" look_back_days = 7 print(f"๐Ÿ“ฐ Fetching news for {symbol}: {look_back_days} days before {curr_date}") try: # Fetch cached news data news_data = get_finnhub_news_cached(symbol, curr_date, look_back_days) if "No cached news found" in news_data: print("โ„น๏ธ No news data available in cache (this is normal for demo)") else: print(f"๐Ÿ“„ Retrieved news length: {len(news_data.split('\\n'))} lines") except Exception as e: print(f"โ„น๏ธ News demo skipped: {e}") print() def demo_google_news_caching(): """Demonstrate Google News caching""" print("๐Ÿ” Google News Caching Demo") print("=" * 50) query = "stock market" curr_date = "2024-01-15" look_back_days = 7 print(f"๐Ÿ” Fetching Google News for '{query}': {look_back_days} days before {curr_date}") try: # Fetch cached Google news news_data = get_google_news_cached(query, curr_date, look_back_days) if "No cached news found" in news_data: print("โ„น๏ธ No Google News data available (API may not be configured)") else: print(f"๐Ÿ“„ Retrieved Google News length: {len(news_data.split('\\n'))} lines") except Exception as e: print(f"โ„น๏ธ Google News demo skipped: {e}") print() def demo_technical_indicators(): """Demonstrate technical indicators caching""" print("๐Ÿ“ˆ Technical Indicators Caching Demo") print("=" * 50) symbol = "AAPL" indicator = "rsi" curr_date = "2024-01-15" look_back_days = 20 print(f"๐Ÿ“ˆ Calculating {indicator.upper()} for {symbol}: {look_back_days} days before {curr_date}") try: # Fetch cached technical indicators indicator_data = get_technical_indicators_cached(symbol, indicator, curr_date, look_back_days) if "No cached indicator data found" in indicator_data: print("โ„น๏ธ No indicator data available (may need price data first)") else: print(f"๐Ÿ“„ Retrieved indicator data length: {len(indicator_data.split('\\n'))} lines") except Exception as e: print(f"โ„น๏ธ Technical indicators demo skipped: {e}") print() def demo_cache_statistics(): """Show cache performance statistics""" print("๐Ÿ“Š Cache Performance Statistics") print("=" * 50) try: stats = get_cache_statistics() print(stats) except Exception as e: print(f"โ„น๏ธ Cache statistics unavailable: {e}") print() def demo_cache_management(): """Demonstrate cache management operations""" print("๐Ÿงน Cache Management Demo") print("=" * 50) print("Available cache management operations:") print("1. Clear cache for specific symbol:") print(" clear_cache_data(symbol='AAPL')") print() print("2. Clear old cache data:") print(" clear_cache_data(older_than_days=30)") print() print("3. Clear cache for symbol older than N days:") print(" clear_cache_data(symbol='AAPL', older_than_days=7)") print() # Demonstrate getting cache help try: help_text = clear_cache_data() print(f"๐Ÿ“ Cache management help: {help_text}") except Exception as e: print(f"โ„น๏ธ Cache management info: {e}") print() def main(): """Run all demonstrations""" print("๐Ÿš€ TradingAgents Time Series Cache Demo") print("=" * 60) print() # Run all demos demo_ohlcv_caching() demo_window_caching() demo_news_caching() demo_google_news_caching() demo_technical_indicators() demo_cache_statistics() demo_cache_management() print("โœ… Demo completed!") print() print("๐Ÿ’ก Key Benefits of Time Series Caching:") print(" โ€ข Reduces API calls and costs") print(" โ€ข Faster data retrieval for repeated queries") print(" โ€ข Intelligent gap-filling for overlapping date ranges") print(" โ€ข Automatic data format standardization") print(" โ€ข Built-in cache management and statistics") print() print("๐Ÿ”ง Integration Tips:") print(" โ€ข Replace get_YFin_data() with get_YFin_data_cached()") print(" โ€ข Use get_cache_statistics() to monitor performance") print(" โ€ข Periodically clear old cache with clear_cache_data()") print(" โ€ข Cache directory: data_cache/time_series/") if __name__ == "__main__": main()