OANDA API PUT /v3/accounts/{accountID}/positions/{instrument}/close
Close out the long side, the short side or both for one instrument.
Risk warning: CFDs and spread bets are complex instruments and come with a high risk of losing money rapidly due to leverage. You can lose money on these products, and each provider publishes its own loss figure under FCA rules. You should consider whether you understand how they work and whether you can afford to take the high risk of losing your money. Automated and API trading can amplify losses through bugs, connectivity failures and runaway orders.
Confirmed on the official position endpoints page. Method, path and request parameters were re-read on that page on 12 Oct 2026 and agree with this record; the mock reply was not compared field by field. The page lists ALL as the default for both sides; send NONE explicitly for the side you want to keep. 400 if nothing matches.
Parameters
| Name | In | Type | Description |
|---|---|---|---|
| Accept-Datetime-Format | header | string | Optional. RFC3339 (default) or UNIX for epoch seconds in datetime fields. |
| Content-Typerequired | header | string | Send application/json on every request with a body. |
| accountIDrequired | path | string | Account ID from GET /v3/accounts. Mock value shown. |
| instrumentrequired | path | string | Instrument name. |
| longUnits | body | string | ALL, NONE or a number of long units to close. |
| shortUnits | body | string | ALL, NONE or a number of short units to close. |
Authentication headers
| Name | Type | Description |
|---|---|---|
| Authorizationrequired | string | Send Bearer followed by your personal access token. Placeholder only: never paste a real token into this site. |
Request
Written from the method, path and parameters recorded on this page, against the demo host. Not executed against a live or demo account. Credentials are read from environment variables on your own machine and are never entered on this site. The request builder writes these same snippets, from the same record.
This call takes a request body. The record marks no body field as required, so the samples send an empty JSON object with a Content-Type of application/json (in Python, passing json= sets that header). Add the fields the call needs from the parameters above before the request can succeed.
Python
# Set OANDA_TOKEN, OANDA_ACCOUNT_ID as environment variables first. Never paste real values into code.
# Replace each {placeholder}, "..." and 0 with a real value before running.
# Optional and not shown: headers Accept-Datetime-Format; body fields longUnits, shortUnits.
import os
import requests
BASE_URL = "https://api-fxpractice.oanda.com" # the demo host
headers = {
"Authorization": "Bearer " + os.environ["OANDA_TOKEN"],
"Content-Type": "application/json",
}
body = {}
resp = requests.put(BASE_URL + "/v3/accounts/" + os.environ["OANDA_ACCOUNT_ID"] + "/positions/{instrument}/close", headers=headers, json=body, timeout=10)
resp.raise_for_status()
print(resp.json())JavaScript
// Set OANDA_TOKEN, OANDA_ACCOUNT_ID as environment variables first. Never paste real values into code.
// Replace each {placeholder}, "..." and 0 with a real value before running.
// Optional and not shown: headers Accept-Datetime-Format; body fields longUnits, shortUnits.
// Run as a module, for example a .mjs file, on a Node version that has fetch built in.
const BASE_URL = "https://api-fxpractice.oanda.com"; // the demo host
const res = await fetch(BASE_URL + "/v3/accounts/" + process.env.OANDA_ACCOUNT_ID + "/positions/{instrument}/close", {
method: "PUT",
headers: {
"Authorization": "Bearer " + process.env.OANDA_TOKEN,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
});
if (!res.ok) throw new Error(`HTTP ${res.status}`);
console.log(await res.json());curl
# Set OANDA_TOKEN, OANDA_ACCOUNT_ID as environment variables first. Never paste real values into code.
# Replace each {placeholder}, "..." and 0 with a real value before running.
# Optional and not shown: headers Accept-Datetime-Format; body fields longUnits, shortUnits.
curl -X PUT "https://api-fxpractice.oanda.com/v3/accounts/$OANDA_ACCOUNT_ID/positions/{instrument}/close" \
-H "Authorization: Bearer $OANDA_TOKEN" \
-H "Content-Type: application/json" \
-d '{}'Response
A mock response. The values are invented, so check field names against the official reference.
{
"_mock": true,
"longOrderCreateTransaction": {
"id": "9007",
"type": "MARKET_ORDER",
"instrument": "EUR_USD",
"units": "-1000",
"reason": "POSITION_CLOSEOUT",
"accountID": "101-004-MOCK-001"
},
"longOrderFillTransaction": {
"id": "9008",
"type": "ORDER_FILL",
"instrument": "EUR_USD",
"units": "-1000",
"price": "1.10120",
"pl": "1.0000",
"accountID": "101-004-MOCK-001"
},
"relatedTransactionIDs": [
"9007",
"9008"
],
"lastTransactionID": "9008"
}Response schema, HTTP 200
Every field this site's OpenAPI file for OANDA declares for this response.
| Field | Type | Description |
|---|---|---|
| longOrderCreateTransaction | object | Not recorded |
| id | string | Not recorded |
| type | string | Not recorded |
| instrument | string | Not recorded |
| units | string | Not recorded |
| reason | string | Not recorded |
| accountID | string | Not recorded |
| longOrderFillTransaction | object | Not recorded |
| id | string | Not recorded |
| type | string | Not recorded |
| instrument | string | Not recorded |
| units | string | Not recorded |
| price | string | Not recorded |
| pl | string | Not recorded |
| accountID | string | Not recorded |
| relatedTransactionIDs | array of string | Not recorded |
| lastTransactionID | string | Not recorded |
Unverifiedno source opened This schema is not sourced to a page of the provider's own that we opened, so check field names against the official reference.
Limits that apply at OANDA
| Limit | Value | Scope | Source |
|---|---|---|---|
| Headline limit | 120/s per IP address, HTTP 429 above it | ip | developer.oanda.comchecked |
OANDA API errors you may meet
- HTTP 401 (invalid or missing token)HTTP 401. The access token is invalid or missing.
- HTTP 405HTTP 405. The endpoint was called with the wrong HTTP method.
- HTTP 429HTTP 429. OANDA returned HTTP 429 because requests arrived faster than it would accept them.
- ACCOUNT_CONFIGURATION_LOCKEDThe account is locked for configuration changes.
- ACCOUNT_DEPOSIT_LOCKEDThe account is locked for deposits.
- ACCOUNT_LOCKEDThe account is locked.
All OANDA API error codes, including 236 more not listed here.