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
5.406
Tracking Error
0.137
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
Lowest Capacity Asset
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Common")

from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
from QuantConnect.Data.Market import TradeBar

### <summary>
### Using rolling windows for efficient storage of historical data; which automatically clears after a period of time.
### </summary>
### <meta name="tag" content="using data" />
### <meta name="tag" content="history and warm up" />
### <meta name="tag" content="history" />
### <meta name="tag" content="warm up" />
### <meta name="tag" content="indicators" />
### <meta name="tag" content="rolling windows" />
class RollingWindowAlgorithm(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(2021,11,20)  #Set Start Date
        self.SetEndDate(datetime.now())  #Set End Date
        self.SetCash(25000)           #Set Strategy Cash
        # Find more symbols here: http://quantconnect.com/data
        self.AddEquity("QQQ", Resolution.Minute)
    
    
        # Creates a Rolling Window indicator to keep the 2 TradeBar
        self.windowqqq = RollingWindow[TradeBar](2)    # For other security types, use QuoteBar
    
        self.qqqrsi = RelativeStrengthIndex("QQQ", 14)
        self.qqqMACD = self.MACD("QQQ", 12, 26, 9, MovingAverageType.Exponential)
        
        #add tqqqrsisma indicator in a rolling window
        
        self.qqqrsisma = IndicatorExtensions.SMA(self.qqqrsi, 14) 
        self.qqqrsisma.Updated += self.qqqrsismaUpdated
        self.qqqrsismarw = RollingWindow[IndicatorDataPoint](14)
        
        
        # create the 5-minutes data consolidator
        fiveMinuteConsolidator = TradeBarConsolidator(timedelta(minutes=5))
        fiveMinuteConsolidator.DataConsolidated += self.fiveMinuteBarHandler
        
        
        # register the 30-minute consolidated bar data to automatically update the indicator
        self.RegisterIndicator("QQQ", self.qqqrsi, fiveMinuteConsolidator)
        self.RegisterIndicator("QQQ", self.qqqMACD, fiveMinuteConsolidator)

        self.SubscriptionManager.AddConsolidator("QQQ", fiveMinuteConsolidator)
        
        
        #Sets the benchmark, brokerage model and warm up time
        self.SetBenchmark("QQQ")
        self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage)
        self.SetWarmUp(500)


    def qqqrsismaUpdated(self, sender, updated):
        '''Adds updated values to rolling window'''
        self.qqqrsismarw.Add(updated)


    def OnData(self, data):
        pass
        
        
    def fiveMinuteBarHandler(self, sender, consolidated):
        # Wait for windows to be ready.
        if not (self.qqqrsismarw.IsReady): return
        if not self.qqqMACD.IsReady: return
        
        currqqqrsisma = self.qqqrsismarw[0]  # Current SMA had index zero.
        pastqqqrsisma = self.qqqrsismarw[1]  # Previous sma
        qqqrsislope = currqqqrsisma.Value - pastqqqrsisma.Value
        
        qqqMACDline = self.qqqMACD.Current.Value
        qqqsignalline = self.qqqMACD.Signal.Current.Value
        qqqMACDHistgram = self.qqqMACD.Histogram.Current.Value
        
        self.Log("qqqrsislope: {0}, Histogram {1}, MACD {2}, Signal {3}".format(qqqrsislope, qqqMACDHistgram, qqqMACDline, qqqsignalline))