Create classic technical analysis stock charts in Python with minimal code.
The library is built around matplotlib
and supports both pandas and polars DataFrames.
Charts can be defined using a declarative interface,
based on a set of drawing primitives like Candlesticks, Volume
and technical indicators like SMA, EMA, RSI, ROC, MACD, etc ...
Warning: This project is experimental and the interface is likely to change. For a related project with a mature api you may want to look into mplfinance.
# Candlesticks chart with SMA, RSI and MACD indicators
import yfinance as yf
from mplchart.chart import Chart
from mplchart.primitives import Candlesticks, Volume, Pane, LinePlot
from mplchart.indicators import SMA, RSI, MACD
from mplchart.utils import normalize_prices
ticker = 'AAPL'
prices = normalize_prices(yf.Ticker(ticker).history('5y'))
Chart(prices, title=ticker, max_bars=250).plot(
Candlesticks(), Volume(), SMA(50), SMA(200),
Pane("above", yticks=(30, 50, 70)),
RSI(14) | LinePlot(overbought=70, oversold=30),
Pane("below"),
MACD(),
).show()Prices data is expected to be a pandas or polars DataFrame
with columns open, high, low, close, volume
and a datetime column named date or datetime (or a datetime index for pandas).
Note:
Chartand all indicators require lowercase column names. Usenormalize_pricesfrommplchart.utilsto normalize your DataFrame before use:from mplchart.utils import normalize_prices prices = normalize_prices(yf.Ticker(ticker).history('5y'))
The library contains drawing primitives that can be used like an indicator in the plot api. Primitives are classes and must be instantiated as objects before being used with the plot api.
# Candlesticks chart
from mplchart.chart import Chart
from mplchart.primitives import Candlesticks
Chart(prices, title=title, max_bars=250).plot(
Candlesticks()
).show()The main drawing primitives are :
Candlesticksfor candlestick plotsOHLCfor open, high, low, close bar plotsPricefor price line plotsVolumefor volume bar plotsPaneto switch to a different pane (above or below)LinePlotdraw an indicator as line plotAreaPlotdraw an indicator as area plotBarPlotdraw an indicator as bar plotZigZaglines between pivot pointsPeaksto mark peaks and valleys
The library includes some standard technical analysis indicators for pandas DataFrames. Indicators are classes and must be instantiated as objects before being used with the plot api.
Some of the indicators included are:
SMASimple Moving AverageEMAExponential Moving AverageWMAWeighted Moving AverageHMAHull Moving AverageROCRate of ChangeRSIRelative Strength IndexATRAverage True RangeNATRNormalized Average True RangeADXAverage Directional IndexDMIDirectional Movement IndexMACDMoving Average Convergence DivergencePPOPrice Percentage OscillatorBOPBalance of PowerCMFChaikin Money FlowMFIMoney Flow IndexSTOCHStochastic OscillatorBBANDSBollinger BandsKELTNERKeltner ChannelDEMADouble Exponential Moving AverageTEMATriple Exponential Moving Average
Use | to bind an indicator to a rendering primitive, or to compose indicators:
SMA(50) | LinePlot(style="dashed", color="red") # bind indicator to primitive
SMA(50) | ROC(1) # chain indicators# Customizing indicator style with LinePlot
from mplchart.indicators import SMA, EMA, ROC
from mplchart.primitives import Candlesticks, LinePlot
indicators = [
Candlesticks(),
SMA(20) | LinePlot(style="dashed", color="red", alpha=0.5, width=3)
]
Chart(prices).plot(indicators)If the indicator returns a DataFrame instead of a Series, specify an item (column name) in the primitive.
Use prices | indicator to apply an indicator directly to data:
prices | SMA(50) # apply indicator to dataFor polars DataFrames, the expressions subpackage provides polars Expr factories
as an alternative to the indicator pattern.
These can be used directly with chart.plot().
# Candlesticks chart with polars expressions
from mplchart.chart import Chart
from mplchart.primitives import Candlesticks, Volume, Pane, LinePlot
from mplchart.expressions import SMA, EMA, RSI, MACD
Chart(prices, title=ticker, max_bars=250).plot(
Candlesticks(), Volume(), SMA(50), SMA(200),
Pane("above", yticks=(30, 50, 70)),
RSI() @ LinePlot(overbought=70, oversold=30),
Pane("below"),
MACD(),
).show()Expressions are plain polars.Expr values — they can be composed with standard polars operators,
passed to df.select(), or used anywhere polars expressions are accepted.
Contrary to indicators, expressions use the @ operator to bind to a primitive:
from mplchart.primitives import LinePlot, AreaPlot
from mplchart.expressions import SMA, RSI
SMA(50) @ LinePlot(color="red") # expression → primitive
RSI(14) @ AreaPlot(color="blue") # expression → primitiveIf you have ta-lib installed you can use its abstract functions as indicators. They are created by calling Function with the name of the function and its parameters. Ta-lib functions work with both pandas and polars backends.
# Candlesticks chart with talib functions
from mplchart.primitives import Candlesticks
from talib.abstract import Function
indicators = [
Candlesticks(),
Function('SMA', 50),
Function('SMA', 200),
]
Chart(prices).plot(indicators).show()Example notebooks live in the examples/ folder — see the examples README for the full list.
pip install mplchartThe indicators module requires pandas; the expressions module requires polars.
If either is already in your environment, mplchart will use it automatically.
The [pandas], [polars], and [all] extras are just a convenience — they
install pandas or polars alongside mplchart, nothing more:
pip install mplchart[pandas]
pip install mplchart[polars]
pip install mplchart[all]Required:
- python >= 3.10
- matplotlib
- numpy
- pyarrow
Optional extras:
[pandas]— pandas[polars]— polars[all]— pandas and polars
- stockcharts.com - Classic stock charts and technical analysis reference
- mplfinance - Matplotlib utilities for the visualization, and visual analysis, of financial data
- matplotlib - Matplotlib: plotting with Python
- pandas - Flexible and powerful data analysis / manipulation library for Python
- polars - Fast DataFrame library for Python
- yfinance - Download market data from Yahoo! Finance's API