I want to get historical data for more than 100 cryptocurrencies with Binance API but when I execute the code I only get the data for one coin. Example code is:
binance_symbols = ['BTCUSDT', 'ETHUSDT', 'XRPUSDT', 'SOLUSDT']
for symbol in binance_symbols:
klines = pd.DataFrame(client.get_historical_klines(symbol, "1h", "1 Aug, 2021", "24 Sep, 2021"))
Is there any alternative way to get data of all coins?
With each cycle of the for-loop, you are overwriting the old Dataframe, because you are declaring klines
over and over again. A better way is to make an empty Dataframe and then append the data for the other coins.
Example:
binance_symbols = ['BTCUSDT', 'ETHUSDT', 'XRPUSDT', 'SOLUSDT']
klines = client.get_historical_klines(binance_symbols[0], "1h", "1 Aug, 2021", "24 Sep, 2021")
for symbol in binance_symbols[1:]:
new_df = pd.DataFrame(client.get_historical_klines(symbol, "1h", "1 Aug, 2021", "24 Sep, 2021"))
klines.append(new_df)
binance_symbols = ['BTCUSDT', 'ETHUSDT', 'SOLUSDT'] klines = []
for symbol in binance_symbols: new_df = pd.DataFrame(client.get_historical_klines(symbol, Client.KLINE_INTERVAL_1DAY, '1 Jan 2020')) klines.append(new_df)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.