Hi I am trying to create an EMA Cross over strategy where it buys when the 9 crosses the 55 to the upside. closes position when 9 crosses the 13 to the downside and is able to reopen the position if the 9-13-21 all cross back to the upside before it crosses the 55 to the downside. vice versa on the short side

I am struggling with my coding of this I can get the code to function when its just a simple cross over strategy but adding in this additional layer of complexity is creating my bot to not place orders

 


#Parameters
CRYPTO = "BTCUSDT"; EMA_S = 55; EMA_C1 = 9; EMA_C2 = 13; EMA_F =  21;

class MovingAverageCrossAlgorithm(QCAlgorithm):

    def Initialize(self):

        self.SetStartDate(2021, 6, 1)   
        self.SetEndDate(2022, 1, 1) 
        self.SetCash("USDT", 100000)
        self.SetBrokerageModel(BrokerageName.Binance, AccountType.Margin)
        self.crypto = self.AddCrypto(CRYPTO, Resolution.Hour).Symbol
       
        self.sell_Ticket = False
        self.buy_Ticket = False
       
        self.position_close_long= False
        self.position_close_short = False
        
        
        self.close_one = self.EMA(self.crypto, EMA_C1, Resolution.Hour)
        self.close_two = self.EMA(self.crypto, EMA_C2, Resolution.Hour)
        self.fast = self.EMA(self.crypto, EMA_F, Resolution.Hour)
        self.slow = self.EMA(self.crypto, EMA_S, Resolution.Hour)
        
        
       
        #warmup
        self.SetWarmUp(5*EMA_S, Resolution.Minute)
        

    def OnData(self, data):
        if self.IsWarmingUp:
            return
        if not self.slow.IsReady:
            return
        
        
        self.Plot(self.crypto, "Price", self.Securities[self.crypto].Price)
        self.Plot(self.crypto, "ema_22", self.fast.Current.Value )
        self.Plot(self.crypto, "ema_55", self.slow.Current.Value )
        self.Plot(self.crypto, "ema_9", self.close_one.Current.Value )
        self.Plot(self.crypto, "ema_13", self.close_two.Current.Value )
        
        #open initial long when ema ribbon crosses to the upside
        if not self.Portfolio[self.crypto].IsLong: 
            if not self.sell_Ticket:
                if not self.position_close_long:
                    if self.close_one.Current.Value > self.close_two.Current.Value > self.fast.Current.Value > self.slow.Current.Value:
                        self.SetHoldings(self.crypto, 0.95)
                        self.sell_Ticket = True
        
            
        #close long when the two fastest emas cross, create a boolan to keep track
        if self.Portfolio[self.crypto].IsLong:    
            if self.close_one.Current.Value < self.close_two.Current.Value:
                self.Liquidate(self.crypto)
                self.position_close_long = True
           
        #reopen position if it was closed and it was a fake out        
        if not self.Portfolio[self.crypto].IsLong:
            if self.sell_Ticket and self.position_close_long:
                if self.fast.Current.Value < self.close_two.Current.Value < self.close_one.Current.Value :
                    self.SetHoldings(self.crypto, 0.95)
                    self.position_close_long = False
               
        #reset all boolans when ribbon flips
        if not self.Portfolio[self.crypto].IsLong:
            if self.position_close_long:
                if self.fast.Current.Value < self.slow.Current.Value:
                    self.sell_ticket = False
                    self.position_close_long = False
                
        
        if not self.Portfolio[self.crypto].IsShort:
            if not self.buy_Ticket:
                if not self.position_close_short:
                    if self.close_one.Current.Value < self.close_two.Current.Value < self.fast.Current.Value < self.slow.Current.Value:
                        self.SetHoldings(self.crypto, -0.95)
                        self.buy_Ticket = True
           
                
        if self.Portfolio[self.crypto].IsShort:    
            if self.close_two.Current.Value > self.close_one.Current.Value:
                self.Liquidate(self.crypto)
                self.position_close_short = True
            
        
        if not self.Portfolio[self.crypto].IsShort:
            if self.buy_Ticket and self.position_close_short:
                if self.fast.Current.Value > self.close_two.Current.Value > self.close_one.Current.Value :
                    self.SetHoldings(self.crypto, -0.95)
                    self.position_close_short = False
                    
        if not self.Portfolio[self.crypto].IsShort:
            if self.buy_Ticket and self.position_close_short:
                if self.slow.Current.Value < self.fast.Current.Value:
                    self.position_close_short = False
                    self.buy_ticket = False