Skip to content

Track financial ratios accounting for stock splits - #10

Open
mikeymc wants to merge 2 commits into
mainfrom
claude/financial-ratios-stock-splits-01WUU8FEUbCHKU9aVB91MYqK
Open

mikeymc wants to merge 2 commits into
mainfrom
claude/financial-ratios-stock-splits-01WUU8FEUbCHKU9aVB91MYqK

Conversation

@mikeymc

@mikeymc mikeymc commented Nov 16, 2025

Copy link
Copy Markdown
Owner

No description provided.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants