Introduction to candlestick chart

Timeseries with python

A candlestick chart, created using the Matplotlib 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.
Mplfinance, a submodule of the matplotlib library, provides the capability to create candlestick charts easily. In this post, we'll create simple candlestick charts with this library.


Creating Candlestick charts with matplotlib requires a library called mplfinance, built by matplotlib.

To install mplfinance, you can use the following command in your command-line interface (such as Terminal or Command Prompt):

pip install mplfinance

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

pip install yfinance

import mplfinance as mpf
import yfinance as yf


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.

According to the documentation of mplfinance: "Non-trading days are simply not shown".

# Define the stock symbol and date range
stock_symbol = "AAPL"  # Example: Apple Inc.
start_date = "2022-01-01"
end_date = "2022-03-30"

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

Simplest candlestick

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

Finally, if our dataset has the properties listed above, we simply call mplfinance's plot() function.


Candle for each date

To get real candlesticks, all we have to do is specify the argument type='candle' in the plot() function.

A candlestick turns black if the last price is higher than the previous close, and it goes white if the last price is lower than the previous close.

mpf.plot(stock_data, type='candle')

Going further

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

For more examples of how to create or customize your time series plots, see the time series section. You may also be interested in how to add moving averages on top.


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 🙏!