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:
Import required librariesimport ccxt
Initialize Binance API connectionbinance = 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 monthsfor 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 to1588669559517
. 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 connectionbinance = 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 timemax_time = int((result['trades'][0]['time'] - result['trades'][1]['time']).total_seconds() / 60)
Iterate through each trade and print all relevant informationfor 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.