daily coinbase close -> rolling 30 day correlation data
import cbpro
import datetime
import pandas as pd
import seaborn as sns
import numpy as np
import matplotlib as plt
public_client = cbpro.PublicClient()
products = public_client.get_products()
pairs = [product['id'] for product in products]
####==================== create dataset of daily close
dataList = []
for pair in pairs:
data = public_client.get_product_historic_rates(
pair,
granularity=86400,
end=datetime.datetime.now().isoformat())
data = pd.DataFrame(data, columns = ['unix', 'low', 'high', 'open', 'close', 'volume'])
data['date'] = pd.to_datetime(data['unix'], unit='s')
data = data[['date', 'close']]
data.set_index('date', inplace=True)
data.rename({'close':pair}, axis=1, inplace=True)
dataList.append(data)
dataset=pd.concat(dataList, axis=1, copy=False)
dataset.plot(legend=False)
dataset=dataset.query('date>"2021-04"').dropna(axis=1)
dataset.plot(legend=False)
# dataset.to_csv('coinbase_daily_close.csv')
<AxesSubplot:xlabel='date'>
###==================== basic corr of pct change
datasetPctChange=dataset.pct_change()
datasetPctChange.plot(legend=False)
corr = datasetPctChange.corr()
sns.heatmap(corr)
<AxesSubplot:>
#####======================= long rolling corr dataset
tempList = []
dates = datasetPctChange.index
n = 30
# for date in dates[n:-n]:
for i, date in enumerate(dates):
if i > 30:
# 30 day lag
date = date - datetime.timedelta(days=n)
temp = datasetPctChange.iloc[i-30:i,:]
corr = temp.corr()
corrLong=pd.DataFrame(np.tril(corr), columns=corr.columns, index=corr.index).melt(ignore_index=False).query('value not in [0,1]')
corrLong['lag'] = date
tempList.append(corrLong)
corrAgg = pd.concat(tempList)
corrAgg.to_csv('coinbase_daily_close_rolling_30_corr_long.csv')
### ================================== wide rolling dataset
rollingCorr = (corrAgg
.reset_index()
.pivot_table(columns=['index', 'variable'], values='value', index='lag')
)
# rollingCorr.to_csv('coinbase_daily_close_rolling_30_corr_wide.csv')
rollingCorr.plot(legend=False)
rollingCorr.iloc[:,1:10].plot(legend=False)
<AxesSubplot:xlabel='lag'>
### ======================== individual coin's corr plot
plt.rcParams.update({'figure.max_open_warning': 0})
products = public_client.get_products()
pairs = [product['id'] for product in products]
coins = [pair.split('-') for pair in pairs]
allcoins = []
for coin in coins:
allcoins.append(coin[0])
allcoins.append(coin[1])
allcoins = pd.Series(allcoins).drop_duplicates()
for coin in allcoins:
try:
# get coin-usd and *-usd
coins = [col for col in rollingCorr.columns if col[0] == f"{coin}-USD" and col[1].split('-')[1] == 'USD']
rollingCorr[coins].plot(legend=False, title=coin)
except:
pass
# print(coin)