binance / binance-signature-examples

Examples of generating HMAC and RSA signature for Binance API
245 stars 91 forks source link

Order market buy and sell not shown on binance transaction history on SPOT Balance. #8

Closed athenasaurav closed 3 years ago

athenasaurav commented 3 years ago

Hello Everyone

I m trying to use you this python binance to buy and sell crypto currency.

Currently i have more than enough balance to place BTCUSDT trade of buy or sell in my account. I have written a simple script to buy crypto first and then after some delay time sell them using your following code :

Code used :

import hmac
import time
import hashlib
import requests
from urllib.parse import urlencode
KEY = '4aoQqPmQKUt8KLyr2BI4XFl8JdiyNN2w7RBkuk9VenqS3CCx96FXJUg93sb6H1Oa'
SECRET = 'FgxGtBtVk4P9goXs0G9k8syBSQNoXyHXKX8tvG7mMAJLKwZ5ZinZMHl64S86iHKT'
BASE_URL = 'https://api.binance.com' # production base url
# BASE_URL = 'https://testnet.binance.vision' # testnet base url

''' ======  begin of functions, you don't need to touch ====== '''
def hashing(query_string):
    return hmac.new(SECRET.encode('utf-8'), query_string.encode('utf-8'), hashlib.sha256).hexdigest()

def get_timestamp():
    return int(time.time() * 1000)

def dispatch_request(http_method):
    session = requests.Session()
    session.headers.update({
        'Content-Type': 'application/json;charset=utf-8',
        'X-MBX-APIKEY': KEY
    })
    return {
        'GET': session.get,
        'DELETE': session.delete,
        'PUT': session.put,
        'POST': session.post,
    }.get(http_method, 'GET')

# used for sending request requires the signature
def send_signed_request(http_method, url_path, payload={}):
    query_string = urlencode(payload, True)
    if query_string:
        query_string = "{}&timestamp={}".format(query_string, get_timestamp())
    else:
        query_string = 'timestamp={}'.format(get_timestamp())

    url = BASE_URL + url_path + '?' + query_string + '&signature=' + hashing(query_string)
    print("{} {}".format(http_method, url))
    params = {'url': url, 'params': {}}
    response = dispatch_request(http_method)(**params)
    return response.json()

# used for sending public data request
def send_public_request(url_path, payload={}):
    query_string = urlencode(payload, True)
    url = BASE_URL + url_path
    if query_string:
        url = url + '?' + query_string
    print("{}".format(url))
    response = dispatch_request('GET')(url=url)
    return response.json()

''' ======  end of functions ====== '''

### public data endpoint, call send_public_request #####
# get klines
response = send_public_request('/api/v3/klines' , {"symbol": "BTCUSDT", "interval": "5m"})
##print(response)

### USER_DATA endpoints, call send_signed_request #####
# get account informtion
# if you can see the account details, then the API key/secret is correct
response = send_signed_request('GET', '/api/v3/account')
##print(response)

# # place an order
# if you see order response, then the parameters setting is correct
params = {
    "symbol": "BTCUSDT",
    "side": "SELL",
    "type": "MARKET",
    "quantity": '0.0015'
}
response = send_signed_request('POST', '/api/v3/order', params)
print(response)

In the terminal i see no errors, buy and sell are successful i even get order id, trade id etc but when i check the history in SPOT order of trade history it doesn't appear in there.

Terminal Window screenshot attached. But in the SPOT wallet order history or trade history it doesn't shows up. Only those trades are shown that I did manually. ceb3fcc7-07f6-41ab-8287-a12cd90e6620

2pd commented 3 years ago

I can't see clear information from the screenshot that showing issue of query order.

Please try to send request to GET /api/v3/order with:

please let us know if you have any concern.

athenasaurav commented 3 years ago

Hello @2pd Thanks for your response.

Sorry it was my fault trade was working I was just checking the wrong Binance account.