Fix order deserialization overflow for values at the edge of the decimal range#9615
Merged
Martin-Molinero merged 1 commit intoJul 14, 2026
Conversation
…mal range Order quantities and prices out of the decimal range are clamped to decimal.MaxValue/MinValue at order creation, e.g. by the QCAlgorithm double quantity order methods through SafeDecimalCast. When read back, JObject parses these values as doubles that round past the decimal range, so Value<decimal>() threw an OverflowException. Clamp them back into range when deserializing. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.
Description
OrderJsonConverterthrew aSystem.OverflowException("Value was either too large or too small for a Decimal") when deserializing orders whose quantity or prices sit at the edge of the decimal range.Quantities/prices out of the decimal range are clamped to
decimal.MaxValue/decimal.MinValueat order creation — e.g. theQCAlgorithmdouble-quantity order methods go throughExtensions.SafeDecimalCast, so a runawaydoublequantity in user code (Python floats bind to these overloads) produces an order withQuantity = decimal.MaxValue. That order serializes fine, but on deserializationJObjectparses the number back as adoublethat rounds past the decimal range (decimal.MaxValueis 2⁹⁶ − 1; the nearest double is exactly 2⁹⁶), soValue<decimal>()overflowed and the stored result file could never be read back.The fix reads decimal fields through a helper that applies the same
SafeDecimalCastclamping used at order creation when the token holds a double, mirroring whatDecimalJsonConverteralready does. Applied toQuantity,Price,OrderSubmissionDataprices, stop/limit/trigger prices, trailing amount andGroupOrderManagerquantity/limit price.Related Issue
N/A — hit in production reading stored backtest results.
Motivation and Context
Stored backtest/live results containing such an order could never be deserialized again, permanently breaking result APIs for that backtest.
Requires Documentation Change
No.
How Has This Been Tested?
New unit tests round-trip orders with
decimal.MaxValue/MinValuequantity, order-type prices and order submission prices, plus direct deserialization of JSON with out-of-range literals (79228162514264337593543950335.0,7.9228162514264338E+28,1E+300) for quantity, price and group order manager fields. All 7 new test cases fail without the fix; fullOrderJsonConverterTestsfixture passes with it (56/56).Types of changes
Checklist:
bug-<issue#>-<description>orfeature-<issue#>-<description>🤖 Generated with Claude Code