# TWAP Execution

`D13-F01-A01` · Execution and Transaction Cost Analysis → Schedule-Based Execution · archetype `record-transform` · difficulty 2/5 · verification **verified**

Full page: https://docs.thefintechbuilder.com/execution-and-transaction-cost-analysis/schedule-based-execution/twap-execution/
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 { twapExecution } from "fintech-algorithms/execution-and-transaction-cost-analysis/schedule-based-execution/twap-execution";
```

## Signature

```ts
twapExecution(total_quantity, start_time_ms, end_time_ms, bucket_count, lot_size)
```

Splits an order into equal slices across equal time buckets. The simplest schedule and the most predictable — predictable enough that it can be detected and traded against, which is the argument for the volume-based alternatives.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `total_quantity` | `number` | yes | Total quantity to execute. · min: 1, integer: true |
| `start_time_ms` | `number` | yes | Schedule start, epoch milliseconds. |
| `end_time_ms` | `number` | yes | Schedule end, epoch milliseconds. |
| `bucket_count` | `number` | yes | Number of time buckets. · min: 1, integer: true |
| `lot_size` | `number` | yes | Lot size; slices round to it and the remainder must be placed somewhere explicit. · min: 1, integer: true |

## Returns

`{ schedule, bucket_quantity, remainder_placement, … }`

The child-order schedule with how the lot-rounding remainder was distributed.

## Errors

- When end_time_ms is not after start_time_ms, or bucket_count is not positive — throws

## Complexity

Time `O(buckets)`, space `O(buckets)`.

## Worked example

Captured by running this function on the input its own test provides. Real output of real code — but not asserted against a published figure.

### Input

`total_quantity`:

```json
12000
```

`start_time_ms`:

```json
0
```

`end_time_ms`:

```json
1440000
```

`bucket_count`:

```json
24
```

`lot_size`:

```json
100
```

### Call

```ts
twapExecution(total_quantity, start_time_ms, end_time_ms, bucket_count, lot_size)
```

### Returns

object with 7 fields: total_quantity, lot_size, bucket_count, scheduled_quantity, remaining_quantity, schedule, state

```json
{
  "total_quantity": 12000,
  "lot_size": 100,
  "bucket_count": 24,
  "scheduled_quantity": 12000,
  "remaining_quantity": 0,
  "schedule": [
    {
      "bucket": 1,
      "start_time_ms": 0,
      "end_time_ms": 60000,
      "target_quantity": 500,
      "target_cumulative_quantity": 500
    },
    {
      "bucket": 2,
      "start_time_ms": 60000,
      "end_time_ms": 120000,
      "target_quantity": 500,
      "target_cumulative_quantity": 1000
    },
    {
      "bucket": 3,
      "start_time_ms": 120000,
      "end_time_ms": 180000,
      "target_quantity": 500,
      "target_cumulative_quantity": 1500
    }
  ],
  "state": "complete-schedule"
}
```

## Other exports

`historicalVwapExecution`, `adaptiveVwapExecution`, `percentageOfVolumeExecution`, `calculate`. Every module additionally exports `run` as an alias of its primary
function, and a `meta` object carrying its catalog id, domain, family, shape and article URL.

## Verification and provenance

Tier: **verified** (via D).

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/execution-and-transaction-cost-analysis/schedule-based-execution/twap-execution/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/execution-and-transaction-cost-analysis/schedule-based-execution/twap-execution/impl.ts
- Package on npm: https://www.npmjs.com/package/fintech-algorithms
- Domain index for agents: https://docs.thefintechbuilder.com/execution-and-transaction-cost-analysis/llms.txt
