Programmatically find all Indicator values in Python

Back

All the examples of indicator usage I've seen access indicator values by explicitly including their names. ie:

self.bb = self.BB(symbol, 20, 1, MovingAverageType.Simple, Resolution.Daily)
self.bb.UpperBand.Current.Value
self.bb.MiddleBand.Current.Value
self.bb.LowerBand.Current.Value

But what if I have an indicator, and I'm not sure what all the variable names are? How would I discover what they are and access them? I tried this but it didn't work because everything came up as type 'str':

for prop in dir(self.bb):
# Never works, everything is a 'str' object
if isinstance(prop, IndicatorDataPoint):
self.Log('We found one! %s is an indicator value!' % prop)

Thanks in advance!

Update Backtest







 
0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


I found a solution! Though it seems pretty hacky. If anyone can show me a more elegant solution I'd appreciate it but I'll post my solution for now in case other people are looking for this as well.

for prop in dir(self.bb):
if isinstance(getattr(self.bb, prop), IndicatorDataPoint):
self.Log('We found one! self.bb.%s' % prop)
else:
for subprop in dir(getattr(self.bb, prop)):
if isinstance(getattr(getattr(self.bb, prop), subprop), IndicatorDataPoint):
self.Log('We found a sub one! self.bb.%s.%s' % (prop, subprop))

The above witll print out:

We found one! self.bb.Current
We found a sub one! self.bb.LowerBand.Current
We found a sub one! self.bb.MiddleBand.Current
We found a sub one! self.bb.StandardDeviation.Current
We found a sub one! self.bb.UpperBand.Current

 

1

That is a decent solution! 

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed