Track fills, cash and marked holdings in a backtest, distinguish cash movements from profit, and reconcile an account before measuring performance.
Cash accounting in backtests
Cash accounting in backtests is the event-by-event recording of money paid and received as a simulated strategy trades and holds positions. It tells you how a strategy funds its actual fills and where its cash goes. It must be reconciled with holdings: a cash balance alone cannot measure the performance of a backtest.
Here, “accounting” means the backtest's economic ledger. It does not prescribe a company's financial-reporting rules or a jurisdiction's tax treatment.
Buying shares is not losing their purchase price
Consider a synthetic account starting with USD 1,000 cash and no shares. Buy 10 shares at an actual fill price of USD 50. Later, value those shares at USD 55 each without trading again. Assume immediate cash booking, no settlement delay, no borrowing, and no fees, interest, dividends, taxes or external contributions or withdrawals.
Event
Cash change, USD
Cash balance, USD
Shares held
Shares' market value, USD
Total equity, USD
Before purchase
—
1,000
0
0
1,000
Buy 10 at 50; mark at 50
−500
500
10
500
1,000
Revalue at 55; no trade
0
500
10
550
1,050
The purchase converts USD 500 of cash into USD 500 of shares. It creates no immediate investment loss under these assumptions. The subsequent price rise creates a USD 50 unrealised gain while cash stays at USD 500.
USD 1,000 cash becomes USD 500 cash and USD 500 of shares after buying 10 at 50. At a later mark of 55, cash remains 500 and shares are worth 550, making total equity USD 1,050.
Original synthetic USD ledger. Stacked cash and share values show the same account before purchase, after its fill and at a later mark. No fees, interest, dividends, external flows or settlement delay are modelled.
The account's gain is 5% of its USD 1,000 starting equity. The share price rose 10%, but only half the initial account was invested. The cash balance fell 50% on entry; calling that a 50% investment loss would confuse an asset purchase with performance.
If all 10 shares are then sold at 55 without costs, USD 550 returns to cash. The account ends with USD 1,050 cash, no shares and USD 50 realised trading profit. The sale converts the marked holding into cash; it does not create a second USD 50 gain.
Keep three quantities separate
For this single-asset account, let C be cash in USD, q the number of shares and m the valuation mark in USD per share. Its equity is
E=C+qm.
A fill of signed quantity Δq at price p, with an explicit fee F≥0, updates the state as
q+=q−+Δq,C+=C−−Δqp−F.
A positive Δq buys shares and reduces cash; a negative quantity sells shares and increases cash before fees. Use filled quantity, not the desired position or an unfilled order. A new mark changes qm without itself changing C.
Cash movement, holding value and profit and loss therefore answer different questions. Cash movement explains payments and receipts. Holding value measures the remaining exposure at the chosen mark. With no external capital flows, total account P&L is the change in equity. A mark is a valuation convention, not a guarantee that the whole holding can be sold at that price.
Positions, Trades, Fills and P&L develops this ledger through partial fills, fees and changing marks. Its public preview is available; the full lesson requires membership.
What a usable cash ledger must record
Record each event's time, currency, signed amount, cause and related position or fill. Then reconcile cash and holdings before calculating the equity curve. In particular:
Costs: explicit fees reduce cash. Spread or slippage already contained in fill prices affects the ledger through those prices; deducting the same cost again double counts it. Transaction costs need a consistent booking convention.
Income and financing: dividends, cash interest, funding charges and stock-borrow charges need their own dated events. If adjusted return data already include a distribution, adding its cash payment again can overstate performance.
External capital: a deposit raises cash and equity but is not strategy profit. Contributions and withdrawals must be identified separately, and percentage returns need a declared treatment of their timing.
Spendability: a booked receipt need not be immediately available to trade or withdraw. Where settlement delays, restricted collateral or borrowing matter, track them explicitly rather than interpreting total cash as buying power.
QuantConnect's official Cashbook documentation illustrates separate settled and unsettled cash records. Its backtest results definitions also distinguish equity from cash and position values. Exact balance labels and booking rules vary across engines and account models; the reconciliation must follow the model actually being tested.
A minimal diagnostic is to replay the ledger from its starting state: every fill must explain a holding change and a signed cash change, every cash-only event must have a cause, and a valuation update must not invent a cash receipt. A correct cash sum is necessary; it becomes a performance record only when the open positions and external flows also reconcile.
Sources
Ernest P. Chan, Quantitative Trading: How to Build Your Own Algorithmic Trading Business, Wiley, 2009, Chapter 2, pp. 20–22. Discusses equity-based performance and the absence of contributions or redemptions in that comparison. The event ledger and all numbers here are original synthetic examples.
QuantConnect: Portfolio — Cashbook. Official engine documentation for trade cash changes, account currency, settled and unsettled balances, deposits and withdrawals.
QuantConnect: Local Platform — Backtesting Results. Official distinction between portfolio equity, position values and unrealised results; used here for the cash-versus-equity distinction rather than a universal liquidation-cost convention.