feat(trading-strategies): momentum-rotation strategy (top 5, monthly rebalance) - #1138
feat(trading-strategies): momentum-rotation strategy (top 5, monthly rebalance)#1138bennycode wants to merge 3 commits into
Conversation
A live strategy that holds the top picks from momentum + scorecard and rebalances when momentum changes (monthly, since the 12-1 window is a monthly snapshot). - computeRebalance: pure, equal-weight, low-churn rebalance. Liquidate names that left the target set, buy new entrants at an equal-weight slice (equity / count), and leave unchanged holdings alone, so an unchanged set trades nothing. - MomentumRotation: orchestrator that ranks the S&P 500 by 12-1 momentum, scores the top winners, keeps the best 5, reads Alpaca positions/equity, and either previews the trades (plan(), read-only) or executes them (rebalance()). - Extract the momentum ranking (price fetch + rank) into a shared util so the report and the rotation share one implementation. The scorecard uses current analyst data, so this runs forward only (live/paper), not as a backtest. Verified end to end with a read-only plan() against a live account.
…ation provider-agnostic Lets the momentum rotation run on free TipRanks data instead of paid FMP. - exchange: new TipRanksAPI client. The TipRanks MCP is a stateless HTTP RPC, so a single tools/call POST returns the data (no SDK or session). Two batch calls (getAssetsData + getTechnicalAnalysis) cover the whole scorecard. - TipRanksScorecard: orchestrator that feeds TipRanks data into the existing pure computeTipRanksScorecard, handling nulls. - MomentumRotation now takes a PortfolioScorer (a rank() method) instead of a fixed FMP scorecard, so either provider can drive it. Added a matching rank() to the FMP MomentumScorecard. Trade-off: the TipRanks rubric is backward-looking (no forward P/E, growth, revision, or falling-knife guard) and its quotes can lag, so it trades a weaker basket — free, but not as sharp as FMP. Verified end to end with a read-only plan().
A one-shot entrypoint that runs the momentum rotation from env config. Reads keys from packages/exchange/.env (Alpaca + TipRanks/FMP), builds the rotation, and prints the plan. - Dry-run by default (read-only, no orders). Set ROTATION_EXECUTE=true to place them. - Scorer defaults to the free TipRanks; ROTATION_SCORER=fmp switches to FMP. - ALPACA_USE_PAPER=true targets the paper account. The rebalance is low-churn and idempotent, so it can be cron'd on any cadence (monthly is natural) and only trades when the top picks actually change.
There was a problem hiding this comment.
🟡 Changes recommended
There are a few correctness and contract-alignment issues (paper/live Alpaca credentials selection, scorer interface mismatch, and missing rebalance input validation) that should be fixed before merging.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Adds a live “momentum-rotation” strategy to the trading-strategies package: compute S&P 500 12-1 momentum, score the top momentum cohort, select the best 5, then generate/execute a low-churn equal-weight rebalance plan against an Alpaca account. It also extracts shared momentum-ranking logic (so the report and strategy use the same implementation) and introduces a TipRanks MCP-backed scorer as an alternative to FMP.
Changes:
- Introduce
MomentumRotationorchestrator +computeRebalancepure planning core (with tests) for equal-weight, low-churn monthly rotation. - Extract shared
momentumRankingutility and refactorSP500MomentumReportto use it. - Add TipRanks MCP client + schemas in
exchange, and aTipRanksScorecardI/O wrapper intrading-strategies; add a one-shot runner script inmessaging.
File summaries
| File | Description |
|---|---|
| packages/trading-strategies/src/util/momentumRanking.ts | New shared price-fetch + momentum-ranking utility used by report and rotation. |
| packages/trading-strategies/src/scorecard/TipRanksScorecard.ts | New TipRanks-backed scorecard orchestrator that feeds the pure TipRanks rubric. |
| packages/trading-strategies/src/scorecard/MomentumScorecard.ts | Adds a convenience rank() method for callers that only need ticker ordering. |
| packages/trading-strategies/src/scorecard/index.ts | Exports TipRanksScorecard. |
| packages/trading-strategies/src/rotation/MomentumRotation.ts | New live strategy orchestrator: select targets, plan, and execute rebalances via Alpaca. |
| packages/trading-strategies/src/rotation/index.ts | Barrel exports for rotation module. |
| packages/trading-strategies/src/rotation/computeRebalance.ts | New pure rebalance planner (sell dropouts, buy entrants, hold unchanged). |
| packages/trading-strategies/src/rotation/computeRebalance.test.ts | Unit tests covering low-churn/equal-weight rebalance behavior. |
| packages/trading-strategies/src/report-sp500-momentum/SP500MomentumReport.ts | Refactors report to use the shared momentum-ranking utility. |
| packages/trading-strategies/src/index.ts | Exports the new rotation module from package root. |
| packages/messaging/src/runMomentumRotation.ts | New CLI/runner script to plan or execute the rotation with env-driven configuration. |
| packages/messaging/package.json | Adds npm run rotation entrypoint. |
| packages/exchange/src/data/tipranks/TipRanksAPI.ts | New TipRanks MCP HTTP client (JSON-RPC over SSE payload). |
| packages/exchange/src/data/tipranks/schema/TipRanksTechnicalSchema.ts | Zod schema for technical-analysis payload (200-day MA). |
| packages/exchange/src/data/tipranks/schema/TipRanksAssetSchema.ts | Zod schema for asset headline payload (price, consensus, etc.). |
| packages/exchange/src/data/tipranks/index.ts | Barrel exports for TipRanks client + schemas. |
| packages/exchange/src/data/index.ts | Exports the new tipranks module. |
Review details
- Files reviewed: 17/17 changed files
- Comments generated: 3
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| const alpaca = new AlpacaAPI({ | ||
| apiKey: required('ALPACA_LIVE_API_KEY'), | ||
| apiSecret: required('ALPACA_LIVE_API_SECRET'), | ||
| usePaperTrading: usePaper, | ||
| }); |
| const targetSet = new Set(targets); | ||
| const heldSet = new Set(holdings.map(holding => holding.ticker)); | ||
| const perPositionUsd = totalEquityUsd / positionCount; | ||
|
|
| } | ||
|
|
||
| /** Scores the tickers and returns them ranked best-first. */ | ||
| async rank(tickers: string[]): Promise<string[]> { |
Builds the live momentum-rotation strategy: hold the top 5 picks from momentum + scorecard, equal-weight, and rebalance monthly (when the 12-1 momentum window rolls).
Design
computeRebalance(pure, tested): equal-weight, low-churn. Sell names that left the target set, buy new entrants atequity / positionCount, hold unchanged names untouched. An unchanged set produces an empty plan (no fees).MomentumRotation(orchestrator): S&P 500 12-1 momentum -> scorecard -> best 5 -> read Alpaca positions/equity -> plan.plan()is read-only (preview/paper);rebalance()places the orders (sells first to free cash, then notional buys).momentumRankingutil: the price-fetch + rank logic is extracted from the report so the report and rotation use one implementation (DRY).Scope / constraint
The scorecard depends on current analyst data (no point-in-time history), so this is live/forward only and cannot be backtested faithfully.
Verification
Read-only
plan()run against a live account produced a correct minimal plan (held the 3 unchanged names, swapped the 2 that changed). Full package suite: 243 tests green.