Somewhere in your code you are using Decimal object (I could reproduce your issue). Probably your quantity parameter (despite your function is waiting for a float).
Try to reuse your self.floatToString() method on the quantity parameters ( in params1 and params2 ):
Many thanks for your answer.
In the binance API guide (https://binance-docs.github.io) they say quantity should be of type decimal, but from the error message I received it seems that json.dumps is not accepting decimals (type not serializable).
I am not sure if translating the parameter to a string will be accepted by binance, for example, if I convert to float I get:
{‘code’: -1022, ‘msg’: ‘Signature for this request is not valid.’}
and I guess this is because Binance is expecting a Decimal value in the parameter quantity.
What do you think?
No, by decimal, it’s mean the mathematical form, that is not an integer, and it’s not the Python Decimal object. You can send float or string it doesn’t matter. Which mean your json.dumps issue is solved.
About your Signature for this request is not valid is a different issue. Your signature is not valid.
You need to puts some logs around your self.signRequest(params)
Somehow your code is not clear: it should be something like that:
signed_params = self.signRequest(params)
With the current code you share with us with have no idea if the params got signed or not.
Also since you manage to post your request you can share the log of your request ( not the signature and key please ).
Sorry Alexis, I am not very good in Python, I am trying to give you additional information.
This is my signrequest function:
def signRequest(self, params:dict):
query_string = '&'.join(["{}={}".format(d, params[d]) for d in params])
signature = hmac.new(testnet_binance_keys['secret_key'].encode('utf-8'), query_string.encode('utf-8'), hashlib.sha256)
params['signature'] = signature.hexdigest()
I used the same signRequest function that worked with my “new order” function to place a single order. I guess this is the problem, In the single order function you are not supposed to have a json list in the parameters.
Is this what you asked with "some logs around my self.signRequest(params)?
If not, please be so kind to guide me in printing what you wish to read from the code.
Many thanks again for your patience.
def signRequest(self, params:dict):
query_string = '&'.join(["{}={}".format(d, params[d]) for d in params])
signature = hmac.new(testnet_binance_keys['secret_key'].encode('utf-8'), query_string.encode('utf-8'), hashlib.sha256)
params['signature'] = signature.hexdigest()
return params // you should return the params
I have been beating my head against the wall with the same issue. That is, that the signature fails when using batchOrders. I have gotten to the conclusion that the string should be setup differently when calculating the signature as compared to in the url. But I cannot find any information on how e.g. @dino would write the string that is used for calculating the signature provided in the example.
Currently I am providing this query_string when calculating the signature:
batchOrders=[{“symbol”:“BTCUSDT”,“side”:“SELL”,“type”:“MARKET”,“quantity”:“0.001”,“reduceOnly”:“False”}]×tamp=1591910444596
and it is calculated with the following code-snippet from python-binance:
m = hmac.new(self.API_SECRET.encode(‘utf-8’), query_string.encode(‘utf-8’), hashlib.sha256)
Does anyone happen to know if there is something obviously wrong in my query_string?
Ah, thank you. I assumed that the .encode(‘utf-8’) would perform that operation for me. But apparently it required me to manually replace the {} and " before generating the signature.
Sorry, I found the original encoding strategies is not available. It’s asking for standard url encoding for the json string, this example script is working for now.