July cohort is now open: How to secure your spot:

Create beautiful stock price charts (in 1 line of code)

Visualizing data is the first step to analyzing it effectively. It’s the first step in spotting trends, patterns, and anomalies that could inform your trading decisions.

Yet, most individuals attempt to build stock price charts themselves using libraries like Matplotlib, which, while powerful, can be painful.

Matplotlib has a steep learning curve and a time-consuming setup for specialized financial charts.

There’s a better way.

After reading today’s newsletter, you’ll be able to skip the complex coding part and jump straight back to what matters: analyzing the market.

Let’s go!

Create beautiful stock price charts (in 1 line of code)

mplfinance is designed for financial data visualization. It makes creating detailed candlestick, OHLC, and other financial charts easy.

It was originally part of the matplotlib library. It has since been spun off as an independent project.

mplfinance abstracts away the complexity of Matplotlib. Despite its simplicity, we still benefit from Matplotlib’s power.

mplfinance is for market data analysts who need to quickly visualize market data but don’t want to waste time with Matplotlib.

Let’s see some examples of how it works.

We’ll import yFinance to quickly download market prices.

import yfinance as yf
import mplfinance as mpf
import warnings
warnings.filterwarnings('ignore')

data = yf.download("AAPL", start="2022-01-01", end="2022-06-30")

The default chart type is a simple OHLC plot.

mpf.plot(data)

The result is a simple OHLC chart.

Create beautiful stock price charts. Visualizing data is the first step to analyzing it effectively. Do it with mplfinance.
Create beautiful stock price charts. Visualizing data is the first step to analyzing it effectively. Do it with mplfinance.

By adding the type argument, we can define the plot type.

mpf.plot(data, type="candle")

The result is a candle chart.

Create beautiful stock price charts (in 1 line of code)
Create beautiful stock price charts. Visualizing data is the first step to analyzing it effectively. Do it with mplfinance.

Some people like simple line charts.

mpf.plot(data, type="line")

The result is a line chart.

Create beautiful stock price charts. Visualizing data is the first step to analyzing it effectively. Do it with mplfinance.
Create beautiful stock price charts. Visualizing data is the first step to analyzing it effectively. Do it with mplfinance.

Other people like more complex charts like Renko charts.

mpf.plot(data, type="renko")

The result is a Renko chart.

Create beautiful stock price charts. Visualizing data is the first step to analyzing it effectively. Do it with mplfinance.
Create beautiful stock price charts. Visualizing data is the first step to analyzing it effectively. Do it with mplfinance.

We can also include moving averages to the plot. Use a single number for a single moving average. Use a tuple of numbers for several.

mpf.plot(data, type="ohlc", mav=15)

The result is an OHLC chart with a 15 period moving average.

Create beautiful stock price charts. Visualizing data is the first step to analyzing it effectively. Do it with mplfinance.
Create beautiful stock price charts. Visualizing data is the first step to analyzing it effectively. Do it with mplfinance.

Let’s include three moving averages.

mpf.plot(data, type="candle", mav=(7, 14, 21))

The result is a candle chart with 7, 15, and 21 day moving averages.

Create beautiful stock price charts. Visualizing data is the first step to analyzing it effectively. Do it with mplfinance.
Create beautiful stock price charts. Visualizing data is the first step to analyzing it effectively. Do it with mplfinance.
mpf.plot(data, type="candle", mav=(7, 14, 21), volume=True)

We can also include volume.

The result is a candle chart with multiple moving averages plotted with volume.

Create beautiful stock price charts. Visualizing data is the first step to analyzing it effectively. Do it with mplfinance.
Create beautiful stock price charts. Visualizing data is the first step to analyzing it effectively. Do it with mplfinance.

We can include non-trading days as well.

mpf.plot(
    data, 
    type="candle", 
    mav=(7, 14, 21), 
    volume=True, 
    show_nontrading=True
)

The result is the same as above including non-trading days (note the gaps in the volume bars).

Create beautiful stock price charts. Visualizing data is the first step to analyzing it effectively. Do it with mplfinance.
Create beautiful stock price charts. Visualizing data is the first step to analyzing it effectively. Do it with mplfinance.

mplfinance handles intraday charting too.

intraday = yf.download(tickers="PLTR", period="5d", interval="1m")
iday = intraday.iloc[-100:, :]
mpf.plot(iday, type="candle", mav=(7, 12), volume=True)

The result is a 1-minute chart showing the last 100 minutes of prices.

Create beautiful stock price charts (in 1 line of code)
Create beautiful stock price charts. Visualizing data is the first step to analyzing it effectively. Do it with mplfinance.

Your next steps

mplfinance has many chart types, including animations. Next time you’re analyzing asset price data, consider using mplfinance to speed up your analysis.