“code” => -2015
“msg” => “Invalid API-key, IP, or permissions for action.”
I get this error when I try to place a **STOP LOSS ** order on the binance api.
I have done the following;
- Whitelist my IP address in the api management
- Tried placing the order manually on binance and it was opened.
- Checked the trading permission on the api management
Below is my code snippet
function getPricision($symbol)
{
// Make a request to get the symbol details
$symbol_info = json_decode(file_get_contents('https://api.binance.com/api/v3/exchangeInfo?symbol=' . $symbol), true);
if (isset($symbol_info['symbols']) && !empty($symbol_info['symbols'])) {
// Get the first symbol in the list (assuming there's only one matching symbol)
$symbol_data = $symbol_info['symbols'][0];
// Find the lot size filter
$lot_size_filter = array_filter($symbol_data['filters'], function ($filter) {
return $filter['filterType'] === 'LOT_SIZE';
});
// Check if the lot size filter is found
if (!empty($lot_size_filter)) {
$lot_size = floatval($lot_size_filter[1]['stepSize']);
$precision = intval(round(-log10($lot_size), 0));
return $precision;
}
}
return false;
}
// Set the required parameters for the limit trade
$api_key = env('BINANCE_API_KEY');
$api_secret = env('BINANCE_SECRET_KEY');
$api_endpoint = 'https://api.binance.com';
$symbol = 'ALGOUSDT';
$side = 'BUY';
$entryPrice = 0.1487;
$side = 'BUY';
$order_type = 'STOP_LOSS';
$stop_price = $entryPrice;
$limit_price = $entryPrice;
$amount = 20; // Set the amount
// Calculate the quantity based on the desired amount
$quantity = $amount / $limit_price;
// Format the quantity, stop price, and limit price to the correct precision
$formatted_quantity = number_format($quantity, getPricision($symbol), '.', '');
// Create the order payload
$timestamp = time() * 1000;
$params = [
'symbol' => $symbol,
'side' => $side,
'type' => $order_type,
'quantity' => $formatted_quantity,
'stopPrice' => $stop_price,
'price' => $limit_price,
'timeInForce' => 'GTC', // Good 'Til Canceled
'timestamp' => $timestamp,
];
//dd($params);
// Generate the query string and sign it
$query_string = http_build_query($params);
$signature = hash_hmac('sha256', $query_string, $api_secret);
$params['signature'] = $signature;
// Send the signed request to place the order
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_endpoint . '/api/v3/order');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string . '&signature=' . $signature);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['X-MBX-APIKEY: ' . $api_key]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
// Process the response
$response_data = json_decode($response, true);
if (isset($response_data['orderId'])) {
echo 'Order placed successfully! Order ID: ' . $response_data['orderId'];
} else {
echo 'Failed to place the order. Error: ' . $response;
}
// Close the cURL request
curl_close($ch);
dd($response_data);
Please what is wrong here, I have been pulling my hair with no luck