> For the complete documentation index, see [llms.txt](https://docs.dmex.app/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.dmex.app/api-docs/websocket/cancel-order.md).

# Cancel Order

## Cancel Order API

Cancel an existing open order by signing its hash.

### Overview

Order cancellation requires:

1. The hash of the order you want to cancel
2. Signing the order hash with your private key
3. Sending the signed cancellation via WebSocket

### Cancel Request Parameters

| Parameter    | Type   | Description                     |
| ------------ | ------ | ------------------------------- |
| `order_hash` | string | The hash of the order to cancel |
| `v`          | number | Signature recovery parameter    |
| `r`          | string | Signature r component           |
| `s`          | string | Signature s component           |

### JavaScript/TypeScript Example

```typescript
import Web3 from 'web3';

async function cancelOrder(privateKey: string, orderHash: string) {
    const web3 = new Web3();
    
    // Sign the order hash
    const signedData = web3.eth.accounts.sign(orderHash, privateKey);
    const signature = signedData.signature;

    // Parse signature into v, r, s
    const sig = signature.slice(2);
    const r = `0x${sig.slice(0, 64)}`;
    const s = `0x${sig.slice(64, 128)}`;
    let v = parseInt(`0x${sig.slice(128, 130)}`, 16);
    if (v < 27) v += 27;

    return {
        order_hash: orderHash,
        v: v,
        r: r,
        s: s
    };
}

// Send cancel request via WebSocket
function sendCancelOrder(ws: WebSocket, cancelRequest: object) {
    const message = JSON.stringify({
        op: 'cancel_order',
        data: cancelRequest
    });
    ws.send(message);
}

// Usage Example
async function main() {
    const privateKey = '0x...'; // Your private key
    const orderHash = '0x...';  // Hash of order to cancel
    
    const cancelRequest = await cancelOrder(privateKey, orderHash);

    const ws = new WebSocket('wss://api.dmex.app/ws');
    
    ws.onopen = () => {
        sendCancelOrder(ws, cancelRequest);
    };

    ws.onmessage = (event) => {
        const response = JSON.parse(event.data);
        console.log('Response:', response);
    };
}
```

### Python Example

```python
import json
import websocket
from web3 import Web3
from eth_account.messages import encode_defunct

def cancel_order(private_key: str, order_hash: str):
    w3 = Web3()
    
    # Sign the order hash
    signed = w3.eth.account.sign_message(
        encode_defunct(hexstr=order_hash),
        private_key=private_key
    )
    
    return {
        'order_hash': order_hash,
        'v': signed.v,
        'r': hex(signed.r),
        's': hex(signed.s)
    }


def send_cancel_order(ws, cancel_request):
    message = json.dumps({
        'op': 'cancel_order',
        'data': cancel_request
    })
    ws.send(message)


# Usage Example
if __name__ == '__main__':
    private_key = '0x...'  # Your private key
    order_hash = '0x...'   # Hash of order to cancel
    
    cancel_request = cancel_order(private_key, order_hash)

    ws = websocket.create_connection('wss://api.dmex.app/ws')
    send_cancel_order(ws, cancel_request)
    
    response = ws.recv()
    print('Response:', response)
    ws.close()
```

### Notes

* You can only cancel orders that you created (signature must match the original order creator)
* The order must still be open (not fully filled or already cancelled)
* To cancel and replace an order atomically, use the `replace_hash` parameter when creating a new order instead
