Overall Statistics
from QuantConnect.Data.Market import TradeBar

class RollingWindowAlgorithm(QCAlgorithm):
    '''Basic template algorithm simply initializes the date range and cash'''

    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(2013,10,1)   # Set Start Date
        self.SetEndDate(2013,11,1)     # Set End Date
        self.SetCash(100000)           # Set Strategy Cash
        self.AddEquity("SPY", Resolution.Daily)

		# Creates a Rolling Window indicator to keep the 2 TradeBar
        self.window = RollingWindow[TradeBar](2)


    def OnData(self, data):
        '''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.'''
        
        # Add SPY TradeBar in rollling window
        self.window.Add(data["SPY"])

        # Wait for window to be ready: needs two additions
        if not self.window.IsReady: return

        currBar = self.window[0]   # Current bar has index zero.
        pastBar = self.window[1]   # Past bar has index one.

        # Remember: avoid logging prices
        self.Log("{0} -> {1} ... {2} -> {3}".format(pastBar.Time, pastBar.Close, currBar.Time, currBar.Close))

        if not self.Portfolio.Invested and currBar.Close < pastBar.Close:
            self.SetHoldings("SPY", 1)