Show Sidebar Hide Sidebar

OHLC Charts in Pandas

How to make interactive OHLC charts in Python with Plotly. Six examples of OHLC charts with Pandas, time series, and yahoo finance data.

OHLC Charts in Python

In [1]:
import plotly
plotly.__version__
Out[1]:
'1.8.8'
Simple OHLC Chart with Pandas
In [2]:
import plotly.plotly as py
from plotly.tools import FigureFactory as FF
from datetime import datetime

import pandas.io.data as web

df = web.DataReader("aapl", 'yahoo', datetime(2008, 8, 15), datetime(2008, 10, 15))
fig = FF.create_ohlc(df.Open, df.High, df.Low, df.Close, dates=df.index)

py.iplot(fig, filename='finance/aapl-ohlc')
Out[2]:
Customizing the Figure with Text and Annotations
In [3]:
import plotly.plotly as py
from plotly.tools import FigureFactory as FF

import pandas.io.data as web

df = web.DataReader("aapl", 'yahoo', datetime(2008, 8, 15), datetime(2008, 10, 15))
fig = FF.create_ohlc(df.Open, df.High, df.Low, df.Close, dates=df.index)

# Update the fig - all options here: https://plot.ly/python/reference/#Layout
fig['layout'].update({
    'title': 'The Great Recession',
    'yaxis': {'title': 'AAPL Stock'},
    'shapes': [{
        'x0': '2008-09-15', 'x1': '2008-09-15', 'type': 'line',
        'y0': 0, 'y1': 1, 'xref': 'x', 'yref': 'paper',
        'line': {'color': 'rgb(40,40,40)', 'width': 0.5}
    }],
    'annotations': [{
        'text': "the fall of Lehman Brothers",
        'x': '2008-09-15', 'y': 1.02,
        'xref': 'x', 'yref': 'paper',
        'showarrow': False, 'xanchor': 'left'
    }]
})

py.iplot(fig, filename='finance/aapl-recession-ohlc', validate=False)
Out[3]:
Custom OHLC Chart Colors
In [4]:
import plotly.plotly as py
from plotly.tools import FigureFactory as FF
from plotly.graph_objs import Line, Marker
from datetime import datetime

import pandas.io.data as web

df = web.DataReader("aapl", 'yahoo', datetime(2008, 1, 1), datetime(2009, 4, 1))

# Make increasing ohlc sticks and customize their color and name
fig_increasing = FF.create_ohlc(df.Open, df.High, df.Low, df.Close, dates=df.index,
    direction='increasing', name='AAPL',
    line=Line(color='rgb(150, 200, 250)'))

# Make decreasing ohlc sticks and customize their color and name
fig_decreasing = FF.create_ohlc(df.Open, df.High, df.Low, df.Close, dates=df.index,
    direction='decreasing',
    line=Line(color='rgb(128, 128, 128)'))

# Initialize the figure
fig = fig_increasing

# Add decreasing data with .extend()
fig['data'].extend(fig_decreasing['data'])

py.iplot(fig, filename='finance/aapl-ohlc-colors', validate=False)
Out[4]:
Simple Example with datetime Objects
In [5]:
import plotly.plotly as py
from plotly.tools import FigureFactory as FF

from datetime import datetime

# Add data
open_data = [33.0, 33.3, 33.5, 33.0, 34.1]
high_data = [33.1, 33.3, 33.6, 33.2, 34.8]
low_data = [32.7, 32.7, 32.8, 32.6, 32.8]
close_data = [33.0, 32.9, 33.3, 33.1, 33.1]
dates = [datetime(year=2013, month=10, day=10),
         datetime(year=2013, month=11, day=10),
         datetime(year=2013, month=12, day=10),
         datetime(year=2014, month=1, day=10),
         datetime(year=2014, month=2, day=10)]

# Create ohlc
fig = FF.create_ohlc(open_data, high_data,
    low_data, close_data, dates=dates)

py.iplot(fig, filename='finance/simple-ohlc', validate=False)
Out[5]:
In [6]:
import cufflinks as cf
import pandas as pd

cf.set_config_file(world_readable=True,offline=False)

# Open, High, Low, Close Data Generation 
ohlc=cf.datagen.ohlc()
ohlc.head()
Out[6]:
open high low close
2015-01-01 00:00:00+00:00 100.000000 101.789089 78.062507 99.197149
2015-01-02 00:00:00+00:00 99.523991 114.548820 94.171902 109.412094
2015-01-03 00:00:00+00:00 110.412538 124.291269 85.566579 95.512897
2015-01-04 00:00:00+00:00 94.187397 110.011045 84.495936 108.208498
2015-01-05 00:00:00+00:00 107.742944 110.223364 91.743416 105.990083
In [7]:
ohlc.iplot(kind='ohlc')
Out[7]:
In [8]:
import cufflinks as cf
import pandas as pd

cf.set_config_file(world_readable=True,offline=False)

ohlc=cf.datagen.ohlc()
ohlc.iplot(kind='ohlc', up_color='rgb(20, 140, 200)', down_color='rgb(100, 100, 100)')
Out[8]:

Reference

In [9]:
help(FF.create_ohlc)
Help on function create_ohlc in module plotly.tools:

create_ohlc(open, high, low, close, dates=None, direction='both', **kwargs)
    BETA function that creates an ohlc chart

    :param (list) open: opening values
    :param (list) high: high values
    :param (list) low: low values
    :param (list) close: closing
    :param (list) dates: list of datetime objects. Default: None
    :param (string) direction: direction can be 'increasing', 'decreasing',
        or 'both'. When the direction is 'increasing', the returned figure
        consists of all units where the close value is greater than the
        corresponding open value, and when the direction is 'decreasing',
        the returned figure consists of all units where the close value is
        less than or equal to the corresponding open value. When the
        direction is 'both', both increasing and decreasing units are
        returned. Default: 'both'
    :param kwargs: kwargs passed through plotly.graph_objs.Scatter.
        These kwargs describe other attributes about the ohlc Scatter trace
        such as the color or the legend name. For more information on valid
        kwargs call help(plotly.graph_objs.Scatter)

    :rtype (dict): returns a representation of an ohlc chart figure.

    Example 1: Simple OHLC chart from a Pandas DataFrame
    ```
    import plotly.plotly as py
    from plotly.tools import FigureFactory as FF
    from datetime import datetime

    import pandas.io.data as web

    df = web.DataReader("aapl", 'yahoo', datetime(2008, 8, 15), datetime(2008, 10, 15))
    fig = FF.create_ohlc(df.Open, df.High, df.Low, df.Close, dates=df.index)

    py.plot(fig, filename='finance/aapl-ohlc')
    ```

    Example 2: Add text and annotations to the OHLC chart
    ```
    import plotly.plotly as py
    from plotly.tools import FigureFactory as FF
    from datetime import datetime

    import pandas.io.data as web

    df = web.DataReader("aapl", 'yahoo', datetime(2008, 8, 15), datetime(2008, 10, 15))
    fig = FF.create_ohlc(df.Open, df.High, df.Low, df.Close, dates=df.index)

    # Update the fig - all options here: https://plot.ly/python/reference/#Layout
    fig['layout'].update({
        'title': 'The Great Recession',
        'yaxis': {'title': 'AAPL Stock'},
        'shapes': [{
            'x0': '2008-09-15', 'x1': '2008-09-15', 'type': 'line',
            'y0': 0, 'y1': 1, 'xref': 'x', 'yref': 'paper',
            'line': {'color': 'rgb(40,40,40)', 'width': 0.5}
        }],
        'annotations': [{
            'text': "the fall of Lehman Brothers",
            'x': '2008-09-15', 'y': 1.02,
            'xref': 'x', 'yref': 'paper',
            'showarrow': False, 'xanchor': 'left'
        }]
    })

    py.plot(fig, filename='finance/aapl-recession-ohlc', validate=False)
    ```

    Example 3: Customize the OHLC colors
    ```
    import plotly.plotly as py
    from plotly.tools import FigureFactory as FF
    from plotly.graph_objs import Line, Marker
    from datetime import datetime

    import pandas.io.data as web

    df = web.DataReader("aapl", 'yahoo', datetime(2008, 1, 1), datetime(2009, 4, 1))

    # Make increasing ohlc sticks and customize their color and name
    fig_increasing = FF.create_ohlc(df.Open, df.High, df.Low, df.Close, dates=df.index,
        direction='increasing', name='AAPL',
        line=Line(color='rgb(150, 200, 250)'))

    # Make decreasing ohlc sticks and customize their color and name
    fig_decreasing = FF.create_ohlc(df.Open, df.High, df.Low, df.Close, dates=df.index,
        direction='decreasing',
        line=Line(color='rgb(128, 128, 128)'))

    # Initialize the figure
    fig = fig_increasing

    # Add decreasing data with .extend()
    fig['data'].extend(fig_decreasing['data'])

    py.iplot(fig, filename='finance/aapl-ohlc-colors', validate=False)
    ```

    Example 4: OHLC chart with datetime objects
    ```
    import plotly.plotly as py
    from plotly.tools import FigureFactory as FF

    from datetime import datetime

    # Add data
    open_data = [33.0, 33.3, 33.5, 33.0, 34.1]
    high_data = [33.1, 33.3, 33.6, 33.2, 34.8]
    low_data = [32.7, 32.7, 32.8, 32.6, 32.8]
    close_data = [33.0, 32.9, 33.3, 33.1, 33.1]
    dates = [datetime(year=2013, month=10, day=10),
             datetime(year=2013, month=11, day=10),
             datetime(year=2013, month=12, day=10),
             datetime(year=2014, month=1, day=10),
             datetime(year=2014, month=2, day=10)]

    # Create ohlc
    fig = FF.create_ohlc(open_data, high_data,
        low_data, close_data, dates=dates)

    py.iplot(fig, filename='finance/simple-ohlc', validate=False)
    ```

Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.