I am having trouble implementing my code. Can someone help me fix the code to make it work. Thank you so much for the help! I would attach the backtest but it is not even backtesting properly, sorry I am not very good at python. I attempted the bootcamp for quantconnect but it is approaching the problem from a different way then I wanted. Please explain if what I am doing is wrong.

  • It needs to have stacked EMAs 8, 21, 34, 55 on the 4 hour and 1 hour time frame to accept the trade. 
  • I also want to plot 2 charts, one for the EMAs to be stacked on the 1 hour and another chart for the 4 hour.

# region imports
from AlgorithmImports import *
# endregion

class AlertFluorescentPinkRhinoceros(QCAlgorithm):
    #Establish start date of backtest, cash amount for back testing, and time frames for data
    def Initialize(self):
        self.SetStartDate(2021, 5, 11)  # Set Start Date
        self.SetCash(10000)  # Set Strategy Cash
        self.ticker = "SPY" #Select Ticker
        #Stacked EMA lengths
        EMA_1 = 8
        EMA_2 = 21
        EMA_3 = 34
        EMA_4 = 55
        #Pull timeframe data
        self.ticker1H = self.AddEquity(self.ticker, Resolution.Hour).Symbol
        self.ticker4H = self.AddEquity(self.ticker, TimeSpan.FromMinutes(240)).Symbol
        #self.ticker5M = self.AddEquity(self.ticker, TimeSpan.FromMinutes(5)).Symbol
        
        #Check that the 1Hr 8EMA, 21EMA, 34EMA, and 55EMA are all stacked 
        self.ema1H_1 = self.EMA(self.ticker1H, EMA_1, Resolution.Hour)
        self.ema1H_2 = self.EMA(self.ticker1H, EMA_2, Resolution.Hour)
        self.ema1H_3 = self.EMA(self.ticker1H, EMA_3, Resolution.Hour)
        self.ema1H_4 = self.EMA(self.ticker1H, EMA_4, Resolution.Hour)

        #Check that the 4Hr 8EMA, 21EMA, 34EMA, and 55EMA are all stacked
        self.ema4H_1 = self.EMA(self.ticker4H, EMA_1, TimeSpan.FromMinutes(240))
        self.ema4H_2 = self.EMA(self.ticker4H, EMA_2, TimeSpan.FromMinutes(240))
        self.ema4H_3 = self.EMA(self.ticker4H, EMA_3, TimeSpan.FromMinutes(240))
        self.ema4H_4 = self.EMA(self.ticker4H, EMA_4, TimeSpan.FromMinutes(240))

        #Pump data prior to start date timing
        self.SetWarmUp(EMA_4, Resolution.Hour)
        self.SetWarmUp(EMA_4, TImeSpan.FromMinutes(240))
        #self.SetWarmUp(EMA_4, TimeSpan.FromMinutes(5))

    def OnData(self, data):
        
        #Plot the 1Hr indicators            
        #self.Plot("EMA_1", "Value" self.ema1H_1.Current.Value)
        #self.Plot("EMA_2", "Value" self.ema1H_2.Current.Value)
        #self.Plot("EMA_3", "Value" self.ema1H_3.Current.Value)
        #self.Plot("EMA_4", "Value" self.ema1H_4.Current.Value)

        if self.IsWarmingUp:
            return

        if ((self.ema1H_1.Current.Value > self.ema1H_2.Current.Value) and \
            (self.ema1H_2.Current.Value > self.ema1H_3.Current.Value) and \
            (self.ema1H_3.Current.Value > self.ema1H_4.Current.Value)) and \
            ((self.ema4H_1.Current.Value > self.ema4H_2.Current.Value) and \
            (self.ema4H_2.Current.Value > self.ema4H_3.Current.Value) and \
            (self.ema4H_3.Current.Value > self.ema4H_4.Current.Value)):
                if not self.Portfolio(self.ticker1H).IsLong:
                    self.SetHoldings(self.ticker1H, 1)
                #if not self.Portfolio[self.ticker5M].IsLong:
                    #self.SetHoldings(self.ticker5M, 1)
        else:
            self.Liquidate()