Overall Statistics
from QuantConnect import *

class MultidimensionalOptimizedFlange(QCAlgorithm):

    def Initialize(self):
        # Set Start Date
        self.SetStartDate(2016, 6, 14)  
        self.SetEndDate(2016, 8, 14)
        # Set Strategy Cash
        self.SetCash(100000)  
        # Set Traded Equity
        self.spy = self.AddEquity("SPY", Resolution.Minute) 
        
        #Add Simple Moving Average and Bolinger Bands Indicators
        self.sma = self.SMA("SPY", 20, Resolution.Minute)
        self.bb = self.BB("SPY", 40, 2.0, MovingAverageType.Simple)
        
    def OnData(self, data):
        #Sets variable for strategy
        i = 0
        
        #Gets current Market Price
        self.currpri = self.Securities["SPY"].Close
        
        #Checks if BB Indicator is ready
        if not self.bb.IsReady:
            return
        
        #Calculates BandWidth of BB Indicator
        self.BandWidth = ((self.bb.UpperBand.Current.Value-(self.bb.LowerBand.Current.Value))/(self.bb.MiddleBand.Current.Value))
            
        #Check if SMA Indicator is ready
        if not self.sma.IsReady:
            return
        
        self.Debug(f"MiddleBand={self.bb.MiddleBand.Current.Value} LowerBand={self.bb.LowerBand.Current.Value} UpperBand={self.bb.UpperBand.Current.Value} BandWidth={self.BandWidth} SMA={self.sma.Current.Value} SPY={self.spy.Price}")
        
        #Test if can trade
        if self.currpri > self.sma.Current.Value and self.BandWidth > 2:
            self.SetHoldings("SPY", 0.5)
                
        if self.currpri < self.sma.Current.Value and self.BandWidth > 2:
            self.SetHoldings("SPY", -0.5)