Plot observer (StopLoss/TakeProfit) on corresponding price plots
I am trying to implement my StopLoss/TakeProfit observer for multiple securities using
BuySellobserver logic. Here is my single security observer (
tp_priceare 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 = self._owner.sl_price if self._owner.tp_price != 0.0: self.lines.take = self._owner.tp_price
I've tried to use
BuySellobserver as a baseline and noticed the following things (I use separate plot for each data):
stdstats=Truebuy/sell arrows are shown on each data plot
BuySellobserver added as
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
githubbut didn't find any differences between calling this observer in
stdstats=Truemode and in
stdstats=False. But final plots are different. What trick did you use to show
BuySellarrows on each data plot in
Also I've noticed that
stdstats=Truemode and in
stdstats=Falsemode use different locations of the buy/sell arrows relative to the price chart. Is there any way to set
Thanks in advance
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?
You seem to look for the parameter
BuySellobserver, which adds extra distance if
True. The distance is controlled by the parameters
BuySellreference: Docs - Observers Reference
@backtrader Thank you! Helped.