POST Cancel and Bracket
Cancel all working orders on an instrument and immediately place new OCO bracket orders (take profit and stop loss) to protect an existing position. This is an atomic operation designed for algo workflows that need to replace protective orders without leaving the position exposed.
How It Works
The command executes in three phases: first, it cancels all non-terminal orders for the specified instrument on the account. After a configurable delay to allow the broker to process the cancellations, it places a new take profit (limit) and stop loss (stop market) as an OCO pair. The two new orders share an OCO ID, meaning if one fills, the other is automatically cancelled by NinjaTrader.
The action field represents the direction of the position you are protecting, not the direction of the exit orders. If you are long and pass "action": "Buy", the command will place Sell limit and stop market orders to protect the long position. The action is inverted internally.
If a position in the underlying instrument already exists and the position is smaller than the requested quantity, the bracket is automatically clamped to match the actual position size. If the position goes flat during the cancel window (e.g., a stop was hit while orders were being cancelled), the bracket placement is aborted entirely to prevent accidental naked entry orders.
Cancel and Bracket can be used as an entry strategy. If no underlying positions exist, an OCO bracket will be entered at the quantity specified. This can be used to "straddle" the underlying price and take position if the spot price moves in either direction.
Cancel and Bracket
POST /v1/api/accounts/{account}/orders/cancel_and_bracket
Headers
| Name | Value |
|---|---|
| Content-Type | application/json |
| Authorization | Bearer <token> |
Path Parameters
| Name | Type | Required | Description |
|---|---|---|---|
account | string | Required | Account name in NT8 |
Body Parameters
| Name | Type | Required | Description |
|---|---|---|---|
instrument | string | Required | Instrument name (e.g., "ES 09-26") |
action | string | Required | Direction of the position being protected: Buy or Sell |
quantity | integer | Required | Number of contracts for each bracket leg |
takeProfit | number | Optional | Limit price for the take profit order |
stopLoss | number | Optional | Stop price for the stop loss order |
ocoId | string | Optional | Custom OCO ID. If omitted, one is generated automatically |
At least one of takeProfit or stopLoss must be provided, or no orders will be placed.
Code Examples
- Python
import requests
token = 'my-secret-token'
url = "https://app.crosstrade.io/v1/api/accounts/Sim101/orders/cancel_and_bracket"
headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json"
}
data = {
"instrument": "ES 03-26",
"action": "Buy",
"quantity": 1,
"takeProfit": 5550.00,
"stopLoss": 5450.00
}
try:
response = requests.post(url, headers=headers, json=data)
print(f"Response Code: {response.status_code}, Response Text: {response.text}")
except Exception as e:
print(f"An error occurred: {e}")
Response
- 200
- 400
{
"orderIds": [
"ddc3f4c244b047fda3f4d9f8f44bc8f3",
"b415488f31ff454682a947684e871a90",
"179b24c22fbd47e98b95b63c8ebd6aa4",
"68d01307c4bf4a6382bb863b42748a28",
"148230092d2144f4b0efa686bf6cb515",
"37101553a8ab466eb2f4f3b138954317",
"2ff65c7460b042da98d5e2936d5a3314",
"46feaed8d96e4261bb0b65fb3f04e417",
"78a571a7ebd9470e8b36af4cbf49809f",
"cfb62ddf171f4e6c9f7ac65939845ea1"
],
"success": true
}
{
"error": "Missing required fields (account, instrument, action, quantity)"
}
WebSocket API
This request can also be made over the WebSocket API. The account path parameter and request body fields are all passed inside args.
{
"action": "rpc",
"id": "my-request-id",
"api": "CancelAndBracket",
"args": {
"account": "Sim101",
"instrument": "ES 09-26",
"action": "Buy",
"quantity": 1,
"takeProfit": 5550.00,
"stopLoss": 5450.00
}
}