{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Pftk Imports\n",
    "import pftk.pftkutil.qsdateutil as du\n",
    "import pftk.pftkutil.tsutil as tsu\n",
    "import pftk.pftkutil.data_access as da\n",
    "\n",
    "# Third Party Imports\n",
    "import datetime as dt\n",
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "\n",
    "# List of symbols\n",
    "#ls_symbols = [\"AGP\", \"CORP\", \"IJR\"]\n",
    "ls_symbols = [\"AAPL\", \"GLD\", \"IJR\", \"SPY\", \"XOM\"]\n",
    "\n",
    "\n",
    "# Start and End date of the charts\n",
    "dt_start = dt.datetime(2008, 1, 1)\n",
    "dt_end = dt.datetime(2017, 12, 31)\n",
    "\n",
    "# We need closing prices so the timestamp should be hours=16.\n",
    "dt_timeofday = dt.timedelta(hours=16)\n",
    "\n",
    "# Get a list of trading days between the start and the end.\n",
    "ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday)\n",
    "\n",
    "# Creating an object of the dataaccess class\n",
    "c_dataobj = da.DataAccess('EODHistoricalData')\n",
    "\n",
    "# Keys to be read from the data, it is good to read everything in one go.\n",
    "ls_keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close']\n",
    "\n",
    "# Reading the data, now d_data is a dictionary with the keys above.\n",
    "# Timestamps and symbols are the ones that were specified before.\n",
    "ldf_data = c_dataobj.get_data(ldt_timestamps, ls_symbols, ls_keys, verbose=False)\n",
    "d_data = dict(zip(ls_keys, ldf_data))\n",
    "\n",
    "# Filling the data for NAN\n",
    "for s_key in ls_keys:\n",
    "    d_data[s_key] = d_data[s_key].fillna(method='ffill')\n",
    "    d_data[s_key] = d_data[s_key].fillna(method='bfill')\n",
    "    d_data[s_key] = d_data[s_key].fillna(1.0)\n",
    "\n",
    "# Getting the numpy ndarray of close prices.\n",
    "na_price = d_data['close'].values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 46736 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Plotting the prices with x-axis=timestamps\n",
    "plt.clf()\n",
    "plt.plot(ldt_timestamps, na_price)\n",
    "plt.legend(ls_symbols)\n",
    "plt.ylabel('Adjusted Close')\n",
    "plt.xlabel('Date')\n",
    "plt.savefig('adjustedclose.pdf', format='pdf')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 36096 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Normalizing the prices to start at 1 and see relative returns\n",
    "na_normalized_price = na_price / na_price[0, :]\n",
    "\n",
    "# Plotting the prices with x-axis=timestamps\n",
    "plt.clf()\n",
    "plt.plot(ldt_timestamps, na_normalized_price)\n",
    "plt.legend(ls_symbols)\n",
    "plt.ylabel('Normalized Close')\n",
    "plt.xlabel('Date')\n",
    "plt.savefig('normalized.pdf', format='pdf')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 40136 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Copy the normalized prices to a new ndarry to find returns.\n",
    "na_rets = na_normalized_price.copy()\n",
    "\n",
    "# Calculate the daily returns of the prices. (Inplace calculation)\n",
    "# returnize0 works on ndarray and not dataframes.\n",
    "tsu.returnize0(na_rets)\n",
    "\n",
    "# Plotting the plot of daily returns\n",
    "plt.clf()\n",
    "plt.plot(ldt_timestamps[0:50], na_rets[0:50, 3])  # $SPX 50 days\n",
    "plt.plot(ldt_timestamps[0:50], na_rets[0:50, 4])  # XOM 50 days\n",
    "plt.axhline(y=0, color='r')\n",
    "plt.legend(['$SPX', 'XOM'])\n",
    "plt.ylabel('Daily Returns')\n",
    "plt.xlabel('Date')\n",
    "plt.savefig('rets.pdf', format='pdf')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 11656 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Plotting the scatter plot of daily returns between XOM VS $SPX\n",
    "plt.clf()\n",
    "plt.scatter(na_rets[:, 3], na_rets[:, 4], c='blue')\n",
    "plt.ylabel('XOM')\n",
    "plt.xlabel('$SPX')\n",
    "plt.savefig('scatterSPXvXOM.pdf', format='pdf')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 11856 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Plotting the scatter plot of daily returns between $SPX VS GLD\n",
    "plt.clf()\n",
    "plt.scatter(na_rets[:, 3], na_rets[:, 1], c='blue')  # $SPX v GLD\n",
    "plt.ylabel('GLD')\n",
    "plt.xlabel('$SPX')\n",
    "plt.savefig('scatterSPXvGLD.pdf', format='pdf')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
