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/

    How to get an array containing the current and past values of an indicator.

    General Code/Help
    3
    4
    2965
    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.
    • R
      Rodrigo Pinto last edited by

      Hello,

      Lets suppose I declare an indicator :

      self.cci = bt.talib.CCI(self.datas[0].high, self.datas[0].low, self.datas[0].close)

      Now, when I try to access the indicator value on the next function I can do:

      self.cci[0], which will get the me current value, or

      self.cci[1], which I believe will get me the previous value.

      But how can I get an array containing the n previous values? I tried something like self.cci[:n], but I keep getting an error.

      Curtis Miller B 2 Replies Last reply Reply Quote 0
      • Curtis Miller
        Curtis Miller @Rodrigo Pinto last edited by

        @Rodrigo-Pinto You're mistaken in thinking that self.cci[1] is a past value; in fact, it's a future value. You'd want self.cci[-1] instead.

        I would first check in next() that there are at least n values in self.cci; len() can do this. Then I would try self.cci[(-n):]. I don't know if this will work but I think it would.

        Blog posts: Walk Forward Analysis Demonstration, Stock Trading Analytics and Optimization

        Books/Video Courses: Unpacking NumPy and Pandas

        1 Reply Last reply Reply Quote 0
        • B
          backtrader administrators @Rodrigo Pinto last edited by backtrader

          @Rodrigo-Pinto said in How to get an array containing the current and past values of an indicator.:

          But how can I get an array containing the n previous values? I tried something like self.cci[:n], but I keep getting an error.

          Not supporting slicing is a design decision, because it doesn't make sense with the indexing chosen for this library.

          As pointed out by @Curtis-Miller, [1] is not the previous value but the future next value. In contexts in which the data has been preloaded (to optimize speed), it won't break, but you will be cheating yourself.

          See the following:

          • Docs - Platform Concepts (where you may want to focus on the section: Indexing: 0 and -1

          Use .get(ago=x, size=y) (where the default values are x=0, y=1)

          This was already also discussed in these threads (good time to put them together) where some discussion about why slicing doesn't make sense is present:

          • Community - Crude Swing Indicator help
          • Community - Porting a pandas dataframe dependent indicator
          • Community - Getting historical volume/close/open/etc data in a strategy
          1 Reply Last reply Reply Quote 0
          • B
            backtrader administrators last edited by

            An explanation has been added to:

            • Community - FAQ
            • Docs - Platform Concepts
            1 Reply Last reply Reply Quote 0
            • 1 / 1
            • First post
              Last post
            Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors