sonvister / Binance

A .NET Standard Binance API library.
MIT License
229 stars 77 forks source link

Problem with the GetValidValue() #101

Closed zbigniew-klasik closed 6 years ago

zbigniew-klasik commented 6 years ago

On symbol ETHBTC. For initial quantity 0.0466234368291005145420025865 GetValidValue() returns 0.0470000000000000000000000000 But it should return 0.047 (without following zeros)

Later Validate() does not throw an exception, but PlaceAsync() throws: Binance.BinanceHttpException: [BadRequest]: 'Bad Request' - Illegal characters found in parameter 'quantity'; legal range is '^([0-9]{1,20})(.[0-9]{1,20})?$'.

For me, it looks like the following zeros are serialized and send to Binance as strings and do not pass the regex validation. The regex looks like constant, it allows 20 decimal places so the easiest fix for this problem is something like that: quantity = decimal.Round(quantity, 20); and now the quantity is 0.04700000000000000000

and the PlaceAsync() works well without throwing the exception.

sonvister commented 6 years ago

@zbigniew-klasik, thanks for mentioning the issue and providing useful information.

I added a public decimal extension, Normalize(), to remove any trailing zeros from decimal values and applied it before the parameters are serialized and added to the HTTP request. This should resolve the error.

I chose not to modify GetValidValue(), GetUpperValidValue(), and GetLowerValidValue() since these are just one possible source of decimal values that may be passed to PlaceOrderAsync(). However, I am currently considering updating these methods to truncate the precision of the returned value to the increment precision (which would also resolve the error).

sonvister commented 6 years ago

@zbigniew-klasik, 0.2.0-beta6 is released. Please let me know if the error is resolved (and feel free to close this issue). I chose not to modify GetValidValue(), GetUpperValidValue(), or GetLowerValidValue() at this time.

zbigniew-klasik commented 6 years ago

@sonvister Now it works well. Thanks for the fix.