0

I'm having an issue arise when trying to make trades using the Binance API with Python. I want to sell 100% of my ether and convert it to another token. The request goes through fine sometimes, but about 75% of the time it doesn't and I get an error code saying I don't have a sufficient balance. However, I know for a fact that I do have a sufficient balance because I go to Binance and click to convert 100% of my ether to another asset and I am getting the exact same number that my script is generating.

Here is the code I am using to make a request to the Binance API:

def marketBuy(symbol, side, quantity, test=False, **kwargs):
    params = {
    "symbol": symbol,
    "side": side,
    "type": MARKET,
    "quantity": quantity,
    }
    params.update(kwargs)
    path = "/api/v3/order/test" if test else "/api/v3/order"
    data = signedRequest("POST", path, params)
    return data

def signedRequest(method, path, params):
    if "apiKey" not in options or "secret" not in options:
        raise ValueError("Api key and secret must be set")

    query = urlencode(sorted(params.items()))
    query += "&timestamp={}".format(int(time.time() * 1000))
    secret = bytes(options["secret"].encode("utf-8"))
    signature = hmac.new(secret, query.encode("utf-8"),
                     hashlib.sha256).hexdigest()
    query += "&signature={}".format(signature)
    resp = requests.request(method,
                            ENDPOINT + path + "?" + query,
                            headers={"X-MBX-APIKEY": options["apiKey"]})
    data = resp.json()
    if "msg" in data:
        logging.error(data['msg'])
    return data

And here is where I call this function:

marketBuy(token+"ETH", binance.BUY, math.floor((10**getDigits(token)*int(float(binance.balances()["ETH"]['free'])*1/float(binance.prices()[token+"ETH"]))))/(10**getDigits(token)), test=False)

the getDigits function returns the min trade size and the step size which Binance supports for the particular asset, so I'm confident my issue is not in the precision of the numbers I am sending.

I have 0.11059760 ETH. I go to binance and select to sell 100% of my ether against DNT and the amount which comes up is 1574 DNT. Now I go to my console and check how much my script says. It also says 1574. Now I attempt to run the trade through my console and I get an error that says I don't have enough funds. I go back to the browser and am able to successfuly trade all my ether for 1574 DNT.

runfile('C:/Users/alexa/Desktop/cryptoalerts/binanceTransfer.py', wdir='C:/Users/alexa/Desktop/cryptoalerts')
math.floor((10**getDigits(token)*int(float(binanc.balances()["ETH"]['free'])*1/float(binanc.prices()[token+"ETH"]))))/(10**getDigits(token))
binanc.marketBuy(token+"ETH", binanc.BUY, math.floor((10**getDigits(token)*int(float(binanc.balances()["ETH"]['free'])*1/float(binanc.prices()[token+"ETH"]))))/(10**getDigits(token)), test=False)
{'code': -2010, 'msg': 'Account has insufficient balance for requested action.'}
ERROR:root:Account has insufficient balance for requested action.

Since I am able to carry out the transactions with the exact same amount on the website, I am wondering if this is an issue with the Binance API.

aydow
  • 3,673
  • 2
  • 23
  • 40
Alex Wallish
  • 1
  • 1
  • 2
  • have you tried printing the output of what is being sent to binance so you can verify that what you're expecting is what's actually being sent? – aydow Jun 24 '18 at 23:33
  • @aydow yes that was one of the first things that I tried, and it is what I'm expecting – Alex Wallish Jun 25 '18 at 00:45
  • can you show a stack trace when this case arises? it would also be useful to add extra output showing what your balance position was at the time as well as what the value of `params` were. also, you should add the code for `signedRequest()` if that's something that you have written. – aydow Jun 25 '18 at 00:52
  • Just added the signedRequest() method as well as some more info about params – Alex Wallish Jun 25 '18 at 02:21
  • also, just to clarify, I am able to programmatically make trades with 100% success in other situations. When I trade USDT for ETH using the same functions I am not having any issues at all. – Alex Wallish Jun 25 '18 at 02:36
  • what is the value of `query` in `signedRequest`? print it before you send the request – aydow Jun 25 '18 at 03:10
  • Printed out query when trading against SNGLS: &timestamp=1529897593648&signature=f84g95f1ebc2932ab681e09g7ff94523ec163f96a93bd9f4d7f2bf181b9091a2 quantity=1415.00000000&side=BUY&symbol=SNGLSETH&type=MARKET&timestamp=1529897594278&signature=5c5c9438ab31d707cacf6d24bbe64b8ef37e6eb7be544e8eca35854adf81894d {'msg': 'Account has insufficient balance for requested action.', 'code': -2010} – Alex Wallish Jun 25 '18 at 03:34
  • I go to the website and select to trade 100% eth for SNGLS and the amount that comes up is 1415 – Alex Wallish Jun 25 '18 at 03:35
  • hmmm.. maybe raise a ticket with their support? – aydow Jun 25 '18 at 03:51
  • yup that's what I've just done. I appreciate your help! – Alex Wallish Jun 25 '18 at 21:41
  • no worries, i'm interested to see what the issues were. i've never actually sent an api trade to any of the crypto exchanges, just used them to monitor pricing – aydow Jun 25 '18 at 22:43
  • hmm have you taken fees into consideration ? Maybe the web interface is using BNB for fees, but perhaps you have to specify that via the api call and that's why you get the error. –  May 02 '19 at 14:55

0 Answers0