> ## Documentation Index
> Fetch the complete documentation index at: https://sdk.avantisfi.com/llms.txt
> Use this file to discover all available pages before exploring further.

# TWAP Orders

> Execute large orders in time-weighted slices.

TWAP orders split a large open or close into slices executed over `run_time_seconds`. Useful when a single fill would eat too much spread or exceed available liquidity.

The SDK signs a TWAP intent (EIP-712) and submits it to the Avantis TWAP API, which registers the order on-chain itself and responds synchronously. The receipt carries the transaction hash and the on-chain `twapId` in `receipt.order_id`.

TWAP is not available on [Upside pairs](/data/markets#upside-markets) (their TWAP params are zeroed on-chain); `twap_open`/`twap_close` raise a `ValidationError` (`UPSIDE_MARKET_ONLY`) on them.

## Open

```python theme={null}
receipt = await client.trade.twap_open(
    "ETH/USD", "long",
    collateral=1000,             # spread over the run time
    run_time_seconds=600,        # 10 minutes
    leverage=10,
    max_leverage=20,             # required by the contract struct
)
print(receipt.tx_hash, receipt.order_id)   # order_id = on-chain twapId
```

| Parameter                   | Default  | Notes                                                    |
| --------------------------- | -------- | -------------------------------------------------------- |
| `collateral`                | required | Total USDC across all slices                             |
| `run_time_seconds`          | required | Must be within the pair's `twap_params` min/max run time |
| `leverage` / `max_leverage` | required | Both required                                            |
| `coin_exposure`             | `None`   | Switches to fixed coin-exposure targeting                |

<Warning>
  Each slice must clear the pair's minimum position size (`PairInfo.min_lev_pos_usdc` in notional). Too little collateral over too long a run time fails with `BelowMinPosition`. Check `pair.twap_params` (min/max run time, frequency, fee) before sizing.
</Warning>

## Close

```python theme={null}
await client.trade.twap_close(
    pos.pair_index, pos.index,
    coin_exposure_to_close=0.5,   # ETH
    run_time_seconds=300,
)
```

## List and cancel

```python theme={null}
twaps = await client.account.twaps()             # paginated (0-based); include_canceled=True for history
twap = await client.account.twap(123)            # one TWAP by twapId; None if unknown (404)
await client.trade.twap_cancel(twap_id=123)      # twapId from receipt.order_id / account.twaps()
```

Cancelling signs a `TwapCancelReq` intent over the `twapId`. The trader key or an active delegate key both work.
