Skip to content

๐Ÿ”’ Balance Validation

Transaction batches perform a final end-of-day balance replay before the response is finalized. The stage wrapper is transaction_batch_stages.validate_balances(); the broker replay itself remains TransactionService._validate_broker_balances().

This is validation inside the caller's current database transaction. It is not a commit and it does not own rollback.


๐Ÿงญ Pipeline Boundary

Balance replay runs after delete, split, update, updated-pair validation, create, promote, create-link resolution, inline WAC/FX handling, and required-cost-basis validation.

TransactionBatchContext.earliest_date_by_broker is updated by successful mutation stages. For each broker, the context retains the minimum date reported by those operations; a moved update uses the earlier of its old and new dates. Immediately before replay, validate_balances():

  1. calls session.flush() so queries see the staged mutations;
  2. maps successful result IDs back to their operation and batch index for issue attribution; and
  3. calls _validate_broker_balances(broker_id, from_date, batch_tx_ids=...) for each affected broker.

โš–๏ธ What Is Validated

Two independent dimensions are tracked per broker:

Dimension Unit Violation
Cash Amount per currency code End-of-day cash balance below zero
Asset holdings Quantity per asset ID End-of-day asset quantity below zero

Broker settings control each check:

Broker flag Effect when True
allow_cash_overdraft Negative cash is permitted
allow_asset_shorting Negative asset quantity is permitted

When both flags are true, _validate_broker_balances() returns without querying or replaying transactions.


๐Ÿ“… End-of-day Replay

For an incremental replay beginning at from_date, the service first obtains cash and asset balances from all rows with date < from_date (_get_balances_before_date(), summed in Python, never in SQL: see Balance Queries). It then queries the broker's rows from from_date onward, groups them by date, and applies every row for a date before checking either balance dimension:

balances = SUM(all rows before from_date)

for from_date, then each later date that holds a transaction, in order:
    apply every cash and quantity delta dated that day
    validate all cash balances unless overdraft is allowed
    validate all asset balances unless shorting is allowed

The invariant is therefore end-of-day, not per-row or intraday. Opposing movements on the same date are netted before validation. A later date cannot repair an earlier negative close because each visited date is checked before replay advances.

The replay visits only those dates (R13-SEC7-3): the days that hold a transaction, and from_date, because the balances carried into it may already be negative. No balance changes on a day without transactions, so the first negative close always falls on one of them, and the result is the one a walk over every calendar day gives. Without a from_date โ€” the full replay BrokerService runs when an update turns allow_cash_overdraft or allow_asset_shorting off โ€” the visited dates are the transaction days alone, from the broker's first row. The cost grows with the number of days that hold a transaction, not with the calendar span between them. Before, the replay stepped through every calendar day from its start to the broker's last row, on the event loop: about 740 000 days for a replay starting at a row dated 0001-01-01, while a row dated 9999-12-31 made every later replay of its broker walk up to it, then fail on the date's overflow (a Balance validation error without a code). Such dates can no longer be written (see Transaction dates).

Starting at the earliest affected date preserves correctness for backdated creates, deletes, splits, promotions, and updates without replaying the broker's complete history on every batch.


๐Ÿšจ Issue Shape and Stop Condition

BalanceValidationError is defined in backend/app/services/transactions/transaction_batch_context.py and carries:

broker_id: int
date: date
currency_or_asset: str
balance: Decimal
code: str
params: dict
batch_index: int
batch_operation: str

The two stable validation codes are:

Code Condition
balanceCashNegative A currency closes a day below zero
balanceAssetNegative An asset quantity closes a day below zero

Before either, the replay counts the broker's transactions whose quantity or amount cannot be read โ€” a stored value that is no number reads None since W-B58 โ€” with count_unreadable_rows() (unreadable_rows.py), every date included: the starting balances skipped such a row (a wrong balance, the batch checked against it) and the walk failed on it (a codeless error). Any such row stops the replay with a third code, at index โˆ’1 since it is the broker's and not a row's (W-B74):

Code Condition
ledgerUnreadable params {brokerId, count}: that many transactions of the broker hold a value that is not a number, so its balances cannot be checked

The broker's list cannot show such rows either; the way out is a data repair (W-B63). A broker that allows both overdraft and shorting is not replayed, so it is not refused either.

The frontend turns each code into transactions.errors.<code> (resolveIssueMessage() in lib/features/transactions/resolveValidationMessage.ts): Saving would cause โ€ฆ to go negative โ€ฆ on {date} at {brokerName}. The bulk workspace groups these issues under This configuration causes data inconsistencies (transactions.validate.balanceIssuesHeader), each with the workspace rows that contribute to it.

When possible, the replay attributes the issue to the last staged batch transaction that reduced that currency or asset on the failing date. Otherwise it uses operation create and index -1 as a broker-level fallback.

validate_balances() wraps the broker loop in one try block. It catches the first BalanceValidationError, appends one TXValidationIssue, and exits the stage; it does not continue collecting later balance exceptions from other currencies, dates, or brokers. Unexpected replay exceptions likewise become one broker-level balance-validation issue.


๐Ÿ” Transaction Ownership

The replay only flushes the session. Neither the stage nor execute_batch() calls commit() or rollback():

  • /transactions/validate always rolls back after receiving the response;
  • /transactions/commit commits only when response.committed is true;
  • any balance issue makes committed false, so the commit route rolls back all staged mutations.

This caller-owned boundary is what makes a multi-broker batch atomic while still allowing the same service pipeline to power a dry run.


โ“ Why End-of-day Instead of a DB Constraint?

Running balances depend on the aggregate of all prior rows and cannot be expressed as a simple row constraint. Checking once after all rows for a date also avoids rejecting a valid same-day set merely because of insertion order, while still rejecting a negative close before a later day's deposit or acquisition can mask it.