A BTC perpetual backtest showed a funding debit on a position that had already been closed. The trade’s price P&L was right. The funding rate was right. The error was in the event sequence: the simulator charged funding using the position from the previous bar, even though the exit had executed before the funding snapshot.
Here’s the trade, taken apart from the exchange event to the portfolio ledger. The numbers are illustrative, but the accounting problem is real: funding belongs to the position held at the applicable funding time, under that venue’s rules.
The contract: BTCUSDT perpetual
Assume a linear BTCUSDT perpetual, a position size of 0.20 BTC, and a mark price of $50,000 at the funding timestamp. The position’s funding notional is therefore $10,000. The published funding rate is +0.01% for the interval.
For this example, use the common convention that a positive rate means longs pay shorts. A long position owes $1.00: $10,000 × 0.0001. A short of the same size receives $1.00. That sign convention is common, but the implementation should follow the venue’s contract and rate definition rather than assume every instrument uses the same one.
| Input | Value | Why it matters |
|---|---|---|
| Position | Long 0.20 BTC | Funding applies to the eligible position at the funding event |
| Mark price | $50,000 | Sets the example’s $10,000 notional |
| Funding rate | +0.01% | Long pays under this venue convention |
| Expected cash flow | −$1.00 | $10,000 × 0.0001, debited from the long |
The timeline: exit before the funding snapshot
At 07:59:58 UTC, the strategy sends a reduce-only sell order. It fills at 07:59:59, flattening the position. The funding event is at 08:00:00. No position remains at the snapshot, so this trade should incur no funding for that event under the assumed venue rule.
The timestamps look almost comically close. That’s why the event order matters. If the backtest stores both actions under an 08:00 minute bar, a bar-level simulation can’t infer which came first from the bar label alone.
The rate: correct value, wrong application
The buggy simulator loads the funding record, sees that the strategy held 0.20 BTC during the preceding interval, and calculates a $1 debit. It has the correct rate and a plausible notional. It still applies the cash flow to the wrong position state.
That bug can survive a basic review because the funding series matches the exchange history and the amount has the expected scale. The missing field is the position snapshot used for the payment. A funding row without a precise event time and a rule for determining eligible positions is only half an accounting instruction.
The position: which state does the event read?
Give the simulator an explicit sequence at the funding boundary. First process fills that the venue says occur before the funding snapshot; then capture eligible positions; then calculate and post funding. If the actual event ordering is uncertain at the data’s resolution, the backtest should represent that uncertainty rather than silently choose the favorable sequence.
For minute data, a fill timestamp of 07:59:59 and a funding event at 08:00:00 may be enough to order events. If the only evidence is an 08:00 bar close, it isn’t. You need finer event data or a conservative assumption, such as charging the position if the ordering cannot be established.
The ledger: one debit that changes the result
Suppose the strategy made $18 gross on the round trip and paid $2.40 in trading fees. Correct net P&L is $15.60. The stale-position funding debit makes it $14.60, understating this trade by $1.00. Across hundreds of funding boundaries, the error can become directional: a backtest that repeatedly closes just before the timestamp may be charged for positions it no longer held.
Funding should post as its own cash ledger event, linked to the instrument, rate record, event time, position quantity and mark used. Don’t bury it inside a per-bar return. That makes a mismatch inspectable: you can ask which position the simulator charged, and reproduce the calculation from the stored inputs.
The replay: test the boundary both ways
For this exact case, replay the same rate and mark with the exit on either side of the funding event. The expected result should be zero funding after a confirmed pre-snapshot close, and a $1 debit for the long if it remains open at the snapshot.
- Close just before the funding snapshot: no payment under the assumed venue rule.
- Keep the long open through the snapshot: debit $1.00.
- Keep an equal short open: credit $1.00 under the same sign convention.
- Move the close timestamp to the snapshot: apply the venue’s documented ordering rule, and record that rule in the test.
A good funding backtest can explain every payment as a cash event against a specific position at a specific instant. If it can only say “the rate was 0.01% that hour,” it hasn’t yet shown that it charged the right trade.
← All posts


