fintech-algorithms
Using a coding agent? Give it the skill: npx skills add IslamBaraka90/Fintech-Algorithms-Library What it does →

Rebalancing Algorithm

Install and import#

bash
npm install fintech-algorithms
ts
import { calculate } from "fintech-algorithms/index-and-benchmark-engineering/governance-and-maintenance/rebalancing-algorithm";

Signature#

calculate(data)

Turns a weight change into the trades that implement it, and reports the turnover. Turnover is the number that predicts what tracking the index will cost.

Parameters#

NameTypeNotes
data{ records: Record[]; notional: number }records carry current and target weights per constituent; notional is the portfolio size the trades are sized against.

Returns#

{ trades, oneWayTurnover, grossTradeValue }

The trade list plus one-way turnover — stated as one-way explicitly, because quoting two-way turnover doubles the apparent cost.

Errors#

  • When notional is not positive — throws

Complexity: time O(n), space O(n).

Worked example#

verified This is the worked example published in the article, replayed by the test suite on every run. The output cannot drift.

Input#

data
{
  "records": [
    {
      "id": "A",
      "price": 50,
      "oldWeight": 0.35,
      "targetWeight": 0.3
    },
    {
      "id": "B",
      "price": 25,
      "oldWeight": 0.25,
      "targetWeight": 0.3
    },
    {
      "id": "C",
      "price": 80,
      "oldWeight": 0.22,
      "targetWeight": 0.2
    }
  ],
  "notional": 100000000
}

Call#

calculate(data)

Returns#

object with 3 fields: trades, oneWayTurnover, grossTradeValue

{
  "trades": [
    {
      "id": "A",
      "tradeValue": -5000000,
      "targetIndexShares": 600000
    },
    {
      "id": "B",
      "tradeValue": 5000000,
      "targetIndexShares": 1200000
    },
    {
      "id": "C",
      "tradeValue": -2000000,
      "targetIndexShares": 250000
    }
  ],
  "oneWayTurnover": 0.07,
  "grossTradeValue": 14000000
}

Diagrams#

Rebalancing Algorithm — article hero
Rebalancing Algorithm — failure guard
Rebalancing Algorithm — worked example

Calculation flow#

Rebalancing Algorithm calculation flow
flowchart LR
    A["Point-in-time inputs"] --> B["Validate units and timing"]
    B --> C{"Contract feasible?"}
    C -->|No| D["Reject with reason"]
    C -->|Yes| E["Calculate Rebalancing Algorithm"]
    E --> F["Recompute invariants"]
    F --> G{"Checks pass?"}
    G -->|No| D
    G -->|Yes| H["Publish audited output"]
Rebalancing Algorithm methodology state
stateDiagram-v2
    [*] --> FrozenInputs
    FrozenInputs --> Validated: contract passes
    FrozenInputs --> Rejected: missing or infeasible
    Validated --> Calculated: apply named rule
    Calculated --> Audited: invariants pass
    Calculated --> Rejected: invariant fails
    Audited --> Published: version and timestamp recorded
    Published --> Revised: approved correction
    Revised --> FrozenInputs: rebuild from retained source state

How it works#

This page states the contract — how to call it correctly. The article explains the concept: why it works, and where it breaks.

Read the article →

References#

The rest of the Governance and Maintenance family#