0

I'm building a bot that uses Binance API, and i want to get the value of each trading pair in USD like in their App (check the screenshot), is there a way to acheive this using their API? or the only way is to convert each pair to USD value programmatically, because there is about 1542 trading pairs.

enter image description here

ALAEDDIN
  • 209
  • 3
  • 10

1 Answers1

1

The Binance API has a symbol price ticker endpoint that returns a price when it receives a pair e.g. DOGE and USD would be "DOGEUSD" and would return something like {'symbol': 'ETHBTC', 'price': '0.06045300'}

Idk anything about JS etc., but in python I can get the price of a symbol by using get_symbol_ticker module from the Python Binance library like this:

from binance.client import Client
client = Client(api_key, api_secret, tld='us')
client.get_symbol_ticker(symbol="DOGEUSD")

Assuming this node package works the same way, then you could try something like this(copy-pasted, again I have never touched JS):

const Binance = require('node-binance-api');
const binance = new Binance().options({
  APIKEY: '<key>',
  APISECRET: '<secret>'
});

binance.prices('BNBBTC', (error, ticker) => {
  console.info("Price of BNB: ", ticker.BNBBTC);
});

https://www.npmjs.com/package/node-binance-api

ANYWAYS, once you get the prices it's just a matter of mathing.

ethbtc = client.get_symbol_ticker(symbol="ETHBTC") # response `{'symbol': 'ETHBTC', 'price': '0.06045300'}`
btcusd = client.get_symbol_ticker(symbol="BTCUSD") # response {'symbol': 'BTCUSD', 'price': '32620.8700'}
float(btcusd["price"]) * float(ethbtc["price"]) # returns 1968.48369144

float(BTCUSD["price"]) takes {'symbol': 'BTCUSD', 'price': '32581.0800'} and returns USD per BTC i.e. 32581.08.

Getting BTCUSD and ETHBTC current prices allows us to find out USD per BTC per 1xETH or how many USD costs to buy enough BTC to exchange for a single ETH which at current rates is roughly 1 ETH == 0.060433 BTC == $1968.48

EDIT 1: Get-Products API

As demonstrated here, you can get all the products and a list of their OHLCV values using the get-products API call. Example:

var url = "https://www.binance.us/exchange-api/v1/public/asset-service/product/get-products";

var xhr = new XMLHttpRequest();
xhr.open("GET", url);

xhr.onreadystatechange = function () {
   if (xhr.readyState === 4) {
      console.log(xhr.status);
      console.log(xhr.responseText);
   }};

xhr.send();
5u5pence
  • 11
  • 2
  • Thank you for the reply , but what i want is to convert each pair to USD value programmatically, because there is about 1542 trading pairs – ALAEDDIN Jul 15 '21 at 12:09
  • @ALAEDDIN Well that's easy, just omit the symbol and it will return all of the symbol prices. [This guy](https://stackoverflow.com/a/67240255/15576236) describes another way to get the symbols. Simply adding another for loop to that could get you the quote and base symbols, which you could use to do what I showed above in another for loop. – 5u5pence Jul 15 '21 at 21:07
  • Time is critical in the app i'm building, it's would consume more time if i'm using loops (i used enough of them lol).. what i'm wondering if there's this option in binance API somewhere because they already showing that data in their app ( check screenshot) – ALAEDDIN Jul 16 '21 at 09:24
  • Do you which symbol that USD amount is for? (This)[https://reqbin.com/fylgwpwb] Will show you all the products. I'll update my answer. – 5u5pence Jul 21 '21 at 12:46
  • I use the python binance wrapper, but I cannot find any symbols that list assets in USD - I only see trading pairs (e.g. BTCUSDT, but not BTCUSD). As an example, these are all the symbols I find that contain 'BTC' and 'USD': ['BTCUSDT', 'BTCTUSD', 'TUSDBTC', 'BTCUSDC', 'BTCUSDS', 'BTCBUSD', 'BTCUPUSDT', 'BTCDOWNUSDT', 'SUSDBTC', 'BTCSTBUSD', 'BTCSTUSDT', 'BTCUSDP']. I'm guessing it might be connected to using the binance.com URL rather than binance.us. Any ideas? – thecpaptain Feb 02 '22 at 21:35