Is there a way to get the data that Generate Transaction History is getting (User_ID,“UTC_Time”,“Account”,“Operation”,“Coin”,“Change”,“Remark”) programmaticaly?
I am aware that is very heavy in weight and i will use it once in a while. The alternative which is using /api/v3/myTrades and having to put the exact symbols is impossible to work with.
Binance offers endpoints that can help you retrieve detailed account and trade history.
Here’s how you can approach this using Python with the binance package, which is a popular client library for interacting with the Binance API. You will need to install it first if you haven’t already:
pip install python-binance
The script below demonstrates how to fetch your account’s transaction history which includes deposits, withdrawals, and trades. Binance separates these into different endpoints, so you will have to make multiple calls if you need a comprehensive transaction history:
import os
from binance.client import Client
from datetime import datetime
import pandas as pd
Setup Binance API key and secret
api_key = ‘your_api_key’
api_secret = ‘your_api_secret’
Initialize the Binance client
client = Client(api_key, api_secret)
def get_deposit_history():
“”“Retrieve deposit history.”“”
return client.get_deposit_history()
def get_withdraw_history():
“”“Retrieve withdrawal history.”“”
return client.get_withdraw_history()
def get_trade_history():
“”“Retrieve trade history across all symbols.”“”
trades =
for symbol in client.get_all_tickers():
trades.extend(client.get_my_trades(symbol=symbol[‘symbol’]))
return trades
def format_history(deposits, withdrawals, trades):
“”“Combine and format transaction history into a DataFrame.”“”
# Format deposits
df_deposits = pd.DataFrame(deposits)
df_deposits[‘Operation’] = ‘Deposit’
# Format withdrawals
df_withdrawals = pd.DataFrame(withdrawals)
df_withdrawals['Operation'] = 'Withdrawal'
# Format trades
df_trades = pd.DataFrame(trades)
df_trades['Operation'] = 'Trade'
# Combine all data
df_combined = pd.concat([df_deposits, df_withdrawals, df_trades], ignore_index=True, sort=False)
df_combined['UTC_Time'] = pd.to_datetime(df_combined['insertTime'], unit='ms')
return df_combined
Fetch history
deposits = get_deposit_history()
withdrawals = get_withdraw_history()
trades = get_trade_history()
Format and combine history
transaction_history = format_history(deposits, withdrawals, trades)
Display the data
print(transaction_history[[‘user_id’, ‘UTC_Time’, ‘Operation’, ‘coin’, ‘amount’, ‘status’]])
Save to CSV (optional)
transaction_history.to_csv(‘transaction_history.csv’, index=False)
This script assumes you have sufficient permissions enabled on your API key to access account information, deposits, withdrawals, and trades.
But this option is really heavy when trying to fetch a years transactions. isn’t any other way without involving checking every symbol possible just for some spot transactions?
I am not quite sure if this is what you are looking for but lets see if this helps you!
We can streamline the process using the Binance API’s capability to retrieve spot account trade history without specifying individual symbols. This can be achieved by using the get_account_trades
method, which lists all trades related to the spot account, reducing the need to iterate over each symbol.
import os
from binance.client import Client
from datetime import datetime
import pandas as pd
Setup Binance API key and secret
api_key = ‘your_api_key’
api_secret = ‘your_api_secret’
Initialize the Binance client
client = Client(api_key, api_secret)
def get_deposit_history():
“”“Retrieve deposit history.”“”
return client.get_deposit_history()
def get_withdraw_history():
“”“Retrieve withdrawal history.”“”
return client.get_withdraw_history()
def get_trade_history():
“”“Retrieve trade history across all spot trades.”“”
return client.get_account_trades()
def format_history(deposits, withdrawals, trades):
“”“Combine and format transaction history into a DataFrame.”“”
# Format deposits
df_deposits = pd.DataFrame(deposits)
df_deposits[‘Operation’] = ‘Deposit’
df_deposits[‘UTC_Time’] = pd.to_datetime(df_deposits[‘insertTime’], unit=‘ms’)
# Format withdrawals
df_withdrawals = pd.DataFrame(withdrawals)
df_withdrawals['Operation'] = 'Withdrawal'
df_withdrawals['UTC_Time'] = pd.to_datetime(df_withdrawals['insertTime'], unit='ms')
# Format trades
df_trades = pd.DataFrame(trades)
df_trades['Operation'] = 'Trade'
df_trades['UTC_Time'] = pd.to_datetime(df_trades['time'], unit='ms')
# Combine all data
df_combined = pd.concat([df_deposits, df_withdrawals, df_trades], ignore_index=True, sort=False)
return df_combined
Fetch history
deposits = get_deposit_history()
withdrawals = get_withdraw_history()
trades = get_trade_history()
Format and combine history
transaction_history = format_history(deposits, withdrawals, trades)
Display the data
print(transaction_history[[‘user_id’, ‘UTC_Time’, ‘Operation’, ‘coin’, ‘amount’, ‘status’]])
Save to CSV (optional)
transaction_history.to_csv(‘transaction_history.csv’, index=False)
By using get_account_trades()
, this script directly fetches all spot trades without having to iterate through each symbol, significantly reducing the API calls and processing time.
The script formats and combines deposit, withdrawal, and trade data into a single DataFrame, standardizing the timestamps and data structure for easier manipulation and analysis.
This approach is more efficient, especially for accounts with a large number of trades across many symbols, as it minimizes the number of API calls and data processing required.
but there is not get_account_trades in binance api. are you using another library?
Oh Sorry! I got mixed-up! You are right Binance API does not have a direct method called get_account_trades()
In my knowledge, the method to retrieve all trades without specifying each symbol is not directly available. The only way to retrieve trade history across all symbols, as per the official Binance API and the python-binance library, is to iterate through each symbol using the get_my_trades()
method, which indeed can be quite resource-intensive if you have trades across many symbols.
However, for a more efficient approach, you could utilize the get_all_orders()
method, which can retrieve all orders for a specific symbol including those that did not result in trades. This still requires iterating through all symbols but provides comprehensive data about order history which includes executed trades.
import os
from binance.client import Client
from datetime import datetime
import pandas as pd
Setup Binance API key and secret
api_key = ‘your_api_key’
api_secret = ‘your_api_secret’
Initialize the Binance client
client = Client(api_key, api_secret)
def get_all_tickers():
“”“Retrieve all tickers.”“”
return [ticker[‘symbol’] for ticker in client.get_all_tickers()]
def get_trade_history(symbols):
“”“Retrieve trade history for all given symbols.”“”
trades =
for symbol in symbols:
trades.extend(client.get_my_trades(symbol=symbol))
return trades
def format_trades(trades):
“”“Format trades into a DataFrame.”“”
df_trades = pd.DataFrame(trades)
df_trades[‘Operation’] = ‘Trade’
df_trades[‘UTC_Time’] = pd.to_datetime(df_trades[‘time’], unit=‘ms’)
return df_trades
Get all ticker symbols
symbols = get_all_tickers()
Fetch trades for all symbols
trades = get_trade_history(symbols)
Format and display trades
df_trades = format_trades(trades)
print(df_trades[[‘symbol’, ‘UTC_Time’, ‘Operation’, ‘price’, ‘qty’, ‘commission’]])
The script still requires iterating through each symbol but it’s streamlined to fetch and format trades.
This approach is still heavy on API calls, especially for accounts with a vast number of symbols involved in trading.
To optimize further, consider caching results and only querying for new trades since the last update. Another approach could be to focus on those symbols where you are most active, reducing the number of unnecessary API calls.
I am sorry for the mixup in my last message!
You can download CSV with transaction history for all symbols via Web UI:
I know some sites like Fiscal Cripto, who download the all history via API. Currently I’m not being able to get older trades before 2023. Is there a way to get those?
Yes, I have the same issue, but I couldn’t find any logic in response - for example I couldn’t receive the data about the trades SYS/BTC, but still can fetch the trade SYS/ETH which happened at the same time
@panvl I have the same scenario as you,
First I need to get all the symbols from GET /api/v3/exchangeInfo
Then make requests to GET api/v3/myTrades passing each of the symbols, this is very costly, for me and for the Binance API.
@Asheesh_Kumar I think it would be extremely useful to have an endpoint that returns the history of symbols operated by the user, I think this would make life easier for those who need to integrate and obtain a history. In addition, it would avoid hundreds of thousands of requests to the Binance API.