@livefolio/sdk / reconcile
Function: reconcile()
reconcile(
targets,portfolio,prices): readonlyRebalanceOrder[]
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:
- Compute total portfolio value as
cash + Σ(held_shares × price). - For each target asset, derive
targetShares = floor(totalValue × weight / price). - Emit a
RebalanceOrderwithdelta = targetShares - heldSharesfor any asset where the delta is non-zero (positive = buy, negative = sell). - Emit exit orders (
delta = -heldShares) for any long position in an asset that does not appear intargets.
Only long positions are considered — short positions in the portfolio are ignored. Share counts are always floored to integer lots.
Parameters
| Parameter | Type | Description |
|---|---|---|
targets | TargetWeights | Desired weight per asset. Keys are AssetId strings. A weight of 0 is valid and will result in a full exit for any existing position. |
portfolio | Portfolio | Current portfolio. Cash and long positions determine total value and existing share counts. |
prices | PriceMap | Current 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 },
// ]