• Open Source.
    These libraries are free forever. Your Pro plan helps keep them top notch.
  • Share Online.
    Share charts, dashboards, Jupyter notebooks, and presentations through our enterprise-grade cloud.
  • Engineering Support.
    Get instant chat support from our awesome engineering team.
Show Sidebar Hide Sidebar

Candlestick Charts in Python

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

Candlestick Charts in Python

In [1]:
import plotly
plotly.__version__
Out[1]:
'1.8.3'
Simple Example 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(2007, 10, 1), datetime(2009, 4, 1))
fig = FF.create_candlestick(df.Open, df.High, df.Low, df.Close, dates=df.index)
py.iplot(fig, filename='finance/aapl-candlestick', validate=False)
Out[2]:
Customizing the Figure with Text and Annotations
In [3]:
import plotly.plotly as py
from plotly.tools import FigureFactory as FF
from datetime import datetime

import pandas.io.data as web

fig = FF.create_candlestick(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': '2007-12-01', 'x1': '2007-12-01',
        'y0': 0, 'y1': 1, 'xref': 'x', 'yref': 'paper',
        'line': {'color': 'rgb(30,30,30)', 'width': 1}
    }],
    'annotations': [{
        'x': '2007-12-01', 'y': 0.05, 'xref': 'x', 'yref': 'paper',
        'showarrow': False, 'xanchor': 'left',
        'text': 'Official start of the recession'
    }]
})
py.iplot(fig, filename='finance/aapl-recession-candlestick', validate=False)
Out[3]:
Custom Candlestick 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))
fig = FF.create_candlestick(df.Open, df.High, df.Low, df.Close, dates=df.index)

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

# Make decreasing ohlc sticks and customize their color and name
fig_decreasing = FF.create_candlestick(df.Open, df.High, df.Low, df.Close, dates=df.index,
    direction='decreasing',
    marker=Marker(color='rgb(128, 128, 128)'),
    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-candlestick-custom', 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_candlestick(open_data, high_data,
    low_data, close_data, dates=dates)

py.iplot(fig, filename='finance/simple-candlestick', validate=False)
Out[5]:
Simple Example Adding a Trace to a Candlestick Chart
In [6]:
import plotly.plotly as py
from plotly.tools import FigureFactory as FF
from plotly.graph_objs import *

from datetime import datetime

import pandas.io.data as web

# Create Candlestick
df = web.DataReader("aapl", 'yahoo', datetime(2007, 10, 1), datetime(2008, 3, 31))
fig = FF.create_candlestick(df.Open, df.High, df.Low, df.Close, dates=df.index)

# Create Line of open values
add_line = Scatter(
    x=df.index, 
    y=df.Open, 
    name= 'Open Vals', 
    line=Line(color='black')
    )

fig['data'].extend([add_line])
py.iplot(fig, filename='candlestick_and_trace', validate=False)
Out[6]:

Reference

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

create_candlestick(open, high, low, close, dates=None, direction='both', **kwargs)
    BETA function that creates a candlestick chart
    
    :param (list) open: opening values
    :param (list) high: high values
    :param (list) low: low values
    :param (list) close: closing values
    :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 candlesticks where the close value is greater than
        the corresponding open value, and when the direction is
        'decreasing', the returned figure consists of all candlesticks
        where the close value is less than or equal to the corresponding
        open value. When the direction is 'both', both increasing and
        decreasing candlesticks 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 candlestick chart figure.
    
    Example 1: Simple candlestick 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(2007, 10, 1), datetime(2009, 4, 1))
    fig = FF.create_candlestick(df.Open, df.High, df.Low, df.Close, dates=df.index)
    py.plot(fig, filename='finance/aapl-candlestick', validate=False)
    ```
    
    Example 2: Add text and annotations to the candlestick chart
    ```
    fig = FF.create_candlestick(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': '2007-12-01', 'x1': '2007-12-01',
            'y0': 0, 'y1': 1, 'xref': 'x', 'yref': 'paper',
            'line': {'color': 'rgb(30,30,30)', 'width': 1}
        }],
        'annotations': [{
            'x': '2007-12-01', 'y': 0.05, 'xref': 'x', 'yref': 'paper',
            'showarrow': False, 'xanchor': 'left',
            'text': 'Official start of the recession'
        }]
    })
    py.plot(fig, filename='finance/aapl-recession-candlestick', validate=False)
    ```
    
    Example 3: Customize the candlestick 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 candlesticks and customize their color and name
    fig_increasing = FF.create_candlestick(df.Open, df.High, df.Low, df.Close, dates=df.index,
        direction='increasing', name='AAPL',
        marker=Marker(color='rgb(150, 200, 250)'),
        line=Line(color='rgb(150, 200, 250)'))
    
    # Make decreasing candlesticks and customize their color and name
    fig_decreasing = FF.create_candlestick(df.Open, df.High, df.Low, df.Close, dates=df.index,
        direction='decreasing',
        marker=Marker(color='rgb(128, 128, 128)'),
        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-candlestick-custom', validate=False)
    ```
    
    Example 4: Candlestick 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_candlestick(open_data, high_data,
        low_data, close_data, dates=dates)
    
    py.iplot(fig, filename='finance/simple-candlestick', validate=False)
    ```

Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to our Premium or Enterprise plans.