{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import hvplot.pandas  # noqa"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`ohlc` is a useful chart type to visualize stock movements"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bokeh.sampledata import stocks\n",
    "\n",
    "df = pd.DataFrame(stocks.AAPL)\n",
    "df['date'] = pd.to_datetime(df.date)\n",
    "\n",
    "df.iloc[-50:].hvplot.ohlc(grid=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can control the `neg_color`, `pos_color`, `line_color` and `bar_width`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.iloc[-50:].hvplot.ohlc(neg_color='indianred', pos_color='chartreuse', line_color='gray', bar_width=0.9, grid=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "By default `ohlc` will assume the `index` OR the first datetime column should be mapped to the x-axis and the first four non-datetime columns correspond to the O (open), H (high), L (low) and C (close) components. The default call is therefore equivalent to:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.iloc[-50:].hvplot.ohlc('date', ['open', 'low', 'high', 'close'], grid=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using the HoloViews `RangeToolLink` we can make it easy to scroll through the data while still seeing an overview of the overall timeseries:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from holoviews.plotting.links import RangeToolLink\n",
    "\n",
    "df_2012 = df[df.date > pd.to_datetime('2012')]\n",
    "\n",
    "ohlc = df_2012.hvplot.ohlc(ylabel='Price ($)', grid=True, xaxis=None)\n",
    "overview = df_2012.hvplot.ohlc(yaxis=None, height=150, fields={'date': 'Date'})\n",
    "volume = df_2012.hvplot.step('date', 'volume', height=100, xaxis=None)\n",
    "\n",
    "RangeToolLink(overview.get(0), ohlc.get(0))\n",
    "\n",
    "layout = (volume + ohlc + overview).cols(1)\n",
    "\n",
    "layout.opts(merge_tools=False)"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
