Search code examples
pythonmatplotlibplotlybokeh

candlestick chart markings using plotting libraries in python


I would like to know which library to use to do interactive markings on my candlestick chart. For example in the below figure.enter image description here

As you can see I would like to add the green arrow down and red arrow up based on some criteria. Also you can see the horizontal lines in blue and red color is my support resistance indicators which i want to add in the plot as well.

Any clue on how to achieve it ?


Solution

  • I guessed from the image you posted that it was plotly, so I drew a candlestick in plotly and added a line and a string annotation. For example, I made the string of triangles a green triangle if it crossed the bottom of the previous day, and red if it didn't. You can also use the list of X and Y axes you want to display.

    import plotly.graph_objects as go
    import pandas as pd
    
    df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
    
    fig = go.Figure(data=[go.Candlestick(x=df['Date'][400:],
                    open=df['AAPL.Open'][400:], high=df['AAPL.High'][400:],
                    low=df['AAPL.Low'][400:], close=df['AAPL.Close'][400:])
                         ])
    
    fig.add_trace(go.Scatter(x=df['Date'][400:],
                             y=[df['AAPL.Low'][400:].min()]*len(df['Date'][400:]),
                             mode="lines",
                             line=dict(color='red', width=2)))
    
    fig.add_trace(go.Scatter(x=df['Date'][400:],
                             y=[df['AAPL.High'][400:].min()]*len(df['Date'][400:]),
                             mode="lines",
                             line=dict(color='blue', width=2)))
    last = 0
    for i in range(len(df['Date'][400:])):
        temp = df.loc[400+i:400+i,'AAPL.Low'].values[0]
        if temp >= last:
            fig.add_annotation(x=df.loc[400+i,'Date'], y=temp+3, text="▲", showarrow=False, font=dict(size=16, color='LightSeaGreen'))
            last = temp
        else:
            fig.add_annotation(x=df.loc[400+i,'Date'], y=temp-3, text="▲", showarrow=False, font=dict(size=16, color='red'))
    
    fig.update_layout(xaxis_rangeslider_visible=False)
    fig.show()
    

    enter image description here