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

How to plot close price +/- ATR

  • Hi guys,
    I havent too much experience with bt and I have an issue with a simple task.
    I would realize this chart:

    The profit target/loss target are simple close price +/- X times ATR(14).
    I tried with following code:

        # Add a ATR indicator for stop loss / take profit calculations
        self.atr = bt.indicators.ATR(self.datas[0], period=14)
        self.higher_band = 3*self.atr + self.dataclose
        self.lower_band = self.dataclose - self.atr

    but I dont get higher_band/lower_band plotted on chart.

    How can I do it?

  • In order to have something automatically plotted in bt it should be data feed, indicator or observer. So you may want to develop an indicator or observer.

    Docs - Indicator Development
    DOcs - Observers and Statistics

    On the other side, formula you are using will not give you a picture shown, it will be only a channel moving together with the close price. It will never cross the price. Hope you understand it and this formula is only first step.

    Some time ago in order to develop similar picture I developed stop loss/take profit observer. Here is the link, hope it might help

    Stop loss/Take profit observer