A candlestick chart, created using the Plotly library in Python, is a graphical representation of financial data. It displays price movements over a specific time period, typically used in stock market analysis. Candlestick charts are composed of individual 'candlesticks', each representing the opening, closing, high, and low prices for a given time interval. These candlesticks are displayed on a two-dimensional coordinate system, with one axis representing time and the other axis representing price.

Plotly has a Candlestick() function that provides the capability to create candlestick charts easily. In this post, we'll create simple candlestick charts with this library.

Libraries

First, we need to install the plotly:

pip install plotly

In this post, we'll use the object oriented API from plotly: you can find more info of why here.

And since we'll load data from yahoo finance, we need the yfinance library:

pip install yfinance

import plotly.graph_objects as go
import yfinance as yf

Dataset

Candlestick charts are mainly used to represent financial data, especially stock prices.

In this post, we'll load Apple's share price data, directly from our Python code via the yfinance library. All we need to do is define the desired start and end data (yyyy-mm-dd format), and the ticker or symbol associated with this company (in this case "AAPL").

Our dataset must have the characteristics needed to produce our graph easily:

  • be a pandas dataframe
  • a date index
  • an Open column
  • a High column
  • a Low column
  • a Close column

The tickers can be found on easily on yahoo finance.

# Define the stock symbol and date range
stock_symbol = "GOOGL"  # Example: Google
start_date = "2021-01-01"
end_date = "2021-03-30"

# Load historical data
stock_data = yf.download(stock_symbol,
                         start=start_date, end=end_date)
[*********************100%%**********************] 1 of 1 completed

Simple candlestick

Once we've opened our dataset, we'll now create the graph.

We have to specify which column to use for opening, closing etc. In our case, it's pretty intuitive:

fig = go.Figure(data=[go.Candlestick(
                x = stock_data.index, # date values
                open = stock_data['Open'],
                high = stock_data['High'],
                low = stock_data['Low'],
                close = stock_data['Close'])])

# Mask a default range slider
fig.update_layout(xaxis_rangeslider_visible=False)

# Set layout size
fig.update_layout(
    autosize=False,
    width=700,
    height=500)

Now, let's save the graph in a HTML file and display it in this website using an iframe

# save this file as a standalong html file:
fig.write_html("../../static/interactiveCharts/candlestick-plotly-basic.html")
%%html
<iframe 
    src="../../interactiveCharts/candlestick-plotly-basic.html" 
    width="800" 
    height="600" 
    title="candlestick with plotly" 
    style="border:none">
</iframe>

Candle chart with a range slider

Plotly lets us easily add a slider below the graph, which defines the date range displayed on the graph.

To do this, simply remove the line of code from the graph above that specified that the slider should be hidden.

fig = go.Figure(data=[go.Candlestick(
                x = stock_data.index, # date values
                open = stock_data['Open'],
                high = stock_data['High'],
                low = stock_data['Low'],
                close = stock_data['Close'])])

# Set layout size
fig.update_layout(
    autosize=False,
    width=700,
    height=500)

# Display the plot
fig.show()

Now, let's save the graph in a HTML file and display it in this website using an iframe

# save this file as a standalong html file:
fig.write_html("../../static/interactiveCharts/candlestick-plotly-basic-2.html")
%%html
<iframe 
    src="../../interactiveCharts/candlestick-plotly-basic-2.html" 
    width="800" 
    height="600" 
    title="candlestick with plotly 2" 
    style="border:none">
</iframe>

Going further

This post explains how to create a candlestick chart with plotly.

For more examples of how to create or customize your candlesticks plots, see the candlestick section. You may also be interested in how to custom style of a plotly candlestick chart.

Timeseries

Contact & Edit


👋 This document is a work by Yan Holtz. You can contribute on github, send me a feedback on twitter or subscribe to the newsletter to know when new examples are published! 🔥

This page is just a jupyter notebook, you can edit it here. Please help me making this website better 🙏!