Create classic technical analysis stock charts in Python with minimal code.
The library is built around matplotlib
and pandas.
Charts can be defined using a declarative interface,
based on a set of drawing primitives like Candleststicks
, Volume
and technical indicators like SMA
, EMA
, RSI
, ROC
, MACD
, etc ...
Warning This project is experimental and the interface can change. For a similar project with a mature api you may want to look into mplfinance.
import yfinance as yf
from mplchart.chart import Chart
from mplchart.primitives import Candlesticks, Volume
from mplchart.indicators import ROC, SMA, EMA, RSI, MACD
ticker = 'AAPL'
prices = yf.Ticker(ticker).history('5y')
max_bars = 250
indicators = [
Candlesticks(), SMA(50), SMA(200), Volume(),
RSI(),
MACD(),
]
chart = Chart(title=ticker, max_bars=max_bars)
chart.plot(prices, indicators)
chart.show()
Price data is expected to be presented as a pandas DataFrame
with columns open
, high
, low
, close
volume
and a timestamp index named date
.
Please note, the library will automatically convert column
and index names to lower case for its internal use.
The library contains drawing primitives that can be used as an indicator in the plot api. All primitives are classes that must be instantiated before being used in the plot api.
from mplchart.chart import Chart
from mplchart.primitives import Candlesticks
indicators = [Candlesticks()]
chart = Chart(title=title, max_bars=max_bars)
chart.plot(prices, indicators)
The main drawing primitives are :
Candlesticks
for candlesticks plotsOHLC
for open, high, low, close bar plotsPrice
for price line plotsVolume
for volume bar plotsPeaks
to plot peaks and valleysSameAxes
to force plot on the same axesNewAxes
to force plot on a new axes
The libary contains some basic technical analysis indicators implemented in pandas/numpy. Indicators are classes that must be instantiated before being used in the plot api.
Some of the indicators included are:
SMA
Simple Moving AverageEMA
Exponential Moving AverageROC
Rate of ChangeRSI
Relative Strength IndexATR
Average True RangeADX
Average Directional IndexMACD
Moving Average Convergence DivergencePPO
Price Percentage OscillatorSLOPE
Slope (linear regression with time)BBANDS
Bolling Bands
If you have ta-lib installed you can use its abstract functions as indicators.
The indicators are created by calling abstract.Function
with the name of the indicator and its parameters.
from mplchart.primitives import Candlesticks
from talib.abstract import Function
indicators = [
Candlesticks(),
Function('SMA', 50),
Function('SMA', 200),
Function('RSI'),
Function('MACD'),
]
Any callable that takes a prices data frame and returns a series as result can be used as indicator. A function can be used as an indicator but you can also implement an indicator as a callable dataclass.
from dataclasses import dataclass
from mplchart.library import get_series, calc_ema
@dataclass
class DEMA:
""" Double Exponential Moving Average """
period: int = 20
same_scale = True
# same_scale is an optional class attribute that indicates
# the indicator should be plot on the same axes by default
def __call__(self, prices):
series = get_series(prices)
ema1 = calc_ema(series, self.period)
ema2 = calc_ema(ema1, self.period)
return 2 * ema1 - ema2
You can find example notebooks and scripts in the examples folder.
You can install the current version of this package with pip
python -mpip install git+https://github.com/furechan/mplchart.git
- python >= 3.9
- matplotlib
- pandas
- numpy
- yfinance
- 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
- yfinance Download market data from Yahoo! Finance's API
- ta-lib Python wrapper for TA-Lib
- pandas Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
- numpy The fundamental package for scientific computing with Python