@livefolio/sdk / BacktestExecutor
Class: BacktestExecutor
Defined in: reference/backtest-executor.ts:104
Reference Executor implementation for backtesting. Fills each order at the next-open price returned by the NextOpenFn callback, with optional slippage and per-share commissions applied.
When to use: suitable for historical simulations and unit tests where real broker connectivity is not needed. For live or paper trading, substitute a broker-backed Executor that satisfies the same interface.
Fill mechanics: for each order in orders, the executor calls opts.nextOpen(asset, t) to obtain the fill price and timestamp. The raw price is then adjusted for slippage:
adjustedPrice = nextOpen.price × (1 + sign × slippageBps / 10 000)where sign is +1 for net-buy direction and −1 for net-sell direction. A flat per-share fee is added to Fill.fees. Orders with zero quantity are silently skipped.
Example
import { BacktestExecutor } from '@livefolio/sdk';
import { getCalendar } from '@livefolio/sdk';
const executor = new BacktestExecutor({
calendar: getCalendar('NYSE'),
nextOpen: async (asset, t) => {
// Return the first open bar strictly after t from your data feed.
const bar = await feed.nextBar(asset, t);
return { t: bar.t, price: bar.open };
},
slippageBps: 5, // 0.05% one-way
perShareFee: 0.005,
});Implements
Constructors
Constructor
new BacktestExecutor(
opts):BacktestExecutor
Defined in: reference/backtest-executor.ts:105
Parameters
| Parameter | Type |
|---|---|
opts | BacktestExecutorOptions |
Returns
BacktestExecutor
Methods
submit()
submit(
orders,t,portfolio):Promise<readonlyFill[]>
Defined in: reference/backtest-executor.ts:107
Submits a batch of orders for execution and returns the resulting fills.
Parameters
| Parameter | Type | Description |
|---|---|---|
orders | readonly Order[] | The orders to execute. Each order has a unique id; implementations use id to correlate fills via fill.orderRef. |
t | Date | The logical "now" timestamp at which orders are being submitted (e.g. end-of-day for a daily-rebalance strategy). |
portfolio | Portfolio | The current portfolio state at time t. Implementations may use this to resolve position details for close/adjust orders. |
Returns
Promise<readonly Fill[]>
A readonly array of Fill records. One fill per executed order; omit orders that result in zero quantity.