-
-
Save simecek/c9cc0b8f336ad5ad1f987492391b4da2 to your computer and use it in GitHub Desktop.
S&P500 diff.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "S&P500 diff.ipynb", | |
"provenance": [], | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/simecek/c9cc0b8f336ad5ad1f987492391b4da2/s-p500-diff.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "dGCJqRo98yzS", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"import pandas as pd\n", | |
"from pandas_datareader import data as wb" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "WV20ysnkBx_H", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"## Getting data" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "SFcfhJUQ8z4d", | |
"colab_type": "code", | |
"outputId": "471fd148-be53-4076-cb36-93d454519283", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 277 | |
} | |
}, | |
"source": [ | |
"# ^GSPC is code for S&P500, see https://finance.yahoo.com/quote/%5EGSPC/\n", | |
"ticker_name = '^GSPC'\n", | |
"\n", | |
"ticker = wb.DataReader(ticker_name, start='2010-1-1', data_source='yahoo')\n", | |
"print(ticker)" | |
], | |
"execution_count": 2, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
" High Low ... Volume Adj Close\n", | |
"Date ... \n", | |
"2010-01-04 1133.869995 1116.560059 ... 3991400000 1132.989990\n", | |
"2010-01-05 1136.630005 1129.660034 ... 2491020000 1136.520020\n", | |
"2010-01-06 1139.189941 1133.949951 ... 4972660000 1137.140015\n", | |
"2010-01-07 1142.459961 1131.319946 ... 5270680000 1141.689941\n", | |
"2010-01-08 1145.390015 1136.219971 ... 4389590000 1144.979980\n", | |
"... ... ... ... ... ...\n", | |
"2019-11-05 3083.949951 3072.149902 ... 4486130000 3074.620117\n", | |
"2019-11-06 3078.340088 3065.889893 ... 4458190000 3076.780029\n", | |
"2019-11-07 3097.770020 3080.229980 ... 4144640000 3085.179932\n", | |
"2019-11-08 3093.090088 3073.580078 ... 3499150000 3093.080078\n", | |
"2019-11-11 3088.330078 3075.820068 ... 1407760565 3087.010010\n", | |
"\n", | |
"[2482 rows x 6 columns]\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "I_QRaLFSArow", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"## Calculating the differences" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "_CqdfDBL83FP", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"# `Adj Close` -> daily difference\n", | |
"ticker['Close_diff'] = ticker['Adj Close'].diff()\n", | |
"ticker['Close_diff'].head()\n", | |
"\n", | |
"starting_value = ticker['Adj Close'][0]" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "aQ3PTTlGAyRE", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"## Recreating the original column" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "3bMtzHBv84Wf", | |
"colab_type": "code", | |
"outputId": "31be902b-3395-489e-e386-c2751611aa96", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 243 | |
} | |
}, | |
"source": [ | |
"# daily difference -> `Adj Close`\n", | |
"ticker['Close_diff'].cumsum().fillna(0) + starting_value\n" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"Date\n", | |
"2010-01-04 1132.989990\n", | |
"2010-01-05 1136.520020\n", | |
"2010-01-06 1137.140015\n", | |
"2010-01-07 1141.689941\n", | |
"2010-01-08 1144.979980\n", | |
" ... \n", | |
"2019-11-05 3074.620117\n", | |
"2019-11-06 3076.780029\n", | |
"2019-11-07 3085.179932\n", | |
"2019-11-08 3093.080078\n", | |
"2019-11-11 3087.010010\n", | |
"Name: Close_diff, Length: 2482, dtype: float64" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 4 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "jX_kyamD87wz", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment