Repository navigation
feat(backtest): add Value at Risk, CVaR, Omega Ratio, and Tail Risk analytics - #324
Merged
brokermr810 merged 3 commits intoOct 27, 2026
Merged
Conversation
…nalytics Introduce tail risk and non-normal distribution analytics into Strategy API V2 backtests: - calculate_tail_risk: historical VaR (95%, 99%), historical CVaR (Expected Shortfall), parametric Gaussian VaR, Tail Ratio, sample skewness, and sample excess kurtosis. - calculate_omega_ratio: probability-weighted ratio of gains to losses relative to a benchmark return threshold. - Strategy API V2 integration: populate valueAtRisk95, valueAtRisk99, cvar95, cvar99, parametricVaR95, parametricVaR99, omegaRatio, tailRatio, skewness, and kurtosis in _result(). - Documentation: update English and Chinese backtest center guides with mathematical formulations and interpretation. - Unit and integration tests: deterministic test coverage with edge-case and input validation. Signed-off-by: huuan26 <phanbuihuuan261103@gmail.com>
Signed-off-by: TIANHE <TIANHE@GMAIL.COM>
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
brokermr810
approved these changes
Oct 27, 2026
brokermr810
left a comment
Collaborator
There was a problem hiding this comment.
Reviewed after synchronizing the Alpaca test fix. Tail-risk estimates now include sample sufficiency thresholds, explicit status/observation/unit fields, non-negative loss semantics, expanded tests, and updated documentation.
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 Strategy API V2 backtesting analytics with tail risk and non-normal return distribution metrics in
app/services/backtest/metrics.pyandapp/services/strategy_v2/runtime.py:calculate_tail_risk):valueAtRisk95andvalueAtRisk99.parametricVaR95andparametricVaR99($$\text{CVaR}\alpha = - \mathbb{E}[R \mid R \le - \text{VaR}\alpha]$$
cvar95andcvar99.calculate_omega_ratio):Nonewhen downside losses are zero with positive returns (infinite payoff) and0.0when returns are flat or zero.skewness,kurtosis,runner._result()inapp/services/strategy_v2/runtime.py, exposingvalueAtRisk95,valueAtRisk99,cvar95,cvar99,parametricVaR95,parametricVaR99,omegaRatio,tailRatio,skewness, andkurtosisin the primary backtest results dictionary.docs/product/BACKTEST_CENTER.mdanddocs/product/BACKTEST_CENTER_CN.mdwith detailed LaTeX mathematical formulations, interpretation guidelines, and edge-case behaviors.Changes
backend_api_python/app/services/backtest/metrics.py:calculate_omega_ratiowith target threshold support and safe zero-downside handling.calculate_tail_risksupporting configurable confidence levels (default 95% and 99%), historical VaR, CVaR/Expected Shortfall, parametric Gaussian VaR, Tail Ratio, sample skewness, and sample excess kurtosis.calculate_omega_ratioandcalculate_tail_riskin__all__.backend_api_python/app/services/strategy_v2/runtime.py:calculate_omega_ratioandcalculate_tail_risk._result()and populate the backtest result dictionary.backend_api_python/tests/test_backtest_metrics.py:calculate_omega_ratio(known values, custom threshold, all-gain/all-loss edge cases, empty series, invalid input validation).calculate_tail_risk(known quantile values, CVaR coherence checkbackend_api_python/tests/test_strategy_v2_runtime.py:_result()includes and correctly calculates all tail risk and Omega metrics.docs/product/BACKTEST_CENTER.md&docs/product/BACKTEST_CENTER_CN.md:Test plan
test_backtest_metrics.pyandtest_strategy_v2_runtime.pypass (pytest tests/test_backtest_metrics.py tests/test_strategy_v2_runtime.py)ruff check app/ tests/passes with zero errors/warningspython scripts/backend_quality_check.pypassesgit diff --checkpasses with zero formatting/whitespace errorsAPI documentation (if routes/schemas changed)
resultdictionary payload in Strategy API V2 backtests