nautechsystems/nautilus_trader · error

invalid betting balance impact

Error message

invalid betting balance impact

What it means

This panic comes from an `.expect("invalid betting balance impact")` in `balance_impact` for betting accounts (crates/model/src/accounts/betting.rs:152). The function computes a signed balance impact from order side, quantity, and price: Sell impacts the balance by -quantity, Buy by -(quantity * (price - 1)). The computed decimal is wrapped into a `Money` via `Money::from_decimal`, which fails (and thus panics) when the decimal cannot be represented as a valid Money amount in the instrument's quote currency (e.g. wrong precision or invalid value).

Source

Thrown at crates/model/src/accounts/betting.rs:152

    /// For `Buy` (lay) the impact is the negative liability (quantity * (price - 1)).
    ///
    /// # Panics
    ///
    /// Panics if the impact cannot be represented in the quote currency.
    #[must_use]
    pub fn balance_impact(
        &self,
        instrument: &InstrumentAny,
        quantity: Quantity,
        price: Price,
        order_side: OrderSide,
    ) -> Money {
        let currency = instrument.quote_currency();
        let impact = match order_side {
            OrderSide::Sell => -quantity.as_decimal(),
            OrderSide::Buy => -(quantity.as_decimal() * (price.as_decimal() - Decimal::ONE)),
        };
        Money::from_decimal(impact, currency).expect("invalid betting balance impact")
    }

    /// Recalculates the account balance for the specified currency based on per-instrument locks.
    pub fn recalculate_balance(&mut self, currency: Currency) {
        base::recalculate_balance(&mut self.base.balances, &self.balances_locked, currency);
    }
}

impl Account for BettingAccount {
    impl_account_base_members!();

    fn is_cash_account(&self) -> bool {
        true
    }

    fn is_margin_account(&self) -> bool {
        false
    }

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Round the computed price and quantity to the instrument's price/size precisions (and the currency's precision) before calling balance_impact.
  2. Verify the instrument's quote_currency is correctly configured and its precision can represent the impact decimal.
  3. Check the input Price/Quantity decimals for abnormal magnitude or precision (e.g. from bad feed data) before the call.
  4. If you control the call site, use Money::from_decimal's Result form (or try_from) and handle the error instead of relying on the internal expect.

Example fix

// before
let impact = balance_impact(OrderSide::Buy, &price, &quantity, &instrument);
// after
let price = Price::new(raw_price, instrument.price_precision());
let quantity = Quantity::new(raw_qty, instrument.size_precision());
let impact = balance_impact(OrderSide::Buy, &price, &quantity, &instrument);
Defensive patterns

Strategy: validation

Validate before calling

// rust
fn balance_impact_inputs_ok(price: Price, quantity: Quantity, instrument: &Instrument) -> bool {
    price.precision <= instrument.price_precision()
        && quantity.precision <= instrument.size_precision()
        && quantity.as_decimal() > Decimal::ZERO
        && price.as_decimal() > Decimal::ZERO
}

Prevention

When it happens

Trigger: Calling `balance_impact` (or the Python binding `py_balance_impact`) with a quantity/price whose computed impact decimal cannot be converted to a valid `Money` in the instrument's quote currency — e.g. a decimal with more precision than the currency allows, or a malformed/NaN-like decimal produced by extreme price values.

Common situations: Betting/exchange adapters computing margin or balance locks with prices carrying more decimal places than the quote currency's precision; custom account code calling `balance_impact` directly with hand-built `Price`/`Quantity` values that violate currency precision; a symbol whose quote currency precision was misconfigured.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08). Data as JSON: /api/errors/9383e12d2b21778e. Report an issue: GitHub.