Skip to content

Instantly share code, notes, and snippets.

@piersstorey
Last active January 14, 2018 14:53
Show Gist options
  • Save piersstorey/b7019151b67271c82f85334f8b0058df to your computer and use it in GitHub Desktop.
Save piersstorey/b7019151b67271c82f85334f8b0058df to your computer and use it in GitHub Desktop.
Cryptocompare Python API wrapper
# Class for cryptocompare API requests
# Core imports
from pprint import pprint
# Third party imports
import requests
class CryptoCompareAPI():
historical_day_url = 'https://min-api.cryptocompare.com/data/histoday'
historical_hour_url = 'https://min-api.cryptocompare.com/data/histohour'
historical_minute_url = 'https://min-api.cryptocompare.com/data/histominute'
def get_historical_day(self, pair_symbol, base_symbol,
exchange='BitTrex', aggregate=1, limit=None, all_data=False):
'''
Method for returning open, high, low, close,
volumefrom and volumeto daily historical data
** Parameters**
pair_symbol <str>: Currency pair, EG ETH
base_symbol <str>: Base currency, Eg BTC
exchange <str>: Target exchange, EG BitTrex
aggregate <int>: Aggregation multipler, Eg 1 = 1day, 2 = 2days
limit <int>: Limit the number of ohlcv records returned
all_data <bool>: Get all data
** Example call **
crypto_compare_api = CryptoCompareAPI()
data = crypto_compare_api.get_historical_day('ETH', 'BTC', aggregate=2, limit=1)
'''
url='{}?fsym={}&tsym={}&e={}&aggregate={}'.format(
self.historical_day_url, pair_symbol.upper(),
base_symbol.upper(), exchange, aggregate)
if all_data:
url += '&allData=true'
if limit:
url += '&limit={}'.format(limit)
r = requests.get(url)
data = r.json()['Data']
return data
def get_historical_hour(self, pair_symbol, base_symbol,
exchange='BitTrex', aggregate=1, limit=None, all_data=False):
'''
Method for returning open, high, low, close,
volumefrom and volumeto hourly historical data
** Parameters**
pair_symbol <str>: Currency pair, EG ETH
base_symbol <str>: Base currency, Eg BTC
exchange <str>: Target exchange, EG BitTrex
aggregate <int>: Aggregation multipler, Eg 1 = 1day, 2 = 2days
limit <int>: Limit the number of ohlcv records returned
all_data <bool>: Get all data
** Example call **
crypto_compare_api = CryptoCompareAPI()
data = crypto_compare_api.get_historical_hour('ETH', 'BTC', aggregate=2, limit=1)
'''
url='{}?fsym={}&tsym={}&e={}&aggregate={}'.format(
self.historical_hour_url, pair_symbol.upper(),
base_symbol.upper(), exchange, aggregate)
if all_data:
url += '&allData=true'
if limit:
url += '&limit={}'.format(limit)
r = requests.get(url)
data = r.json()['Data']
return data
def get_historical_minute(self, pair_symbol, base_symbol,
exchange='BitTrex', aggregate=1, limit=None, all_data=False):
'''
Method for returning open, high, low, close,
volumefrom and volumeto minute historical data
** Parameters**
pair_symbol <str>: Currency pair, EG ETH
base_symbol <str>: Base currency, Eg BTC
exchange <str>: Target exchange, EG BitTrex
aggregate <int>: Aggregation multipler, Eg 1 = 1day, 2 = 2days
limit <int>: Limit the number of ohlcv records returned
all_data <bool>: Get all data
** Example call **
crypto_compare_api = CryptoCompareAPI()
data = crypto_compare_api.get_historical_minute('ETH', 'BTC', aggregate=2, limi
'''
url='{}?fsym={}&tsym={}&e={}&aggregate={}'.format(
self.historical_minute_url, pair_symbol.upper(),
base_symbol.upper(), exchange, aggregate)
if all_data:
url += '&allData=true'
if limit:
url += '&limit={}'.format(limit)
r = requests.get(url)
data = r.json()['Data']
return data
if __name__ == '__main__':
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment