RiskEngine admits buys that together exceed a cash account's free balance
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,devdevelop, oranightly) 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, becauseAccountRiskis built per command (1359). - A cash account's
lockedcovers open orders only (crates/portfolio/src/manager.rs:405,is_open()fromAccepted), and a wallet account's covers in-flight orders too (:522). Until the venue acknowledges an approved order,freedoes not include it. - In the backtest, the first order is still
INITIALIZEDwhen the second is checked, becauseOrderSubmittedis 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
- Cash account holding 1,000 USD, borrowing disabled. Instrument AUD/USD.SIM.
- Submit BUY LIMIT 600 @ 1.00000. It is approved.
- Before any venue event for it, submit a second BUY LIMIT 600 @ 1.00000.
- 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: 1Cost-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
freeis 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.
- Each approved order's charge against
- B. The simulated venue rejects a cash-account buy whose notional exceeds
freeat 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_idunset onOrderUpdatedandOrderModifyRejected, and theevents.order.*handler skips re-entrant events (174-187). - So holding an increase until its outcome needs one of two things: venues set
causation_idto 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_traderversion: develop @ f73a6acb2d9dcdbad2c801fb8067646908558792- Installed from: built from source (
cargo test -p nautilus-risk), Rust 1.98.1 - Adapter/venue: none (
RiskEnginewith a simulated cash account)
Source: nautechsystems/nautilus_trader