Error When Trying to Draw A Candlestick Chart with st.plotly_chart()

I am getting an error when I am trying to draw a candlestick using st.plotly_chart(). I am using yfinance to pull the stock data. For some reason it does not like the date key. Here is the error I am getting.

I believe these are all the relevant lines of code.

spy = yf.Ticker('SPY')

spy_History = spy.history(period='5d')

fig = go.Figure(data=[go.Candlestick(x=spy_History['Date'],
                                     open=spy_History['Open'],
                                     high=spy_History['High'],
                                     low=spy_History['Low'],
                                     close=spy_History['Close'])])

fig.update_layout(xaxis_rangeslider_visible=False)
st.plotly_chart(fig, theme='streamlit')

Also, here is what the Dataframe of the information looks like. Perhaps the time stamp after the date is messing it up? Any way to remove that? Any help is appreciated!

Imgur

Hi @RustyPie,

Thanks for posting!

So the issue might be coming from the Date which might not exist. You could use spy_History.index instead of spy_History['Date'].

  • Try this out :point_down:t5:
import streamlit as st
import yfinance as yf
import plotly.graph_objects as go

spy = yf.Ticker('SPY')

spy_History = spy.history(period="5d")

fig = go.Figure(data=[go.Candlestick(x=spy_History.index,
                                     open=spy_History['Open'],
                                     high=spy_History['High'],
                                     low=spy_History['Low'],
                                     close=spy_History['Close'])])
fig.update_layout(xaxis_rangeslider_visible=False)
spy_History
st.plotly_chart(fig, theme='streamlit')
  • This is what it outputs :point_down:t5:

Let me know if this is helpful.

Oh wow, that worked! Thanks @tonykip . Not totally sure why it worked but thank you for your response!

Awesome! Glad it worked. It must have been the Date as noted above.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.