Repository navigation
feat(backtest): expand benchmark-relative metrics with Beta, Jensen's Alpha, Treynor, and Capture Ratios - #303
Open
huuan26 wants to merge 3 commits into
Conversation
… Alpha, Treynor, and Capture Ratios - Add _align_curves_and_returns helper in app/services/backtest/metrics.py to share common benchmark curve alignment and interval validation. - Implement calculate_beta_and_alpha for systematic risk (Beta), CAPM excess return (Jensen's Alpha), Treynor Ratio, Pearson Correlation, and R-squared. - Implement calculate_capture_ratios for compounded Up-Market Capture, Down-Market Capture, and Overall Capture Ratio. - Implement calculate_benchmark_relative_metrics combining Information Ratio, Beta/Alpha, and Capture metrics while maintaining backward compatibility with calculate_information_ratio. - Update Strategy API V2 backtest service to compute comprehensive benchmark-relative metrics. - Document metrics and formulas in docs/product/BACKTEST_CENTER.md. - Add comprehensive unit test coverage in tests/test_backtest_metrics.py.
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.
Summary
Expands the benchmark-relative performance metrics in
app/services/backtest/metrics.pyand Strategy API V2:_align_curves_and_returnsto unify timestamp inner joining, percentage return computation, non-continuous gap filtering, and exchange-calendar session alignment across all benchmark metrics."zero_benchmark_variance".Safely returns
NonewhenupCapture: Compounded portfolio return divided by compounded benchmark return across periods wheredownCapture: Compounded portfolio return divided by compounded benchmark return across periods wherecaptureRatio: Ratio of upside capture to downside capture (upMarketPeriodsanddownMarketPeriodsfor market regime diagnostics.calculate_benchmark_relative_metricsintegrates Information Ratio, Beta/Alpha, and Capture metrics in a single structured result.calculate_information_ratioand existing fields ofresult["benchmarkRelativeMetrics"].StrategyV2BacktestService(app/services/strategy_v2/service.py).docs/product/BACKTEST_CENTER.mddetailing the new metrics, formulas, and edge cases.Changes
backend_api_python/app/services/backtest/metrics.py:_align_curves_and_returnshelper.calculate_beta_and_alpha.calculate_capture_ratios.calculate_benchmark_relative_metrics.__all__.backend_api_python/app/services/strategy_v2/service.py:calculate_benchmark_relative_metricsforresult["benchmarkRelativeMetrics"].backend_api_python/tests/test_backtest_metrics.py:docs/product/BACKTEST_CENTER.md:Test plan
pytest backend_api_python/tests/test_backtest_metrics.pypasses (22/22 tests in 0.91s)ruff check app/services/backtest/metrics.py tests/test_backtest_metrics.py app/services/strategy_v2/service.pypasses with zero violationsgit diff --checkpasses with zero whitespace/formatting issuescalculate_information_ratiocontractAPI documentation (if routes/schemas changed)
benchmarkRelativeMetricsdictionary payload in backtest results