node-binance-api icon indicating copy to clipboard operation
node-binance-api copied to clipboard

Best way to convert usd to coin volume?

Open Jkurbs opened this issue 2 years ago • 8 comments

What's the best way to convert USD to volume?

Jkurbs avatar Oct 11 '21 22:10 Jkurbs

Can you provide more details? Example or screenshot on the desired result?

This? Screenshot 2021-10-12 at 15 51 50

Have you checked this part of the documentation? https://github.com/jaggedsoft/node-binance-api#get-candlestick-updates-via-websocket

pawelangelow avatar Oct 12 '21 12:10 pawelangelow

No, let's say I want to place a limit order for 100$. To do that I need the quantity(volume) of coins to buy for 100$.

binance.buy("BNBETH", quantity, price, {type:'LIMIT'}, (error, response) => {
  console.info("Limit Buy response", response);
  console.info("order id: " + response.orderId);
});

What's the best way to calculate that quantity?

Here's what I'm currently using but I wonder if there's a better and faster way to do it:

async function convert_volume(symbol, amount) {
  lot_size = 0
  volume = 0
  return new Promise((resolve, reject) => {
      client.prices(symbol, (error, ticker) => {
        if (error != null) {reject(functions.logger.error('Error getting prices: ', error))}
        client.exchangeInfo(function(error, data) {
          if (error != null) {
            if (error.code == 'ESOCKETTIMEDOUT' || 'ETIMEDOUT') {
              functions.logger.error("Converting volume failed Retrying", error['body'])
              convert_volume(symbol, amount)
              return
            }
          }
          for ( let obj of data.symbols ) {
            if (obj['symbol'] == symbol) {
              let step_size = obj['filters'][2]['stepSize']
              lot_size = step_size.indexOf('1') - 1
              if (lot_size < 0) {
                lot_size = 0
              }           
            }
          }
          let volume = parseFloat(amount/ticker[symbol])
          volume = volume.toFixed(lot_size)
          resolve(volume)
      });
    })
  })
} 

Jkurbs avatar Oct 12 '21 14:10 Jkurbs

Okay. Thanks for clarifying the question. IMO you need to change the title as well to empower future searches.

There are few things:

  • client.exchangeInfo is kind of static. The information there won't change every second/minute or even day, so it's safe to cache this information somewhere and not call it on every client.prices(). You can create getLotSize(symbol), where the result is cached.
  • parseFloat(amount/ticker[symbol]) may get the Math wrong, so consider using external library, for example https://mikemcl.github.io/decimal.js/ or https://github.com/MikeMcl/big.js/

There's no built-in solution to provide you with what quantity is the result of the [pair ratio * amount of one of the pair parts], a.k.a how much BTC can I buy for 100 USDT. [BTC-USDT].

Kind of out of the context of the question, but what is the intended usage? What is the problem you are trying to solve?

Are you trying to create an order at a specific time moment? If this is the case, is there a problem to use a market order? Even with a proper price for a limit order, there's a chance for a partial fulfill or even to become stale - if you hit the max price for a pair, and then the price drops.

pawelangelow avatar Oct 13 '21 08:10 pawelangelow

Thanks for the insight, will use decimal.js and save Exchange infos! I'm creating a simple bot that is buying at the average low and selling at the average high by placing limit orders. If you have some time take look and let me know what you think!

Here's the bot(Not done yet): Bot

And do you know what's wrong with this function:

binance.orderStatus("ETHBTC", orderid, (error, orderStatus, symbol) => {
  console.info(symbol+" order status:", orderStatus);
});

I get this error every time I try to use it, even if I pass a valid orderId: {"code":-1102,"msg":"Param 'origClientOrderId' or 'orderId' must be sent, but both were empty/null!"}

Here's the link to the issue: https://github.com/jaggedsoft/node-binance-api/pull/716

Any help is appreciated.

Jkurbs avatar Oct 13 '21 13:10 Jkurbs

Thanks for the insight, will use decimal.js and save Exchange infos! I'm creating a simple bot that is buying at the average low and selling at the average high by placing limit orders. If you have some time take look and let me know what you think!

IMO you can go with market orders instead of limit - you probably won't buy/sell at the target price, but the difference would be really tiny. You can get what happened (the actual amounts of the target pair) with the callback.

Please, update the title of this issue and mark it as done 😸

About the help with the bot will take a look at the other issue! Let's keep the things separated 😄

pawelangelow avatar Oct 13 '21 14:10 pawelangelow

Okay thanks, but please look at the other issue! Link

Jkurbs avatar Oct 13 '21 15:10 Jkurbs

Will do, after we close this one. The work left to be done:

  • update title
  • mark it as closed

pawelangelow avatar Oct 14 '21 07:10 pawelangelow

Thanks for the insight, will use decimal.js and save Exchange infos! I'm creating a simple bot that is buying at the average low and selling at the average high by placing limit orders. If you have some time take look and let me know what you think!

Here's the bot(Not done yet): Bot

And do you know what's wrong with this function:

binance.orderStatus("ETHBTC", orderid, (error, orderStatus, symbol) => {
  console.info(symbol+" order status:", orderStatus);
});

I get this error every time I try to use it, even if I pass a valid orderId: {"code":-1102,"msg":"Param 'origClientOrderId' or 'orderId' must be sent, but both were empty/null!"}

Here's the link to the issue: #716

Any help is appreciated.

Hello, to fix it you need to change the version of node-binance-api from your package.json to 0.12.5 cos this error occur only on the last version

Enjoy 😉

MesterFri avatar Jan 12 '22 05:01 MesterFri