Skip to content

@livefolio/sdk / applyOrders

Function: applyOrders()

applyOrders(portfolio, orders): Portfolio

Defined in: portfolio/apply.ts:188

Projects a portfolio forward through a set of pending (unfilled) orders, returning a structurally updated snapshot. Used by strategy build helpers to read the expected post-step state before fills arrive.

v0.4 contract — structural projection only. Quantities are updated exactly as the orders specify, but:

  • cash is left unchanged (no price is available at projection time).
  • Newly opened positions have basis: 0 and entry.price: 0 as provisional values. A price-aware projection is planned for a later phase.

Use applyFills (not this function) to settle the portfolio after confirmed execution.

Parameters

ParameterTypeDescription
portfolioPortfolioThe current portfolio state to project from.
ordersreadonly Order[]The pending orders to apply structurally. Order must have a valid id and kind; price fields are ignored.

Returns

Portfolio

A new Portfolio with positions reflecting the orders. cash and t are copied unchanged from portfolio.

Example

ts
import { applyOrders } from '@livefolio/sdk';
import type { Portfolio, Order } 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 projected = applyOrders(portfolio, [order]);
// projected.positions.length === 1, projected.cash === 10_000 (unchanged)

Released under the MIT License.