gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
Give me code based on those params: DELETE /fapi/v1/order (HMAC SHA256) Cancel an active…
- User
Give me code based on those params: DELETE /fapi/v1/order (HMAC SHA256)
Cancel an active order.
Weight: 1
Parameters:
Name Type Mandatory Description symbol STRING YES orderId LONG NO origClientOrderId STRING NO recvWindow LONG NO timestamp LONG YES Either orderId or origClientOrderId must be sent.
For I can close all trades
- Assistant
Here's an example code to cancel an active order using the HMAC SHA256 signature:
import requests import hashlib import hmac import time def cancel_order(api_key, secret_key, symbol, order_id=None, orig_client_order_id=None, recv_window=None): base_url = "https://api.binance.com" endpoint = "/fapi/v1/order" # Generate timestamp timestamp = int(time.time() * 1000) # Create query parameters params = { "symbol": symbol, "orderId": order_id, "origClientOrderId": orig_client_order_id, "recvWindow": recv_window, "timestamp": timestamp } # Remove parameters with None values params = {k: v for k, v in params.items() if v is not None} # Create query string query_string = '&'.join([f"{k}={v}" for k, v in params.items()]) # Create signature signature = hmac.new(secret_key.encode(), query_string.encode(), hashlib.sha256).hexdigest() # Set request headers headers = { "X-MBX-APIKEY": api_key } # Send DELETE request response = requests.delete(base_url + endpoint + '?' + query_string + f"&signature={signature}", headers=headers) return response.json() # Usage example api_key = "<Your API Key>" secret_key = "<Your Secret Key>" symbol = "BTCUSDT" order_id = 12345 orig_client_order_id = None recv_window = None response = cancel_order(api_key, secret_key, symbol, order_id, orig_client_order_id, recv_window) print(response)