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 a boolean indicator



  • Hi there,
    I am developing my own indicator and for debugging purposes I would like to plot the signal it generates over the actual data points, much like the buy/ sell arrows.
    Here is some sample code so far (removed all unnecessary code for clarity purposes and generated random True / False indicator), as you can see, it plots a 1 if the indicator is true in a subplot. How do I convert that into an arrow above / below the candle?
    Many thanks!
    Marc

    from __future__ import (absolute_import, division, print_function,
                            unicode_literals)
    import backtrader as bt
    import datetime  # For datetime objects
    import os.path  # To manage paths
    import sys  # To find out the script name (in argv[0])
    import random
    
    
    class DummyInd(bt.Indicator):
        lines = ('dummyline',)
    
        def next(self):
            if random.randint(0, 100) > 90:
                result = 1
            else:
                result =0
            self.lines.dummyline[0] = result
    
    
    # Create a Stratey
    class TestStrategy(bt.Strategy):
    
        def __init__(self):
            self.myindicator = DummyInd()
    
        def next(self):
            pass
    
    
    cerebro = bt.Cerebro()
    
    cerebro.addstrategy(TestStrategy)
    
    modpath = os.path.dirname(os.path.abspath(sys.argv[0]))
    datapath = modpath + "\\datas\\orcl-1995-2014.txt"
    
    data = bt.feeds.YahooFinanceCSVData(
        dataname=datapath,
        # Do not pass values before this date
        fromdate=datetime.datetime(2000, 1, 1),
        # Do not pass values after this date
        todate=datetime.datetime(2000, 12, 31),
        reverse=False)
    
    cerebro.adddata(data)
    
    cerebro.broker.setcash(100000.0)
    cerebro.run()
    cerebro.plot()
    

  • administrators

    To do that, look at the definition of how lines are plotted in the BuySell observer.

    https://github.com/mementum/backtrader/blob/700380c8bc41ab9da8295cfc7b64091bab75866b/backtrader/observers/buysell.py#L47-L54



  • Hi Backtrader,

    Thanks a lot! that worked...
    Here the code in case someone else needs it:

    from __future__ import (absolute_import, division, print_function,
                            unicode_literals)
    import backtrader as bt
    import datetime  # For datetime objects
    import os.path  # To manage paths
    import sys  # To find out the script name (in argv[0])
    import random
    
    
    class DummyInd(bt.Indicator):
        lines = ('dummyline',)
        plotinfo = dict(plot=True, subplot=False, plotlinelabels=True)
        plotlines = dict(dummyline=dict(marker='$\u21E9$', markersize=6.0, color='red', fillstyle='full', ls='') )
        params = (
            ('barplot', False),  # plot above/below max/min for clarity in bar plot
            ('bardist', 0.055),  # distance to max/min in absolute perc
        )
    
        def next(self):
    
    
            if random.randint(0, 100) > 96: # to get a ~4% chance of this dummy indicator triggering
               self.lines.dummyline[0] = self.datas[0].high[0]
            else:
                pass
    
    
    # Create a Stratey
    class TestStrategy(bt.Strategy):
    
        def __init__(self):
            self.myindicator = DummyInd()
    
        def next(self):
            pass
    
    
    cerebro = bt.Cerebro()
    
    cerebro.addstrategy(TestStrategy)
    
    modpath = os.path.dirname(os.path.abspath(sys.argv[0]))
    datapath = modpath + "\\datas\\orcl-1995-2014.txt"
    
    data = bt.feeds.YahooFinanceCSVData(
        dataname=datapath,
        # Do not pass values before this date
        fromdate=datetime.datetime(2000, 1, 1),
        # Do not pass values after this date
        todate=datetime.datetime(2000, 12, 31),
        reverse=False)
    
    cerebro.adddata(data)
    
    cerebro.broker.setcash(100000.0)
    cerebro.run()
    cerebro.plot()
    
    

    and here a screenshot of the indicator:
    boolean indicator.png


Log in to reply
 

});