Sell
TradingAuth RequiredSell an active contract before expiry to realise a profit or limit a loss. The loginid parameter is removed; the response is unchanged.
1 Breaking Change
The
loginid parameter is removed. The price parameter and the response are the same in the Legacy API and the New API.Quick comparison
Breaking changes
1. loginid parameter removed
In the Legacy API, loginid selected the account to sell the contract from when you authorised several accounts. The New API does not accept it.
Required change: Remove loginid from your sell requests. Your token selects the account.
Request structure
Legacy APIrequest example
{
"sell": 11542203588,
"price": 0,
"loginid": "CR123456",
"req_id": 1
}New APIrequest example
{
"sell": 11542203588,
"price": 0,
"req_id": 1
}Response structure
Legacy APIresponse example
{
"echo_req": {
"sell": 11542203588,
"price": 0,
"req_id": 1
},
"msg_type": "sell",
"req_id": 1,
"sell": {
"balance_after": 10450.5,
"contract_id": 11542203588,
"reference_id": 11542203587,
"sold_for": 23.45,
"transaction_id": 11542203589
}
}New APIresponse example
{
"echo_req": {
"sell": 11542203588,
"price": 0,
"req_id": 1
},
"msg_type": "sell",
"req_id": 1,
"sell": {
"balance_after": 10450.5,
"contract_id": 11542203588,
"reference_id": 11542203587,
"sold_for": 23.45,
"transaction_id": 11542203589
}
}Code examples
// price 0 sells at the market price
function sellContract(contractId, loginId) {
const request = { sell: contractId, price: 0, req_id: 1 };
// Needed only when you authorised several accounts
if (loginId) request.loginid = loginId;
ws.send(JSON.stringify(request));
ws.onmessage = (event) => {
const response = JSON.parse(event.data);
if (response.msg_type !== "sell") return;
if (response.error) {
console.error(response.error.message);
return;
}
if (response.sell) {
console.log("Sold for: " + response.sell.sold_for);
console.log("New balance: " + response.sell.balance_after);
}
};
}Any other questions? Get in touch