import requests
import json
import hmac
import hashlib
import time
API endpoint
url = "https://fapi.binance.com/fapi/v1/order"
API key and secret
api_key = ""
api_secret = ""
Request parameters
symbol = "BTCUSDT"
side = "SELL"
type = "MARKET"
notional_value = 30 # Notional value in USD
timestamp = int(time.time() * 1000)
recvWindow = 5000
Fetch last traded price
ticker_url = "https://fapi.binance.com/fapi/v1/ticker/price"
ticker_payload = {
"symbol": symbol
}
ticker_response = requests.get(ticker_url, params=ticker_payload)
last_price = float(ticker_response.json()["price"])
Calculate quantity
quantity = notional_value / last_price
Create the timestamp
timestamp = int(time.time() * 1000)
Create the signature
message = f"{timestamp}{recvWindow}".encode('utf-8')
signature = hmac.new(api_secret.encode('utf-8'), message, hashlib.sha256).hexdigest()
Create the request payload
payload = {
"symbol": symbol,
"side": side,
"type": type,
"quantity": quantity,
"timestamp": timestamp,
"recvWindow": recvWindow,
"signature": signature
}
Send the request
response = requests.post(url, headers={"X-MBX-APIKEY": api_key}, json=payload)
Print the response
print(response.json())
import requests
import json
import hmac
import hashlib
import time
API endpoint
url = "https://fapi.binance.com/fapi/v1/order"
API key and secret
api_key = ""
api_secret = ""
Request parameters
symbol = "BTCUSDT"
side = "SELL"
type = "MARKET"
notional_value = 30 # Notional value in USD
timestamp = int(time.time() * 1000)
recvWindow = 5000
Fetch last traded price
ticker_url = "https://fapi.binance.com/fapi/v1/ticker/price"
ticker_payload = {
"symbol": symbol
}
ticker_response = requests.get(ticker_url, params=ticker_payload)
last_price = float(ticker_response.json()["price"])
Calculate quantity
quantity = notional_value / last_price
Create the timestamp
timestamp = int(time.time() * 1000)
Create the signature
message = f"{timestamp}{recvWindow}".encode('utf-8')
signature = hmac.new(api_secret.encode('utf-8'), message, hashlib.sha256).hexdigest()
Create the request payload
payload = {
"symbol": symbol,
"side": side,
"type": type,
"quantity": quantity,
"timestamp": timestamp,
"recvWindow": recvWindow,
"signature": signature
}
Send the request
response = requests.post(url, headers={"X-MBX-APIKEY": api_key}, json=payload)
Print the response
print(response.json())