Skip to content

fix: avoid decimal overflow in portfolio statistics alpha - #9861

Merged
Martin-Molinero merged 1 commit into
QuantConnect:masterfrom
Romazes:bug-portfolio-statistics-alpha-overflow
Oct 2, 2026
Merged

Martin-Molinero merged 1 commit into
QuantConnect:masterfrom
Romazes:bug-portfolio-statistics-alpha-overflow

Conversation

@Romazes

@Romazes Romazes commented Oct 2, 2026

Copy link
Copy Markdown
Collaborator

Description

PortfolioStatistics computed alpha in decimal. When the annual performance is capped at decimal.MaxValue and the benchmark term is negative, the subtraction overflows:

BaseResultsHandler.GenerateStatisticsResults(): Error generating statistics packet System.OverflowException: Value was either too large or too small for a Decimal.
   at System.Number.ThrowOverflowException(String message)
   at System.Decimal.DecCalc.ScaleResult(Buf24* bufRes, UInt32 hiRes, Int32 scale)
   at System.Decimal.DecCalc.DecAddSub(DecCalc& d1, DecCalc& d2, Boolean sign)
   at System.Decimal.op_Subtraction(Decimal d1, Decimal d2)
   at QuantConnect.Statistics.PortfolioStatistics..ctor(SortedDictionary`2 profitLoss, SortedDictionary`2 equity, SortedDictionary`2 portfolioTurnover, List`1 listPerformance, List`1 listBenchmark, Decimal startingCapital, IRiskFreeInterestRateModel riskFreeInterestRateModel, Int32 tradingDaysPerYear, Nullable`1 winCount, Nullable`1 lossCount)

Alpha is now computed in double and cast back with SafeDecimalCast, the same way the information and Treynor ratios already are.

Related PR(s)

#8256 fixed the same overflow for the information and Treynor ratios.

Related Issue

N/A

Motivation and Context

Seen in a live deployment. A cash deposit more than doubled the account value, so one daily return was about +156% and the annual performance hit the decimal cap. From the next day on, every statistics update failed with the error above, 45 times in 2.5 days, until the algorithm stopped.

Requires Documentation Change

No

How Has This Been Tested?

  • New PortfolioStatisticsTests.PortfolioStatisticsDoesNotFailWhenAlphaExceedsDecimalRange: fails on master with the stack above, passes with the fix.
  • QuantConnect.Tests.Common.Statistics: 379 of 379 passed.
  • C# BasicTemplate* regression algorithms: 40 of 40 passed, so their expected alpha did not change.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • Refactor (non-breaking change which improves implementation)
  • Performance (non-breaking change which improves performance. Please add associated performance test and results)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Non-functional change (xml comments/documentation/etc)

Checklist:

  • My code follows the code style of this project.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • My branch follows the naming convention bug-<issue#>-<description> or feature-<issue#>-<description>

- compute alpha in double and cast it back with SafeDecimalCast, like the information and treynor ratios
- add test with a capped annual performance and a negative benchmark term
@Romazes Romazes self-assigned this Oct 2, 2026
@Romazes Romazes added the bug label Oct 2, 2026
@Martin-Molinero
Martin-Molinero merged commit 2c6dc55 into QuantConnect:master Oct 2, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants