0

I have a Dataframe (lets call this "df") which looks like the below, that is used to plot a candlestick chart using plotly:

         Date  Open  ...  Close                                   Headline
0  2020-10-23   190  ...    195  As Shares Tread Water Wait for a Pullback
1  2020-10-24   187  ...    177                    Why You Should Buy AAPL

In the above Dataframe, if there is no Headline data i.e "na", I have added a default value "No News" by using:

df['Headline'].fillna('No News', inplace=True)

To plot the Dataframe into a candlestick chart, I have used the folllowing:

fig = go.Figure(data=[go.Candlestick(......

I have used the "hovertext" parameter so that when the user hovers over a particular candlestick, you can see the "Headline" for that specific date:

hovertext= df['Headline'] 

Now, what I also want is to annotate this candlestick with an arrow and text that says "News" to highlight that there is a Headline for this particular candle, so that the user doesn't need to hover to find out if there was a headline (only to find out what the specific Headline was). The x reference would be the "Date" and the y reference I could probably use the "High".

I think I need the code to loop through each Headline and annotate the candlestick where News!="No News". If News=="No News", then I do not want to annotate in that particular instance.

From what I understand, based on this answer, annotations accepts list and parameters for each arrow is a dictionary. So, I have used a for loop to create a list of arrow dicts, and then use fig.update_layout(annotations = list) to draw multiple arrows. However, this doesn't seem to work (unless I have misunderstood):

arrow_list=[]
counter=0
for i in df['Headline'].tolist():
       if i !="No News":
            arrow=dict(x=df['Headline'].values[counter],y=df['high'].values[counter],xref="x",yref="y",text="News",ax=20,ay=-30,arrowhead = 3,
                arrowwidth=1.5,
                arrowcolor='rgb(255,51,0)',)
            arrow_list.append(arrow)
            counter+=1
        else:
            counter+=1
fig.update_layout(title=f"{ticker} Stock Price for the past {days} day(s)",yaxis_title=f'{ticker} Price',
                    xaxis_title='Date',yaxis=dict(autorange=True, fixedrange=False, ),annotations=arrow_list)

Any help would be appreciated. Thanks.

ZPython
  • 3
  • 4

1 Answers1

0

Ok. So I think I know what went wrong here. It was a silly mistake. The "x" value for my arrow dictionaries were wrong.

Currently it's:

arrow=dict(x=df['Headline'].values[counter]....

But instead should be:

arrow=dict(x=df['date'].values[counter]....
ZPython
  • 3
  • 4