In [2]:
URL = ('http://coinmarketcap.northpole.ro')
Load required components
In [22]:
import requests, pandas as pd
import matplotlib.pyplot as plt,matplotlib.dates as mdates
from matplotlib.ticker import FuncFormatter,FormatStrFormatter
import datetime
View valid choices. Print first 10 options.
In [29]:
def get_valid_coins():
r = requests.get(URL+'/coins.json')
return tuple(pd.DataFrame(r.json()['coins'])['identifier'])
In [30]:
get_valid_coins()[:10]
Out[30]:
Perform API calls. Will get data for Ethereum YTD.
In [23]:
def get_data(coin, period):
r = requests.get(URL+f'/history.json?coin={coin}&period={period}')
df = pd.DataFrame(r.json()['history'])
df = pd.concat([df,df['marketCap'].apply(pd.Series).add_prefix('marketCap_')],axis=1)
df = pd.concat([df,df['price'].apply(pd.Series).add_prefix('price_')],axis=1)
df = pd.concat([df,df['volume24'].apply(pd.Series).add_prefix('volume24_')],axis=1)
df['date'] = pd.to_datetime(df['date'])
df = df[df['date']<datetime.datetime.now()]
return df
In [24]:
df = get_data('ethereum',2018)
View data as a Pandas DataFrame
In [32]:
df.head()
Out[32]:
Plot data series
In [26]:
def plot(df):
%matplotlib inline
plt.style.use('seaborn-white')
x = sorted(df['date'])
y = df['price_usd']
z = df['volume24_usd']
def millions(x, pos):
'The two args are the value and tick position'
return '$%1.1fB' % (x*1e-9)
formatter = FuncFormatter(millions)
fig = plt.figure()
fig.set_size_inches(14, 6)
ax1 = fig.add_subplot(1,1,1)
plt.xticks(rotation=90)
ax1.yaxis.set_major_formatter(FormatStrFormatter('$%1.1f'))
ax1.xaxis.set_major_locator(mdates.DayLocator(interval=28))
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
ax1.plot(x,y,'r')
ax1.set_ylabel('Price')
ax1.tick_params('y')
ax2 = ax1.twinx()
ax2.plot(x,z,'b--')
ax2.set_ylabel('Volume')
ax2.yaxis.set_major_formatter(formatter)
lines, labels = ax1.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()
ax2.legend(lines + lines2, labels + labels2, loc=2)
In [27]:
plot(df)