Skip to content

Instantly share code, notes, and snippets.

@jmoz
Created August 24, 2022 02:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmoz/567163be72712cdf6d4913a40ebd6256 to your computer and use it in GitHub Desktop.
Save jmoz/567163be72712cdf6d4913a40ebd6256 to your computer and use it in GitHub Desktop.
ccxt fetch all candles
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)
@jmoz
Copy link
Author

jmoz commented Aug 24, 2022

fetch_candles is a proxy method with retry code and model wrapper. Replace with ccxt's fetch_ohlcv.

Client code should look like:

async for candles in exchange.fetch_all_candles('BTC-PERP', '1m'):
    print(candles)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment