Hello,
Is it possible to get historical indicators in the algorithm environment? In the research environment the following works:
import datetime as dt
import QuantConnect.Indicators as qci
start_date = dt.datetime(2019, 6, 1)
end_date = dt.datetime(2019, 12, 31)
qb = QuantBook()
spy = qb.AddEquity("SPY")
history = qb.History(qb.Securities.Keys, 360, Resolution.Daily)
bb = qci.BollingerBands(30, 2)
bb_df = qb.Indicator(bb, spy.Symbol, start_date, end_date, Resolution.Daily)
bb_df.drop('standarddeviation', 1).plot()
I would now like to get historical indicators in a pandas dataframe during backtest initialisation. In the algo environment I set:
qb = self
And do the same as above (without the plot), however I get the following error:
During the algorithm initialization, the following exception has occurred: AttributeError : 'MyAlgo' object has no attribute 'Indicator'
at Initialize in main.py:line 54
at calc_indicators in main.py:line 92
AttributeError : 'MyAlgo' object has no attribute 'Indicator'
Any help would be appreciated. Thanks!
Alexandre Catarino
Hey Dr Damage ,
You are getting that error because in the algorithm backtesting environment, QCAlgorithm doesn't have an Indicator method. qb.Indicator is a helper method in the research environment for plotting purposes. Instead, to create an Indicator, you must find the specific indicator constructor for each indicator. These can be found in the indicator reference table in the documentation.
In the algorithm backtesting environment, you can create a BB indicator and then subscribe to daily resolution SPY data. This will automatically update the indicator as historical data is fed into the algorithm:
bbIndicator = self.BB(spy.Symbol, 30, 2, Resolution.Daily)
It is also possible to create a "blank" indicator and then populate it your own data with the Update method. Learn more about indicators here.
Here is an example of using rolling windows to store historical indicator values.
Dr Damage
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!