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 a list of all indicators inside the observer

    Indicators/Strategies/Analyzers
    1
    3
    144
    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
      ronnyBravo last edited by

      Hey,

      Is it possible to somehow get a list of all indicators that are added to the strategy when inside the observer next() method. I know that we can access datas, broker, trades and orders data via
      self._owner attribute. The indicator objects are listed there as well, so we can access them.

      However, is there an attribute which holds a list of all indicators that are added to this strategy? So we could actually loop over indicators and would not need to have a prior knowledge of what indicators are added.

      1 Reply Last reply Reply Quote 0
      • R
        ronnyBravo last edited by

        I figured out part of this on my own.

        So, when inside observers next() method we have reference to strategy object (via self._owner). Strategy object has a method getindicators() which returns an iterator, so we can plug this straight into for-loop and loop over all indicators one-by-one. To access data (lines) inside those indicator objects we can access lines attribute which again returns an iterator so when plugged into for-loop we can loop over all defined lines one-by-one.

        Example code:

        for ind in self._owner.getindicators():
             for line in ind.lines:
                  print(line[0])
        

        Right now I'm trying to figure out how can I get the indicator lines name so I know which data I'm dealing with.

        Also, if indicators are not defined as a separate class, but are calculated inside the strategy and then plotted using LinePlotterIndicator() method then I'm not sure yet how to access those samples. This is next thing that I hope to figure out

        1 Reply Last reply Reply Quote 0
        • R
          ronnyBravo last edited by

          Okay, so I figured out getting the name part as well. Instead of using iterator directly, we can get a tuple of lines defined inside the indicator with _getlines() method. Then we can loop over that tuple and each time call _getline() method with line name to return the actual lines object from which we can get the data sample.

          Example code:

          for ind in self._owner.getindicators():
              line_names=ind._getlines()
              ind_name=type(ind).__name__
              for line_name in line_names:
                  line=ind._getline(line_name)
                  print("Indicator "+ind_name+" line "+str(line_name)+" value is "+str(line[0]))
          

          Still working on the other issue with LinePlotterIndicator() method as mentioned above so if anybody can help then that would be greatly appreciated!

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