NautilusTrader
ConceptsBacktesting

Nightly docs

These docs track unreleased changes and may change without notice. Their code examples can use APIs that the latest release lacks, so run them with a development wheel or switch to the latest release docs.

Backtest Accounts and Margin

Backtest venues use simulated accounts for balances, margin, and funding settlement. For the full account model and margin formulas, see Accounting.

Funding

Backtests settle perpetual funding from FundingRateUpdate data at funding boundaries:

  • With next_funding_ns, the simulated exchange stores the latest rate, and the backtest clock emits one FundingSettlement at that timestamp.
  • Without next_funding_ns, the exchange settles only when ts_event lands on the interval boundary.
  • Updates without a boundary stay strategy data and do not create funding payments.

The settlement adjusts the open position and the matching account balance before the portfolio observes the new state.

PositionAdjusted stays the position accounting event. A positive funding rate debits long positions and credits short positions. The adjustment changes realized PnL, and the matching account balance update records the cash movement.

Accounts

Each backtest venue uses one of three account_type values: CASH, MARGIN, or BETTING. The low-level API accepts model types directly:

from decimal import Decimal

from nautilus_trader.backtest import BacktestEngine
from nautilus_trader.config import BacktestEngineConfig
from nautilus_trader.execution import MakerTakerFeeModel
from nautilus_trader.model import AccountType
from nautilus_trader.model import Money
from nautilus_trader.model import OmsType
from nautilus_trader.model import Venue

engine = BacktestEngine(BacktestEngineConfig())
engine.add_venue(
    venue=Venue("BINANCE"),
    oms_type=OmsType.NETTING,
    account_type=AccountType.CASH,
    starting_balances=[Money.from_str("10_000 USDT")],
    fee_model=MakerTakerFeeModel(
        maker_rate=Decimal("0.001"),
        taker_rate=Decimal("0.001"),
    ),
)

The high-level API accepts the same enum values but represents starting balances as strings:

from decimal import Decimal

from nautilus_trader.config import BacktestVenueConfig
from nautilus_trader.execution import MakerTakerFeeModel
from nautilus_trader.model import AccountType
from nautilus_trader.model import BookType
from nautilus_trader.model import OmsType

venue = BacktestVenueConfig(
    name="SIM",
    oms_type=OmsType.NETTING,
    account_type=AccountType.CASH,
    book_type=BookType.L1_MBP,
    starting_balances=["10_000 USDT"],
    fee_model=MakerTakerFeeModel(
        maker_rate=Decimal("0"),
        taker_rate=Decimal("0"),
    ),
)

Margin models

Margin accounts use LeveragedMarginModel by default. To reserve the instrument's fixed initial and maintenance margin percentages without account leverage reductions, pass StandardMarginModel.

from decimal import Decimal

from nautilus_trader.config import BacktestVenueConfig
from nautilus_trader.execution import MakerTakerFeeModel
from nautilus_trader.model import AccountType
from nautilus_trader.model import BookType
from nautilus_trader.model import OmsType
from nautilus_trader.model import StandardMarginModel

venue = BacktestVenueConfig(
    name="SIM",
    oms_type=OmsType.NETTING,
    account_type=AccountType.MARGIN,
    book_type=BookType.L1_MBP,
    starting_balances=["1_000_000 USD"],
    fee_model=MakerTakerFeeModel(
        maker_rate=Decimal("0"),
        taker_rate=Decimal("0"),
    ),
    margin_model=StandardMarginModel(),
)

BacktestVenueConfig accepts built-in and custom Python margin model objects directly. See Python model selection for the callback contract. The high-level configuration does not load custom margin models from class-path strings.

On this page