Created
August 24, 2022 02:43
-
-
Save jmoz/567163be72712cdf6d4913a40ebd6256 to your computer and use it in GitHub Desktop.
ccxt fetch all candles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async def fetch_all_candles(self, symbol, timeframe='1d', limit=5000, end_time=None): | |
"""If `end_time` is passed it is inclusive in results. | |
This will page through all results and remove the duplicate record, finally make an extra call which will be | |
empty and break the generator. Had to make the extra call as `limit` was not always max 5000 sometimes it | |
returned 5001 or even 4995, so it was impossible to just break if the len() was < 5000. | |
Ccxt paging with `since` is broken, so we must use params end_time and hack the start_time to 0 else | |
library code will override and break pagination. | |
""" | |
end_time = int(time.time()) if not end_time else end_time | |
while True: | |
candles = await self.fetch_candles(symbol, timeframe=timeframe, limit=limit, | |
params={'end_time': end_time, 'start_time': 0}) | |
if not candles: | |
break | |
yield candles | |
end_time = int(candles[0].timestamp / 1000) - self.api.parse_timeframe(timeframe) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
fetch_candles
is a proxy method with retry code and model wrapper. Replace with ccxt'sfetch_ohlcv
.Client code should look like: