Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mplfinance

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mplfinance

Utilities for the visualization, and visual analysis, of financial data

  • 0.12.10b0
  • PyPI
  • Socket score

Maintainers
1

mplfinance Checks

mplfinance

matplotlib utilities for the visualization, and visual analysis, of financial data

Installation

pip install --upgrade mplfinance

Latest Release Information

Older Release Information

Contents and Tutorials


The New API

This repository, matplotlib/mplfinance, contains a new matplotlib finance API that makes it easier to create financial plots. It interfaces nicely with Pandas DataFrames.

More importantly, the new API automatically does the extra matplotlib work that the user previously had to do "manually" with the old API. (The old API is still available within this package; see below).

The conventional way to import the new API is as follows:

    import mplfinance as mpf

The most common usage is then to call

    mpf.plot(data)

where data is a Pandas DataFrame object containing Open, High, Low and Close data, with a Pandas DatetimeIndex.

Details on how to call the new API can be found below under Basic Usage, as well as in the jupyter notebooks in the examples folder.

I am very interested to hear from you regarding what you think of the new mplfinance, plus any suggestions you may have for improvement. You can reach me at dgoldfarb.github@gmail.com or, if you prefer, provide feedback or a ask question on our issues page.


Basic Usage

Start with a Pandas DataFrame containing OHLC data. For example,

import pandas as pd
daily = pd.read_csv('examples/data/SP500_NOV2019_Hist.csv',index_col=0,parse_dates=True)
daily.index.name = 'Date'
daily.shape
daily.head(3)
daily.tail(3)
(20, 5)
OpenHighLowCloseVolume
Date
2019-11-013050.723066.953050.723066.91510301237
2019-11-043078.963085.203074.873078.27524848878
2019-11-053080.803083.953072.153074.62585634570

...

OpenHighLowCloseVolume
Date
2019-11-263134.853142.693131.003140.52986041660
2019-11-273145.493154.263143.413153.63421853938
2019-11-293147.183150.303139.343140.98286602291

After importing mplfinance, plotting OHLC data is as simple as calling mpf.plot() on the dataframe

import mplfinance as mpf
mpf.plot(daily)

png


The default plot type, as you can see above, is 'ohlc'. Other plot types can be specified with the keyword argument type, for example, type='candle', type='line', type='renko', or type='pnf'

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

png

mpf.plot(daily,type='line')

png

year = pd.read_csv('examples/data/SPY_20110701_20120630_Bollinger.csv',index_col=0,parse_dates=True)
year.index.name = 'Date'
mpf.plot(year,type='renko')

png

mpf.plot(year,type='pnf')

png



We can also plot moving averages with the mav keyword

  • use a scalar for a single moving average
  • use a tuple or list of integers for multiple moving averages
mpf.plot(daily,type='ohlc',mav=4)

png

mpf.plot(daily,type='candle',mav=(3,6,9))

png


We can also display Volume

mpf.plot(daily,type='candle',mav=(3,6,9),volume=True)

png

Notice, in the above chart, there are no gaps along the x-coordinate, even though there are days on which there was no trading. Non-trading days are simply not shown (since there are no prices for those days).

  • However, sometimes people like to see these gaps, so that they can tell, with a quick glance, where the weekends and holidays fall.

  • Non-trading days can be displayed with the show_nontrading keyword.

    • Note that for these purposes non-trading intervals are those that are not represented in the data at all. (There are simply no rows for those dates or datetimes). This is because, when data is retrieved from an exchange or other market data source, that data typically will not include rows for non-trading days (weekends and holidays for example). Thus ...
    • show_nontrading=True will display all dates (all time intervals) between the first time stamp and the last time stamp in the data (regardless of whether rows exist for those dates or datetimes).
    • show_nontrading=False (the default value) will show only dates (or datetimes) that have actual rows in the data. (This means that if there are rows in your DataFrame that exist but contain only NaN values, these rows will still appear on the plot even if show_nontrading=False)
  • For example, in the chart below, you can easily see weekends, as well as a gap at Thursday, November 28th for the U.S. Thanksgiving holiday.

mpf.plot(daily,type='candle',mav=(3,6,9),volume=True,show_nontrading=True)

png


We can also plot intraday data:

intraday = pd.read_csv('examples/data/SP500_NOV2019_IDay.csv',index_col=0,parse_dates=True)
intraday = intraday.drop('Volume',axis=1) # Volume is zero anyway for this intraday data set
intraday.index.name = 'Date'
intraday.shape
intraday.head(3)
intraday.tail(3)
(1563, 4)
OpenCloseHighLow
Date
2019-11-05 09:30:003080.803080.493081.473080.30
2019-11-05 09:31:003080.333079.363080.333079.15
2019-11-05 09:32:003079.433079.683080.463079.43

...

OpenCloseHighLow
Date
2019-11-08 15:57:003090.733090.703091.023090.52
2019-11-08 15:58:003090.733091.043091.133090.58
2019-11-08 15:59:003091.163092.913092.913090.96

The above dataframe contains Open,High,Low,Close data at 1 minute intervals for the S&P 500 stock index for November 5, 6, 7 and 8, 2019. Let's look at the last hour of trading on November 6th, with a 7 minute and 12 minute moving average.

iday = intraday.loc['2019-11-06 15:00':'2019-11-06 16:00',:]
mpf.plot(iday,type='candle',mav=(7,12))

png

The "time-interpretation" of the mav integers depends on the frequency of the data, because the mav integers are the number of data points used in the Moving Average (not the number of days or minutes, etc). Notice above that for intraday data the x-axis automatically displays TIME instead of date. Below we see that if the intraday data spans into two (or more) trading days the x-axis automatically displays BOTH TIME and DATE

iday = intraday.loc['2019-11-05':'2019-11-06',:]
mpf.plot(iday,type='candle')

png


In the plot below, we see what an intraday plot looks like when we display non-trading time periods with show_nontrading=True for intraday data spanning into two or more days.

mpf.plot(iday,type='candle',show_nontrading=True)

png


Below: 4 days of intraday data with show_nontrading=True

mpf.plot(intraday,type='ohlc',show_nontrading=True)

png


Below: the same 4 days of intraday data with show_nontrading defaulted to False.

mpf.plot(intraday,type='line') 

png


Below: Daily data spanning across a year boundary automatically adds the YEAR to the DATE format

df = pd.read_csv('examples/data/yahoofinance-SPY-20080101-20180101.csv',index_col=0,parse_dates=True)
df.shape
df.head(3)
df.tail(3)
(2519, 6)
OpenHighLowCloseAdj CloseVolume
Date
2007-12-31147.100006147.610001146.059998146.210007118.624741108126800
2008-01-02146.529999146.990005143.880005144.929993117.586205204935600
2008-01-03144.910004145.490005144.070007144.860001117.529449125133300

...

OpenHighLowCloseAdj CloseVolume
Date
2017-12-27267.380005267.730011267.010010267.320007267.32000757751000
2017-12-28267.890015267.920013267.450012267.869995267.86999545116100
2017-12-29268.529999268.549988266.640015266.859985266.85998596007400
mpf.plot(df[700:850],type='bars',volume=True,mav=(20,40))

png

For more examples of using mplfinance, please see the jupyter notebooks in the examples directory.


Some History

My name is Daniel Goldfarb. In November 2019, I became the maintainer of matplotlib/mpl-finance. That module is being deprecated in favor of the current matplotlib/mplfinance. The old mpl-finance consisted of code extracted from the deprecated matplotlib.finance module along with a few examples of usage. It has been mostly un-maintained for the past three years.

It is my intention to archive the matplotlib/mpl-finance repository soon, and direct everyone to matplotlib/mplfinance. The main reason for the rename is to avoid confusion with the hyphen and the underscore: As it was, mpl-finance was installed with the hyphen, but imported with an underscore mpl_finance. Going forward it will be a simple matter of both installing and importing mplfinance.


Old API availability

With this new mplfinance package installed, in addition to the new API, users can still access the old API.
The old API may be removed someday, but for the foreseeable future we will keep it ... at least until we are very confident that users of the old API can accomplish the same things with the new API.

To access the old API with the new mplfinance package installed, change the old import statements

from:

    from mpl_finance import <method>

to:

    from mplfinance.original_flavor import <method>

where <method> indicates the method you want to import, for example:

    from mplfinance.original_flavor import candlestick_ohlc

Keywords

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc