Search code examples
pythoncandlestick-chart

Candlestick Graph is not correctly shaped


I try to graph candlestick chart with candlestick_ohlc method from mplfinance module. I have a DataFrame indexed with Date data in datetime type and prices in OHLC order.

My code for matplotlib:

from mplfinance.original_flavor import candlestick_ohlc

# creating Subplots
fig, ax = plt.subplots()
 
# plotting the data
candlestick_ohlc(ax, df.values, width = 0.6,
                 colorup = 'green', colordown = 'red',
                 alpha = 0.8)

plt.show()

I have unexpected result for my graph.

enter image description here

  • I thought that candlesticks on my graph were showing also Date in timestamps and this was why I had misshaped thickness in the graph, but it wasn't.

  • I tried to adjust several parameters and ended up with the most simple code and the resulting graph was the same.

  • I went through all of the questions of stackoverflow, my problem persists.

Why can't I have correct Candlestick Charts?


Solution

  • The comment is correct that, for candlestick_ohlc(), the "time must be in float date format - see date2num".

    That said, candlestick_ohlc() is obsolete and no longer supported. It requires that you convert your datetimes to matplotlib float date format, and that you format the x-axis dates yourself.

    I strongly recommend you use the new version of mplfinance which does all that for you, into which you can simply pass a dataframe with a datetime index. See documentation here.

    You code can then simply be:

    import mplfinance as mpf
    
    # plotting the data
    mpf.plot(df, style='yahoo')
    

    For further documentation (how to customize plots, colors, etc) click here.