""" Test script to compare Stock Oracle real SEC data with yfinance_plus data """ import requests import yfinance_plus as yf from datetime import datetime, timezone import pandas as pd import json from typing import Dict, Any # API configuration API_URL = "http://localhost:18001/api/v1" def get_yfinance_plus_data(ticker: str) -> Dict[str, Any]: """Get financial data directly from yfinance_plus""" print(f"\n=== Getting yfinance_plus data for {ticker} ===") # Create ticker object stock = yf.Ticker(ticker) # Get basic info info = stock.info print(f"Company: {info.get('longName', 'N/A')}") print(f"Sector: {info.get('sector', 'N/A')}") print(f"Industry: {info.get('industry', 'N/A')}") print(f"Market Cap: ${info.get('marketCap', 0):,.0f}") print(f"Shares Outstanding: {info.get('sharesOutstanding', 0):,.0f}") print(f"Current Price: ${info.get('currentPrice', info.get('regularMarketPrice', 0)):.2f}") # Get quarterly financial statements quarterly_income = stock.quarterly_income_stmt quarterly_balance = stock.quarterly_balance_sheet quarterly_cashflow = stock.quarterly_cashflow print(f"\nQuarterly Income Statement shape: {quarterly_income.shape}") print(f"Quarterly Balance Sheet shape: {quarterly_balance.shape}") print(f"Quarterly Cash Flow shape: {quarterly_cashflow.shape}") # Show latest quarter data if not quarterly_income.empty: latest_quarter = quarterly_income.columns[0] print(f"\n=== Latest Quarter: {latest_quarter} ===") # Key income statement items revenue = quarterly_income.loc['Total Revenue', latest_quarter] if 'Total Revenue' in quarterly_income.index else None net_income = quarterly_income.loc['Net Income', latest_quarter] if 'Net Income' in quarterly_income.index else None eps = quarterly_income.loc['Diluted EPS', latest_quarter] if 'Diluted EPS' in quarterly_income.index else None print(f"Revenue: ${revenue:,.0f}") print(f"Net Income: ${net_income:,.0f}") print(f"EPS: ${eps:.2f}") # Balance sheet items if not quarterly_balance.empty and latest_quarter in quarterly_balance.columns: total_assets = quarterly_balance.loc['Total Assets', latest_quarter] if 'Total Assets' in quarterly_balance.index else None total_equity = quarterly_balance.loc['Total Equity Gross Minority Interest', latest_quarter] if 'Total Equity Gross Minority Interest' in quarterly_balance.index else None total_debt = quarterly_balance.loc['Total Debt', latest_quarter] if 'Total Debt' in quarterly_balance.index else None print(f"Total Assets: ${total_assets:,.0f}") print(f"Total Equity: ${total_equity:,.0f}") print(f"Total Debt: ${total_debt:,.0f}") return { 'info': info, 'quarterly_income': quarterly_income.to_dict() if not quarterly_income.empty else {}, 'quarterly_balance': quarterly_balance.to_dict() if not quarterly_balance.empty else {}, 'quarterly_cashflow': quarterly_cashflow.to_dict() if not quarterly_cashflow.empty else {}, } def get_stock_oracle_real_data(ticker: str, start_date: str, end_date: str) -> Dict[str, Any]: """Get real financial data from Stock Oracle API""" print(f"\n=== Getting Stock Oracle REAL SEC data for {ticker} ===") # Request data from API with force refresh to get real data response = requests.post( f"{API_URL}/financial/data", json={ "ticker": ticker, "start_date": start_date, "end_date": end_date, "period_type": "all", "include_metrics": True, "force_refresh": True # Force refresh to fetch real data } ) if response.status_code == 200: data = response.json() print(f"Company: {data['company']['name']}") print(f"Sector: {data['company']['sector']}") print(f"Industry: {data['company']['industry']}") print(f"Number of periods: {len(data['financial_data'])}") # Check if we have real data (not estimated) real_data_count = sum(1 for fd in data['financial_data'] if not fd.get('is_estimated', True)) print(f"Real data periods: {real_data_count}") print(f"Estimated data periods: {len(data['financial_data']) - real_data_count}") return data else: print(f"Error: {response.status_code} - {response.text}") return None def compare_real_financial_data(yfinance_data: Dict, oracle_data: Dict, ticker: str): """Compare real financial data between yfinance_plus and Stock Oracle""" print(f"\n=== REAL DATA COMPARISON FOR {ticker} ===") if not oracle_data or 'financial_data' not in oracle_data: print("No Stock Oracle data to compare") return # Find real (non-estimated) data in Stock Oracle real_oracle_data = [fd for fd in oracle_data['financial_data'] if not fd.get('is_estimated', True)] if not real_oracle_data: print("No real (non-estimated) data found in Stock Oracle") return print(f"Found {len(real_oracle_data)} real periods in Stock Oracle") # Get the latest real period from Stock Oracle latest_oracle = real_oracle_data[-1] if real_oracle_data else None if not latest_oracle: print("No real financial data in Stock Oracle") return print(f"\nStock Oracle Latest REAL Period: {latest_oracle['period_date']}") print(f"Data Source: {latest_oracle.get('data_source', 'Unknown')}") print(f"Is Estimated: {latest_oracle.get('is_estimated', 'Unknown')}") # Compare with yfinance_plus if 'quarterly_income' in yfinance_data and yfinance_data['quarterly_income']: quarters = list(yfinance_data['quarterly_income'].keys()) if quarters: latest_quarter = quarters[0] print(f"YFinance_plus Latest Quarter: {latest_quarter}") yf_data = yfinance_data['quarterly_income'][latest_quarter] # Compare key metrics print(f"\n--- REVENUE COMPARISON ---") oracle_revenue = latest_oracle.get('revenue', 0) yf_revenue = yf_data.get('Total Revenue', 0) print(f"Stock Oracle Revenue: ${oracle_revenue:,.2f}") print(f"YFinance_plus Revenue: ${yf_revenue:,.2f}") if oracle_revenue > 0 and yf_revenue > 0: diff_pct = ((oracle_revenue - yf_revenue) / yf_revenue) * 100 print(f"Difference: {diff_pct:.2f}%") print(f"\n--- NET INCOME COMPARISON ---") oracle_net_income = latest_oracle.get('net_income', 0) yf_net_income = yf_data.get('Net Income', 0) print(f"Stock Oracle Net Income: ${oracle_net_income:,.2f}") print(f"YFinance_plus Net Income: ${yf_net_income:,.2f}") if oracle_net_income > 0 and yf_net_income > 0: diff_pct = ((oracle_net_income - yf_net_income) / yf_net_income) * 100 print(f"Difference: {diff_pct:.2f}%") print(f"\n--- EPS COMPARISON ---") oracle_eps = latest_oracle.get('eps', 0) yf_eps = yf_data.get('Diluted EPS', 0) print(f"Stock Oracle EPS: ${oracle_eps:.2f}") print(f"YFinance_plus EPS: ${yf_eps:.2f}") if oracle_eps > 0 and yf_eps > 0: diff_pct = ((oracle_eps - yf_eps) / yf_eps) * 100 print(f"Difference: {diff_pct:.2f}%") print(f"\n--- SHARES OUTSTANDING COMPARISON ---") oracle_shares = latest_oracle.get('shares_outstanding', 0) yf_shares = yfinance_data['info'].get('sharesOutstanding', 0) print(f"Stock Oracle Shares: {oracle_shares:,.0f}") print(f"YFinance_plus Shares: {yf_shares:,.0f}") if oracle_shares > 0 and yf_shares > 0: diff_pct = ((oracle_shares - yf_shares) / yf_shares) * 100 print(f"Difference: {diff_pct:.2f}%") print(f"\n--- MARKET CAP COMPARISON ---") oracle_market_cap = latest_oracle.get('market_cap', 0) yf_market_cap = yfinance_data['info'].get('marketCap', 0) print(f"Stock Oracle Market Cap: ${oracle_market_cap:,.2f}") print(f"YFinance_plus Market Cap: ${yf_market_cap:,.2f}") if oracle_market_cap > 0 and yf_market_cap > 0: diff_pct = ((oracle_market_cap - yf_market_cap) / yf_market_cap) * 100 print(f"Difference: {diff_pct:.2f}%") print(f"\n--- P/E RATIO COMPARISON ---") oracle_pe = latest_oracle.get('pe_ratio', 0) yf_pe = yfinance_data['info'].get('trailingPE', 0) print(f"Stock Oracle P/E: {oracle_pe:.2f}") print(f"YFinance_plus P/E: {yf_pe:.2f}") if oracle_pe > 0 and yf_pe > 0: diff_pct = ((oracle_pe - yf_pe) / yf_pe) * 100 print(f"Difference: {diff_pct:.2f}%") def main(): """Main comparison function""" # Test with Apple ticker = "AAPL" start_date = "2024-01-01" end_date = "2024-12-31" print(f"Comparing REAL financial data for {ticker}") print("=" * 80) # Get data from both sources yfinance_data = get_yfinance_plus_data(ticker) oracle_data = get_stock_oracle_real_data(ticker, start_date, end_date) # Compare the data compare_real_financial_data(yfinance_data, oracle_data, ticker) # Test with Microsoft print("\n" + "=" * 80) ticker = "MSFT" print(f"\nComparing REAL financial data for {ticker}") print("=" * 80) yfinance_data = get_yfinance_plus_data(ticker) oracle_data = get_stock_oracle_real_data(ticker, start_date, end_date) compare_real_financial_data(yfinance_data, oracle_data, ticker) if __name__ == "__main__": main()