tiagosiebler / binance

Node.js & JavaScript SDK for Binance REST APIs & WebSockets, with TypeScript & browser support, integration tests, beautification & more.
MIT License
762 stars 265 forks source link

Round to tick size function #309

Open posman opened 1 year ago

posman commented 1 year ago

Don't know if this function already exists but I wasn't able to find it (if it already exists then close the issue)

It'd be very helpful to have a function that can round the price and help pass the Binance Filters https://binance-docs.github.io/apidocs/futures/en/#filters

One of the filters is the PRICEFILTER where Binance will return error when price is not aligned to tickSize -4014 Price not increased by tick size_

The information for the tickSize is in the informationr returned by /fapi/v1/exchangeInfo

{
  ...
  "symbols": [
    {
      "symbol": "BTCUSDT",
      ...
      "filters": [
        {
          "filterType": "PRICE_FILTER",
          "maxPrice": "300",
          "minPrice": "0.0001", 
          "tickSize": "0.10"
        },
      ]
    }
  ]
}

For example, placing an order for BTCUSDT with entry price $19500.38 will return an error. The correct price that Binance will accept is $19500.30, based on tickSize

One way I found to round the price is: Math.round(price / tickSize) * tickSize

This works for every tickSize as long as it uses number "1" (just as I think is the case to all symbols in Binance). But it won't work if the number is different, like happens in Futures (in NASDAQ) where ticks could be "0.0005"

tiagosiebler commented 1 year ago

Completely agree, would like to get utility functions like this added to the SDK, including adding unit test coverage to make sure all the different scenarios we see are covered and never accidentally broken. It's one of the painful aspects of trading on binance that could be made easier like this. I'll get to adding it eventually but I don't have bandwidth for this right now, but if anyone wants to contribute, I'd love to see some proposals/pull requests or even just code snippets here.