HI!.
I’m currently getting trading volume for certain symbols in the following way:
Iterating every symbol in binance, get trading volume for each symbol-pair, using get_historical_klines
.
def get_24h_volume(symbol):
# get trading volumes for the last 24 hours
trading_volume = 0
prices = client.get_all_tickers()
for coin in prices:
if coin['symbol'].startswith(symbol) and coin['symbol'].startswith('BTCDOWN') == False and coin['symbol'].startswith('BTCUP') == False and coin['symbol'].startswith('BTCST') == False:
try:
klines = client.get_historical_klines(coin['symbol'], Client.KLINE_INTERVAL_1MINUTE, "1 day ago")
for history in klines:
trading_volume += float(history[5])
print(coin['symbol'], trading_volume)
except:
pass
return trading_volume
print(get_24h_volume('BTC'))
But there’s a problem, which I have to manually add condition such as coin['symbol'].startswith('BTCST') == False
.
But What if new coin named ‘BTCXY’ or ‘BTCAC’ and so on created?. Then I will have to add a new condtion like coin['symbol'].startswith('BTCXY') == False
again into my code again…
Is there any better way to get 24hours trading volume of symbol than this?.
Thanks
If your intention is to get the aggregated volume of an asset (ex: BTC) throughout the exchange, perform your checks against the quoteAsset
or baseAsset
properties instead of the symbol
property.
1 Like
HI, Thanks for the help all the time.
Following your advice, I modified mu code in following way:
Iterating symbol-pair if coin[‘symbol’] ends with ‘DOGE’, get klines of it and add trading volume. and add
quoteAsset of it, right?
But only SHIBDOGE ends with DOGE among all symbol-pair. So The func below won’t be adding trading volume for DOGEUSDT, DOGEETH and so on. Am I missing here something or should I try to get 24h trading volume in other way?
Thanks
def get_24h_volume(symbol):
# get trading volumes for the last 24 hours
trading_volume = 0
prices = client.get_all_tickers()
for coin in prices:
if coin['symbol'].endswith(symbol):
try:
klines = client.get_historical_klines(coin['symbol'], Client.KLINE_INTERVAL_1MINUTE, "1 day ago")
for history in klines:
trading_volume += float(history[7])
print(coin['symbol'], trading_volume)
except:
pass
return trading_volume
You are using the incorrect endpoint to obtain a list of symbols.
Use the Exchange Information endpoint, instead of the tickers endpoint
https://binance-docs.github.io/apidocs/spot/en/#exchange-information
{
"timezone": "UTC",
"serverTime": 1565246363776,
"rateLimits": [
{
}
],
"exchangeFilters": [
],
"symbols": [
{
"symbol": "ETHBTC",
"status": "TRADING",
"baseAsset": "ETH",
"baseAssetPrecision": 8,
"quoteAsset": "BTC", // Use this property to check the asset
"quotePrecision": 8,
"quoteAssetPrecision": 8,
"orderTypes": [
"LIMIT",
"LIMIT_MAKER",
"MARKET",
"STOP_LOSS",
"STOP_LOSS_LIMIT",
"TAKE_PROFIT",
"TAKE_PROFIT_LIMIT"
],
"icebergAllowed": true,
"ocoAllowed": true,
"isSpotTradingAllowed": true,
"isMarginTradingAllowed": true,
"filters": [
],
"permissions": [
"SPOT",
"MARGIN"
]
}
]
}
1 Like