|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import time |
| 6 | + |
| 7 | +# ----------------------------------------------------------------------------- |
| 8 | + |
| 9 | +root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 10 | +sys.path.append(root + '/python') |
| 11 | + |
| 12 | +# ----------------------------------------------------------------------------- |
| 13 | + |
| 14 | +import ccxt # noqa: E402 |
| 15 | + |
| 16 | +# ----------------------------------------------------------------------------- |
| 17 | +# common constants |
| 18 | + |
| 19 | +msec = 1000 |
| 20 | +minute = 60 * msec |
| 21 | +hold = 30 |
| 22 | + |
| 23 | +# ----------------------------------------------------------------------------- |
| 24 | + |
| 25 | +exchange = ccxt.bitfinex({ |
| 26 | + 'rateLimit': 3000, |
| 27 | + 'enableRateLimit': True, |
| 28 | + # 'verbose': True, |
| 29 | +}) |
| 30 | + |
| 31 | +# ----------------------------------------------------------------------------- |
| 32 | + |
| 33 | +from_datetime = '2017-01-01 00:00:00' |
| 34 | +from_timestamp = exchange.parse8601(from_datetime) |
| 35 | + |
| 36 | +# ----------------------------------------------------------------------------- |
| 37 | + |
| 38 | +now = exchange.milliseconds() |
| 39 | + |
| 40 | +# ----------------------------------------------------------------------------- |
| 41 | + |
| 42 | +data = [] |
| 43 | + |
| 44 | +while from_timestamp < now: |
| 45 | + |
| 46 | + try: |
| 47 | + |
| 48 | + print(exchange.milliseconds(), 'Fetching candles starting from', exchange.iso8601(from_timestamp)) |
| 49 | + ohlcvs = exchange.fetch_ohlcv('BTC/USD', '1m', from_timestamp) |
| 50 | + print(exchange.milliseconds(), 'Fetched', len(ohlcvs), 'candles') |
| 51 | + first = ohlcvs[0][0] |
| 52 | + last = ohlcvs[-1][0] |
| 53 | + print('First candle epoch', first, exchange.iso8601(first)) |
| 54 | + print('Last candle epoch', last, exchange.iso8601(last)) |
| 55 | + from_timestamp += len(ohlcvs) * minute |
| 56 | + data += ohlcvs |
| 57 | + |
| 58 | + except (ccxt.ExchangeError, ccxt.AuthenticationError, ccxt.ExchangeNotAvailable, ccxt.RequestTimeout) as error: |
| 59 | + |
| 60 | + print('Got an error', type(error).__name__, error.args, ', retrying in', hold, 'seconds...') |
| 61 | + time.sleep(hold) |
0 commit comments