{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "URL: https://docs.bokeh.org/en/2.4.1/docs/gallery/stocks.html\n",
    "\n",
    "Most examples work across multiple plotting backends, this example is also available for:\n",
    "\n",
    "* [Matplotlib - stocks example](../matplotlib/stocks_example.ipynb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import holoviews as hv\n",
    "from holoviews import opts\n",
    "hv.extension('bokeh')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Defining the data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from holoviews.operation.timeseries import rolling\n",
    "from bokeh.sampledata.stocks import AAPL, GOOG, IBM, MSFT\n",
    "\n",
    "color_cycle = hv.Cycle(values=['#A6CEE3', '#B2DF8A','#33A02C', '#FB9A99'])\n",
    "\n",
    "def get_curve(data, label=''):\n",
    "    df = pd.DataFrame(data)\n",
    "    df['date'] = df.date.astype('datetime64[ns]')\n",
    "    return hv.Curve(df, ('date', 'Date'), ('adj_close', 'Price'), label=label)\n",
    "\n",
    "hv.Dimension.type_formatters[np.datetime64] = '%Y'\n",
    "\n",
    "aapl = get_curve(AAPL, label='AAPL')\n",
    "goog = get_curve(GOOG, label='GOOG')\n",
    "ibm  = get_curve(IBM, label='IBM')\n",
    "msft = get_curve(MSFT, label='MSFT')\n",
    "\n",
    "avg_curve = rolling(aapl, rolling_window=30).relabel('Average')\n",
    "avg_scatter = hv.Scatter((np.array(AAPL['date'], dtype=np.datetime64), np.array(AAPL['adj_close'])), \n",
    "                         ('date', 'Date'), ('adj_close', 'Price'), label='close')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "((aapl * goog * ibm * msft) + (avg_scatter * avg_curve)).opts(\n",
    "    opts.Curve(color=color_cycle),\n",
    "    opts.Curve('Curve.Average', color='navy'),\n",
    "    opts.Scatter(alpha=0.2, size=4, color='darkgrey'),\n",
    "    opts.Overlay(width=400, height=400, legend_position='top_left'))"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
