Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    How to control the color of the Histogram in MACDHisto?

    General Code/Help
    1
    3
    612
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • weibudda
      weibudda last edited by

      In many stock charts, we can find MACD Histogram bar plots with different colors for positive and negtive ones. But in backtrader, they have the same color in default. How to implement different color for positive and negtive bars?

      I have tried to append a 'color=[]' parameter as follows,

      class MACDHisto2(bt.indicators.MACDHisto):
      
          alias = ('MACDHistogram',)
      
          lines = ('histo',)
      
          colors = ['r','g']
      
          # colord = {True: 'red', False: 'green'}
          # colors = [colord[histo_0 < 0] for histo_0 in lines.histo]
      
          plotlines = dict(histo=dict(_method='bar', alpha=0.50, width=1.0, color=colors))
      
          def __init__(self):
              super(MACDHisto2, self).__init__()
      

      it works, the bar plots becomes alternating red and green bars
      But how to construct a list, correspongding to the correct color of each bars?
      I have tried the following, but it doesn't works? any suggestions?

      class MACDHisto2(bt.indicators.MACDHisto):
      
          alias = ('MACDHistogram',)
      
          lines = ('histo',)
      
          # colors = ['r','g']
      
          colord = {True: 'red', False: 'green'}
          colors = [colord[histo_0 < 0] for histo_0 in lines.histo]
      
          plotlines = dict(histo=dict(_method='bar', alpha=0.50, width=1.0, color=colors))
      
          def __init__(self):
              super(MACDHisto2, self).__init__()
      
      ERROR:
      
      File "D:/Python/Jupyter/BackTrader/Test/Record/Order_History_C1.py", line 79, in MACDHisto2
          colors = [colord[histo_0 < 0] for histo_0 in lines.histo]
      AttributeError: 'tuple' object has no attribute 'histo'
      
      

      or

      class MACDHisto2(bt.indicators.MACDHisto):
          alias = ('MACDHistogram',)
      
          lines = ('histo',)
      
          # colors = ['r','g']
          plotlines = {}
      
          def __init__(self):
              colord = {True: 'red', False: 'green'}
              colors = [colord[histo_0 < 0] for histo_0 in self.lines.histo]
      
              self.plotlines = dict(histo=dict(_method='bar', alpha=0.50, width=1.0, color=colors))
              super(MACDHisto2, self).__init__()
      
      

      No error reports, but the histogram bar is missing! Checking the varialbles, find the plotlines passed to the bar plot is still [].

      1 Reply Last reply Reply Quote 0
      • weibudda
        weibudda last edited by

        I tried the following:

        class MACDHisto2(bt.indicators.MACDHisto):
            alias = ('MACDHistogram',)
        
            lines = ('histo',)
        
            # colors = ['r','g']
            plotlines = dict(histo=dict(_method='bar', alpha=0.50, width=1.0))
        
            def __init__(self):
                colord = {True: 'red', False: 'green'}
                colors = [colord[histo_0 < 0] for histo_0 in self.lines.histo]
        
                self.plotlines['histo'].update(color=colors)
                super(MACDHisto2, self).__init__()
        

        it reports:

        File "D:/Python/C1.py", line 82, in __init__
            self.plotlines['histo'].update(color=colors)
        TypeError: 'AutoInfoClass_pl_LineSeries_pl_LineIterator_pl_DataAccessor_pl_IndicatorBase_pl_Indicator_pl_MACD_pl_MACDHisto_pl_MACDHisto2' object is not subscriptable
        

        Still did not Work...

        1 Reply Last reply Reply Quote 0
        • weibudda
          weibudda last edited by

          A method has been found, not very genious.

          A few lines can be added to the plotind function of plot.py in the source code:

                      if plotkwargs['color'] == 'bar':
                          colord = {True: 'red', False: 'green'}
                          colors = [colord[lplot_value > 0] for lplot_value in lplotarray]
                          plotkwargs['color'] = colors
          
                      plottedline = pltmethod(xdata, lplotarray, **plotkwargs)
          

          And when defining a new indicator:

          simply add:

          plotlines = dict(histo=dict(_method='bar', alpha=0.50, width=1.0, color='bar'))
          
          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors