Future cancel LimitOrder problem

Hello,
I’m new here.
I try to cancel a Limit order from API with php but don’t work.
When I execute the code I receive the order information, it don’t cancel the order.
If I want to put a new order the code work fine.
My code for new order:
$response = signedRequest(‘POST’, ‘fapi/v1/order’, [
‘symbol’ => ‘ETHUSDT’,
‘side’ => ‘BUY’,
‘positionSide’ => ‘LONG’,
‘type’ => ‘LIMIT’,
‘timeInForce’ => ‘GTC’,
‘quantity’ => 0.025,
‘price’ => 1275,
// ‘newClientOrderId’ => ‘my_order’, // optional
‘newOrderRespType’ => ‘FULL’ //optional
]);
echo json_encode($response);

My code for cancel order:

$response = signedRequest(‘DELETE’, ‘fapi/v1/order’, [
‘symbol’ => ‘ETHUSDT’,
‘origClientOrderId’ => ‘aigpUOssQqJu9GlHqBsBAI2’
]);
print_r($response);

This is the response:

Array
(
[orderId] => 8.3897654933889E+18
[symbol] => ETHUSDT
[status] => NEW
[clientOrderId] => aigpUOssQqJu9GlHqBsBAI2
[price] => 1275
[avgPrice] => 0.00000
[origQty] => 0.025
[executedQty] => 0
[cumQuote] => 0
[timeInForce] => GTC
[type] => LIMIT
[reduceOnly] =>
[closePosition] =>
[side] => BUY
[positionSide] => LONG
[stopPrice] => 0
[workingType] => CONTRACT_PRICE
[priceProtect] =>
[origType] => LIMIT
[time] => 1614521378942
[updateTime] => 1614521378942
)

This looks like the response from your request to place an order not the one to cancel an order. Please use cURL command or postman to place/cancel. Your php function is a black box to us

In documentation the command is:
Cancel Order (TRADE)
DELETE /fapi/v1/order (HMAC SHA256)
parameter: symbol, orderId or origClientOrderId

This is my code:

$KEY= ‘xxxxxxxxxxxxx111111111111111’;
$SECRET = ‘zzzzzzzzzzzz1111111111111’;

$BASE_URL = ‘https://fapi.binance.com/’;

function signature($query_string, $secret) {
return hash_hmac(‘sha256’, $query_string, $secret);
}

function sendRequest($method, $path) {
global $KEY;
global $BASE_URL;

$url = “${BASE_URL}${path}”;

//echo "requested URL: ". PHP_EOL;
//echo $url. PHP_EOL;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘X-MBX-APIKEY:’.$KEY));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, $method == “POST” ? true : false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$execResult = curl_exec($ch);
$response = curl_getinfo($ch);

// if you wish to print the response headers
// echo print_r($response);

curl_close ($ch);
return json_decode($execResult, true);
}

function signedRequest($method, $path, $parameters = ) {
global $SECRET;

$parameters[‘timestamp’] = round(microtime(true) * 1000);
$query = buildQuery($parameters);
$signature = signature($query, $SECRET);
return sendRequest($method, “${path}?${query}&signature=${signature}”);
}

function buildQuery(array $params)
{
$query_array = array();
foreach ($params as $key => $value) {
if (is_array($value)) {
$query_array = array_merge($query_array, array_map(function ($v) use ($key) {
return urlencode($key) . ‘=’ . urlencode($v);
}, $value));
} else {
$query_array = urlencode($key) . ‘=’ . urlencode($value);
}
}
return implode(’&’, $query_array);
}

$response = signedRequest(‘DELETE’, ‘fapi/v1/order’, [
‘symbol’ => ‘ETHUSDT’,
‘origClientOrderId’ => ‘xNkEor5xc7YON2J5kFp1wQ’
]);
print_r($response);

Are you sure you pass the “DELETE” method to the request? This is a pure coding issue and please review your own code or find a PHP forum for further help

issue is on line
curl_setopt($ch, CURLOPT_POST, $method == “POST” ? true : false);
this is not a DELETE request for cancelling orders you need a DELETE like below
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, “DELETE”);
your code will work for GET and POST requests but not DELETE requests. I used your code and adjusted it with the fllowing.
if ($method==‘DELETE’)
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, “DELETE”);
else
curl_setopt($ch, CURLOPT_POST, $method == “POST” ? true : false);

1 Like