Skip to content

@livefolio/sdk / reconcile

Function: reconcile()

reconcile(targets, portfolio, prices): readonly RebalanceOrder[]

Defined in: strategy/reconcile.ts:67

Converts target portfolio weights into a minimal set of RebalanceOrder instructions that move the current portfolio toward the desired allocation.

The algorithm:

  1. Compute total portfolio value as cash + Σ(held_shares × price).
  2. For each target asset, derive targetShares = floor(totalValue × weight / price).
  3. Emit a RebalanceOrder with delta = targetShares - heldShares for any asset where the delta is non-zero (positive = buy, negative = sell).
  4. Emit exit orders (delta = -heldShares) for any long position in an asset that does not appear in targets.

Only long positions are considered — short positions in the portfolio are ignored. Share counts are always floored to integer lots.

Parameters

ParameterTypeDescription
targetsTargetWeightsDesired weight per asset. Keys are AssetId strings. A weight of 0 is valid and will result in a full exit for any existing position.
portfolioPortfolioCurrent portfolio. Cash and long positions determine total value and existing share counts.
pricesPriceMapCurrent prices for all assets that appear in targets or are currently held. Throws Error if a target asset is missing from this map.

Returns

readonly RebalanceOrder[]

A readonly array of RebalanceOrder objects. The array may be empty if the portfolio is already at the target allocation. Order IDs are deterministic within a single call (rebal_<assetId>_<counter>).

Example

ts
import { reconcile } from '@livefolio/sdk';

const targets = new Map([
  ['US:SPY', 0.6],
  ['US:BND', 0.4],
]);
const prices = new Map([
  ['US:SPY', 440.0],
  ['US:BND', 75.0],
]);

// Empty portfolio with $100 000 cash
const portfolio = { cash: 100_000, positions: [] };
const orders = reconcile(targets, portfolio, prices);
// orders => [
//   { id: 'rebal_US:SPY_0', kind: 'rebalance', asset: { ... }, delta: 136 },
//   { id: 'rebal_US:BND_1', kind: 'rebalance', asset: { ... }, delta: 533 },
// ]

Released under the MIT License.