In order to import our data, we need to use two important libraries called requests and pandas. The requests library will allow us to pull data from the web and pandas will process it.
import requests
import pandas as pd
def get_historic_price(symbol, after='2018-09-01'):
url = '<https://api.kraken.com/0/public/OHLC>'
pair = f"{symbol.upper()}USD" # XBTUSD when symbol='xbt' for example
resp = requests.get(url, params={
"pair": pair,
'interval': 60,
'since': str(int(pd.Timestamp(after).timestamp()))
})
resp.raise_for_status()
data = resp.json()
results_key = [k for k in data['result'].keys() if k != 'last'][0]
results = [
(close_time, float(open), float(high), float(low), float(close), float(volume))
for (close_time, open, high, low, close, vwap, volume, count)
in data['result'][results_key]
]
df = pd.DataFrame(results, columns=[
'CloseTime', 'OpenPrice', 'HighPrice', 'LowPrice', 'ClosePrice', 'Volume'
])
df['CloseTime'] = pd.to_datetime(df['CloseTime'], unit='s')
df.set_index('CloseTime', inplace=True)
return df
This python function simply takes the URL of kraken which is an API and allows us to import this data. It specifies the results which are the closing time, the open, the high, the low , the close, and lastly the volume. this code also creates the columns needed for it.
This will allow us to create a time stamp for this data
last_week = (pd.timestamp.now() - pd.offsets.day(7))
last_week
Following this, we can use the get_historic_price
function we created to pull the actual data from kraken
btc = get_historic_price('btc', after = last_week)
eth = get_historic_price('eth', after = last_week)
btc.head()
btc.info()
Bokeh is a library which allows us to plot interactive graphs that can be manipulated within a browser. The imports for this are the following :
import bokeh.plotting import figure, output_file, show
output_notebook()
We can plot the BTC price action chart through the following code :
p1 = figure(x_axis_type = "datetime", title = "crypto prices", width = 800)
p1.grid.grid_line_alpha = 0.3
p1.xaxis.axis_label = "Date"
p1.yaxis.axis_label = "Price"
p1.line(btc.index,btc['ClosePrice', color = "yellow",legend_label = "Bitcoin")
p1.legend.location = "bottom_left"
show(p1)