Skip to content

Instantly share code, notes, and snippets.

@piersstorey
Created January 16, 2018 07:49
Show Gist options
  • Save piersstorey/1ca37ff914dc9aa71cd274025b774898 to your computer and use it in GitHub Desktop.
Save piersstorey/1ca37ff914dc9aa71cd274025b774898 to your computer and use it in GitHub Desktop.
Jupyter OHLCV aggregator
# coding: utf-8
# In[1]:
import datetime
import numpy as np
import pandas as pd
import requests
import talib
import matplotlib.pyplot as plt
# # Cryptocompare API Class
# **Simple class to return Cryptocompare market historical data sets**
#
# In[2]:
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
# # Get historical hour and minute data
#
# In[3]:
# Instantiate CryptoCompareAPI and return historical hour period
crypto_compare_api = CryptoCompareAPI()
hour_data = crypto_compare_api.get_historical_hour('ETH', 'BTC')
minute_data = crypto_compare_api.get_historical_minute('ETH', 'BTC')
# In[4]:
def return_ohclv_data_frame(data_set):
'''
Return a clean ohclv DataFrame
'''
df = pd.DataFrame(data_set) # Create dataFrame
df['timestamp'] = [datetime.datetime.fromtimestamp(d) for d in df.time] # create timestamp
# Return OHLCV specific columns
df = df[['close', 'high', 'low', 'open', 'volumefrom', 'timestamp']].rename(columns={"volumefrom": "volume"})
df = df.set_index('timestamp') #Set timestamp as index
return df
# # Resample dataframe to show 2, 4, 6 hour periods
#
# In[5]:
df_hour = return_ohclv_data_frame(hour_data)
df_2hour = df_hour.resample('2H').mean() # Two hour period
df_4hour = df_hour.resample('4H').mean() # Four hour period
df_6hour = df_hour.resample('6H').mean() # Six hour period
# In[6]:
df_6hour.head()
# # Resample dataframe to show 5, 15, 30 minute periods
# In[7]:
df_min = return_ohclv_data_frame(minute_data)
df_5min = df_min.resample('5T').mean() # Five minute period
df_15min = df_min.resample('15T').mean() # Fifteen minute period
df_30min = df_min.resample('30T').mean() # Thirty minute period
# In[8]:
df_5min.head()
# In[9]:
plt.plot(df_5min.index, df_5min.close)
# In[13]:
df_30min['close'].plot()
df_15min['close'].plot()
plt.gcf().autofmt_xdate()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment