1

I've been trying to plot candlesticks from a pandas DataFrame containing ohlc data (in m5 timeframe), as well as data from another pandas array (in s1 timeframe), but I didn't manage to make it work.

My data looks like this:

In [83]: df[:3]
Out[83]: 
                        open     high      low    close
DateTime                                               
2015-01-04 22:00:00  1.19548  1.19551  1.18627  1.19257
2015-01-04 22:05:00  1.19257  1.19455  1.18976  1.19116
2015-01-04 22:10:00  1.19115  1.19404  1.19068  1.19353

In [84]: sec[:5]
Out[84]: 
                        open     high      low    close
DateTime                                               
2015-01-04 22:00:00  1.19548  1.19551  1.19525  1.19551
2015-01-04 22:00:01  1.19535  1.19535  1.19479  1.19482
2015-01-04 22:00:02  1.19483  1.19496  1.19474  1.19485
2015-01-04 22:00:03  1.19487  1.19490  1.19487  1.19490
2015-01-04 22:00:04  1.19453  1.19453  1.19388  1.19397

Both my DataFrames' indexes are Datatime index, and both are holding ohlc data.

When I try to plot data from both DataFrame on the same graph, it works perfectly well:

fig,ax1 = plt.subplots()

ax1.plot(sec['open'],linewidth=2)
ax1.plot(df['close'],'ko')

enter image description here

But after I plot the candlesticks, I can't find a way to change the x-axis so that I can plot the above data on the same figure:

fig,ax1 = plt.subplots()

candlestick2_ohlc(ax1,opens=df["open"].values,highs=df["high"].values,lows=df["low"].values,closes=df["close"].values,width=.4,colorup='g')

enter image description here

I can't find a way to plot both on the same figure, I looked a bit on how to tweak the x-axis after I plot the candlesticks, but I think that I have a bad understanding of how matplotlib axis work, and of why I can plot data from pandas array with different TimeSeries on the same graph.

Would anyone have an idea on how to make it work?

Edit:

I didn't notice I was using the wrong function (candlestick_ohlc instead of candlestick2_ohlc), which explained why I didn't manage to make work the documentation I looked about my issue.

I tried to do the same as I found here: Overlapping Dates in Candlestick Plot from a Pandas DataFrame

fig,ax1 = plt.subplots()

ax1.xaxis_date()

ohlc = [df.index.map(mdates.date2num),df["open"].values,df['high'].values,df['low'].values,df['close'].values]

candlestick_ohlc(ax1,ohlc,width=.4,colorup='g')

Which doesn't work and yields the error ValueError: ordinal must be >= 1

I also tried something similar that I found here: candlestick plot from pandas dataframe, replace index by dates

fig,ax1 = plt.subplots()

ax1.xaxis_date()
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y'))

ohlc = [mdates.date2num(df.index.astype(datetime.date)),df["open"].values,df['high'].values,df['low'].values,df['close'].values]

candlestick_ohlc(ax1,ohlc,width=.4,colorup='g')

Which yields another error message:

TypeError: dtype <class 'datetime.date'> not understood

I'm clearly doing something wrong, but I seriously can't figure out what.

I looked at the documentation about the candlestick_ohlc function of matplotlib.finance, and they say that:

time must be in float days format - see date2num

I assume my issue is that I didn't do the conversion properly, would anyone know what part of what I did is wrong, and why it doesn't yield the correct result?

Erlinska
  • 433
  • 5
  • 16
  • 1
    Currently you do not supply any dates to the candlestick function. So it cannot know which dates to show. You will need to use the `candlestick_ohlc` function instead and supply the dates together with the other data to the quotes argument. There are some questions on that already, so please refer to those. – ImportanceOfBeingErnest Jul 29 '18 at 00:58
  • The solution will also depend on what type the input dates are. This is not clear from this question. Are they strings? Are they numpy.datetime64? – ImportanceOfBeingErnest Jul 29 '18 at 12:01
  • @ImportanceOfBeingErnest My DataFrames' indexes are Datatime indexes ( `dtype='datetime64[ns]`), when I do `df.index.map(mdates.date2num)` the returning index is of type `float64` – Erlinska Jul 29 '18 at 18:31

0 Answers0