@livefolio/sdk / applyFills
Function: applyFills()
applyFills(
portfolio,fills,orders):Portfolio
Defined in: portfolio/apply.ts:54
Applies a batch of confirmed fills to a portfolio, returning a new Portfolio snapshot. This is the single function that advances portfolio state after order execution.
For each fill the corresponding order is looked up in orders by fill.orderRef. The order's kind determines the accounting treatment:
'open'— adds a new Position and debits cash.'close'— removes shares from an existing position and credits cash.'adjust'— updates the position'squantity; only fees are debited.'rebalance'— buys or sells shares in the long position forasset; creates or removes the position as needed.
The returned portfolio.t is updated to the maximum fill timestamp.
Parameters
| Parameter | Type | Description |
|---|---|---|
portfolio | Portfolio | The current portfolio state before this batch. |
fills | readonly Fill[] | Execution confirmations returned by Executor.submit. Each fill's orderRef MUST match an id in orders. |
orders | readonly Order[] | The full order batch that was submitted. Used to look up order details for each fill. |
Returns
A new Portfolio with updated positions, cash, and timestamp. The input portfolio is not mutated.
Example
ts
import { applyFills } from '@livefolio/sdk';
import type { Portfolio, Order, Fill } from '@livefolio/sdk';
const portfolio: Portfolio = { cash: 10_000, positions: [], t: new Date('2024-01-01') };
const order: Order = {
id: 'ord_1', kind: 'open',
asset: { kind: 'equity', id: 'AAPL', symbol: 'AAPL' },
side: 'long', quantity: 10,
};
const fill: Fill = { orderRef: 'ord_1', t: new Date('2024-01-02'), quantity: 10, price: 185, fees: 0 };
const next = applyFills(portfolio, [fill], [order]);
// next.cash === 8_250, next.positions.length === 1