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

# Order Lifecycle

> Recommended order workflow and order state change matrices for the Prime WebSocket API.

To send orders, first subscribe to the [Execution Report](https://docs.kraken.com/institutional/api-reference/prime-websocket/execution-report.md) stream. All order responses and updates are sent as ExecutionReport messages. Send requests with the [NewOrderSingle](https://docs.kraken.com/institutional/api-reference/prime-websocket/new-order-single.md), [OrderCancelRequest](https://docs.kraken.com/institutional/api-reference/prime-websocket/order-cancel-request.md), [OrderCancelReplaceRequest](https://docs.kraken.com/institutional/api-reference/prime-websocket/order-cancel-replace-request.md) and [OrderControlRequest](https://docs.kraken.com/institutional/api-reference/prime-websocket/order-control-request.md) commands.

## Typical workflow

1. Subscribe to [MarketDataSnapshot](https://docs.kraken.com/institutional/api-reference/prime-websocket/market-data-snapshot.md) for the relevant securities.
2. Subscribe to ExecutionReport with `StartDate` set to the `Timestamp` of the last ExecutionReport you processed, to recover order state.
3. Subscribe to [Trade](https://docs.kraken.com/institutional/api-reference/prime-websocket/trade.md) with `StartDate` set to the `Timestamp` of the last Trade you processed, to recover any missed trades.
4. Keep a map of `OrderID` to the last ExecutionReport received for each open order.
5. Keep a set of the `ClOrdID` of each pending request.
6. To submit an order, generate a new `ClOrdID` and send a NewOrderSingle with the `ClOrdID`, `Side`, `Price`, `OrderQty`, `Strategy` and other fields for the request.
7. The response arrives on the ExecutionReport stream with the `ClOrdID` from the NewOrderSingle. If the order is accepted (`ExecType` `PendingNew`), add it to your open orders.
8. To cancel or modify an open order, generate a new `ClOrdID` and send an OrderCancelRequest or OrderCancelReplaceRequest with that `ClOrdID`, the `OrderID` or the `OrigClOrdID` from the previous successful request, and the remaining fields.
9. Fills arrive as ExecutionReport messages with `ExecType` `Trade`.

## Order state change matrices

Order states and transitions on the WebSocket API follow FIX 4.4 closely. The main difference is how a rejected cancel or replace is reported. In FIX 4.4, and on the [Prime FIX API](https://docs.kraken.com/institutional/api-reference/prime-fix/ocrej.md), it arrives as a separate OrderCancelReject message. The WebSocket API has no OrderCancelReject message: it sends an ExecutionReport with `ExecType` `CancelRejected` or `ReplaceRejected` instead.

In the tables below, `A` and `B` are `ClOrdID` values. A response shown as `(B,A)` has `ClOrdID` B and `OrigClOrdID` A. Rows with the same time are alternatives.

### Filled order

| Time | Request | Response | ExecType | OrdStatus | OrderQty | CumQty | LeavesQty | LastQty | Comment |
| :- | :- | :- | :- | :- | :- | :- | :- | :- | :- |
| 1 | NewOrderSingle(A) | | | | 10 | | | | |
| 2 | | ExecutionReport(A) | PendingNew | PendingNew | 10 | 0 | 10 | | |
| 3 | | ExecutionReport(A) | Rejected | Rejected | 10 | 0 | 10 | | If the order is rejected |
| 3 | | ExecutionReport(A) | New | New | 10 | 0 | 10 | | |
| 4 | | ExecutionReport(A) | Trade | PartiallyFilled | 10 | 2 | 8 | 2 | |
| 5 | | ExecutionReport(A) | Trade | PartiallyFilled | 10 | 3 | 7 | 1 | |
| 6 | | ExecutionReport(A) | Trade | Filled | 10 | 10 | 0 | 7 | |

An order with `CumQty` > 0 receives ExecutionReports with `CumQty` set to the filled size. An order that is partially filled and then canceled receives an ExecutionReport with the terminal status `Canceled` and `CumQty` > 0.

### Canceled order

| Time | Request | Response | ExecType | OrdStatus | OrderQty | CumQty | LeavesQty | LastQty | Comment |
| :- | :- | :- | :- | :- | :- | :- | :- | :- | :- |
| 1 | NewOrderSingle(A) | | | | 10 | | | | |
| 2 | | ExecutionReport(A) | PendingNew | PendingNew | 10 | 0 | 10 | | |
| 3 | | ExecutionReport(A) | Rejected | Rejected | 10 | 0 | 10 | | If the order is rejected |
| 3 | | ExecutionReport(A) | New | New | 10 | 0 | 10 | | |
| 4 | OrderCancelRequest(B,A) | | | | | | | | |
| 5 | | ExecutionReport(B,A) | CancelRejected | New | 10 | 0 | 10 | | If the cancel is rejected |
| 5 | | ExecutionReport(B,A) | PendingCancel | PendingCancel | 10 | 0 | 10 | | |
| 6 | | ExecutionReport(B,A) | CancelRejected | New | 10 | 0 | 10 | | If the cancel is rejected |
| 7 | | ExecutionReport(B,A) | Canceled | Canceled | 10 | 0 | 0 | | |

### Replace to increase quantity

| Time | Request | Response | ExecType | OrdStatus | OrderQty | CumQty | LeavesQty | LastQty | Comment |
| :- | :- | :- | :- | :- | :- | :- | :- | :- | :- |
| 1 | NewOrderSingle(A) | | | | 10 | | | | |
| 2 | | ExecutionReport(A) | PendingNew | PendingNew | 10 | 0 | 10 | | |
| 3 | | ExecutionReport(A) | New | New | 10 | 0 | 10 | | |
| 4 | OrderCancelReplaceRequest(B,A) | | | | 11 | | | | |
| 5 | | ExecutionReport(B,A) | ReplaceRejected | New | 10 | 0 | 10 | | If the replace is rejected |
| 5 | | ExecutionReport(B,A) | PendingReplace | PendingReplace | 10 | 0 | 10 | | |
| 6 | | ExecutionReport(B,A) | ReplaceRejected | New | 10 | 0 | 10 | | If the replace is rejected |
| 7 | | ExecutionReport(B,A) | Replaced | New | 11 | 0 | 11 | | |
| 8 | | ExecutionReport(B) | Trade | PartiallyFilled | 11 | 1 | 10 | 1 | |

### Replace during fill

| Time | Request | Response | ExecType | OrdStatus | OrderQty | CumQty | LeavesQty | LastQty | Comment |
| :- | :- | :- | :- | :- | :- | :- | :- | :- | :- |
| 1 | NewOrderSingle(A) | | | | 10 | | | | |
| 2 | | ExecutionReport(A) | PendingNew | PendingNew | 10 | 0 | 10 | | |
| 3 | | ExecutionReport(A) | New | New | 10 | 0 | 10 | | |
| 4 | OrderCancelReplaceRequest(B,A) | | | | 8 | | | | |
| 5 | | ExecutionReport(A) | Trade | PartiallyFilled | 10 | 1 | 9 | 1 | Fill before the replace is received |
| 6 | | ExecutionReport(B,A) | PendingReplace | PendingReplace | 10 | 1 | 9 | | |
| 7 | | ExecutionReport(A) | Trade | PendingReplace | 10 | 3 | 7 | 2 | Fill before the replace is processed |
| 8 | | ExecutionReport(B,A) | Replaced | New | 8 | 3 | 5 | | |
| 9 | | ExecutionReport(B) | Trade | Filled | 8 | 8 | 0 | 5 | |
