Plotly candlestick in python with flag

set up a candlestick chart using plotly and would like to know if you can create on top of that chart based on the flag column containing the number 1, a rectangle that overlaps candlesticks
Exemplo:

import pandas as pd
import plotly.graph_objects as go

df = pd.DataFrame({"data_minu": ['30/10 09:00','30/10 09:05','30/10,09:10','30/10 09:15','30/10 09:20','30/10 09:25','30/10 09:30','30/10 09:35','30/10 09:40','30/10 09:45'],
                   "Open":['10','17','23','20','8','22','24','25','29','22'],
                   "High":['21','27','25','29','24','27','28','32','29','25'],
                   "Low":['6','12','18','9','5','8','24','18','15','10'],
                   "Close":['17','24','22','10','21','25','26','30','18','10'],
                   "Flag": ['0','1','1','1','0','1','1','1','0','0']})     

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

fig.update_layout(xaxis_rangeslider_visible=False)    
fig.show()  

@haraujo To add rectangular shapes to your flagged candlesticks we are performing a trick:
map the time interval to an arbitrary interval, let us say, [0, 4.5] defining:
tickvals = [k*0.5 for k in range(len(df))]

Then we define the candlestick with values x=tickvals, but take care to display as tick labels at the locations of these values the strings in df["data_minu"].

In this way we can easily define rectangular shapes:

import pandas as pd
import plotly.graph_objects as go

df = pd.DataFrame({"data_minu": ['30/10 09:00','30/10 09:05','30/10,09:10','30/10 09:15','30/10 09:20','30/10 09:25','30/10 09:30','30/10 09:35','30/10 09:40','30/10 09:45'],
                   "Open":['10','17','23','20','8','22','24','25','29','22'],
                   "High":['21','27','25','29','24','27','28','32','29','25'],
                   "Low":['6','12','18','9','5','8','24','18','15','10'],
                   "Close":['17','24','22','10','21','25','26','30','18','10'],
                   "Flag": ['0','1','1','1','0','1','1','1','0','0']})     

fig = go.Figure(data=[go.Candlestick(x=tickvals, #df['data_minu'],
                open=df['Open'], high=df['High'],
                low=df['Low'], close=df['Close'])
                     ])  

tickvals =[k*0.5 for k in range(len(df))]
ticktext=list(df["data_minu"])
fig.update_layout(xaxis_rangeslider_visible=False, xaxis_tickvals=tickvals, xaxis_ticktext=ticktext) 
for k, flag in  enumerate(df['Flag']):
    if int(flag):
        fig.add_shape(dict(type='rect',
                          xref='x', yref='y',
                          layer='below', 
                          x0 = tickvals[k]-0.2, y0 = float(df.loc[k, 'Low'])-1,
                          x1 = tickvals[k]+0.2, y1 = float(df.loc[k, 'High'])+1,
                          fillcolor='orange', #'RoyalBlue',
                          opacity=0.35))

1 Like

Sensational!!! Many thanks for the reply.