Python Forum
CandleStick & MACD Indicator using plotly.graph_object
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CandleStick & MACD Indicator using plotly.graph_object
#1
Hello!

I have a csv file with data about crypto symbol such as timestamp, open, high, low, close, volume and I wanna display a Candlestick with MACD indicator.

I have the following function to calculate MACD indicator :

    def calculate_macd(self):
        if self.df is not None and not self.df.empty:
            self.df['12EMA'] = self.df['close'].ewm(span=12, adjust=False).mean()
            self.df['26EMA'] = self.df['close'].ewm(span=26, adjust=False).mean()
            self.df['MACD'] = self.df['12EMA'] - self.df['26EMA']
            self.df['Signal Line'] = self.df['MACD'].ewm(span=9, adjust=False).mean()
And the following code to display the Candlestick:

    def show_candlestick_chart(self):
        self.calculate_macd()
        if self.df is not None and not self.df.empty:
            fig = make_subplots(rows=2, cols=1, shared_xaxes=True, vertical_spacing=0.2, row_heights=[0.7, 0.3])

            candlestick_trace = go.Candlestick(x=self.df['timestamp'],
                                               open=self.df['open'],
                                               high=self.df['high'],
                                               low=self.df['low'],
                                               close=self.df['close'],
                                               name='Candlestick')

            macd_trace = go.Scatter(x=self.df['timestamp'], y=self.df['MACD'], mode='lines', line=dict(width=2),
                                    name='MACD')
            signal_trace = go.Scatter(x=self.df['timestamp'], y=self.df['Signal Line'], mode='lines',
                                      name='Signal Line')

            fig.add_trace(candlestick_trace, row=1, col=1)
            fig.add_trace(macd_trace, row=2, col=1)
            fig.add_trace(signal_trace, row=2, col=1)

            fig.update_layout(height=1000)
            self.browser.setHtml(fig.to_html(include_plotlyjs='cdn'))
        else:
            self.browser.setHtml("<p>No data available for plotting.</p>")
And I get the following output :

Fig 1.

The problem is that I don't want to display the CandleStick and MACD in different plots, just a single plot including both, like this:

Fig 2.

I modified the code as it follows :

    def show_candlestick_chart(self):
        self.calculate_macd()
        if self.df is not None and not self.df.empty:
            fig = make_subplots(rows=1, cols=1, shared_xaxes=True, vertical_spacing=0.05)

            candlestick_trace = go.Candlestick(x=self.df['timestamp'],
                                               open=self.df['open'],
                                               high=self.df['high'],
                                               low=self.df['low'],
                                               close=self.df['close'],
                                               name='Candlestick')

            macd_trace = go.Scatter(x=self.df['timestamp'], y=self.df['MACD'], mode='lines', line=dict(width=2),
                                    name='MACD')
            signal_trace = go.Scatter(x=self.df['timestamp'], y=self.df['Signal Line'], mode='lines',
                                      name='Signal Line')

            fig.add_trace(candlestick_trace, row=1, col=1)
            fig.add_trace(macd_trace, row=1, col=1)
            fig.add_trace(signal_trace, row=1, col=1)

            fig.update_layout(height=800)
            self.browser.setHtml(fig.to_html(include_plotlyjs='cdn'))
        else:
            self.browser.setHtml("<p>No data available for plotting.</p>")
But my output is like this :

Fig 3.

Any ideas how to solve this?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem Converting Tradingview Indicator to Python code kralxs 1 332 Apr-27-2024, 06:10 PM
Last Post: kralxs
  binning_endpoints ->plotly Luis_liverpool 0 895 Aug-09-2022, 10:13 AM
Last Post: Luis_liverpool
  plotly expression problem Visiting 2 2,041 May-16-2021, 12:28 AM
Last Post: Visiting
  Progress Indicator for Xmodem 0.4.6 KenHorse 1 2,018 Jan-30-2021, 07:12 PM
Last Post: bowlofred
  Plotly library with AIX 6.X / 7.X ? HK2432 0 1,489 Jan-14-2020, 06:04 PM
Last Post: HK2432
  Plotly error - AttributeError: module 'plotly.plotly' has no attribute 'iplot' fernando_santos 0 5,647 Jul-24-2019, 02:35 PM
Last Post: fernando_santos
  Scraping a javascript RSSI indicator jon333 8 6,063 Aug-10-2017, 07:11 PM
Last Post: jon333

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020