{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from math import pi\n",
    "import pandas as pd\n",
    "\n",
    "from bokeh.sampledata.stocks import MSFT\n",
    "from bokeh.plotting import figure, show, output_notebook"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df = pd.DataFrame(MSFT)[:50]\n",
    "df['date'] = pd.to_datetime(df['date'])\n",
    "\n",
    "mids = (df.open + df.close)/2\n",
    "spans = abs(df.close-df.open)\n",
    "\n",
    "inc = df.close > df.open\n",
    "dec = df.open > df.close\n",
    "w = 12*60*60*1000 # half day in ms"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "output_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "p = figure(x_axis_type=\"datetime\", plot_width=1000)\n",
    "\n",
    "p.segment(df.date, df.high, df.date, df.low, color='black')\n",
    "p.rect(df.date[inc], mids[inc], w, spans[inc], fill_color=\"#D5E1DD\", line_color=\"black\")\n",
    "p.rect(df.date[dec], mids[dec], w, spans[dec], fill_color=\"#F2583E\", line_color=\"black\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "p.title = \"MSFT Candlestick\"\n",
    "p.xaxis.major_label_orientation = pi/4\n",
    "p.grid.grid_line_alpha=0.3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "show(p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "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.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
