{
 "metadata": {
  "name": "",
  "signature": "sha256:0f527ca4bd8a7da6c2a0da7a7292512219fdfebd8834db74bb48f9484e38522e"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from math import pi\n",
      "import pandas as pd\n",
      "\n",
      "from bokeh.sampledata.stocks import MSFT\n",
      "from bokeh.plotting import *"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "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"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "output_notebook()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "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\")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "p.title = \"MSFT Candlestick\"\n",
      "p.xaxis.major_label_orientation = pi/4\n",
      "p.grid.grid_line_alpha=0.3"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "show(p)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}