{"record":{"id":"ab97be71ce4acb3b","repo":"nautechsystems/nautilus_trader","slug":"overflow-occurred-when-adding-price","errorCode":null,"errorMessage":"Overflow occurred when adding `Price`","messagePattern":"Overflow occurred when adding `Price`","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/model/src/types/price.rs","lineNumber":683,"sourceCode":"        Self {\n            raw: -self.raw,\n            precision: self.precision,\n        }\n    }\n}\n\nimpl Add for Price {\n    type Output = Self;\n    fn add(self, rhs: Self) -> Self::Output {\n        assert!(\n            raw_scales_match(self.precision, rhs.precision),\n            \"Cannot add `Price` values with mismatched decimal scales\"\n        );\n        Self {\n            raw: self\n                .raw\n                .checked_add(rhs.raw)\n                .expect(\"Overflow occurred when adding `Price`\"),\n            precision: self.precision.max(rhs.precision),\n        }\n    }\n}\n\nimpl Sub for Price {\n    type Output = Self;\n    fn sub(self, rhs: Self) -> Self::Output {\n        assert!(\n            raw_scales_match(self.precision, rhs.precision),\n            \"Cannot subtract `Price` values with mismatched decimal scales\"\n        );\n        Self {\n            raw: self\n                .raw\n                .checked_sub(rhs.raw)\n                .expect(\"Underflow occurred when subtracting `Price`\"),\n            precision: self.precision.max(rhs.precision),","sourceCodeStart":665,"sourceCodeEnd":701,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/model/src/types/price.rs#L665-L701","documentation":"Price implements Add by checked_add on the raw fixed-point integers; if the sum exceeds the raw integer range, checked_add returns None and this expect panics. The library refuses to wrap prices silently because a wrapped price would be silently wrong in trading logic.","triggerScenarios":"Using the + operator (impl Add for Price) on two Price values with equal precision whose raw sum exceeds PRICE_RAW_MAX; also via iterated addition accumulating large prices.","commonSituations":"Aggregating notional/price values in a loop (sums of thousands of large prices); porting code that used f64 and never overflowed; configuring instruments with very high precision leaving little headroom in the raw integer.","solutions":["Check before adding (e.g. compare raw values against PRICE_RAW_MAX - lhs.raw) or use a checked/widened accumulation type such as Decimal or i128 for intermediate sums.","Reduce operand magnitudes: the sum of the intended values should be representable; if not, the operation is semantically invalid for Price.","Track running sums with rust_decimal::Decimal and construct a Price only at the end after range validation.","Enable the high-precision build so PriceRaw is wider if large sums are legitimate."],"exampleFix":"// before\nlet total: Price = prices.iter().copied().fold(Price::zero(precision), |a, b| a + b); // panics on overflow\n// after\nlet total_dec = prices.iter().fold(Decimal::ZERO, |a, p| a + p.as_decimal());\nlet total = Price::new(total_dec, precision); // validate range at construction","handlingStrategy":"fallback","validationCode":"// Rust\nfn checked_price_add(a: Price, b: Price) -> Option<Price> {\n    (a.precision == b.precision)\n        .then(|| a.raw.checked_add(b.raw))\n        .flatten()\n        .map(|raw| Price { raw, precision: a.precision })\n}","typeGuard":null,"tryCatchPattern":"// Prices are non-panicking only via checked math; accumulate in Decimal:\nlet sum: Decimal = prices.iter().fold(Decimal::ZERO, |a, p| a + p.as_decimal());\nassert!(sum <= PRICE_MAX_DECIMAL, \"price accumulation exceeded representable range\");","preventionTips":["Never accumulate prices in a fold over Price; use Decimal for running totals.","Check headroom before large aggregations.","Remember Price addition is a domain operation with bounded range, not a general numeric sum.","Test aggregation code paths with max-precision max-value prices."],"tags":["rust","arithmetic-overflow","panic","price","addition"],"backgroundTag":"arithmetic-overflow","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}