For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Plotting: how to get lines to have specific colours and thickness?



  • I just want to customize the colours and thicknesses of the various lines in Ichimoku.
    I had no idea it was going to be this hard:

    1. There is no way to customize colours using the plotinfo variable, so I have to do this via PlotScheme (which didn't work either)
    2. There also isn't a way to customize line thickness, only on horizontal lines. Why?

    From the documentation on Plotting:
    https://www.backtrader.com/docu/plotting/plotting.html

    I can pass a custom list to cerebro.plot as lcolors:
    lcolors=['blue', 'green', 'red', 'turquoise']

    But since PlotScheme.color() uses tab10_index, I cannot change the order in which the colours are used!
    tab10_index = [3, 0, 2, 1, 2, 4, 5, 6, 7, 8, 9]
    So since 'turquoise' is index 3, it will be used first. Then blue, red, green. Why does there need to be an array that picks specific colours from the Tableau 10 palette in that specific order anyway?

    OK, let's say the proper solution is to subclass PlotScheme. But I cannot import it either, even though I can import every other folder around it, and there is a init.py in backtrader/plot/:

    In [3]: import backtrader as bt
    
    In [4]: bt.plot
    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    <ipython-input-4-f26508c6d1b4> in <module>()
    ----> 1 bt.plot
    
    AttributeError: module 'backtrader' has no attribute 'plot'
    
    In [5]: bt.observers
    Out[5]: <module 'backtrader.observers' from '/Users/shinichi/source/cryptocoins/backtrader/observers/__init__.py'>
    
    In [6]: bt.signals
    Out[6]: <module 'backtrader.signals' from '/Users/shinichi/source/cryptocoins/backtrader/signals/__init__.py'>
    
    In [7]: bt.sizers
    Out[7]: <module 'backtrader.sizers' from '/Users/shinichi/source/cryptocoins/backtrader/sizers/__init__.py'>
    
    In [8]: bt.stores
    Out[8]: <module 'backtrader.stores' from '/Users/shinichi/source/cryptocoins/backtrader/stores/__init__.py'>
    
    In [9]: 
    

    I must be missing something here because backtrader looks pythonic, but is actually very confusing.

    The documentation should make it more obvious that in order to customize plotting, the intended route is to subclass the Indicator and override its self.plotlines via self._plotinit(). Looking at plotlines=(dict()) doesn't help.



  • I spent some time investigating this problem also.

    For multiple lines plotting, I managed to change the colour inside ichimoku.py with plotlines.

    Just paste the following inside the ichimoku.py and restart the iPython to make it work.

    plotlines = dict(
        senkou_span_a=dict(_fill_gt=('senkou_span_b', 'cyan'),
                           _fill_lt=('senkou_span_b', 'magenta'),
                           color='blue'),
        
        senkou_span_b=dict(color='yellow'),
                           
        tenkan_sen=dict(_name='TenK', color='red'),
                        
        kijun_sen=dict(_name='KJ',color='green'),
        
        chikou_span=dict(_name='chi', color='black')
        
    )


  • What about simply subclassing Ichimoku and having that plotlines definition?. As in

    class MyIchimoku(bt.indicators.Ichimoku):
        plotlines = dict(
            senkou_span_a=dict(_fill_gt=('senkou_span_b', 'cyan'), _fill_lt=('senkou_span_b', 'magenta'), color='blue'),
            senkou_span_b=dict(color='yellow'),
            tenkan_sen=dict(_name='TenK', color='red'),
            kijun_sen=dict(_name='KJ',color='green'),
            chikou_span=dict(_name='chi', color='black')    
        )
    

    And then of course using your subclass instead of the internal indicator.