Skip to content

Add a strategy optimizer for the cash-deploy backtest - #3

Merged
valuecodes merged 3 commits into
mainfrom
feat/cash-deploy-optimizer
Jul 18, 2026
Merged

Add a strategy optimizer for the cash-deploy backtest#3
valuecodes merged 3 commits into
mainfrom
feat/cash-deploy-optimizer

Conversation

@valuecodes

Copy link
Copy Markdown
Owner

What

  • The cash-deploy strategy's parameters (cash reserve, refill rate, and the drawdown→deploy tranches) were hand-picked. This adds a Strategy Optimizer that searches them automatically for configurations that beat the index (100%-stocks buy-and-hold).
  • New Streamlit-free engine module optimizer.py: an Optuna (TPE Bayesian) search with a threshold-grid encoding that makes every sampled DeployRule valid by construction, a selectable objective (excess CAGR vs index by default, plus a drawdown-capped variant, Sharpe-vs-cash, and raw CAGR), and the optimizer choosing the number of buckets (1–5).
  • Walk-forward validation is the honesty layer: each fold optimizes on a train window and scores the frozen winner on a later, unseen test window, so the page leads with an out-of-sample number instead of a curve-fit in-sample one.
  • New app/pages/2_Optimize.py page: button-gated search with progress, the recommended config + bucket table, per-fold train-vs-test results, comparison against 100% stocks and the four presets, equity + search-history charts, and prominent curve-fitting / price-return-only warnings.
  • Small refactor: lifted _sharpe_vs_cash from the cash-deploy page into metrics.sharpe_vs_cash; added Optuna as a dependency; re-exported the optimizer and cash-deploy engine from the package root.

How to test

uv run poe check
uv run poe test
uv run poe dev

In the app, open the Optimizer page, pick a start year and a small trials/folds budget, press Run search, and confirm the recommended config, walk-forward table, comparison table, and charts render, and that the out-of-sample column is shown.

Security review

No security-impacting changes.

valuecodes and others added 2 commits July 14, 2026 22:59
Bayesian (Optuna TPE) search over the cash-deploy reserve, refill rate
and drawdown deploy tranches to find configs that beat the index, with
walk-forward validation for an honest out-of-sample estimate.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
study.best_value raises until a trial completes; skip the progress
report in that window so a pruning objective can never crash the search.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings July 18, 2026 05:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an Optuna-driven optimizer and walk-forward validation workflow for the cash-deploy backtest, plus a new Streamlit “Optimizer” page, so strategy parameters can be searched automatically and reported with out-of-sample results.

Changes:

  • Introduces a new engine module optimizer.py implementing Optuna search, multiple objective functions, and walk-forward validation.
  • Adds a new Streamlit page to run the optimizer, display recommended parameters, fold results, and charts.
  • Refactors Sharpe-vs-cash into metrics.sharpe_vs_cash, adds Optuna dependency, and expands package-root exports.

Reviewed changes

Copilot reviewed 8 out of 9 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
uv.lock Locks Optuna and its transitive dependencies (SQLAlchemy/Alembic/etc.).
pyproject.toml Adds optuna>=3.6 to project dependencies.
src/portfolio_research_lab/optimizer.py New optimizer engine (search space encoding, objectives, optimize + walk-forward).
src/portfolio_research_lab/metrics.py Adds reusable sharpe_vs_cash() helper.
src/portfolio_research_lab/init.py Re-exports optimizer + cash-deploy symbols from package root.
app/pages/2_Optimize.py New Streamlit page to run optimization and render results/visuals.
app/pages/1_Cash_Deploy.py Replaces local Sharpe-vs-cash helper with metrics.sharpe_vs_cash().
tests/test_optimize.py New test suite covering encoding invariants, objective behavior, optimize(), walk_forward().
tests/test_metrics.py Adds unit tests for metrics.sharpe_vs_cash().

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/portfolio_research_lab/optimizer.py
Comment thread src/portfolio_research_lab/optimizer.py
Comment thread src/portfolio_research_lab/optimizer.py Outdated
Comment thread app/pages/2_Optimize.py
- optimize() raises when split<1.0 leaves no holdout instead of silently
  dropping the test slice
- compute run metrics once per trial (objective takes the metrics dict)
- move Optuna verbosity change out of import into optimize()
- pass on_trial through to walk_forward so the page progress bar updates

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@valuecodes
valuecodes merged commit 7097879 into main Jul 18, 2026
8 checks passed
@valuecodes
valuecodes deleted the feat/cash-deploy-optimizer branch July 18, 2026 06:01
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