How to get a list of all indicators inside the observer
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.
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.
for ind in self._owner.getindicators(): for line in ind.lines: print(line)
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
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.
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))
Still working on the other issue with LinePlotterIndicator() method as mentioned above so if anybody can help then that would be greatly appreciated!