Add a moving-average trend-timing backtest - #5
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a fourth strategy to Portfolio Research Lab: a moving-average trend-timing backtest that switches between 100% stocks and 100% cash using a one-bar-lagged MA signal, optional hysteresis band, and per-switch transaction cost, plus a Streamlit page to explore and compare it against baseline portfolios.
Changes:
- Introduces a new path-dependent timing engine (
run_ma_timing) returning aTimingResultwith equity/position series and a switch-event log. - Adds a
TimingConfigPydantic model (incl. MA kind validation, band, and transaction cost parameters) and exports the new API from the package root. - Adds a dedicated Streamlit “Trend Timing” page and comprehensive unit tests covering lag, band behavior, costs, warm-up masking, and input validation.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| tests/test_timing.py | Adds unit tests validating timing engine correctness (lag, switching, costs, warm-up, schema, and error cases). |
| src/portfolio_research_lab/timing.py | Implements the moving-average trend-timing simulation loop and result container. |
| src/portfolio_research_lab/models.py | Adds TimingConfig and MA kind validation constants. |
| src/portfolio_research_lab/init.py | Exposes TimingConfig, TimingResult, and run_ma_timing in the public API. |
| README.md | Updates features + repository structure to include the new strategy and page. |
| app/pages/3_Trend_Timing.py | Adds the Streamlit UI page for running and comparing the trend-timing backtest. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
What
holds 100% stocks while the S&P is above its moving average and steps aside to
cash (money-market at the fed funds rate) when it falls below. Answers the
classic "does market timing beat buy-and-hold?" question none of the existing
pages cover.
src/portfolio_research_lab/timing.py(run_ma_timing→TimingResult), aTimingConfigPydantic model, and a3_Trend_Timing.pypage. Built as a dedicated path-dependent daily loop (like
cash_deploy.py)because the
Strategyprotocol only sets fixed initial weights.from the previous close, executed at today's close — no look-ahead), a
hysteresis band damps whipsaws, and an optional per-switch transaction
cost (bps) is modelled — timing is exactly where costs bite.
time_in_marketand switch-count surfaced in the UI. README features + structure updated.
Over 2000–2025 (200-day SMA, 10 bps): max drawdown −23.1% vs −56.8% for
buy-and-hold, 72.4% time in market — the expected "less return, much less risk"
result.
How to test
Security review
No security-impacting changes.