How to download free OHLC historical cryptocurrency daily data using a python script

With bitcoin recently reaching above the 11,000 USD per BTC mark and other cryptocurrencies such as ethereum gaining traction as well, it has now become common place for people to start developing trading systems for the cryptocurrency space. One issue with this however is that cryptocurrency data is by no means centralized – like regular Forex data – which makes it difficult for traders to get a good historical data source. Many of the data repositories that are available online are not free – charging even thousands of dollars for data – while others contain data that is simply not updated regularly (such as this kaggle data source). Today I am going to talk about an easy and reliable way to get cryptocurrency data, allowing you to update your data whenever you want and get access to years of daily historical data for many different cryptocurrencies and exchanges in only a few seconds.

import numpy as np
import datetime
import time
import re
import pandas as pd
import requests
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-e', '--exchange')
parser.add_argument('-p','--pair')
args = parser.parse_args()

pair = args.pair
exchange = args.exchange

resp = requests.get("http://bit.ly/2Bwl5ZX(exchange, pair))
resp = resp.json()
resp = resp["result"]["86400"]

all_candles = []
for c in resp:
    all_candles.append({"Date": time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(c[0])), "Open": float(c[1]), "High": float(c[2]), "Low": float(c[3]), "Close": float(c[4]), "Volume": float(c[5])})

df = pd.DataFrame(all_candles)
df = df.set_index(["Date"])
df = df[["Open", "High", "Low", "Close", "Volume"]]
print df
df.to_csv("{}_{}_1440.csv".format(exchange, pair))

Although different exchanges offer interfaces that allow us to download data from each one of them it is quite cumbersome to implement a separate solution to download data from each different cryptocurrency exchange. Thankfully there is a website called cryptowatch which has pooled cryptocurrency data from several different exchanges into an easy to use resource. Even better, cryptowatch offers a REST API that allows us to easily access their service in order to obtain historical data and general cryptocurrency information (even order-book data). Today we are going to be taking advantage of their OHLC request function in order to download historical data from their servers from any particular broker that interests us.

The script I have posted above allows you to easily obtain cryptocurrency OHLC daily data for any exchange/pair you want that is carried by cryptowatch. The python script needs to be executed with the parameters “-p” (pair) and “-e” (exchange) which tell it exactly what exchange/pair combination you desire. For example running the above scripts by using the command “python script.py -p btcusd -e kraken” will download all available historical data for bitcoin (for kraken this is from 2013 to 2017), the data is then printed on the terminal and also saved into a csv file for future loading/use without having to call the servers again. The OHLC prices seem to be Bid prices and the time zone of the candles is GMT 0:00. 

Use of this script has some limits as well, when using the cryptowatch servers you’re limited to a given amount of server time per hour, so it is a good idea to avoid calling scripts that request a lot of data frequently. If you want to update your data more frequently then you should modify the API calls and dataframe processing to append and modify your already downloaded full history file instead of having to redownload the entire historical data set each time. It is also worth noting that you can also request candles for other timeframes if you wish, but you will have to modify the calls to do this (you can refer to the cryptowatch documentation for more information about the parameter calls for this API functions). For lower timeframes the total amount of data available will be less and – if the dataframe is too small – you will probably need several hours and calls structured with the before/after parameters to properly get all the data in chunks.

It is also worth mentioning that although there are several python libraries to interface with the cryptowatch API – such as this one and this one – I couldn’t get any of these libraries to work correctly as their data processing seemed to always give me incorrect times when asking for OHLC data, probably due to some issues with how these libraries parse the return OHLC values. The easiest way I found to get the data was to actually write the REST API call myself and to then parse the returned json values manually to put them into a pandas dataframe. Besides showing you how to download the data the script also shows you how to get the data into a pandas dataframe, a very useful thing if you want to perform some trading or data analysis in python using these data sets.

The above should give anyone interested in the design of cryptocurrency trading systems – or even just the analysis of cryptocurrency data – a good start. The first problem with system development is always finding some historical data, once this is solved we can start working with it. Whether something useful can be done with it is an entirely different matter altogether. If you would like to learn more about how we design systems and how you too can learn how to trade using portfolios of algorithmic trading strategies please consider joining Asirikuy.com, a website filled with educational videos, trading systems, development and a sound, honest and transparent approach towards automated trading.

from Mechanical Forex http://bit.ly/2jbeJIQ
via IFTTT