Skip to content

Instantly share code, notes, and snippets.

@anthonytxie
Created June 25, 2018 06:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anthonytxie/e4e612f1a4c9f663928f74aa69173fd1 to your computer and use it in GitHub Desktop.
Save anthonytxie/e4e612f1a4c9f663928f74aa69173fd1 to your computer and use it in GitHub Desktop.
Rolling 30 Day Correlations between BTC & BCH
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py:2698: DtypeWarning: Columns (5) have mixed types. Specify dtype option on import or set low_memory=False.\n",
" interactivity=interactivity, compiler=compiler, result=result)\n"
]
}
],
"source": [
"import pandas as pd \n",
"import matplotlib.pyplot as plt\n",
"import pylab as pl\n",
"import seaborn as sns\n",
"% matplotlib inline\n",
"\n",
"# Plot styles\n",
"plt.style.use('seaborn-poster')\n",
"plt.style.use('fivethirtyeight')\n",
"plt.rcParams['axes.edgecolor'] = '#ffffff'\n",
"plt.rcParams['axes.facecolor'] = '#ffffff'\n",
"plt.rcParams['figure.facecolor'] = '#ffffff'\n",
"plt.rcParams['patch.edgecolor'] = '#ffffff'\n",
"plt.rcParams['patch.facecolor'] = '#ffffff'\n",
"plt.rcParams['savefig.edgecolor'] = '#ffffff'\n",
"plt.rcParams['savefig.facecolor'] = '#ffffff'\n",
"plt.rcParams['xtick.labelsize'] = 16\n",
"plt.rcParams['ytick.labelsize'] = 16\n",
"\n",
"\n",
"\n",
"# Read in historical data for every coin on CMC. CSV is generated by cryptomarketcap-historical-prices scraper\n",
"df = pd.read_csv('/Users/axie/Desktop/coindata.csv')\n",
"df['Close'] = pd.to_numeric(df['Close'], errors='coerce')\n",
"df['Market Cap'] = pd.to_numeric(df['Market Cap'], errors='coerce')\n",
"df['Date'] = pd.to_datetime(df['Date'])\n",
"\n",
"#Pivot the table and take only rows after June 22, 2016\n",
"df_pivot = df[df['Date'] > '2016-06-22'].pivot(index='Date', columns='Coin', values='Market Cap').sort_index()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def calc_rolling_corr(df, days, fork_coin, main_coin):\n",
" return df.rolling(window=days).corr().xs(fork_coin, level=1, drop_level=False)[main_coin].dropna().reset_index().drop(['Coin'], axis=1)\n",
"\n",
"\n",
"bcash_bitcoin_rolling_corr = calc_rolling_corr(df_pivot, 30, 'bitcoin-cash', 'bitcoin')\n",
"bcash_bitcoin_rolling_corr.columns = ['Date', 'Correlation Coefficient']\n",
"bcash_bitcoin_rolling_corr.plot(x='Date', y='Correlation Coefficient', title='BCash Correlation with Bitcoin on a Rolling 30 Day Window')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment