Backtrader Community

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Creating a new line and accessing previous value in each step

    General Code/Help
    2
    3
    510
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Tyler Collins
      Tyler Collins last edited by

      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)
      
      1 Reply Last reply Reply Quote 0
      • A
        ab_trader last edited by

        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

        • If my answer helped, hit reputation up arrow at lower right corner of the post.
        • Python Debugging With Pdb
        • New to python and bt - check this out
        Tyler Collins 1 Reply Last reply Reply Quote 0
        • Tyler Collins
          Tyler Collins @ab_trader last edited by

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

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors