Accounting for Perpetual Futures Funding in Backtests
Summary
This document describes a ledger for applying funding cash flows to perpetual futures positions during a backtest. At each funding timestamp, it uses the position’s signed quantity, the current mark, any contract multiplier, and the funding rate to calculate the cash adjustment. A positive funding rate therefore has opposite effects for long and short positions.
The ledger normalizes timestamps to UTC, rejects missing or duplicate settlement keys and non-finite rates, and prevents a timestamp from being applied twice. It wraps the broker’s time update so settlement follows the mark update, and reports cumulative funding P&L, event count, and settlement count. Metrics require every supplied funding-rate key to have reached the engine timeline. The approach depends on valid marks being available at settlement for open positions; it raises an error when one is missing. This is accounting infrastructure for backtests, not evidence that a trading strategy is profitable.
Key ideas
- Funding cash flow is calculated from signed position size, marked value, multiplier, and the funding rate.
- Settlement is applied after the broker updates marks for the timestamp.
- UTC normalization and duplicate protection help prevent missed or repeated settlements.
- The metrics check requires all supplied settlement timestamps to appear on the engine timeline.
- An open position without a current mark causes settlement to fail.
Tags
Full text
# funding_backtest.py
```py
"""Funding-settlement accounting for perpetual-futures engine backtests."""
from __future__ import annotations
import math
from datetime import UTC, datetime
from functools import wraps
from typing import Any
import polars as pl
def _as_utc(value: datetime) -> datetime:
return value.replace(tzinfo=UTC) if value.tzinfo is None else value.astimezone(UTC)
class FundingSettlementLedger:
"""Apply position-signed funding during the engine's bar-time update."""
def __init__(self, funding_rates: pl.DataFrame) -> None:
required = {"symbol", "timestamp", "funding_rate"}
missing = required - set(funding_rates.columns)
if missing:
raise ValueError(f"funding rates are missing columns: {sorted(missing)}")
selected = funding_rates.select("symbol", "timestamp", "funding_rate")
if selected.null_count().row(0) != (0, 0, 0):
raise ValueError("funding settlements cannot contain null keys or rates")
if selected.n_unique(["symbol", "timestamp"]) != selected.height:
raise ValueError("funding settlement keys must be unique")
self._rates: dict[datetime, dict[str, float]] = {}
for row in selected.sort("timestamp", "symbol").iter_rows(named=True):
rate = float(row["funding_rate"])
if not math.isfinite(rate):
raise ValueError("funding rates must be finite")
self._rates.setdefault(_as_utc(row["timestamp"]), {})[str(row["symbol"])] = rate
self._rate_count = selected.height
self._settled_timestamps: set[datetime] = set()
self._funding_pnl = 0.0
self._funding_events = 0
self._funding_settlements = 0
self._installed = False
def settle(self, timestamp: datetime, broker: Any) -> float:
"""Settle one timestamp exactly once against positions marked on that bar."""
normalized = _as_utc(timestamp)
if normalized in self._settled_timestamps:
return 0.0
rates = self._rates.get(normalized)
if rates is None:
return 0.0
self._settled_timestamps.add(normalized)
self._funding_settlements += len(rates)
event_cash = 0.0
for symbol, rate in rates.items():
position = broker.positions.get(symbol)
if position is None or float(position.quantity) == 0.0:
continue
mark = broker.get_mark_price(symbol, quantity=position.quantity)
if mark is None:
raise RuntimeError(f"funding settlement has no current mark for {symbol!r}")
event_cash -= (
float(position.quantity)
* float(mark)
* float(getattr(position, "multiplier", 1.0))
* rate
)
if event_cash:
broker.cash = float(broker.cash) + event_cash
self._funding_pnl += event_cash
self._funding_events += 1
return event_cash
def install(self, broker: Any) -> None:
"""Install settlement immediately after each engine mark update."""
if self._installed:
raise RuntimeError("funding settlement ledger is already installed")
original_update_time = broker._update_time
@wraps(original_update_time)
def update_time_with_funding(timestamp, *args, **kwargs):
result = original_update_time(timestamp, *args, **kwargs)
self.settle(timestamp, broker)
return result
broker._update_time = update_time_with_funding
self._installed = True
def metrics(self) -> dict[str, float]:
"""Return cashflows actually presented to the engine timeline."""
if self._funding_settlements != self._rate_count:
raise RuntimeError(
"funding settlement coverage is incomplete: "
f"{self._funding_settlements}/{self._rate_count} keys reached the engine timeline"
)
return {
"funding_pnl": self._funding_pnl,
"funding_events": float(self._funding_events),
"funding_settlements": float(self._funding_settlements),
}
```Shown in full with attribution under the source's licence. Licence: MIT
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.