Show Sidebar Hide Sidebar

Candlestick Charts in Pandas

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.8'
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]:
Adding a Trace to a Candlestick Chart
In [5]:
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[5]:
Simple Example with datetime Objects
In [6]:
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[6]:
In [7]:
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[7]:
open high low close
2015-01-01 00:00:00+00:00 100.000000 131.185378 99.561930 131.185378
2015-01-02 00:00:00+00:00 129.400135 131.818872 105.125754 107.131008
2015-01-03 00:00:00+00:00 107.343323 125.251750 105.314314 113.207397
2015-01-04 00:00:00+00:00 115.106811 120.011895 85.565535 87.832330
2015-01-05 00:00:00+00:00 88.929139 109.595695 79.723708 90.299211
In [8]:
ohlc.head().iplot(kind='candle',theme='ggplot',up_color='blue',down_color='pink')
Out[8]:

Reference

Still need help?
Contact Us

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