plotly Candlestick in python

Asked

Viewed 542 times

0

I built a Candlestick chart using the plotly and would like to know if it is possible to create on top of this graph, based on the column flag which contains the number 1, a rectangle superimposing the Candlesticks.

Code example:

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'],`insira o código aqui`
                   "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()

inserir a descrição da imagem aqui

Current result:

inserir a descrição da imagem aqui

Expected result:

inserir a descrição da imagem aqui

1 answer

0


blz. The staff of the plotly site helped me with that doubt. I share with vcs the response link. It is not the same, but it is easy to make adjustments to the initial idea.

https://community.plot.ly/t/plotly-candlestick-in-python-with-flag/31154

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))  

Browser other questions tagged

You are not signed in. Login or sign up in order to post.