{
 "metadata": {
  "name": "",
  "signature": "sha256:58df9db2e7df5f8fc95b8e0c4be966f02b4670c528f367f99bbd802555fb4af7"
 },
 "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": [
      "figure(x_axis_type=\"datetime\", width=1000, name=\"candlestick\",\n",
      "       tools=\"pan,wheel_zoom,box_zoom,reset,previewsave\")\n",
      "\n",
      "hold()\n",
      "\n",
      "segment(df.date, df.high, df.date, df.low, color='black')\n",
      "rect(df.date[inc], mids[inc], w, spans[inc], fill_color=\"#D5E1DD\", line_color=\"black\")\n",
      "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": [
      "curplot().title = \"MSFT Candlestick\"\n",
      "xaxis().major_label_orientation = pi/4\n",
      "grid().grid_line_alpha=0.3"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# open a browser\n",
      "show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}