jaggedsoft / node-binance-api

Node Binance API is an asynchronous node.js library for the Binance API designed to be easy to use.
MIT License
1.58k stars 768 forks source link

Best way to convert usd to coin volume? #739

Open Jkurbs opened 2 years ago

Jkurbs commented 2 years ago

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

pawelangelow commented 2 years ago

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

Jkurbs commented 2 years ago

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)
      });
    })
  })
} 
pawelangelow commented 2 years ago

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

There are few things:

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.

Jkurbs commented 2 years ago

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.

pawelangelow commented 2 years ago

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 😄

Jkurbs commented 2 years ago

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

pawelangelow commented 2 years ago

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

MesterFri commented 2 years ago

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 😉