Skip to content

Instantly share code, notes, and snippets.

@drbh
Created December 11, 2019 02:51
Show Gist options
  • Select an option

  • Save drbh/706cfafaefb52ef85bc2177420dab2aa to your computer and use it in GitHub Desktop.

Select an option

Save drbh/706cfafaefb52ef85bc2177420dab2aa to your computer and use it in GitHub Desktop.
Checking out the VIX performance by comparing weekly changes
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"source": [
"%matplotlib inline \n",
"import pandas as pd\n",
"import pandas_datareader.data as web\n",
"import datetime as dt\n",
"import numpy as np \n",
"from datetime import datetime, timedelta\n",
"from scipy import stats\n",
"\n\n",
"def pad_array(r):\n",
" if len(r) >= 5:\n",
" return r\n",
" return np.pad(r, (0, 5-len(r)), mode='constant', constant_values=np.nan)\n",
"\n",
"start = dt.datetime(2005, 1, 1)\n",
"end = dt.datetime.now()\n",
"\n",
"df = web.DataReader(\"TVIX\", 'yahoo', start, end)\n",
"\n",
"grouped = df.groupby(pd.Grouper(level='Date',freq='W-SUN'))\n",
"\n",
"rows = []\n",
"indices = []\n",
"for index, gr in grouped:\n",
" first_price_that_week = gr[\"Open\"].iloc[0]\n",
" \n",
" change_compared_to_week_start = (gr[\"Close\"] / first_price_that_week) - 1\n",
" numerical_change = gr[\"Close\"] - first_price_that_week\n",
" features = change_compared_to_week_start.values\n",
" \n",
" index = gr.index[0]\n",
" rows.append(pad_array(features))\n",
" \n",
" indices.append(index)\n",
"\n",
"fd = pd.DataFrame(rows)\n",
"fd.index = indices\n",
"\n\n",
"todays_days_since_monday = datetime.today().weekday()\n",
"t = fd.tail(1)[todays_days_since_monday].values[0]\n",
"vals = fd[todays_days_since_monday].values\n",
"\n",
"x = vals[~np.isnan(vals)]\n",
"ans = stats.percentileofscore(x, t)\n",
"\n\n",
"todays_price = df.tail(1)[\"Close\"].values[0]\n",
"week_start_date = fd.tail(1).index\n",
"weeks_open_price = df.loc[week_start_date][\"Open\"].values[0]\n",
"\n",
"print(\n",
" \"Todays close of\", round(todays_price, 3), \"is a change of\", \n",
" str(round(t, 3)), \"from the weeks open of\", str(round(weeks_open_price, 3)),\"which is\", \n",
" str(round(ans, 3))+\"%\", \"above all of the other rates of change on the\",\n",
" todays_days_since_monday, \"th day of the week. Starting on\", str(week_start_date.date[0]),\n",
" \"this is based on\", str(len(x)), \"samples\"\n",
")\n"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Todays close of 70.13 is a change of 0.094 from the weeks open of 64.12 which is 89.831% above all of the other rates of change on the 1 th day of the week. Starting on 2019-12-09 this is based on 472 samples\n"
]
}
],
"execution_count": 1,
"metadata": {
"collapsed": false,
"outputHidden": false,
"inputHidden": false
}
},
{
"cell_type": "code",
"source": [
"# df.tail(4)"
],
"outputs": [],
"execution_count": 2,
"metadata": {
"collapsed": false,
"outputHidden": false,
"inputHidden": false
}
},
{
"cell_type": "code",
"source": [
"# fd.tail(4)"
],
"outputs": [],
"execution_count": 3,
"metadata": {
"collapsed": false,
"outputHidden": false,
"inputHidden": false
}
}
],
"metadata": {
"kernel_info": {
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.7.3",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"kernelspec": {
"name": "python3",
"language": "python",
"display_name": "Python 3"
},
"nteract": {
"version": "0.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment