How do i add my ta indicator to my mplfinance candlstick chart?

1k views Asked by At

I do apologize for this question but I am seriously struggling. Below is my code so far. Im 2 months into python and here Ive been sitting for a whole day trying to figure this out. Please can someone take a look and maybe provide a comment on it. https://colab.research.google.com/drive/1aah4KDMIBLtNVHXwvEeW0TDKMdBL2tvz?usp=sharing

Output

2

There are 2 answers

0
Daniel Goldfarb On

There are a number of mplfinance tutorials listed here.

Recommend you read the Adding Your Own Technical Studies to Plots tutorial.

The basic idea is to call mpf.make_addplot() to generate the information needed to display your TA on the candlestick chart, and then use kwarg addplot to add it to the candelstick chart. Something like this:

ap = mpf.make_addplot( ta_data, kwargs, ... )

mpf.plot( df, type='candle', addplot=ap )

...


P.S. I am not very familiar with pandas_ta, but i do know that it uses mplfinance under the hood, and there should be, I believe, a way to simply ask pandas_ta to plot both your candlestick chart and the technical analysis.

1
r-beginners On

Looking at the code in your question, I see that the error is at the end where the indicator is added with plotly, so I will also add a graph using plotly as an example.

import plotly.graph_objects as go
import pandas as pd

fig = go.Figure()
fig.add_trace(go.Candlestick(x=df.index,
                             open=df['Open'],
                             high=df['High'],
                             low=df['Low'],
                             close=df['Close'],
                             name='EURUSD=X'
                             ))

fig.add_trace(go.Scatter(x=df.index,
                         y=df['Final Lowerband'],
                         mode='lines',
                         line=dict(color='green'),
                         name='Final Lowerband'
                         ))

fig.add_trace(go.Scatter(x=df.index,
                         y=df['Final Upperband'],
                         mode='lines',
                         line=dict(color='red'),
                         name='Final Upperband'
                         ))

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

enter image description here