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 += "×tamp={}".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.