{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", " \n", "
\n", " \n", " BokehJS successfully loaded.\n", "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import pandas as pd\n", "from bokeh.charts import TimeSeries, show, output_file, vplot, output_notebook\n", "output_notebook()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "GSPC\n", " Date Open High Low Close Volume \\\n", "0 2016-05-25 2078.929932 2094.729980 2078.929932 2090.540039 3859160000 \n", "1 2016-05-24 2052.649902 2079.669922 2052.649902 2076.060059 3627340000 \n", "2 2016-05-23 2052.229980 2055.580078 2047.260010 2048.040039 3055480000 \n", "3 2016-05-20 2041.880005 2058.350098 2041.880005 2052.320068 3507650000 \n", "4 2016-05-19 2044.209961 2044.209961 2025.910034 2040.040039 3846770000 \n", "\n", " Adj Close Str Date \n", "0 2090.540039 2016-05-25 \n", "1 2076.060059 2016-05-24 \n", "2 2048.040039 2016-05-23 \n", "3 2052.320068 2016-05-20 \n", "4 2040.040039 2016-05-19 \n", "()\n", "FBIDX\n", " Date Open High Low Close Volume Adj Close Str Date\n", "0 2016-05-25 11.74 11.74 11.74 11.74 0 11.74 2016-05-25\n", "1 2016-05-24 11.75 11.75 11.75 11.75 0 11.75 2016-05-24\n", "2 2016-05-23 11.76 11.76 11.76 11.76 0 11.76 2016-05-23\n", "3 2016-05-20 11.76 11.76 11.76 11.76 0 11.76 2016-05-20\n", "4 2016-05-19 11.76 11.76 11.76 11.76 0 11.76 2016-05-19\n", "()\n", "NDX\n", " Date Open High Low Close Volume \\\n", "0 2016-05-25 4459.259766 4488.520020 4453.049805 4476.430176 1784420000 \n", "1 2016-05-24 4381.339844 4449.209961 4380.459961 4444.299805 1942280000 \n", "2 2016-05-23 4364.729980 4382.399902 4353.359863 4355.100098 1865110000 \n", "3 2016-05-20 4330.069824 4379.979980 4329.569824 4362.899902 1970520000 \n", "4 2016-05-19 4319.859863 4331.580078 4284.370117 4315.580078 1805890000 \n", "\n", " Adj Close Str Date \n", "0 4476.430176 2016-05-25 \n", "1 4444.299805 2016-05-24 \n", "2 4355.100098 2016-05-23 \n", "3 4362.899902 2016-05-20 \n", "4 4315.580078 2016-05-19 \n", "()\n", "SO\n", " Date Open High Low Close Volume Adj Close \\\n", "0 2016-05-25 48.419998 48.770000 48.189999 48.549999 3825300 48.549999 \n", "1 2016-05-24 48.310001 48.540001 48.049999 48.459999 4159200 48.459999 \n", "2 2016-05-23 48.759998 48.830002 48.070000 48.090000 3789000 48.090000 \n", "3 2016-05-20 48.820000 48.860001 48.380001 48.650002 4090000 48.650002 \n", "4 2016-05-19 47.990002 48.689999 47.619999 48.669998 4039400 48.669998 \n", "\n", " Str Date \n", "0 2016-05-25 \n", "1 2016-05-24 \n", "2 2016-05-23 \n", "3 2016-05-20 \n", "4 2016-05-19 \n" ] } ], "source": [ "# read in the S&P 500 Index Fund from the Yahoo Finance API\n", "GSPC = pd.read_csv(\"https://ichart.yahoo.com/table.csv?s=^GSPC\",\n", " parse_dates=['Date'])\n", "FBIDX = pd.read_csv(\"https://ichart.yahoo.com/table.csv?s=FBIDX\",\n", " parse_dates=['Date'])\n", "NDX = pd.read_csv(\"https://ichart.yahoo.com/table.csv?s=^NDX\",\n", " parse_dates=['Date'])\n", "SO = pd.read_csv(\"https://ichart.yahoo.com/table.csv?s=SO\", \n", " parse_dates=['Date'])\n", "\n", "# add column data for correct printing of dates in hovertool\n", "GSPC['Str Date'] = GSPC['Date'].map(lambda x: x.strftime('%Y-%m-%d'))\n", "FBIDX['Str Date'] = FBIDX['Date'].map(lambda x: x.strftime('%Y-%m-%d'))\n", "NDX['Str Date'] = NDX['Date'].map(lambda x: x.strftime('%Y-%m-%d'))\n", "SO['Str Date'] = SO['Date'].map(lambda x: x.strftime('%Y-%m-%d'))\n", "\n", "# show sample data\n", "print('GSPC')\n", "print(GSPC.head())\n", "print()\n", "print('FBIDX')\n", "print(FBIDX.head())\n", "print()\n", "print('NDX')\n", "print(NDX.head())\n", "print()\n", "print('SO')\n", "print(SO.head())" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n" ] } ], "source": [ "data = dict(\n", " Date = GSPC['Date'],\n", " GSPC = GSPC['Adj Close'],\n", " FBIDX = FBIDX['Adj Close'],\n", " NDX = NDX['Adj Close'],\n", " SO = SO['Adj Close'],\n", " SDate = GSPC['Str Date'],\n", ")\n", "print(type(data['Date'][0]))\n", "print(type(data['GSPC'][0]))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from bokeh.models import HoverTool\n", "\n", "hover = HoverTool(\n", " tooltips = [\n", " (\"Date\", \"@SDate\"),\n", " (\"Adj Close\", \" $y{$0.00}\"),\n", " ]\n", ")\n", "\n", "ts_stocks = TimeSeries(data,\n", " x = 'Date', y = ['GSPC', 'FBIDX', 'NDX', 'SO'],\n", " color = ['cadetblue', 'firebrick', 'limegreen', 'hotpink'], \n", " tools=['box_zoom', 'pan', 'save', hover, 'resize', 'reset', 'wheel_zoom'],\n", " title = 'Stock Time Series', \n", " ylabel = 'Stock Prices', legend=True\n", ")\n", "\n", "show(ts_stocks)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" } }, "nbformat": 4, "nbformat_minor": 0 }