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

Creating a new line and accessing previous value in each step

  • I am trying to create a line where in the line I use the previous value to calculate the next value. I'm transferring from code i have written in Pine Script which is quite intuitive with this. However, I'm struggling to figure out how to initialize a line with a value of 0, step forward and use the [-1] value. Here is my code.

    I am running this in init.

        self.up_trend_last = 0.0 if self.first_run else self.up_trend(-1)
        self.up_trend = bt.If(self.dataclose > self.up_trend_last, bt.Max(self.up_lev, self.up_trend_last), self.up_lev)

  • In order to initialize the line use

    self.some_line = backtrader.LineNum(0.0)

    On the other side, if the task you want to resolve is recursive, you may need to use next(), not __init__() only. Check out Blog - Recursive Indicator

  • @ab_trader Thank you. I was able to solve my issue by reading the Recursive Indicator blog and using nextstart()

Log in to reply