I am getting an error and cannot seem to get this code to add the necessary data to my rolling window, or I am making some other mistake I am not able to recognize. Can someone with experience using a rolling window of indicator values for futures trading take a look at this, as always it is much appreciated.
class BBConsolidatorTest(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2020, 5, 1)
self.SetEndDate(2020, 6, 4)
self.SetCash(100000)
future = self.AddFuture(Futures.Indices.SP500EMini, Resolution.Minute)
future.SetFilter(lambda x: x.FrontMonth().OnlyApplyFilterAtMarketOpen())
self.symbol = None
self.BBIndicator = BollingerBands(20, 2, MovingAverageType.Simple)
self.BBUpperWindow = RollingWindow[float](4)
self.BBLowerWindow = RollingWindow[float](4)
self.RSIIndicator = RelativeStrengthIndex(10, MovingAverageType.Exponential)
self.RSIWindow = RollingWindow[float](4)
self.curr_contract = None
self.SetWarmUp(timedelta(days=30))
def OnData(self, slice):
for i, chain in enumerate(slice.FutureChains):
contracts = [contract for contract in chain.Value]
if len(contracts) > 0:
contract = contracts[0]
if self.curr_contract is not None and self.curr_contract != contract:
self.curr_contract = contracts[0]
self.BBIndicator.Reset()
self.BBUpperWindow.Reset()
self.BBLowerWindow.Reset()
self.RSIIndicator.Reset()
self.BBIndicator.Update(slice.Time, contract.LastPrice)
if self.BBIndicator.IsReady:
self.BBUpperWindow.Add(self.BBIndicator.UpperBand.Current.Value)
self.BBLowerWindow.Add(self.BBIndicator.LowerBand.Current.Value)
if self.RSIIndicator.IsReady:
self.RSIWindow.Add(self.RSIIndicator.Current.Value)
if self.symbol is not None:
price = self.Securities[self.symbol].Price
if not self.Portfolio.Invested:
if price < self.BBLowerWindow[0]:
if self.RSIWindow[0] > self.RSIWindow[1]:
self.MarketOrder(self.curr_contract, 1)
def OnSecuritiesChanged(self, changes):
if len(changes.RemovedSecurities) > 0:
if self.symbol is not None and self.consolidator is not None:
self.BBIndicator.Reset()
self.RSIIndicator.Reset()
self.symbol = changes.AddedSecurities[0].Symbol
Louis Szeto
Hi AegeanFutures,
In the above algorithm, the indicators need to be updated manually as we are using an indicator constructor to create the indicators(RelativeStrengthIndex ). Thus the indicator is never ready and the windows will not be updated. To resolve this, we recommend using the indicator helper function to create the indicators(RSI, BB) or we can also update each indicator manually.
Please check out the docs, under the Indicators section, for details.
Best,
Louis
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.
AegeanFutures
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!