{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from pandas_datareader import data\n",
"import pandas as pd\n",
"%matplotlib inline\n",
"\n",
"\n",
"# Define the instruments to download. We would like to see Apple, Microsoft and the S&P500 index.\n",
"# tickers = ['W', 'HD', 'AMZN','LOW','LL']\n",
"tickers = ['ROIC', 'SKT', 'TCO' ,'SPG' ,'MAC']\n",
"# Define which online source one should use\n",
"data_source = 'yahoo'\n",
"\n",
"# We would like all available data from 01/01/2000 until 12/31/2016.\n",
"start_date = '2016-12-01'\n",
"end_date = '2017-12-31'\n",
"\n",
"# User pandas_reader.data.DataReader to load the desired data. As simple as that.\n",
"panel_data = data.DataReader(tickers, data_source, start_date, end_date)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"
\n",
" \n",
" \n",
" | \n",
" MAC | \n",
" ROIC | \n",
" SKT | \n",
" SPG | \n",
" TCO | \n",
"
\n",
" \n",
" \n",
" \n",
" 2016-12-01 | \n",
" 67.410004 | \n",
" 20.320000 | \n",
" 34.299999 | \n",
" 176.820007 | \n",
" 71.510002 | \n",
"
\n",
" \n",
" 2016-12-02 | \n",
" 68.129997 | \n",
" 20.620001 | \n",
" 34.650002 | \n",
" 179.360001 | \n",
" 72.080002 | \n",
"
\n",
" \n",
" 2016-12-05 | \n",
" 68.800003 | \n",
" 20.730000 | \n",
" 34.970001 | \n",
" 180.759995 | \n",
" 72.610001 | \n",
"
\n",
" \n",
" 2016-12-06 | \n",
" 69.050003 | \n",
" 20.799999 | \n",
" 35.270000 | \n",
" 180.110001 | \n",
" 73.489998 | \n",
"
\n",
" \n",
" 2016-12-07 | \n",
" 70.720001 | \n",
" 21.200001 | \n",
" 35.900002 | \n",
" 183.750000 | \n",
" 74.500000 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" MAC ROIC SKT SPG TCO\n",
"2016-12-01 67.410004 20.320000 34.299999 176.820007 71.510002\n",
"2016-12-02 68.129997 20.620001 34.650002 179.360001 72.080002\n",
"2016-12-05 68.800003 20.730000 34.970001 180.759995 72.610001\n",
"2016-12-06 69.050003 20.799999 35.270000 180.110001 73.489998\n",
"2016-12-07 70.720001 21.200001 35.900002 183.750000 74.500000"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Getting just the adjusted closing prices. This will return a Pandas DataFrame\n",
"# The index in this DataFrame is the major index of the panel_data.\n",
"close = panel_data.ix['Close']\n",
"\n",
"# Getting all weekdays between 01/01/2000 and 12/31/2016\n",
"all_weekdays = pd.date_range(start=start_date, end=end_date, freq='B')\n",
"\n",
"# How do we align the existing prices in adj_close with our new set of dates?\n",
"# All we need to do is reindex close using all_weekdays as the new index\n",
"close = close.reindex(all_weekdays)\n",
"\n",
"close.head()\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" MAC | \n",
" ROIC | \n",
" SKT | \n",
" SPG | \n",
" TCO | \n",
"
\n",
" \n",
" \n",
" \n",
" count | \n",
" 282.000000 | \n",
" 282.000000 | \n",
" 282.000000 | \n",
" 282.000000 | \n",
" 282.000000 | \n",
"
\n",
" \n",
" mean | \n",
" 60.359130 | \n",
" 19.702806 | \n",
" 27.499412 | \n",
" 163.018773 | \n",
" 59.680361 | \n",
"
\n",
" \n",
" std | \n",
" 4.765473 | \n",
" 0.695494 | \n",
" 3.795979 | \n",
" 7.915956 | \n",
" 7.001413 | \n",
"
\n",
" \n",
" min | \n",
" 52.056969 | \n",
" 17.806831 | \n",
" 21.890348 | \n",
" 148.815491 | \n",
" 45.844936 | \n",
"
\n",
" \n",
" 25% | \n",
" 56.158371 | \n",
" 19.218705 | \n",
" 24.376531 | \n",
" 156.912205 | \n",
" 54.580725 | \n",
"
\n",
" \n",
" 50% | \n",
" 60.819279 | \n",
" 19.729540 | \n",
" 25.674796 | \n",
" 161.862656 | \n",
" 59.257576 | \n",
"
\n",
" \n",
" 75% | \n",
" 64.790953 | \n",
" 20.204208 | \n",
" 31.491758 | \n",
" 168.811760 | \n",
" 64.934494 | \n",
"
\n",
" \n",
" max | \n",
" 69.344276 | \n",
" 21.429085 | \n",
" 34.961044 | \n",
" 180.091019 | \n",
" 73.055801 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" MAC ROIC SKT SPG TCO\n",
"count 282.000000 282.000000 282.000000 282.000000 282.000000\n",
"mean 60.359130 19.702806 27.499412 163.018773 59.680361\n",
"std 4.765473 0.695494 3.795979 7.915956 7.001413\n",
"min 52.056969 17.806831 21.890348 148.815491 45.844936\n",
"25% 56.158371 19.218705 24.376531 156.912205 54.580725\n",
"50% 60.819279 19.729540 25.674796 161.862656 59.257576\n",
"75% 64.790953 20.204208 31.491758 168.811760 64.934494\n",
"max 69.344276 21.429085 34.961044 180.091019 73.055801"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Getting just the adjusted closing prices. This will return a Pandas DataFrame\n",
"# The index in this DataFrame is the major index of the panel_data.\n",
"adj_close = panel_data.ix['Adj Close']\n",
"\n",
"# Getting all weekdays between 01/01/2000 and 12/31/2016\n",
"all_weekdays = pd.date_range(start=start_date, end=end_date, freq='B')\n",
"\n",
"# How do we align the existing prices in adj_close with our new set of dates?\n",
"# All we need to do is reindex adj_close using all_weekdays as the new index\n",
"adj_close = adj_close.reindex(all_weekdays)\n",
"\n",
"# Reindexing will insert missing values (NaN) for the dates that were not present\n",
"# in the original set. To cope with this, we can fill the missing by replacing them\n",
"# with the latest available price for each instrument.\n",
"adj_close = adj_close.fillna(method='ffill')\n",
"adj_close.describe()"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'df' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Define your own bins\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mmybins\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mMAC\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mMAC\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;31m# Cut the data with the help of the bins\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mdf\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'MAC_bucket'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpd\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcut\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mMAC\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbins\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mmybins\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mNameError\u001b[0m: name 'df' is not defined"
]
}
],
"source": [
"# Define your own bins\n",
"mybins = range(df.MAC.min(), df.MAC.max(), 10)\n",
"\n",
"# Cut the data with the help of the bins\n",
"df['MAC_bucket'] = pd.cut(df.MAC, bins=mybins)\n",
"\n",
"# Count the number of values per bucket\n",
"df['MAC_bucket'].value_counts()"
]
}
],
"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.0"
},
"nikola": {
"category": "",
"date": "2018-02-02 00:00:32 UTC-05:00",
"description": "",
"link": "",
"slug": "simple-exercise-analysing-stocks-using-ipython",
"tags": "",
"title": "Simple exercise analysing stocks using IPython",
"type": "text"
}
},
"nbformat": 4,
"nbformat_minor": 2
}