{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import pandas_datareader.data as web\n", "import datetime\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "start = datetime.datetime(2012, 1, 1)\n", "end = datetime.datetime(2017, 12, 31)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "f = web.DataReader('SNE', 'morningstar', start, end)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Close High Low Open Volume\n", "Symbol Date \n", "SNE 2012-01-02 18.04 18.04 18.04 18.04 0\n", " 2012-01-03 18.38 18.50 18.28 18.28 1414748\n", " 2012-01-04 18.22 18.27 18.14 18.24 1146367\n", " 2012-01-05 17.70 17.85 17.60 17.83 1464843\n", " 2012-01-06 17.44 17.57 17.37 17.57 594057\n" ] } ], "source": [ "print(f.head())" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "f2 = web.DataReader(['SNE', 'AAPL'], 'morningstar', start, end)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", " Close High Low Open Volume\n", "Symbol Date \n", "SNE 2012-01-02 18.04 18.04 18.04 18.04 0\n", " 2012-01-03 18.38 18.50 18.28 18.28 1414748\n", " 2012-01-04 18.22 18.27 18.14 18.24 1146367\n", " 2012-01-05 17.70 17.85 17.60 17.83 1464843\n", " 2012-01-06 17.44 17.57 17.37 17.57 594057\n", " Close High Low Open Volume\n", "Symbol Date \n", "AAPL 2017-12-25 175.01 175.01 175.010 175.01 0\n", " 2017-12-26 170.57 171.47 169.679 170.80 33185536\n", " 2017-12-27 170.60 170.78 169.710 170.10 21498213\n", " 2017-12-28 171.08 171.85 170.480 171.00 16480187\n", " 2017-12-29 169.23 170.59 169.220 170.52 25999922\n" ] } ], "source": [ "print(type(f2.index))\n", "print(f2.head())\n", "print(f2.tail())" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Close High Low Open \\\n", "Symbol AAPL SNE AAPL SNE AAPL SNE AAPL SNE \n", "Date \n", "2012-01-02 57.8571 18.04 57.8571 18.04 57.8571 18.04 57.8571 18.04 \n", "2012-01-03 58.7471 18.38 58.9286 18.50 58.4286 18.28 58.5000 18.28 \n", "2012-01-04 59.0629 18.22 59.2400 18.27 58.4686 18.14 58.6000 18.24 \n", "2012-01-05 59.7186 17.70 59.7929 17.85 58.9529 17.60 59.2786 17.83 \n", "2012-01-06 60.3429 17.44 60.3929 17.57 59.8886 17.37 59.9671 17.57 \n", "\n", " Volume \n", "Symbol AAPL SNE \n", "Date \n", "2012-01-02 0 0 \n", "2012-01-03 75564699 1414748 \n", "2012-01-04 65061108 1146367 \n", "2012-01-05 67816805 1464843 \n", "2012-01-06 79596412 594057 \n" ] } ], "source": [ "f2_u = f2.unstack(0)\n", "print(f2_u.head())" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Symbol AAPL SNE\n", "Date \n", "2012-01-02 57.8571 18.04\n", "2012-01-03 58.7471 18.38\n", "2012-01-04 59.0629 18.22\n", "2012-01-05 59.7186 17.70\n", "2012-01-06 60.3429 17.44\n" ] } ], "source": [ "print(f2_u['Close'].head())" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "f2_u['Close'].plot(title='SNE vs AAPL', grid=True)\n", "# plt.show()\n", "plt.savefig('data/dst/pandas_datareader_morningstar.png')" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "f2_u['Close', 'AAPL'] /= f2_u['Close'].loc[f2_u.index[0], 'AAPL']\n", "f2_u['Close', 'SNE'] /= f2_u['Close'].loc[f2_u.index[0], 'SNE']" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "f2_u['Close'].plot(title='SNE vs AAPL', grid=True)\n", "# plt.show()\n", "plt.savefig('data/dst/pandas_datareader_morningstar_normalize.png')" ] } ], "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.4" } }, "nbformat": 4, "nbformat_minor": 2 }