Plot observer (StopLoss/TakeProfit) on corresponding price plots



  • Hi @backtrader

    I am trying to implement my StopLoss/TakeProfit observer for multiple securities using BuySell observer logic. Here is my single security observer (sl_price and tp_price are defined in the startegy):

    class SLTPTracking(bt.Observer):
    
        lines = ('stop', 'take')
    
        plotinfo = dict(plot=True, subplot=False)
    
        plotlines = dict(stop=dict(ls=':', linewidth=1.5),
                         take=dict(ls=':', linewidth=1.5))
    
        def next(self):
    
            if self._owner.sl_price != 0.0:
                self.lines.stop[0] = self._owner.sl_price
    
            if self._owner.tp_price != 0.0:
                self.lines.take[0] = self._owner.tp_price
    

    I've tried to use BuySell observer as a baseline and noticed the following things (I use separate plot for each data):

    1. With stdstats=True buy/sell arrows are shown on each data plot
    2. With stdstats=False and BuySell observer added as
    cerebro.addobserver(bt.observers.BuySell)
    

    buy/sell arrows are shown only on the 1st data plot. Other data plots don't have these arrows. With my modified observer I obtained exactly the same results - only 1st data plot shows the observer.

    I was looking thru the code on the github but didn't find any differences between calling this observer in stdstats=True mode and in stdstats=False. But final plots are different. What trick did you use to show BuySell arrows on each data plot in stdstats=True mode?

    Also I've noticed that stdstats=True mode and in stdstats=False mode use different locations of the buy/sell arrows relative to the price chart. Is there any way to set stdstats=True approach for stdstats=False?

    Thanks in advance


  • administrators

    You have to use: addobservermulti, which adds the observer as many times as data feeds are in the system. It is actually missing from the docs, even if documented in the source. To be added.

    The actual docstring

        def addobservermulti(self, obscls, *args, **kwargs):
            '''
            Adds an ``Observer`` class to the mix. Instantiation will be done at
            ``run`` time
    
            It will be added once per "data" in the system. A use case is a
            buy/sell observer which observes individual datas.
    
            A counter-example is the CashValue, which observes system-wide values
            '''
    


  • Thank you! It works now.
    Could you please clarify my second question:

    Also I've noticed that stdstats=True mode and in stdstats=False mode use different locations of the buy/sell arrows relative to the price chart. Is there any way to set stdstats=True approach for stdstats=False?


  • administrators

    You seem to look for the parameter barplot to the BuySell observer, which adds extra distance if True. The distance is controlled by the parameters bardist

    See the BuySell reference: Docs - Observers Reference



  • @backtrader Thank you! Helped.


Log in to reply
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.