{ "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
MACROICSKTSPGTCO
2016-12-0167.41000420.32000034.299999176.82000771.510002
2016-12-0268.12999720.62000134.650002179.36000172.080002
2016-12-0568.80000320.73000034.970001180.75999572.610001
2016-12-0669.05000320.79999935.270000180.11000173.489998
2016-12-0770.72000121.20000135.900002183.75000074.500000
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
MACROICSKTSPGTCO
count282.000000282.000000282.000000282.000000282.000000
mean60.35913019.70280627.499412163.01877359.680361
std4.7654730.6954943.7959797.9159567.001413
min52.05696917.80683121.890348148.81549145.844936
25%56.15837119.21870524.376531156.91220554.580725
50%60.81927919.72954025.674796161.86265659.257576
75%64.79095320.20420831.491758168.81176064.934494
max69.34427621.42908534.961044180.09101973.055801
\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 }