1.7 KiB
1.7 KiB
Task: Complete rotation ranking service
Goal
Implement the ranking logic for sector rotation in services/rotation_service.py. The service currently has skeleton methods for ranking but needs the actual implementation.
Requirements
Implement ranking methods in services/rotation_service.py
-
rank_sectors(sector_data, timeframe="20d")method:- Accept a list of sector data dicts (from the database)
- Sort sectors by momentum score (descending)
- Assign rank_now to each sector
- Calculate rank_change compared to previous ranking
- Return ranked sector list
-
get_ranking_history(ticker, days=30)method:- Query historical rankings from the database (TimescaleDB hypertable)
- Return time-series of rank data for charting
- Use
execute_querywith parameterized queries
-
calculate_relative_strength(ticker, benchmark="SPY")method:- Calculate price ratio between sector ETF and benchmark
- Compute relative strength as a percentage change
- Return strength metric
Database interactions
- Read from
sector_rankingshypertable (TimescaleDB) - Use parameterized queries
- Handle missing data gracefully (return empty lists)
Acceptance Criteria
rank_sectors()returns properly ranked sector listget_ranking_history()returns time-series datacalculate_relative_strength()returns strength metric- All methods are async
- File stays under 200 lines
Files to Modify
src/backend/services/rotation_service.py
Files to Read First
src/backend/services/rotation_service.py— current statesrc/backend/database.py— query patternssrc/backend/tests/test_sector_rotation.py— test expectations