Skip to main content

POST Flatten Everything

Flatten all NT8 positions and orders NT8​

Closes positions in all or one account that meet all of the specified criteria: account, instrument, market position.

Endpoint​

POST /v1/api/positions/flatten

Headers​

NameValue
Content-Typeapplication/json
AuthorizationBearer <token>

Body parameters​

NameTypeRequiredDescription
accountstringOptionalName of the account
instrumentstringOptionalName of underlying instrument
marketPositionstringOptional"long" or "short"
cancelOrdersboolOptionalFor a filtered flatten, run an explicit working-order cancellation pass after flattening.

Code examples​

import requests

url = "https://app.crosstrade.io/v1/api/positions/flatten"
headers = {
"Authorization": "Bearer my-secret-token",
"Content-Type": "application/json"
}
data = {
"account": "Sim101",
"instrument": "MES 12-25",
"marketPosition": "short"
}

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​

{
"orderIds": [
"adb3868865de4b59b0e147b8300e4a29",
"2141df9fc0254ba9b34c8a5e38722249",
"16f6774e302748ce962f2d60b58b2eb1"
],
"success": true,
"pending": true,
"verified": false,
"closedPositions": [],
"closingPositions": [
{
"type": "NinjaTrader.Cbi.Position",
"account": "Sim101",
"instrument": "ES 12-25",
"instrumentType": "Future",
"marketPosition": "Short",
"quantity": 2,
"averagePrice": 5780.125,
"marketPrice": 5774.0,
"unrealizedProfitLoss": 612.5
}
],
"closeRequests": [
{
"success": true,
"pending": true,
"verified": false,
"disposition": "pending",
"remainingQuantity": 2,
"orderId": "9f2c1b7d43a04e0a8e2f6c5d1b3a7e40",
"account": "Sim101",
"instrument": "ES 12-25"
}
]
}

Platform nuances​

WARNING: An empty JSON payload (i.e., no filters applied) to this endpoint will close all positions and all orders across all accounts.

Confirmed vs. still closing

A close is submitted first and confirmed second. closedPositions lists only positions the add-on saw reach flat before it answered. Anything still working is listed in closingPositions with pending: true and verified: false, and the response carries remainingQuantity and the orderId of the closing order. Poll GET Positions to confirm the final state.

Overlapping close requests for the same account and instrument join the operation already running instead of starting a second one, so a retry cannot leave two closing orders in the market. When a close cannot be completed safely the response carries an error code, such as close_in_progress or close_needs_reconciliation, with a detail message, and no further order is sent.

When using any filter, the request runs the filtered Flatten operation. Set cancelOrders: true when broker-held working stops or targets must be explicitly cancelled after the position closes.

warning

When providing filters (account, instrument, marketPosition), use "api": "Flatten". Use "api": "FlattenEverything" only for a complete flatten with no filters.

The REST endpoint (/v1/api/positions/flatten) handles this implicitly. The WebSocket function must be called explicitly.

WebSocket API​

This request can also be made over the WebSocket API.

Flatten everything (no filters)

{
"action": "rpc",
"id": "my-request-id",
"api": "FlattenEverything",
"args": {}
}

Flatten with filters

{
"action": "rpc",
"id": "my-request-id",
"api": "Flatten",
"args": {
"account": "Sim101",
"instrument": "MES 12-25",
"marketPosition": "short"
}
}