Search
Close this search box.

Ethereum: Python binance fetchMyTrades gives only 3 month of personal trades, so how do one iterates through results?

Iterating through fetchMyTrades results on Binance Python with ccxt library

The issue you are facing is due to the short expiry time specified by the since parameter. When using fetchMyTrades, the result will be fetched up to a certain point in time, but it may not include all trades from that point onwards.

To retrieve trades older than 3 months, you need to use the before parameter with the since and limit parameters. Here is an example code snippet:








Ethereum: Python binance fetchMyTrades gives only 3 month of personal trades, so how do one iterates through results?

Import required libraries

import ccxt


Initialize Binance API connection

binance = ccxt.binance({

'apiKey': 'YOUR_API_KEY',

'apiSecret': 'YOUR_API_SECRET'

})


Set the initial result of trades to fetch for a period of 3 months (994,893 trades)

result = binance.fetchMyTrades('BTC/USDT', since=1588669559517, limit=10000)


Print all trades from the previous 3 months

for trade in result:

print(trade['id'], trade['time'], trade['symbol'])

In this example:

  • We initialize binance with your Binance API credentials.
  • We set the initial trade result to fetch for a period of 3 months (994,893 trades) using the since parameter set to 1588669559517. This corresponds to January 1, 2020, 10:35 AM UTC.
  • We use the limit parameter set to 10000 to retrieve only the top 10,000 trades from that point onwards (i.e., in the last 3 months).
  • Finally, we iterate through each trade result and print all relevant information, including the trade ID, time, and symbol.

Note: The number of trades retrieved will depend on the API rate limit. To avoid hitting the rate limit, you can set a limit parameter that is slightly lower than the desired results. Also, be aware that searching for trades up to 3 months ago may not include all trades from that point forward due to trade history limits.

Workaround:

To retrieve trades older than 3 months using just fetchMyTrades, you can use a combination of the since and before parameters. Here is a workaround:


Initialize Binance API connection

binance = ccxt.binance({

'apiKey': 'YOUR_API_KEY',

'apiSecret': 'YOUR_API_SECRET'

})


Set the initial trade result to fetch for a period of 3 months (994,893 trades)

result = binance.fetchMyTrades('BTC/USDT', since=1588669559517)


Calculate the maximum possible trade time

max_time = int((result['trades'][0]['time'] - result['trades'][1]['time']).total_seconds() / 60)


Iterate through each trade and print all relevant information

for trades in result:

print(trade['id'], trade['time'], trade['symbol'])

In this example, we calculate the maximum possible trade time by subtracting the oldest trade time from the most recent trade time. We then iterate through each trade result and print all relevant information.

Keep in mind that both solutions will still have limitations due to API rate limits. To avoid hitting these limits, it is recommended to use the limit parameter with the before and since parameters when fetching trades.

Ethereum Mining Come

Facebook
Twitter
LinkedIn
Telegram
WhatsApp

Table of Contents

Most Populer