-2

Basically I want a graph with 3 subplots. The 2nd plot will be a candlestick chart and is the one I am currently working on now. However, I keep getting a "not enough values to unpack" error and I don't really know why???

start = dt.datetime(2016,1,1)
end = dt.datetime(2016,12,31)    

fig = plt.figure()
ax1 = plt.subplot2grid((6,1), (0,0), rowspan = 1, colspan = 1)
ax2 = plt.subplot2grid((6,1), (1,0), rowspan = 4, colspan = 1)
ax3 = plt.subplot2grid((6,1), (5,0), rowspan = 1, colspan = 1)

df2 = pdr.data.DataReader('TSLA', 'yahoo', startdate, enddate)
df2.drop('Adj Close', axis = 1, inplace = True)
MA20 = df2['Close'].rolling(20).mean()
MA50 = df2['Close'].rolling(50).mean()

cols = ['Open', 'High', 'Low', 'Close', 'Volume']
df2 = df2[cols] #reordering columns to OHLC order

candlestick_ohlc(ax2, df2, width=0.4, colorup='#77d879', colordown='#db3f3f')

plt.show()

I get this error:

>---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-329-f2bd49065390> in <module>
     18 df2 = df2[cols]
     19 
---> 20 candlestick_ohlc(ax2, df2, width=0.4, colorup='#77d879', colordown='#db3f3f')
     21 
     22 ax1.plot(MA20, label = '20-day')

>c:\users\cecilia\appdata\local\programs\python\python35\lib\site-packages\mpl_finance.py in candlestick_ohlc(ax, quotes, width, colorup, colordown, alpha)
    234     return _candlestick(ax, quotes, width=width, colorup=colorup,
    235                         colordown=colordown,
--> 236                         alpha=alpha, ochl=False)
    237 
    238 

>c:\users\cecilia\appdata\local\programs\python\python35\lib\site-packages\mpl_finance.py in _candlestick(ax, quotes, width, colorup, colordown, alpha, ochl)
    281             t, open, close, high, low = q[:5]
    282         else:
--> 283             t, open, high, low, close = q[:5]
    284 
    285         if close >= open:

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

The chart I get also does not seem right. It's fully red, but for candlesticks that closed higher than it opened, I have set the color to be green but no green candles appear. I have arranged the columns in the 'OHLC' order to correspond with the function, so I don't know why it comes out like this too.

1 Answers1

-1

candlestick_ohlc needs to take the date as input. Also, dates need be converted to numbers. Hence the following would work.

import datetime as dt
import matplotlib.pyplot as plt
import pandas_datareader as pdr
from mpl_finance import candlestick_ohlc
import matplotlib.dates as mdates


start = dt.datetime(2016,1,1)
end = dt.datetime(2016,12,31)    

fig, ax = plt.subplots()
df2 = pdr.data.DataReader('TSLA', 'yahoo', start, end)
df2.drop('Adj Close', axis = 1, inplace = True)

df2.reset_index(inplace=True)
df2["Date"] = mdates.date2num(df2["Date"].values)
print(df2.head())

cols = ['Date', 'Open', 'High', 'Low', 'Close', 'Volume']
df2 = df2[cols] #reordering columns to OHLC order

candlestick_ohlc(ax, df2.values, width=0.4, colorup='#77d879', colordown='#db3f3f')

ax.xaxis_date()

plt.show()

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712