Positions
Add take-profit/stop-loss to a position
Adds a take-profit, a stop-loss, or both to one open derivatives position,
protecting size of it.
- Name the position in
position: bypositionId, or byinstrumentwithpositionSidein hedge mode. - Set at least one of
takeProfitandstopLoss. When both are set they form one pair, identified by the returnedpairId: when one triggers, the exchange cancels the other. - Where the connector’s
addPositionTpSlcapabilities saywholePositionOnly,sizemust equal the position size; where they saylimitPriceUnsupported,limitPriceis rejected.
Not safe to retry. The exchange does not deduplicate this request: a retry after a timeout can create a duplicate take-profit/stop-loss.
202 means the request was accepted for asynchronous processing. The
outcome arrives on the events stream as a notification frame
(addPositionTpSl), and the created orders carry tpSlLeg.pairId.
POST
/
api
/
v1
/
trading
/
position
/
add-tpsl
Add take-profit/stop-loss to a position
curl --request POST \
--url https://public-api.charting.com/api/v1/trading/position/add-tpsl \
--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"
}
},
"size": "0.001",
"takeProfit": {
"triggerPrice": "65000"
},
"stopLoss": {
"triggerPrice": "58000"
}
}
'import requests
url = "https://public-api.charting.com/api/v1/trading/position/add-tpsl"
payload = {
"accountId": "4b8e2f6a-91c3-4d57-a0e8-3f1c7b9d2e45",
"connectorCode": "binance_usdm",
"position": { "instrument": {
"exchange": "CXBNCE",
"symbol": "BTC/USDT"
} },
"size": "0.001",
"takeProfit": { "triggerPrice": "65000" },
"stopLoss": { "triggerPrice": "58000" }
}
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'}},
size: '0.001',
takeProfit: {triggerPrice: '65000'},
stopLoss: {triggerPrice: '58000'}
})
};
fetch('https://public-api.charting.com/api/v1/trading/position/add-tpsl', 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/add-tpsl",
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'
]
],
'size' => '0.001',
'takeProfit' => [
'triggerPrice' => '65000'
],
'stopLoss' => [
'triggerPrice' => '58000'
]
]),
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/add-tpsl"
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 \"size\": \"0.001\",\n \"takeProfit\": {\n \"triggerPrice\": \"65000\"\n },\n \"stopLoss\": {\n \"triggerPrice\": \"58000\"\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/add-tpsl")
.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 \"size\": \"0.001\",\n \"takeProfit\": {\n \"triggerPrice\": \"65000\"\n },\n \"stopLoss\": {\n \"triggerPrice\": \"58000\"\n }\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://public-api.charting.com/api/v1/trading/position/add-tpsl")
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 \"size\": \"0.001\",\n \"takeProfit\": {\n \"triggerPrice\": \"65000\"\n },\n \"stopLoss\": {\n \"triggerPrice\": \"58000\"\n }\n}"
response = http.request(request)
puts response.read_body{
"requestId": "<string>",
"pairId": "<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
Set at least one of takeProfit and stopLoss.
Was this page helpful?
⌘I
Add take-profit/stop-loss to a position
curl --request POST \
--url https://public-api.charting.com/api/v1/trading/position/add-tpsl \
--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"
}
},
"size": "0.001",
"takeProfit": {
"triggerPrice": "65000"
},
"stopLoss": {
"triggerPrice": "58000"
}
}
'import requests
url = "https://public-api.charting.com/api/v1/trading/position/add-tpsl"
payload = {
"accountId": "4b8e2f6a-91c3-4d57-a0e8-3f1c7b9d2e45",
"connectorCode": "binance_usdm",
"position": { "instrument": {
"exchange": "CXBNCE",
"symbol": "BTC/USDT"
} },
"size": "0.001",
"takeProfit": { "triggerPrice": "65000" },
"stopLoss": { "triggerPrice": "58000" }
}
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'}},
size: '0.001',
takeProfit: {triggerPrice: '65000'},
stopLoss: {triggerPrice: '58000'}
})
};
fetch('https://public-api.charting.com/api/v1/trading/position/add-tpsl', 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/add-tpsl",
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'
]
],
'size' => '0.001',
'takeProfit' => [
'triggerPrice' => '65000'
],
'stopLoss' => [
'triggerPrice' => '58000'
]
]),
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/add-tpsl"
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 \"size\": \"0.001\",\n \"takeProfit\": {\n \"triggerPrice\": \"65000\"\n },\n \"stopLoss\": {\n \"triggerPrice\": \"58000\"\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/add-tpsl")
.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 \"size\": \"0.001\",\n \"takeProfit\": {\n \"triggerPrice\": \"65000\"\n },\n \"stopLoss\": {\n \"triggerPrice\": \"58000\"\n }\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://public-api.charting.com/api/v1/trading/position/add-tpsl")
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 \"size\": \"0.001\",\n \"takeProfit\": {\n \"triggerPrice\": \"65000\"\n },\n \"stopLoss\": {\n \"triggerPrice\": \"58000\"\n }\n}"
response = http.request(request)
puts response.read_body{
"requestId": "<string>",
"pairId": "<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"
}
}