# Rebalancing Algorithm

`D03-F06-A06` · Index and Benchmark Engineering → Governance and Maintenance · archetype `record-transform` · difficulty 4/5 · verification **verified**

Full page: https://docs.thefintechbuilder.com/index-and-benchmark-engineering/governance-and-maintenance/rebalancing-algorithm/
Agent skill: `npx skills add IslamBaraka90/Fintech-Algorithms-Library` — https://docs.thefintechbuilder.com/guides/agent-skill/

## Install and import

```bash
npm install fintech-algorithms
```

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

## Signature

```ts
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

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `data` | `{ records: Record[]; notional: number }` | yes | `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

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

### Input

`data`:

```json
{
  "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

```ts
calculate(data)
```

### Returns

object with 3 fields: trades, oneWayTurnover, grossTradeValue

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

## Verification and provenance

Tier: **verified** (via input-expected).

The worked example below is the figure published in this algorithm's article, replayed and asserted by the test suite on every build. The arithmetic cannot drift without the build failing.

Both tiers guarantee the signature. Full explanation: https://docs.thefintechbuilder.com/guides/verification/

Generated from the docs.json payload shipped inside fintech-algorithms@0.13.1.
The signature and parameter list are checked against the compiled implementation at build time,
so a description that contradicts the code fails the build rather than reaching this file.

## Links

- Article (how it works, step by step): https://thefintechbuilder.com/index-and-benchmark-engineering/governance-and-maintenance/rebalancing-algorithm/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/index-and-benchmark-engineering/governance-and-maintenance/rebalancing-algorithm/impl.ts
- Package on npm: https://www.npmjs.com/package/fintech-algorithms
- Domain index for agents: https://docs.thefintechbuilder.com/index-and-benchmark-engineering/llms.txt
