RiskEngine admits buys that together exceed a cash account's free balance

Author: ngaridCreated Sep 27, 2026Updated Sep 29, 2026
Labelsbug

Bug Report

Confirmation

  • I've re-read the relevant sections of the documentation.
  • I've searched existing issues and discussions to avoid duplicates.
  • I've reviewed or skimmed the source code (or examples) to confirm the behavior is not by design.
  • I've tested this issue using a recent pre-release or development wheel (2.0.0rcN, dev develop, or a nightly) and can still reproduce it.

Reproduced on develop (f73a6acb2d) with an integration test against the Rust RiskEngine, and end to end in a BacktestEngine on 2.0.0rc5 and a current develop wheel.

Expected behavior

The risk engine checks "Cash-account balance impact for non-margin accounts" (docs/concepts/execution/index.md:197). A cash account locks the "Notional for pending buy orders" (docs/concepts/accounting.md:22), and free is the "amount available for new orders" (accounting.md:87). With borrowing disabled, a buy the account can no longer fund should be denied NOTIONAL_EXCEEDS_FREE_BALANCE, whether the earlier buy was sent in an earlier callback or in the same one.

Actual behavior

The check only sees orders the venue has already acknowledged. Two 600 USD buys sent from one callback on a 1,000 USD cash account both pass and both fill. On 2.0.0rc5 the backtest completes with the second debit dropped. On develop it stops with "Cash account balance would become negative: -200.00 USD" (#5077). Sent one quote apart, the second buy is denied (free=400.00 USD, notional=600.00 USD).

In crates/risk/src/engine/mod.rs:

  • The balance checks read account.balance_free(...) (2603, 2764; 1574 and 2503 for sells and margin). The cumulative totals only span one command, because AccountRisk is built per command (1359).
  • A cash account's locked covers open orders only (crates/portfolio/src/manager.rs:405, is_open() from Accepted), and a wallet account's covers in-flight orders too (:522). Until the venue acknowledges an approved order, free does not include it.
  • In the backtest, the first order is still INITIALIZED when the second is checked, because OrderSubmitted is buffered until the command batch drains (crates/backtest/src/execution_client.rs:196, engine.rs:2141). In live trading the window lasts until the venue acknowledges the order. I traced the live case in code only.

Steps to reproduce

  1. Cash account holding 1,000 USD, borrowing disabled. Instrument AUD/USD.SIM.
  2. Submit BUY LIMIT 600 @ 1.00000. It is approved.
  3. Before any venue event for it, submit a second BUY LIMIT 600 @ 1.00000.
  4. Expected: the second order is denied NOTIONAL_EXCEEDS_FREE_BALANCE (free 400, notional 600). Actual: both orders are sent to execution.

Code snippets or logs

I added this test to crates/risk/tests/risk_engine.rs, using the existing fixtures: test_submit_order_counts_approved_orders_not_yet_reflected_in_free_balance. On develop it fails at the denial count:

test test_submit_order_counts_approved_orders_not_yet_reflected_in_free_balance ... FAILED
  left: 0
 right: 1

Cost-raising amendments have the same gap. check_modify_orders_risk (1073) checks an amendment's increase against free, projected from the cached order. That order keeps its old values until OrderUpdated arrives. Take a resting 600 USD buy (free 400) amended to 1,000, then a new 400 USD buy in the same callback: both are approved, and the same-shaped test fails with left: 0, right: 1. The comment at 1491 covers pending reductions. A pending increase is the mirror case.

I have a fix and can open a PR if the approach is acceptable. The direction is your call:

  • A. The risk engine counts what it approved but the account does not show yet (my fix).
    • Each approved order's charge against free is kept until the cached order shows the account reflects it, or the order closes. The account reflects an open order, and for wallet accounts an in-flight one too.
    • The balance and margin checks deduct these charges from free.
    • Entries are released from order status at each command. This needs no order events, and the cost scales with unacknowledged orders only.
    • No new config, the existing denial reasons, and the same behavior in backtest and live.
  • B. The simulated venue rejects a cash-account buy whose notional exceeds free at acceptance, as a broker would. This covers backtest and sandbox only.

My fix covers new orders only. Amendments need a decision first:

  • Order status cannot show when an amendment is reflected. The simulated venue leaves causation_id unset on OrderUpdated and OrderModifyRejected, and the events.order.* handler skips re-entrant events (174-187).
  • So holding an increase until its outcome needs one of two things: venues set causation_id to the command id, or outcomes are matched per order in FIFO order.
  • Which do you prefer? Or should amendments stay venue-enforced?

Related: #5077, #5047, #4911, #5103, #4946, #4989.

Specifications

  • OS platform: macOS 26.0.1 (arm64)
  • Python version: n/a (Rust integration test)
  • nautilus_trader version: develop @ f73a6acb2d9dcdbad2c801fb8067646908558792
  • Installed from: built from source (cargo test -p nautilus-risk), Rust 1.98.1
  • Adapter/venue: none (RiskEngine with a simulated cash account)

Source: nautechsystems/nautilus_trader