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