Download Historical Data of All Cryptocoins with CCXT for free

Download Historical Data of All Cryptocoins with CCXT for free

In my previous post, I explained how to download historical data for Binance coins. The good news is that it is also possible to download historical data of ALL coins, in various frequencies from minutes to weeks, available in the market without any subscription cost whatsoever thanks to an API called ccxt which is available in JavaScript, Python and PHP. I will share the script I put together in Python that manages that. The ccxt library is pretty straightforward to install:

pip install ccxt

By all cryptocoins I mean almost all coins available out there against BTC such as ETH/BTC, ADA/BTC, BNB/BTC, etc. There are more than 100 exchanges supported by ccxt, all of which I tested and included the ones that support historical download of OHLCV data in the below script. In the end you should end up with historical data of more than 1400 coins.

import ccxt
import pandas as pd

exch = 'binance' # initial exchange
t_frame = '1d' # 1-day timeframe, usually from 1-minute to 1-week depending on the exchange
symbol = 'ADA/BTC' # initial symbol
exchange_list = ['binance','bitfinex','bytetrade','ftx','kraken','poloniex','upbit','acx','bequant','bigone','bitforex','bitkk','bitz','btcalpha','coinex','crex24','digifinex','gateio','hitbtc2','huobipro','huobiru','kucoin','lbank','okex','okex3','stex','upbit','whitebit','zb']
 
# Get our Exchange
try:
    exchange = getattr (ccxt, exch) ()
except AttributeError:
    print('-'*36,' ERROR ','-'*35)
    print('Exchange "{}" not found. Please check the exchange is supported.'.format(exch))
    print('-'*80)
    quit()
 
# Check if fetching of OHLC Data is supported
if exchange.has["fetchOHLCV"] != True:
    print('-'*36,' ERROR ','-'*35)
    print('{} does not support fetching OHLC data. Please use another  exchange'.format(exch))
    print('-'*80)
    quit()
 
# Check requested timeframe is available. If not return a helpful error.
if (not hasattr(exchange, 'timeframes')) or (t_frame not in exchange.timeframes):
    print('-'*36,' ERROR ','-'*35)
    print('The requested timeframe ({}) is not available from {}\n'.format(t_frame,exch))
    print('Available timeframes are:')
    for key in exchange.timeframes.keys():
        print('  - ' + key)
    print('-'*80)
    quit()
 
# Check if the symbol is available on the Exchange
exchange.load_markets()
if symbol not in exchange.symbols:
    print('-'*36,' ERROR ','-'*35)
    print('The requested symbol ({}) is not available from {}\n'.format(symbol,exch))
    print('Available symbols are:')
    for key in exchange.symbols:
        print('  - ' + key)
    print('-'*80)
    quit()
 
 
# Get data
data = exchange.fetch_ohlcv(symbol, t_frame)
header = ['Timestamp', 'Open', 'High', 'Low', 'Close', 'Volume']
df = pd.DataFrame(data, columns=header).set_index('Timestamp')
df['symbol'] = symbol
syms = [symbol]
filename = '{}.csv'.format(t_frame)

for exch in exchange_list:
    try:
        exchange = getattr (ccxt, exch) ()
    except AttributeError:
        print('-'*36,' ERROR ','-'*35)
        print('Exchange "{}" not found. Please check the exchange is supported.'.format(exch))
        print('-'*80)
        quit()
    if exchange.has["fetchOHLCV"] != True:
        print('-'*36,' ERROR ','-'*35)
        print('{} does not support fetching OHLC data. Please use another exchange'.format(exch))
        print('-'*80)
        quit()
    if (not hasattr(exchange, 'timeframes')) or (t_frame not in exchange.timeframes):
        print('-'*36,' ERROR ','-'*35)
        print('The requested timeframe ({}) is not available from {}\n'.format(t_frame,exch))
        print('Available timeframes are:')
        for key in exchange.timeframes.keys():
            print('  - ' + key)
        print('-'*80)
        quit()
    exchange.load_markets()
    for coin in exchange.symbols:
        if coin in syms or coin[-3:] != 'BTC':
            continue
        else:
            try:
                data = exchange.fetch_ohlcv(coin, t_frame)
            except:
                continue
            data_df = pd.DataFrame(data, columns=header).set_index('Timestamp')
            data_df['symbol'] = coin
            df = df.append(data_df)
            syms.append(coin)
df.index = df.index/1000 #Timestamp is 1000 times bigger than it should be in this case
df['Date'] = pd.to_datetime(df.index,unit='s')
df.to_csv(filename)

This Linkedin format for code is kind of funny, so apologies for a few possible indent issues, but you should be able to fix them easily. In a matter of minutes you should also be able to download all the data in usual time series format of OHLCV as a csv file. Be aware of hitting rate limits occasionally, then you should try again after a while. Of course the exchange list should be updated from time to time.

References:

https://github.com/ccxt/ccxt

https://backtest-rookies.com/2018/03/08/download-cryptocurrency-data-with-ccxt/


Pavel Valentov

Founder and CTO at Angry Robot Deals

2y

Hello. Try to get candles of the '15m' timeframe from 'btcturk' by CCXT. You will be surprised. 😉

Like
Reply
Samia NACIRI

Experte Data | CDMP certified

2y

Hi , have you observed any delay in historical data for large timeframe?

Christopher Collins

Business owner at TenderAPI

3y

Great article Gursel Karacor thanks for sharing.

Pedro Birmann

Start-up Co-Founder, Blockchain, Cryptocurrency, Trading, Technology, Portfolio Manager

3y

Great

To view or add a comment, sign in

Explore topics