How to control the color of the Histogram in MACDHisto?
-
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 [].
-
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...
-
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'))