The backtest ended with a position still open: how to reconcile final equity

The strategy bought ten shares and sold four. The closed-trade table shows a $24.02 loss, but account equity fell by $77.13 over the same period. Equity includes the six shares still in the account. Using the unrounded figures, the gap is $53.10450, or $53.10 to the nearest cent.
These figures come from a worked example in backtesting.py 0.6.5. To reconcile an XTester report, use the prices and closing rules from your own run.
What made it into the journal
We use eight GOOG daily bars from the backtesting.py example dataset, August 19–30, 2004. The account starts with $10,000 and no shares. The model charges 0.1% of each fill’s value, with zero spread. There are no deposits, payouts or other instruments. The strategy sends two market orders: buy ten shares, then close 40% of the position. Both fill at the next bar’s open.
August 23: buy 10 shares at $110.75. The entry fee is $1.10750 for all ten shares.
August 25: sell 4 shares at $104.96. The sale incurs a $0.41984 fee. Six shares remain.
August 30: final valuation. The bar closes at $102.01. In the main run, final liquidation is disabled: there is no further sale in the journal. The report shows one closed trade, six shares still open at the end of the day, and equity of $9,922.87266.
Where the $53.10 comes from
The four shares sold lost $23.16 before fees: 4 × (104.96 − 110.75). Their share of the entry fee is $0.44300, and the sale cost $0.41984. The closed trade therefore shows −$24.02284. Those fees are already in the row; don’t subtract them again.
The six unsold shares lost $52.44 relative to entry: 6 × (102.01 − 110.75). Buying them also cost $0.66450. The engine charged that portion of the entry fee on August 23. Including the fee, the remaining shares account for a loss of $53.10450.
Without intermediate rounding: 10,000 − 24.02284 − 52.44 − 0.66450 = 9,922.87266. That is the last point on the equity curve. Round every line to cents first and you introduce a one-cent discrepancy.
What if the remaining shares are closed?
For the second run we change just one setting: we enable finalize_trades. The strategy emits no new signal, but the engine records a sale of six shares. In this version, with these settings, the fill price is $105.28, the open of the last daily bar. In the first run the remaining shares were valued at the close of that same bar, $102.01.
There are now two closed trades, no open position and final equity of $9,941.86. That is $18.99 higher: the price difference contributed 6 × (105.28 − 102.01) = $19.62, less $0.63168 in selling fees. The difference comes from the forced sale; the strategy signals stayed the same. Another engine might use the same price for the exit and the final valuation.
How to check your XTester report
Subtract initial equity from final equity and compare the difference with the closed-trade result for the same run. In the XTester overview, follow the solid strategy line. The dotted line shows buy and hold. If you traded several instruments, include trades in each one.
Find the last fill and use the quantities to calculate the remaining position. The XTester help example for 1.0.0-preview.1 shows the open position separately from closed trades. Check whether the panel warns that only some rows are visible. The last visible trade is not necessarily the last in the complete journal.
For a nonzero position, find the price and time used to value it. A sale row with quantity, price and fee confirms a simulated fill; the last chart point alone does not. Match fees already charged against those included in closed-trade results. Reconcile any funding, deposits or withdrawals separately. If the valuation price or part of the journal is missing, leave the difference unexplained: the final candle cannot replace a missing record.
By August 30 the strategy had sold four of its ten shares for a loss of $24.02. The engine valued the remaining shares at $102.01 each, bringing the account loss to $77.13. When comparing two runs, compare their changes in equity first, then their closed-trade results.
Sources: XTester help: statistics and trades · backtesting.py documentation: Backtest parameters · Engine source, version 0.6.5.
This material is for information only and is not investment advice. Backtest results do not guarantee future returns.


