Positions
Close a position
Closes one position with a market order.
- Name it in
position: bypositionId, or byinstrumentwithpositionSidein hedge mode (omitted in one-way mode). slippageTolerancelimits how far the closing order may fill from the best quote; the kinds a connector accepts are in its capabilities.
Safe to retry: the second time there is nothing left to close.
202 means the request was accepted for asynchronous processing. The
outcome arrives on the events stream as a notification frame
(closePosition).
POST
/
api
/
v1
/
trading
/
position
/
close
Close a position
curl --request POST \
--url https://public-api.charting.com/api/v1/trading/position/close \
--header 'Content-Type: application/json' \
--header 'X-API-KEY: <api-key>' \
--header 'X-API-SIGNATURE: <api-key>' \
--header 'X-API-TIMESTAMP: <api-key>' \
--data '
{
"accountId": "4b8e2f6a-91c3-4d57-a0e8-3f1c7b9d2e45",
"connectorCode": "binance_usdm",
"position": {
"instrument": {
"exchange": "CXBNCE",
"symbol": "BTC/USDT"
}
}
}
'import requests
url = "https://public-api.charting.com/api/v1/trading/position/close"
payload = {
"accountId": "4b8e2f6a-91c3-4d57-a0e8-3f1c7b9d2e45",
"connectorCode": "binance_usdm",
"position": { "instrument": {
"exchange": "CXBNCE",
"symbol": "BTC/USDT"
} }
}
headers = {
"X-API-KEY": "<api-key>",
"X-API-TIMESTAMP": "<api-key>",
"X-API-SIGNATURE": "<api-key>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {
'X-API-KEY': '<api-key>',
'X-API-TIMESTAMP': '<api-key>',
'X-API-SIGNATURE': '<api-key>',
'Content-Type': 'application/json'
},
body: JSON.stringify({
accountId: '4b8e2f6a-91c3-4d57-a0e8-3f1c7b9d2e45',
connectorCode: 'binance_usdm',
position: {instrument: {exchange: 'CXBNCE', symbol: 'BTC/USDT'}}
})
};
fetch('https://public-api.charting.com/api/v1/trading/position/close', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://public-api.charting.com/api/v1/trading/position/close",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'accountId' => '4b8e2f6a-91c3-4d57-a0e8-3f1c7b9d2e45',
'connectorCode' => 'binance_usdm',
'position' => [
'instrument' => [
'exchange' => 'CXBNCE',
'symbol' => 'BTC/USDT'
]
]
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json",
"X-API-KEY: <api-key>",
"X-API-SIGNATURE: <api-key>",
"X-API-TIMESTAMP: <api-key>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://public-api.charting.com/api/v1/trading/position/close"
payload := strings.NewReader("{\n \"accountId\": \"4b8e2f6a-91c3-4d57-a0e8-3f1c7b9d2e45\",\n \"connectorCode\": \"binance_usdm\",\n \"position\": {\n \"instrument\": {\n \"exchange\": \"CXBNCE\",\n \"symbol\": \"BTC/USDT\"\n }\n }\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("X-API-KEY", "<api-key>")
req.Header.Add("X-API-TIMESTAMP", "<api-key>")
req.Header.Add("X-API-SIGNATURE", "<api-key>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://public-api.charting.com/api/v1/trading/position/close")
.header("X-API-KEY", "<api-key>")
.header("X-API-TIMESTAMP", "<api-key>")
.header("X-API-SIGNATURE", "<api-key>")
.header("Content-Type", "application/json")
.body("{\n \"accountId\": \"4b8e2f6a-91c3-4d57-a0e8-3f1c7b9d2e45\",\n \"connectorCode\": \"binance_usdm\",\n \"position\": {\n \"instrument\": {\n \"exchange\": \"CXBNCE\",\n \"symbol\": \"BTC/USDT\"\n }\n }\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://public-api.charting.com/api/v1/trading/position/close")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["X-API-KEY"] = '<api-key>'
request["X-API-TIMESTAMP"] = '<api-key>'
request["X-API-SIGNATURE"] = '<api-key>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"accountId\": \"4b8e2f6a-91c3-4d57-a0e8-3f1c7b9d2e45\",\n \"connectorCode\": \"binance_usdm\",\n \"position\": {\n \"instrument\": {\n \"exchange\": \"CXBNCE\",\n \"symbol\": \"BTC/USDT\"\n }\n }\n}"
response = http.request(request)
puts response.read_body{
"requestId": "<string>"
}{
"error": {
"code": "invalid_argument",
"message": "field \"price\" is required for a limit order"
}
}{
"error": {
"code": "unauthenticated",
"message": "invalid API key or signature"
}
}{
"error": {
"code": "permission_denied",
"message": "permission denied"
}
}{
"error": {
"code": "not_found",
"message": "account not found"
}
}{
"error": {
"code": "rate_limited",
"message": "rate limit exceeded"
}
}Authorizations
Body
application/json
Was this page helpful?
⌘I
Close a position
curl --request POST \
--url https://public-api.charting.com/api/v1/trading/position/close \
--header 'Content-Type: application/json' \
--header 'X-API-KEY: <api-key>' \
--header 'X-API-SIGNATURE: <api-key>' \
--header 'X-API-TIMESTAMP: <api-key>' \
--data '
{
"accountId": "4b8e2f6a-91c3-4d57-a0e8-3f1c7b9d2e45",
"connectorCode": "binance_usdm",
"position": {
"instrument": {
"exchange": "CXBNCE",
"symbol": "BTC/USDT"
}
}
}
'import requests
url = "https://public-api.charting.com/api/v1/trading/position/close"
payload = {
"accountId": "4b8e2f6a-91c3-4d57-a0e8-3f1c7b9d2e45",
"connectorCode": "binance_usdm",
"position": { "instrument": {
"exchange": "CXBNCE",
"symbol": "BTC/USDT"
} }
}
headers = {
"X-API-KEY": "<api-key>",
"X-API-TIMESTAMP": "<api-key>",
"X-API-SIGNATURE": "<api-key>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {
'X-API-KEY': '<api-key>',
'X-API-TIMESTAMP': '<api-key>',
'X-API-SIGNATURE': '<api-key>',
'Content-Type': 'application/json'
},
body: JSON.stringify({
accountId: '4b8e2f6a-91c3-4d57-a0e8-3f1c7b9d2e45',
connectorCode: 'binance_usdm',
position: {instrument: {exchange: 'CXBNCE', symbol: 'BTC/USDT'}}
})
};
fetch('https://public-api.charting.com/api/v1/trading/position/close', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://public-api.charting.com/api/v1/trading/position/close",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'accountId' => '4b8e2f6a-91c3-4d57-a0e8-3f1c7b9d2e45',
'connectorCode' => 'binance_usdm',
'position' => [
'instrument' => [
'exchange' => 'CXBNCE',
'symbol' => 'BTC/USDT'
]
]
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json",
"X-API-KEY: <api-key>",
"X-API-SIGNATURE: <api-key>",
"X-API-TIMESTAMP: <api-key>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://public-api.charting.com/api/v1/trading/position/close"
payload := strings.NewReader("{\n \"accountId\": \"4b8e2f6a-91c3-4d57-a0e8-3f1c7b9d2e45\",\n \"connectorCode\": \"binance_usdm\",\n \"position\": {\n \"instrument\": {\n \"exchange\": \"CXBNCE\",\n \"symbol\": \"BTC/USDT\"\n }\n }\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("X-API-KEY", "<api-key>")
req.Header.Add("X-API-TIMESTAMP", "<api-key>")
req.Header.Add("X-API-SIGNATURE", "<api-key>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://public-api.charting.com/api/v1/trading/position/close")
.header("X-API-KEY", "<api-key>")
.header("X-API-TIMESTAMP", "<api-key>")
.header("X-API-SIGNATURE", "<api-key>")
.header("Content-Type", "application/json")
.body("{\n \"accountId\": \"4b8e2f6a-91c3-4d57-a0e8-3f1c7b9d2e45\",\n \"connectorCode\": \"binance_usdm\",\n \"position\": {\n \"instrument\": {\n \"exchange\": \"CXBNCE\",\n \"symbol\": \"BTC/USDT\"\n }\n }\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://public-api.charting.com/api/v1/trading/position/close")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["X-API-KEY"] = '<api-key>'
request["X-API-TIMESTAMP"] = '<api-key>'
request["X-API-SIGNATURE"] = '<api-key>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"accountId\": \"4b8e2f6a-91c3-4d57-a0e8-3f1c7b9d2e45\",\n \"connectorCode\": \"binance_usdm\",\n \"position\": {\n \"instrument\": {\n \"exchange\": \"CXBNCE\",\n \"symbol\": \"BTC/USDT\"\n }\n }\n}"
response = http.request(request)
puts response.read_body{
"requestId": "<string>"
}{
"error": {
"code": "invalid_argument",
"message": "field \"price\" is required for a limit order"
}
}{
"error": {
"code": "unauthenticated",
"message": "invalid API key or signature"
}
}{
"error": {
"code": "permission_denied",
"message": "permission denied"
}
}{
"error": {
"code": "not_found",
"message": "account not found"
}
}{
"error": {
"code": "rate_limited",
"message": "rate limit exceeded"
}
}