matplotlib / mpl-finance

This package is DEPRECATED, replaced by https://github.com/matplotlib/mplfinance

Home Page:https://pypi.org/project/mplfinance/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error calling candlestick_ohlc

Mark531 opened this issue · comments

Hi,

My data has the following structure:

df.head()
Out[90]: 
                Time     Open     High      Low    Close
33750  736859.197917  1.18242  1.18245  1.18218  1.18221
33751  736859.201389  1.18221  1.18252  1.18199  1.18232
33752  736859.204861  1.18232  1.18275  1.18232  1.18253
33753  736859.208333  1.18254  1.18256  1.18172  1.18172
33754  736859.211806  1.18173  1.18211  1.18159  1.18187

But when I call:

fig, ax = plt.subplots()
candlestick_ohlc(ax, df.values)

I get the following error:

ValueError: not enough values to unpack (expected 5, got 4)

What's the problem?

Regards,
Mark

@Mark531 -- I am unable to reproduce your issue, using the exact data that you posted. The API that you are using, candlestick_ohlc, expects the second argument to be a list of lists (which df.values provides, so the code as you have written it above looks fine) and each sub-list ("row") should have 5 items (time,open,high,low,close) but the API is only getting 4. I would guess that you have some code in between that is eliminating one of your columns. Perhaps you turned the Time column into an index for the dataframe before calling candlestick_ohlc(), as that would reduce the number of columns from 5 to 4? Please provide more of you code to be able to reproduce, if you still think this is an issue with candlestick_ohlc(), otherwise please indicate if you have found the issue in your own code. Thank you.

@DanielGoldfarb Well, I don't know what caused the error. I retried and I don't get this error anymore. Though, the display is very bad:
image
The dates have been converted to numbers with matplotlib.dates.date2num, is it correct?

@Mark531 - That is exactly what I would expect to see given the data and code you have posted; everything is working as it should.

There are a couple things going on here. First, your data is intraday data (each ohlc point is 5 minutes apart) whereas the default candlestick width assumes daily data (one data point per day) so the candlesticks you are seeing above a too wide and are overlapping. This can be adjusted using the width parameter (see below).

The other thing is that candlestick_ohlc() is a relatively low level function. It just draws the candlesticks, so there are other matplotlib things that you have to do, specifically matplotlib needs to be told to interpret the x-axis as dates and times.

This week, I started working on a higher level API that does this stuff automatically, so the caller doesn't have to worry about it. Ideally, all you have to do is pass in an ohlc dataframe. I expect the new mpl_finance API to be ready for beta testing in the next 5 to 10 days, so please let me know if you are interested in helping test it.

In the meantime, if you want to see one way to make the above data work with the existing API, take a look at the following gist mpl_finance_issue24

@DanielGoldfarb Hi Daniel, and sorry for the delay. Ok, I understand now why my snippet didn't work. It would certainly be a good thing to create a high-level wrapper to encapsulate all these manipulations.
In the meantime, I've installed plotly which offers a candlestick plotting feature. On top of that, it creates dynamic HTML graphs which make it easy to scroll within an entire graph. I guess you could take inspiration from it.

Issue resolved: user error / unclear documentation.