Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Probabilistic Sharpe Ratio
0%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
-7.704
Tracking Error
0.165
Treynor Ratio
0
Total Fees
$0.00
class BBConsolidatorTest(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2020, 5, 1) 
        self.SetEndDate(2020, 6, 4)
        self.SetCash(1000000) 

        future = self.AddFuture(Futures.Indices.SP500EMini, Resolution.Minute)
        future.SetFilter(lambda x: x.FrontMonth().OnlyApplyFilterAtMarketOpen())

        self.symbol = None
        self.consolidator = None

        self.BBIndicator = BollingerBands(12, 2, MovingAverageType.Simple)
        self.BBUpperWindow = RollingWindow[float](4)
        self.BBLowerWindow = RollingWindow[float](4)
        
        self.curr_contract = None

    def OnDataConsolidated(self, sender, quoteBar):
        if self.BBUpperWindow.IsReady:
            self.Plot("BB", "UpperBand", self.BBUpperWindow[0])
            self.Plot("BB", "LowerBand", self.BBLowerWindow[0])

    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.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)
        return

    def OnSecuritiesChanged(self, changes):
        if len(changes.RemovedSecurities) > 0:
            if self.symbol is not None and self.consolidator is not None:
                self.SubscriptionManager.RemoveConsolidator(self.symbol, self.consolidator)
                self.BBIndicator.Reset()

        self.symbol = changes.AddedSecurities[0].Symbol

        # Create a new consolidator and register the indicators to it
        self.consolidator = QuoteBarConsolidator(900)
        self.consolidator.DataConsolidated += self.OnDataConsolidated
        self.SubscriptionManager.AddConsolidator(self.symbol, self.consolidator)