45

I am trying to import matplotlib.finance module in python so that I can make a Candlestick OCHL graph. My matplotlib.pyplot version is 2.00. I've tried to import it using the following commands:

import matplotlib.finance
from matplotlib.finance import candlestick_ohlc

I get this error:

warnings.warn(message, mplDeprecation, stacklevel=1) MatplotlibDeprecationWarning: The finance module has been deprecated in mpl 2.0 and will be removed in mpl 2.2. Please use the module mpl_finance instead.

Then instead of using the above lines in python I tried using the following line:

import mpl_finance

I get this error:

ImportError: No module named 'mpl_finance'

What should I do to import candlestick from matplotlib.pyplot?

Daniel Goldfarb
  • 6,937
  • 5
  • 29
  • 61
Furqan Hashim
  • 1,304
  • 2
  • 15
  • 35

10 Answers10

50

I've stopped using mpl_finance (and plotly) since they are too slow. Instead I've written an optimized finance plotting library, finplot, which I use to backtest up to 107 candles.

Here's a small example:

import yfinance as yf
import finplot as fplt

df = yf.download('SPY',start='2018-01-01', end = '2020-04-29')
fplt.candlestick_ochl(df[['Open','Close','High','Low']])
fplt.plot(df.Close.rolling(50).mean())
fplt.plot(df.Close.rolling(200).mean())
fplt.show()

Examples included show SMA, EMA, Bollinger bands, Accumulation/Distribution, Heikin Ashi, on balance volume, RSI, TD sequential, MACD, scatter plot indicators, heat maps, histograms, real-time updating charts and interactive measurements; all with sensible defaults ready for use.

MACD S&P 500 example

I do dogfooding every day, drop me a note or a pull request if there is something you want. Hope you take it for a spin!

Jonas Byström
  • 25,316
  • 23
  • 100
  • 147
  • 4
    Really cool work dude! I have just tried and it goes smooth like a charm! Thumbs up – Giuseppe Salvatore Jun 20 '20 at 17:54
  • 2
    Wow this is EPIC. Been needing something like this for ages. Wish I was the OP so I could mark this as the answer! – ddm-j Jul 05 '20 at 15:56
  • 3
    Another very happy user here, nothing like it in Python ecosystem. – yamen Sep 24 '20 at 21:25
  • It's amazing. If there were some documentation it will be super useful. Thank you man – Seyfi Nov 18 '20 at 20:47
  • Could not install it unless using command pip install --user finplot. By doing that I got the error PyQt5 DLL load failed and Anaconda crashed. Cannot open it again – Betelgeuse May 02 '21 at 21:33
  • @Betelgeuse Strange, don't use conda myself. Did you figure it out? Do you know if pyqt5 works on your installation? – Jonas Byström May 10 '21 at 04:55
  • 1
    @Jonas Byström pyqt5 was working but I guess there was some conflicts after installing the package. I solved it by updating anaconda and uninstalling and installing again pyqt5. – Betelgeuse May 17 '21 at 21:24
  • Wonderful library, thanks! One thing that puts me off, why using OCHL instead of common OHLC in some function names? – lenik May 26 '21 at 15:05
  • @lenik Oops, total mistake on my part! I'll look into fixing that... – Jonas Byström May 27 '21 at 05:57
  • 2
    Legend, always wondered why there was so little financial charting options out there, you obviously took it personally. Great job. – ajsp Jun 13 '21 at 10:13
  • @JonasByström, is this integrate-able with tkinter, as a tkinter widget, as matplotlib supports? – SamTheProgrammer Feb 21 '22 at 10:45
  • 1
    @Password-Classified Good question, for which I don't know the answer. You'd have to give it a shot. – Jonas Byström Feb 21 '22 at 19:26
  • @JonasByström it gives me the following error: `Sorry, "python" cannot be run on this version of macOS. Qt requires macOS 11.0.0 or later, you have macOS 10.15.7.` – Volatil3 Aug 25 '23 at 06:26
  • @Volatil3 sounds like you need to install or upgrade python or your OS. – Jonas Byström Aug 27 '23 at 20:13
44

In 2020, one can now pip install mplfinance

duhaime
  • 25,611
  • 17
  • 169
  • 224
33

What this warning tells you is that the finance module will be removed at some point.

At the moment you don't need to worry about this warning. It will only affect you when you update to a yet to be released version 2.2 of matplotlib, in which case you'll need to change your imports.

If you already want to be compatible with future versions now, you can download the mpl_finance module from https://github.com/matplotlib/mpl_finance .

After having downloaded the files, you may install in the usual way,

python setup.py install

Alternatively you may try installing through pip,

pip install https://github.com/matplotlib/mpl_finance/archive/master.zip

The reason for this is that the people at matplotlib want to keep their code clean and not maintain a specialized sidepackage like this in the main code. They probably also do not want to maintain the package and spend resources on it, which can be better used in the core development.

Chris Adams
  • 18,389
  • 4
  • 22
  • 39
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • Downloaded the zip file... How do i install it? – niken Mar 19 '17 at 16:30
  • 1
    under normal conditions you need to install it using `python setup.py install` – ImportanceOfBeingErnest Mar 19 '17 at 16:46
  • 1
    mpl_finance no longer works. My guess is because 'http://ichart.yahoo.com/table.csv? ..' url is not working – seedhom Jan 24 '18 at 18:52
  • 2
    @seedhom mpl_finance still works fine, but it can no longer be used to directly fetch data from Yahoo. One would need other datasources; still its plotting capabilities are unchanged. – ImportanceOfBeingErnest Jan 24 '18 at 21:33
  • after installing like you said, I now end up getting this error: ||NameError: name 'candlestick2_ohlc' is not defined|| I am using || import mpl_finance||, any help would be appreciated. – Adib Aug 30 '18 at 23:47
26

Since mpl_finace is not on pip now, you may also want to use following command to install mpl_finance by pip:

pip install https://github.com/matplotlib/mpl_finance/archive/master.zip

Kattern
  • 2,949
  • 5
  • 20
  • 28
  • 11
    `mpl_finance` doesn't appear to be maintained any more, and it's started its way into oblivion and obsolescence, what should a honest newborn python developer use these days to plot candlesticks with some extra lines and graphics? – Don Giulio Nov 04 '18 at 10:44
13

mpl_finance is no longer part of matplotlib. Install the module directly from gitHub via pip

pip install https://github.com/matplotlib/mpl_finance/archive/master.zip

and import it with

from mpl_finance import candlestick_ohlc

Then it works the same as before.

magraf
  • 420
  • 5
  • 8
12

There is a new version of matplotlib finance, with documentation, here:

Install with:   pip install --upgrade mplfinance

Or with:   conda install -c conda-forge mplfinance

NOTE: The package name no longer has the dash or underscore:
It is now mplfinance (not mpl-finance, nor mpl_finance)

Daniel Goldfarb
  • 6,937
  • 5
  • 29
  • 61
  • 1
    `from mplfinance.original_flavor import candlestick_ohlc` is how `candlestick_ohlc` is imported now. – csg May 14 '21 at 01:37
6

I'm working on google colab , i got the same problem . then what i did -for python3.6

import mpl_finance

from mpl_finance import candlestick_ohlc

Priyansh gupta
  • 906
  • 12
  • 10
  • After install using pip and before executing the sentences indicated above, I still have the same problem. Next I have done "conda list" and I see that "mpl_finance" are in the lista. What could be the problem? – efueyo Jul 02 '19 at 20:20
1

Plotly.py, a web-browser based, interactive plotting module has finance plotting functions https://plot.ly/python/candlestick-charts/. And it is maintained.

Ben2209
  • 1,071
  • 4
  • 11
  • 24
0

Simply use pip install mpl_finance for Windows or pip3 install mpl_finance for Linux/Unix for installation.

Then use from mpl_finance import candlestick_ohlc to call the library in the Jupyter notebook!

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
Quantum Prophet
  • 337
  • 2
  • 8
0

Replace from matplotlib.finance import candlestick_ohlc with from mplfinance.original_flavor import candlestick_ohlc , That should work.