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
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
0
Tracking Error
0
Treynor Ratio
0
Total Fees
$0.00
class RollingWindowsMultipleSymbols(QCAlgorithm):

    def Initialize(self):
        # Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.

        self.SetStartDate(2019, 6, 1)  # Set Start Date
        self.SetEndDate(2019, 7, 20) # Set End Date
        self.SetCash(100000)  # Set Strategy Cash
        
        # Holds all of our data keyed by each symbol
        self.Data = {}
        
        # This is the number of consolidated bars we'll hold in symbol data
        self.RollingWindowSize = 5
        # This is the period of our sma indicators
        SimpleMovingAveragePeriod = 5
        # This is the period of bars we'll be creating
        BarPeriod = TimeSpan.FromDays(5)

        # Contains all of equity symbols
        Symbols = ["SPY", "AAPL", "IBM"]
        # initialize our equity data
        for symbol in Symbols:
            equity = self.AddEquity(symbol, Resolution.Daily)
            self.Data[symbol] = SymbolData(equity.Symbol, BarPeriod, self.RollingWindowSize)
        
        # loop through all our symbols and request data subscriptions and initialize indicator
        for symbol, symbolData in self.Data.items():
            # define the indicator
            symbolData.SMA = SimpleMovingAverage(self.CreateIndicatorName(symbol, "SMA" + str(SimpleMovingAveragePeriod), Resolution.Daily), SimpleMovingAveragePeriod)
            # define a consolidator to consolidate data for this symbol on the requested period
            consolidator = TradeBarConsolidator(BarPeriod) if symbolData.Symbol.SecurityType == SecurityType.Equity else QuoteBarConsolidator(BarPeriod)
            # write up our consolidator to update the indicator
            consolidator.DataConsolidated += self.OnDataConsolidated
            # we need to add this consolidator so it gets auto updates
            self.SubscriptionManager.AddConsolidator(symbolData.Symbol, consolidator)
            
    
    def OnDataConsolidated(self, sender, bar):
        
        self.Data[bar.Symbol.Value].SMA.Update(bar.Time, bar.Close)
        self.Data[bar.Symbol.Value].Bars.Add(bar)

    def OnData(self, data):
        '''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
            Arguments:
                data: Slice object keyed by symbol containing the stock data
        '''
        
        # access rolling windows of multiple symbols
        for symbol in self.Data.keys():
            symbolData = self.Data[symbol]
            if symbolData.IsReady():
                symbolData.smaWindow.Add(symbolData.SMA.Current.Value)
                if symbolData.smaWindow.Count == self.RollingWindowSize:
                    window_list = [i for i in symbolData.smaWindow]
                    self.Debug("sma Window of {0} is {1}".format(str(symbol), str(window_list)))
        
        
class SymbolData(object):
    
    def __init__(self, symbol, barPeriod, windowSize):
        self.Symbol = symbol
        self.BarPeriod = barPeriod
        self.Bars = RollingWindow[IBaseDataBar](windowSize)
        self.SMA = None
        self.smaWindow = RollingWindow[float](windowSize)
    
    # Returns true if all the data in this instance is ready (indicators, rolling windows, ect...)
    def IsReady(self):
        return self.Bars.IsReady and self.SMA.IsReady