Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save normanlmfung/7458c6400eceafb3e1a1ec7e96948564 to your computer and use it in GitHub Desktop.
Save normanlmfung/7458c6400eceafb3e1a1ec7e96948564 to your computer and use it in GitHub Desktop.
ccxt loop 'since'
# Sample from @Kroitor 20220426
import ccxt
def table(values):
first = values[0]
keys = list(first.keys()) if isinstance(first, dict) else range(0, len(first))
widths = [max([len(str(v[k])) for v in values]) for k in keys]
string = ' | '.join(['{:<' + str(w) + '}' for w in widths])
return "\n".join([string.format(*[str(v[k]) for k in keys]) for v in values])
def main():
exchange = ccxt.gateio()
markets = exchange.load_markets()
# exchange.verbose = True # uncomment for debugging purposes if necessary
since = exchange.parse8601('2022-01-01T00:00:00Z')
symbol = 'BTC/USDT'
timeframe = '1h'
all_ohlcvs = []
while True:
ohlcvs = exchange.fetch_ohlcv(symbol, timeframe, since)
all_ohlcvs += ohlcvs
if len(ohlcvs):
print('Fetched', len(ohlcvs), symbol, timeframe, 'candles from', exchange.iso8601(ohlcvs[0][0]))
since = ohlcvs[-1][0] + 1
else:
break
print(len(all_ohlcvs))
print(table([[exchange.iso8601(o[0])] + o[1:] for o in all_ohlcvs]))
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment