Retrieve all Trade History from Binance API

Question:

I am trying to retrieve all Trade History data from Binance using the API. I understand that Binance only allows you to get the data by specifying the ticker, which is unfortunate, but assuming I have all the tickers in a list, and want to download them all, how can I do it in an optimized way? Currently doing the following:

import pandas as pd
from binance import Client, ThreadedWebsocketManager, ThreadedDepthCacheManager
from datetime import datetime

client = Client(Binance_key, Binance_secret)

tickers = ['ADABTC', 'ADABUSD', 'ADAUSDT', 'DOGEUSDT', 'LUNABUSD', 'SOLBUSD', 'SOLUSDT', 
        'USTBUSD', 'XRPBTC', 'XRPBUSD', 'XRPUSDT']

adabtc_trades = client.get_my_trades(symbol=tickers[0])
adabtc_trades_df = pd.DataFrame(adabtc_trades)

Then after getting all dataframes, I’d join them all, however, I feel this is not a good practice, to download and put in dataframes one by one. Recommendations?

Additionally, is there any endpoint for deposits and withdrawals? couldn’t find it , thanks

Asked By: Ricardo Castillo

||

Answers:

This is how I learned Python — extracting data from an API into pandas dataframes. Here is a slight improvement to what you have so far:

Iterate through your tickers and use the ticker value implicitly in your iteration (so you don’t have to use the index). Use pd.concat to concatenate your dataframes together automatically.

df = pd.DataFrame()

for ticker in tickers:
    data = client.get_my_trades(symbol=ticker)
    da = pd.DataFrame(data)
    df = pd.concat([df, da], axis=0, ignore_index=True, sort=False)

df.set_index("id", inplace=True)

Overwrite SQL data

connection = os.environ.get('SQL_CONNECTION_STRING')
df.to_sql('StockDataTable', con=connection, if_exists='replace', index=True)

For more df.to_sql options read here: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_sql.html

Happy to help with anything else you need!

Answered By: juntunen

if you use python.binance there are client.get_deposit_history(coin, status, startTime, endTime, recvWindow) and client.get_withdraw_history(coin, status, startTime, endTime, recvWindow)
List with all endpoints:
https://github.com/sammchardy/python-binance/blob/master/Endpoints.md

Answered By: mariq vlahova
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.