Conversation
Added comprehensive analysis showing that historical P/E ratios are incorrect due to mismatch between split-adjusted prices and non-adjusted EPS data. Includes: - Detailed problem explanation with real-world examples (AAPL, NVDA, TSLA) - Impact analysis showing ratios can be off by 10x-28x for split stocks - Three solution options with recommendation to adjust EPS for splits - Complete implementation plan with code samples and database schema - Testing strategy and migration plan Also added test script for verifying split data availability from yfinance.
PROBLEM FIXED: Historical P/E ratios were incorrect because prices from yfinance are split-adjusted by default but EPS data from EDGAR/yfinance is not adjusted. This created massive errors (10x-28x) for stocks with historical splits. IMPLEMENTATION: 1. Database Schema (database.py) - Added stock_splits table with symbol, split_date, split_ratio - Created indexes on symbol and date for efficient lookups - Added save_stock_splits() and get_stock_splits() methods 2. Split Data Fetching (data_fetcher.py) - fetch_stock_splits(): Gets split history from yfinance - Automatically fetches splits when fetching stock data - Stores splits in database for caching 3. Adjustment Factor Calculation (data_fetcher.py) - get_split_adjustment_factor(): Calculates cumulative split ratio - For date before splits: factor = product of all future splits - For date after splits: factor = 1.0 (no adjustment) - Handles multiple splits correctly (e.g., AAPL: 7x then 4x = 28x) 4. P/E Ratio Correction (app.py) - Updated /api/stock/<symbol>/history endpoint - Now adjusts EPS before calculating P/E ratio - Formula: adjusted_eps = original_eps / adjustment_factor - Then: pe_ratio = split_adjusted_price / adjusted_eps 5. Comprehensive Test Suite (tests/test_stock_splits.py) - 17 test cases covering all functionality - Tests split fetching, storage, retrieval - Tests adjustment factor calculations - Tests edge cases: no splits, multiple splits, reverse splits - All tests passing ✓ EXAMPLES OF CORRECTIONS: - AAPL: 28x adjustment (7:1 * 4:1 splits) - NVDA: 10x adjustment (10:1 split in 2024) - TSLA: 15x adjustment (5:1 * 3:1 splits) TESTING: Run: uv run pytest tests/test_stock_splits.py -v All 17 tests passing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.