Navigation

    Backtrader Community

    • Register
    • 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/

    How to assign arrays to lines?

    General Code/Help
    1
    2
    310
    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.
    • James Barber
      James Barber last edited by

      So I'm new to the platform, but I have a question with no answer I can find in the docs.

      I have an indicator function I wrote for use outside of backtrader for my own libraries, but I would like to use it for backtesting on backtrader without having to rewrite it specifically for backtrader. So I am wrapping it. When called, it returns a data structure containing a number of arrays. All of these arrays correspond to custom 'Lines'. When trying to assign the arrays to the indicator lines, all values read 'nan' accessing them within a strategy.

      So the question is boiled down to why does the following not work? How do I get my oracle'd arrays into custom indicator lines?

      {code}
      class CustomIndicator(bt.Indicator):
      lines = ("custom_line", )

      # Price flip is 6 bars
      params = (('period', 6),)
      
      def __init__(self):
          self.addminperiod(self.params.period)
          self.line.custom_line =[0.0] * self.data.close.buflen() # => filled with 'nan's when accessed in a strategy
      

      {code}

      Is there any way to convert an array to a Line object or mass assign its values into a line?

      Would very much appreciate any pointers here

      1 Reply Last reply Reply Quote 0
      • James Barber
        James Barber last edited by

        • clearner code
        class CustomIndicator(bt.Indicator):
        lines = ("custom_line", )
        
        # Price flip is 6 bars
        params = (('period', 6),)
        
        def __init__(self):
            self.addminperiod(self.params.period)
            self.line.custom_line =[0.0] * self.data.close.buflen() # => filled with 'nan's when accessed in a strategy
        
        1 Reply Last reply Reply Quote 0
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors