Obtain ohlcv data with ccxt library in Python
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
#!/usr/bin/python | |
import ccxt | |
import calendar | |
from datetime import datetime, date, timedelta | |
binance = ccxt.binance() | |
def min_ohlcv(dt, pair, limit): | |
# UTC native object | |
since = calendar.timegm(dt.utctimetuple())*1000 | |
ohlcv1 = binance.fetch_ohlcv(symbol=pair, timeframe='1m', since=since, limit=limit) | |
ohlcv2 = binance.fetch_ohlcv(symbol=pair, timeframe='1m', since=since, limit=limit) | |
ohlcv = ohlcv1 + ohlcv2 | |
return ohlcv | |
def ohlcv(dt, pair, period='1d'): | |
ohlcv = [] | |
limit = 1000 | |
if period == '1m': | |
limit = 720 | |
elif period == '1d': | |
limit = 365 | |
elif period == '1h': | |
limit = 24 | |
elif period == '5m': | |
limit = 288 | |
for i in dt: | |
start_dt = datetime.strptime(i, "%Y%m%d") | |
since = calendar.timegm(start_dt.utctimetuple())*1000 | |
if period == '1m': | |
ohlcv.extend(min_ohlcv(start_dt, pair, limit)) | |
else: | |
ohlcv.extend(binance.fetch_ohlcv(symbol=pair, timeframe=period, since=since, limit=limit)) | |
df = pd.DataFrame(ohlcv, columns = ['Time', 'Open', 'High', 'Low', 'Close', 'Volume']) | |
df['Time'] = [datetime.fromtimestamp(float(time)/1000) for time in df['Time']] | |
df['Open'] = df['Open'].astype(np.float64) | |
df['High'] = df['High'].astype(np.float64) | |
df['Low'] = df['Low'].astype(np.float64) | |
df['Close'] = df['Close'].astype(np.float64) | |
df['Volume'] = df['Volume'].astype(np.float64) | |
df.set_index('Time', inplace=True) | |
return df | |
dt = ['20190101', '20200101'] | |
df = ohlcv(dt, 'ETH/BTC', '1h') |
@zeeskhan1990 sorry this might not work well, but I can tell you why I call fetch_ohlcv twice. limit is set 720 for minute data. The reason I set the limit to 720 was because of the limitation of 1000. The first fetch_ohlcv must obtain 720 minutes candles, then the second one could get the latter 720 minutes candles, which 1440 minutes candles in total. As a result, the min_ohlcv function can return 1440 minutes candles (relevant to 1 day) to the caller where we've got the for loop for each day. I believe that the basic idea is fine.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Since
ohlcv1
andohlcv2
are exactly the same here, can you please shed some light as to why you are adding them to get the final OHLCV ?