Add backtest metrics module (Sharpe, max drawdown, inventory-time-weighted PnL)#52
Open
Pattermesh wants to merge 1 commit into
Open
Add backtest metrics module (Sharpe, max drawdown, inventory-time-weighted PnL)#52Pattermesh wants to merge 1 commit into
Pattermesh wants to merge 1 commit into
Conversation
…ghted PnL) Extract reusable, side-effect-free metric functions into src/backtest/metrics.py and cover them with unit tests against known numeric vectors. Wire BacktestEngine.run to consume the shared functions instead of inline duplicates (behaviour preserved). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Contributor
|
Welcome to kcolbchain, @Pattermesh — glad you're here. 🌱 Here's what happens from this PR:
While you wait:
What happens after your first merge
Thanks for writing the code. We're building this to last. |
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
Adds a reusable backtest-metrics module to the backtest engine with unit tests against known numeric vectors.
New:
src/backtest/metrics.pyPure, side-effect-free functions (numpy only):
sharpe_ratio(returns, risk_free_rate=0.0, periods_per_year=1.0)— mean(excess)/std(excess) with optional annualisation; returns0.0for <2 points or zero volatility.max_drawdown(equity_curve)— largest running-peak-to-trough drop, in curve units;0.0for monotonic or trivial curves.inventory_time_weighted_pnl(pnl_curve, inventory_curve)— sum ofabs(inventory_carried) * pnl_incrementper step, surfacing whether PnL is compensation for inventory risk.Tests:
tests/unit/backtest/test_metrics.py17 tests with hand-computed expected values, e.g. drawdown of
[100,120,90,110,80,130]== 40, ITW-PnL ofpnl=[0,10,5,20], inv=[2,-3,4,1]== 65. Covers risk-free subtraction, sqrt-periods annualisation, short-inventory magnitude, length-mismatchValueError, and edge/empty cases.Refactor:
src/backtest/engine.pyBacktestEngine.runnow consumes the sharedsharpe_ratio/max_drawdowninstead of inline duplicates. Behaviour preserved (same mean/std Sharpe, same running-peak drawdown).Test output
CI (
.github/workflows/ci.yml) already exists and runspython -m pytest tests/ -v, which auto-discovers the newtests/unit/backtest/directory — no workflow change needed.🤖 Generated with Claude Code