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/

    Numpy Array to LineBuffer

    General Code/Help
    3
    3
    1031
    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.
    • Developing Coder
      Developing Coder last edited by Developing Coder

      I'm doing some math in an indicator and in the process LineBuffer is converted into a Numpy ndarray. Is it possible to convert back?

      Example:

      log_hl = np.log(self.datas[0].high / self.datas[0].low)
      
      1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators last edited by

        As one of the goals of backtrader is to NOT depend on external packages, there is neither a

        @developing-coder said in Numpy Array to LineBuffer:

        LineBuffer is converted into a Numpy ndarray

        nor

        @developing-coder said in Numpy Array to LineBuffer:

        Is it possible to convert back?

        You have gotten a standard Python list from from of the lines and used to create a ndarray.

        In any case there is no need to do neither a nor b. Use ApplyN and use the function of your choosing (apparently log) - Docs - Indicator Reference

        1 Reply Last reply Reply Quote 0
        • A
          adamb032 last edited by

          im not particularly sure that it matters, but there is a workaround:

          inside your indicator class

          from copy import deepcopy
          
          some_data_linebuff = deepcopy(self.data.close)
          my_new_array = [.....] #same len as self.data.close
          for idx in range(len(my_new_array)):
               some_data_linebuff.set(my_new_array[idx], ago=idx)
          
          self.lines.new_line = some_data_linebuff
          

          basically whats happening is you make a copy of a linebuffer object, with all the dates already determined, and then reassign all the values for your array into it. presto chango.

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