Skip to content

Instantly share code, notes, and snippets.

@yuyasugano
Created September 14, 2020 12:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yuyasugano/a1aa0112f5cd469a41dcf248f7dc9754 to your computer and use it in GitHub Desktop.
Save yuyasugano/a1aa0112f5cd469a41dcf248f7dc9754 to your computer and use it in GitHub Desktop.
Obtain ohlcv data with ccxt library in Python
#!/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
Copy link

zeeskhan1990 commented Jun 15, 2021


def min_ohlcv(dt, pair, limit):   
  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

Since ohlcv1 and ohlcv2 are exactly the same here, can you please shed some light as to why you are adding them to get the final OHLCV ?

@yuyasugano
Copy link
Author

@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