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
0
Tracking Error
0
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
Lowest Capacity Asset
Portfolio Turnover
0%
from AlgorithmImports import *
import config
from create_lists import *

class BEAT_THE_MARKET_LEV():

    def __init__(self, algorithm, symbols):
        self.algorithm = algorithm
        self.symbols = symbols

    def BEAT_THE_MARKET(self, beat_the_market_outer_allocation):
        if self.symbols["SPY"].SMA_21.Current_Value > self.symbols["SPY"].SMA_210.Current_Value:
            symbols = [ self.symbols["SOXX"],
                        self.symbols["QQQ"], 
                        self.symbols["XLK"],
                        self.symbols["FNGS"],
                        ]
            Create_Return_MA_List(self, symbols, Return_MA_4, False, 1, 1, 1)

        else:
            if self.symbols["QQQ"].Rsi_10.Current_Value < 30:
                symbols = [ self.symbols["SOXL"],
                            self.symbols["HIBL"], 
                            self.symbols["FNGU"],
                            self.symbols["ARKK"],
                            self.symbols["GUSH"], 
                            self.symbols["DRN"],
                            ]
                Create_Return_MA_List(self, symbols, Return_MA_4, False, 1, 1, 1)

            else:
                if self.algorithm.Securities["SPY"].Price > self.symbols["SPY"].SMA_31.Current_Value:
                    symbols = [ self.symbols["SOXX"],
                                self.symbols["QQQ"], 
                                self.symbols["IEF"],
                                ]
                    Create_Return_MA_List(self, symbols, Return_MA_4, False, 1, 1, 1)

                else:
                    symbols = [ self.symbols["TBF"],
                                self.symbols["TBX"], 
                                self.symbols["USDU"],
                                ]
                    Create_Return_MA_List(self, symbols, Return_MA_4, False, 1, 1, 1)
from AlgorithmImports import *
import config

class BEAT_THE_MARKET_LEV():

    def __init__(self, algorithm, symbols):
        self.algorithm = algorithm
        self.symbols = symbols

    def BEAT_THE_MARKET(self, beat_the_market_outer_allocation):
        if self.symbols["SPY"].SMA_21.Current_Value > self.symbols["SPY"].SMA_210.Current_Value:
            ma_retrun_list = [(self.symbols["SOXX"], self.symbols["SOXX"].Return_MA_4.Current_Value), 
                        (self.symbols["QQQ"], self.symbols["QQQ"].Return_MA_4.Current_Value), 
                        (self.symbols["XLK"], self.symbols["XLK"].Return_MA_4.Current_Value),
                        (self.symbols["FNGS"], self.symbols["FNGS"].Return_MA_4.Current_Value),
                        ]
            
            sorted_ma_retrun_list = sorted(ma_retrun_list, key=lambda x: x[1], reverse=False)
            counter_ = 0
            for symbol, symbol_return in sorted_ma_retrun_list:
                if counter_ >= 2:
                    break
                symbol.Counter += (beat_the_market_outer_allocation/2) - (config.CASH_BUFFER * (beat_the_market_outer_allocation/2))
                symbol.Function = (f'beat_the_market_outer_allocation-1 = SOXX = {round(self.symbols["SOXX"].Return_MA_4.Current_Value,4)},  QQQ = {round(self.symbols["QQQ"].Return_MA_4.Current_Value, 4)}, XLK = {round(self.symbols["XLK"].Return_MA_4.Current_Value ,4)}, FNGS =  {round(self.symbols["FNGS"].Return_MA_4.Current_Value,4)}')
                counter_ += 1

        else:
            if self.symbols["QQQ"].Rsi_10.Current_Value < 30:
                ma_retrun_list = [(self.symbols["SOXL"], self.symbols["SOXL"].Return_MA_4.Current_Value), 
                            (self.symbols["HIBL"], self.symbols["HIBL"].Return_MA_4.Current_Value), 
                            (self.symbols["FNGU"], self.symbols["FNGU"].Return_MA_4.Current_Value),
                            (self.symbols["ARKK"], self.symbols["ARKK"].Return_MA_4.Current_Value),
                            (self.symbols["GUSH"], self.symbols["GUSH"].Return_MA_4.Current_Value),
                            (self.symbols["DRN"], self.symbols["DRN"].Return_MA_4.Current_Value),
                            ]
                
                sorted_ma_retrun_list = sorted(ma_retrun_list, key=lambda x: x[1], reverse=False)
                counter_ = 0
                for symbol, symbol_return in sorted_ma_retrun_list:
                    if counter_ >= 2:
                        break
                    symbol.Counter += (beat_the_market_outer_allocation/2) - (config.CASH_BUFFER * (beat_the_market_outer_allocation/2))
                    symbol.Function = (f'beat_the_market_outer_allocation-2 = SOXL = {round(self.symbols["SOXL"].Return_MA_4.Current_Value,4)}, HIBL = {round(self.symbols["HIBL"].Return_MA_4.Current_Value, 4)}, FNGU = {round(self.symbols["FNGU"].Return_MA_4.Current_Value,4)}, ARKK =  {round(self.symbols["ARKK"].Return_MA_4.Current_Value,4)}, GUSH =  {round(self.symbols["GUSH"].Return_MA_4.Current_Value,4)}, DRN =  {round(self.symbols["DRN"].Return_MA_4.Current_Value,4)}')
                    
                    counter_ += 1
            else:
                if self.algorithm.Securities["SPY"].Price > self.symbols["SPY"].SMA_31.Current_Value:
                        ma_retrun_list = [(self.symbols["SOXX"], self.symbols["SOXX"].Return_MA_4.Current_Value), 
                                    (self.symbols["QQQ"], self.symbols["QQQ"].Return_MA_4.Current_Value), 
                                    (self.symbols["IEF"], self.symbols["IEF"].Return_MA_4.Current_Value),
                                    ]
                        
                        sorted_ma_retrun_list = sorted(ma_retrun_list, key=lambda x: x[1], reverse=False)
                        counter_ = 0
                        for symbol, symbol_return in sorted_ma_retrun_list:
                            if counter_ >= 2:
                                break
                            symbol.Counter += (beat_the_market_outer_allocation/2) - (config.CASH_BUFFER * (beat_the_market_outer_allocation/2))
                            symbol.Function = (f'beat_the_market_outer_allocation-3 = SOXX = {round(self.symbols["SOXX"].Return_MA_4.Current_Value,4)}, QQQ = {round(self.symbols["QQQ"].Return_MA_4.Current_Value, 4)}, IEF = {round(self.symbols["IEF"].Return_MA_4.Current_Value , 4)}')
                            counter_ += 1
                else:
                    ma_retrun_list = [(self.symbols["TBF"], self.symbols["TBF"].Return_MA_4.Current_Value), 
                            (self.symbols["TBX"], self.symbols["TBX"].Return_MA_4.Current_Value), 
                            (self.symbols["USDU"], self.symbols["USDU"].Return_MA_4.Current_Value),
                            ]
                
                    sorted_ma_retrun_list = sorted(ma_retrun_list, key=lambda x: x[1], reverse=False)
                    counter_ = 0
                    for symbol, symbol_return in sorted_ma_retrun_list:
                        if counter_ >= 2:
                            break
                        symbol.Counter += (beat_the_market_outer_allocation/2) - (config.CASH_BUFFER * (beat_the_market_outer_allocation/2))
                        symbol.Function = (f'beat_the_market_outer_allocation-4 = TBF = {round(self.symbols["TBF"].Return_MA_4.Current_Value,4)}, TBX = {round(self.symbols["TBX"].Return_MA_4.Current_Value, 4)}, USDU = {round(self.symbols["USDU"].Return_MA_4.Current_Value, 4)}')
                        counter_ += 1
from AlgorithmImports import *
import config


def Create_Return_List(self, symbols, indicator, sorting_order, threshold, inner_allocation, outer_allocation):
    return_list = []


    for symbol in symbols:
        return_list.append((symbol, getattr(symbol,indicator).Current_Value))


    sorted_return_list = sorted(return_list, key=lambda x: x[1], reverse=sorting_order)
    

    counter_ = 0
    for symbol, symbol_return in sorted_return_list:
        if counter_ >= 1:      
            break
        symbol.Counter +=  inner_allocation*outer_allocation - (config.CASH_BUFFER * 1)
        counter_ += 1



def Create_Return_MA_List(self, symbols, indicator, sorting_order, threshold, inner_allocation, outer_allocation):
    return_ma_list = []


    for symbol in symbols:
        return_ma_list.append((symbol, getattr(symbol,indicator).Current_Value))


    sorted_return_ma_list = sorted(return_ma_list, key=lambda x: x[1], reverse=sorting_order)
    

    counter_ = 0
    for symbol, symbol_return_ma in sorted_return_ma_list:
        if counter_ >= 1:      
            break
        symbol.Counter += inner_allocation*outer_allocation - (config.CASH_BUFFER * 1)
        counter_ += 1



def Create_RSI_List(self, symbols, indicator, sorting_order, threshold, inner_allocation, outer_allocation):
    rsi_list = []


    for symbol in symbols:
        rsi_list.append((symbol, getattr(symbol,indicator).Current_Value))


    sorted_rsi_list = sorted(rsi_list, key=lambda x: x[1], reverse=sorting_order)
    

    counter_ = 0
    for symbol, symbol_rsi in sorted_rsi_list:
        if counter_ >= 1:      
            break
        symbol.Counter +=  inner_allocation*outer_allocation - (config.CASH_BUFFER * 1)
        counter_ += 1
    


from AlgorithmImports import *
import config

class TQQQForTheLongTerm():



    def __init__(self, algorithm, symbols):
        self.algorithm = algorithm
        self.symbols = symbols
    

    


    def TQQQFTLT(self,TQQQFTLT_outer_allocation):
        if self.algorithm.Securities["SPY"].Price > self.symbols["SPY"].SMA_200.Current_Value:
            if self.symbols["TQQQ"].Rsi_14.Current_Value > 75:
                #self.Debug(f"TQQQ.Rsi_14 {self.TQQQ.Rsi_14.Current_Value}")
                self.symbols["UVXY"].Counter += TQQQFTLT_outer_allocation - (config.CASH_BUFFER * TQQQFTLT_outer_allocation)
                #self.UVXY.Function = "Main_Strategy TQQQ.Rsi_14 > 75"
                self.algorithm.Debug(TQQQFTLT_outer_allocation)
                self.algorithm.Debug(self.algorithm.Time)

            elif self.symbols["SPXL"].Rsi_10.Current_Value > 80:     
                #self.Debug(f"SPXL.Rsi_10 {self.SPXL.Rsi_10.Current_Value}")       
                self.symbols["UVXY"].Counter += TQQQFTLT_outer_allocation - (config.CASH_BUFFER * TQQQFTLT_outer_allocation)
                self.algorithm.Debug(TQQQFTLT_outer_allocation)
                self.algorithm.Debug(self.algorithm.Time)
                #self.UVXY.Function = "Main_Strategy SPXL.Rsi_10 > 80"
            else:
                self.a_better_buy_nasdaq(TQQQFTLT_outer_allocation)
        else:
           self.sideways_market_mod(TQQQFTLT_outer_allocation)


    def sideways_market_mod(self, TQQQFTLT_outer_allocation):
        # IF NO, use Sideways Market Mod Below the SPY 200d SMA | FINAL | DereckN
            if self.symbols["TQQQ"].Rsi_10.Current_Value < 31:
                #self.Debug(f"TQQQ.Rsi_10 {self.TQQQ.Rsi_10.Current_Value}")   
                self.symbols["TECL"].Counter += TQQQFTLT_outer_allocation - (config.CASH_BUFFER * TQQQFTLT_outer_allocation)
                self.algorithm.Debug(TQQQFTLT_outer_allocation)
                self.algorithm.Debug(self.algorithm.Time)
            elif self.symbols["SMH"].Rsi_10.Current_Value < 30:   
                #self.Debug(f"SMH.Rsi_10 {self.SMH.Rsi_10.Current_Value}")  
                self.symbols["SOXL"].Counter += TQQQFTLT_outer_allocation - (config.CASH_BUFFER * TQQQFTLT_outer_allocation)
                self.algorithm.Debug(TQQQFTLT_outer_allocation)
                self.algorithm.Debug(self.algorithm.Time)
            elif self.symbols["DIA"].Rsi_10.Current_Value < 27:
                #self.Debug(f"DIA.Rsi_10 {self.DIA.Rsi_10.Current_Value}")
                self.symbols["UDOW"].Counter += TQQQFTLT_outer_allocation - (config.CASH_BUFFER * TQQQFTLT_outer_allocation)
                self.algorithm.Debug(TQQQFTLT_outer_allocation)
                self.algorithm.Debug(self.algorithm.Time)
            elif self.symbols["SPY"].Rsi_14.Current_Value < 28:
                self.symbols["UPRO"].Counter += TQQQFTLT_outer_allocation - (config.CASH_BUFFER * TQQQFTLT_outer_allocation)
                self.algorithm.Debug(TQQQFTLT_outer_allocation)
                self.algorithm.Debug(self.algorithm.Time)
            # IF NO : Weight these groups equally : Sub-strategy 1 & 4 (50%/50%)
            else:
                self.TQQQFTLT_Sub_Strategy_1(TQQQFTLT_outer_allocation*0.5) #50% each
                self.TQQQFTLT_Sub_Strategy_4(TQQQFTLT_outer_allocation*0.5) #50% each



    def a_better_buy_nasdaq(self, TQQQFTLT_outer_allocation):
        if self.symbols["QQQ"].Return_5.Current_Value < -0.06:
            if self.symbols["TQQQ"].Return_1.Current_Value > 0.05:
                #self.Debug(f"QQQ Return 5 {self.TQQQQ.Return_5.Current_Value}")
                #self.Debug(f"TQQQ Return 1 {self.TQQQQ.Return_1.Current_Value}")
                self.symbols["SQQQ"].Counter  += TQQQFTLT_outer_allocation - (config.CASH_BUFFER * TQQQFTLT_outer_allocation)
                self.algorithm.Debug(TQQQFTLT_outer_allocation)
                self.algorithm.Debug(self.algorithm.Time)
                #self.SQQQ.Function = "Main_Strategy QQQ.Return_5 < 6% & TQQQ.Return_1 > 5%"

            else:
                if self.symbols["TQQQ"].Rsi_10.Current_Value > 31:
                    self.symbols["SQQQ"].Counter += TQQQFTLT_outer_allocation - (config.CASH_BUFFER * TQQQFTLT_outer_allocation)
                    self.algorithm.Debug(TQQQFTLT_outer_allocation)
                    self.algorithm.Debug(self.algorithm.Time)
                else:
                    self.symbols["TQQQ"].Counter += TQQQFTLT_outer_allocation - (config.CASH_BUFFER * TQQQFTLT_outer_allocation)
                    self.algorithm.Debug(TQQQFTLT_outer_allocation)
                    self.algorithm.Debug(self.algorithm.Time)
        else:
            if self.symbols["QQQ"].Rsi_10.Current_Value > 80:
                self.symbols["SQQQ"].Counter += TQQQFTLT_outer_allocation - (config.CASH_BUFFER * TQQQFTLT_outer_allocation)
                self.algorithm.Debug(TQQQFTLT_outer_allocation)
                self.algorithm.Debug(self.algorithm.Time)
            else:
                if self.symbols["QQQ"].Rsi_10.Current_Value < 31:
                    self.symbols["TQQQ"].Counter += TQQQFTLT_outer_allocation - (config.CASH_BUFFER * TQQQFTLT_outer_allocation)
                    self.algorithm.Debug(TQQQFTLT_outer_allocation)
                    self.algorithm.Debug(self.algorithm.Time)
                else:
                    self.an_left(TQQQFTLT_outer_allocation)


    def an_left(self, TQQQFTLT_outer_allocation):
        return_list = [(self.symbols["TQQQ"], self.symbols["TQQQ"].Rsi_7.Current_Value), 
                        (self.symbols["TECL"], self.symbols["TECL"].Rsi_7.Current_Value), 
                        (self.symbols["ERX"], self.symbols["ERX"].Rsi_7.Current_Value),
                        (self.symbols["LABU"], self.symbols["LABU"].Rsi_7.Current_Value),
                        (self.symbols["UTSL"], self.symbols["UTSL"].Rsi_7.Current_Value),
                        (self.symbols["DRN"], self.symbols["DRN"].Rsi_7.Current_Value)
                        ]
                
                    

        sorted_return_list = sorted(return_list, key=lambda x: x[1], reverse=True)
        counter_ = 0
        for symbol, symbol_return in sorted_return_list:
            if counter_ >= 1:
                break
            symbol.Counter += TQQQFTLT_outer_allocation - (config.CASH_BUFFER * TQQQFTLT_outer_allocation)
            self.algorithm.Debug(TQQQFTLT_outer_allocation)
            self.algorithm.Debug(self.algorithm.Time)
            counter_ += 1     
           
    # For Sub-Strategy 1
    # Ask whether the QQQ 200-day cumulative return is below −20%
    def TQQQFTLT_Sub_Strategy_1(self, TQQQFTLT_outer_allocation):
        self.algorithm.Debug(TQQQFTLT_outer_allocation)
        if self.symbols["QQQ"].Return_200.Current_Value < -0.2:
            self.TQQQFTLT_Nasdaq_In_Crash_Territory(TQQQFTLT_outer_allocation)
        else:
            if self.algorithm.Securities["QQQ"].Price < self.symbols["QQQ"].SMA_20.Current_Value:
                if self.symbols["TLT"].Rsi_10.Current_Value > self.symbols["SQQQ"].Rsi_10.Current_Value:
                    self.symbols["TQQQ"].Counter += TQQQFTLT_outer_allocation - (config.CASH_BUFFER * TQQQFTLT_outer_allocation)
                    self.symbols["TQQQ"].Function = "TQQQFTLT_Sub_Strategy_1 TLT.Rsi_10 > SQQQ_Rsi_10"
                    self.algorithm.Debug(TQQQFTLT_outer_allocation)
                    self.algorithm.Debug(self.algorithm.Time)
                elif self.symbols["TLT"].Rsi_10.Current_Value <= self.symbols["SQQQ"].Rsi_10.Current_Value:
                    self.symbols["SQQQ"].Counter += TQQQFTLT_outer_allocation - (config.CASH_BUFFER * TQQQFTLT_outer_allocation)
                    self.symbols["SQQQ"].Function = "TQQQFTLT_Sub_Strategy_1 TLT_Rsi_10 < SQQQ_Rsi_10"
                    self.algorithm.Debug(TQQQFTLT_outer_allocation)
                    self.algorithm.Debug(self.algorithm.Time)
            else:
                if self.symbols["SQQQ"].Rsi_10.Current_Value < 31:
                    self.symbols["SQQQ"].Counter += TQQQFTLT_outer_allocation - (config.CASH_BUFFER * TQQQFTLT_outer_allocation)
                    self.symbols["SQQQ"].Function = "TQQQFTLT_Sub_Strategy_1 SQQQ_Rsi_10 < 31"
                    self.algorithm.Debug(TQQQFTLT_outer_allocation)
                    self.algorithm.Debug(self.algorithm.Time)
                elif self.symbols["QQQ"].Return_10.Current_Value >= 0.055:
                    self.symbols["SQQQ"].Counter += TQQQFTLT_outer_allocation - (config.CASH_BUFFER * TQQQFTLT_outer_allocation)
                    self.symbols["SQQQ"].Function = "TQQQFTLT_Sub_Strategy_1 QQQ_RETRUN < 5.5"
                    self.algorithm.Debug(TQQQFTLT_outer_allocation)
                    self.algorithm.Debug(self.algorithm.Time)
                else:
                    if self.symbols["TQQQ"].Rsi_10.Current_Value > self.symbols["SOXL"].Rsi_10.Current_Value:
                        self.symbols["TQQQ"].Counter += TQQQFTLT_outer_allocation - (config.CASH_BUFFER * TQQQFTLT_outer_allocation)
                        self.symbols["TQQQ"].Function = "TQQQFTLT_Sub_Strategy_1 TQQQ_Rsi_10 > SOXL_Rsi_10"
                        self.algorithm.Debug(TQQQFTLT_outer_allocation)
                        self.algorithm.Debug(self.algorithm.Time)
                    elif self.symbols["TQQQ"].Rsi_10.Current_Value <= self.symbols["SOXL"].Rsi_10.Current_Value:
                        self.symbols["SOXL"].Counter += TQQQFTLT_outer_allocation - (config.CASH_BUFFER * TQQQFTLT_outer_allocation)
                        self.symbols["SOXL"].Function = "TQQQFTLT_Sub_Strategy_1 TQQQ_Rsi_10 < SOXL_Rsi_10"
                        self.algorithm.Debug(TQQQFTLT_outer_allocation)
                        self.algorithm.Debug(self.algorithm.Time)
    

    def TQQQFTLT_Nasdaq_In_Crash_Territory(self, TQQQFTLT_outer_allocation):
        
        if self.algorithm.Securities["QQQ"].Price < self.symbols["QQQ"].SMA_20.Current_Value:
            if self.symbols["QQQ"].Return_60.Current_Value < -0.12:
                self.Sideways_Market_Deleverage_1(TQQQFTLT_outer_allocation) #50% each
            else:
                if self.symbols["TLT"].Rsi_10.Current_Value > self.symbols["SQQQ"].Rsi_10.Current_Value:
                    self.symbols["TQQQ"].Counter += TQQQFTLT_outer_allocation - (config.CASH_BUFFER * TQQQFTLT_outer_allocation)
                    self.symbols["TQQQ"].Function = "TQQQFTLT_Nasdaq_In_Crash_Territory TLT.Rsi_10 > SQQQ.Rsi_10"
                    self.algorithm.Debug(TQQQFTLT_outer_allocation)
                    self.algorithm.Debug(self.algorithm.Time)
                elif  self.symbols["TLT"].Rsi_10.Current_Value <= self.symbols["SQQQ"].Rsi_10.Current_Value:
                    self.symbols["SQQQ"].Counter += TQQQFTLT_outer_allocation - (config.CASH_BUFFER * TQQQFTLT_outer_allocation)
                    self.symbols["SQQQ"].Function = "TQQQFTLT_Nasdaq_In_Crash_Territory TLT.Rsi_10 <= SQQQ.Rsi_10"
                    self.algorithm.Debug(TQQQFTLT_outer_allocation)
                    self.algorithm.Debug(self.algorithm.Time)
        else:
            if self.symbols["SQQQ"].Rsi_10.Current_Value < 31:
                self.symbols["PSQ"].Counter += TQQQFTLT_outer_allocation - (config.CASH_BUFFER * TQQQFTLT_outer_allocation)
                self.symbols["PSQ"].Function = "TQQQFTLT_Nasdaq_In_Crash_Territory SQQQ.Rsi_10 < 31"
                self.algorithm.Debug(TQQQFTLT_outer_allocation)
                self.algorithm.Debug(self.algorithm.Time)
            elif self.symbols["QQQ"].Return_10.Current_Value >= 0.055:
                self.symbols["PSQ"].Counter += TQQQFTLT_outer_allocation - (config.CASH_BUFFER * TQQQFTLT_outer_allocation)
                self.symbols["PSQ"].Function = "TQQQFTLT_Nasdaq_In_Crash_Territory QQQ.Return_10_days > 5.5%"
                self.algorithm.Debug(TQQQFTLT_outer_allocation)
                self.algorithm.Debug(self.algorithm.Time)
            else:
                if self.symbols["QQQ"].Rsi_10.Current_Value > self.symbols["SMH"].Rsi_10.Current_Value:
                    self.symbols["QQQ"].Counter += TQQQFTLT_outer_allocation - (config.CASH_BUFFER * TQQQFTLT_outer_allocation)
                    self.symbols["QQQ"].Function = "TQQQFTLT_Nasdaq_In_Crash_Territory QQQ.Rsi_10 > SMH.Rsi_10"
                    self.algorithm.Debug(TQQQFTLT_outer_allocation)
                    self.algorithm.Debug(self.algorithm.Time)
                elif self.symbols["QQQ"].Rsi_10.Current_Value <= self.symbols["SMH"].Rsi_10.Current_Value:
                    self.symbols["SMH"].Counter += TQQQFTLT_outer_allocation - (config.CASH_BUFFER * TQQQFTLT_outer_allocation)
                    self.symbols["SMH"].Function = "TQQQFTLT_Nasdaq_In_Crash_Territory QQQ.Rsi_10 <= SMH.Rsi_10"
                    self.algorithm.Debug(TQQQFTLT_outer_allocation)
                    self.algorithm.Debug(self.algorithm.Time)


    def Sideways_Market_Deleverage_1(self, TQQQFTLT_outer_allocation):
        self.TQQQFTLT_Sub_Strategy_2(TQQQFTLT_outer_allocation*0.5) #50% each
        self.TQQQFTLT_Sub_Strategy_3(TQQQFTLT_outer_allocation*0.5) #50% each

    def Sideways_Market_Deleverage_2(self, TQQQFTLT_outer_allocation):
        self.TQQQFTLT_Sub_Strategy_2(TQQQFTLT_outer_allocation*0.5) #50% each
        self.TQQQFTLT_Sub_Strategy_3(TQQQFTLT_outer_allocation*0.5) #50% each        
    

    def TQQQFTLT_Sub_Strategy_4(self, TQQQFTLT_outer_allocation): #50% allocation
        if self.algorithm.Securities["QQQ"].Price < self.symbols["QQQ"].SMA_20.Current_Value:
            if self.symbols["QQQ"].Return_60.Current_Value < -0.12:
                self.Sideways_Market_Deleverage_2(TQQQFTLT_outer_allocation) #50% each
            else:
                if self.symbols["TLT"].Rsi_10.Current_Value > self.symbols["SQQQ"].Rsi_10.Current_Value:
                    # Comment - TECL Missing here, should be fine
                    self.symbols["TQQQ"].Counter += TQQQFTLT_outer_allocation - (config.CASH_BUFFER * TQQQFTLT_outer_allocation)
                    self.symbols["TQQQ"].Function = "TQQQFTLT_Sub_Strategy_4 TLT.Rsi_10 > SQQQ.Rsi_10"
                    self.algorithm.Debug(TQQQFTLT_outer_allocation)
                    self.algorithm.Debug(self.algorithm.Time)
                elif self.symbols["TLT"].Rsi_10.Current_Value <= self.symbols["SQQQ"].Rsi_10.Current_Value:
                    self.symbols["SQQQ"].Counter += TQQQFTLT_outer_allocation - (config.CASH_BUFFER * TQQQFTLT_outer_allocation)
                    self.symbols["SQQQ"].Function = "TQQQFTLT_Sub_Strategy_4 TLT.Rsi_10 <= SQQQ.Rsi_10"
                    self.algorithm.Debug(TQQQFTLT_outer_allocation)
                    self.algorithm.Debug(self.algorithm.Time)
        else:
            if self.symbols["SQQQ"].Rsi_10.Current_Value < 31:
                self.symbols["SQQQ"].Counter += TQQQFTLT_outer_allocation - (config.CASH_BUFFER * TQQQFTLT_outer_allocation)
                self.symbols["SQQQ"].Function = "TQQQFTLT_Sub_Strategy_4 SQQQ.Rsi_10 < 31"
                self.algorithm.Debug(TQQQFTLT_outer_allocation)
                self.algorithm.Debug(self.algorithm.Time)
            else:
                if self.symbols["QQQ"].Return_70.Current_Value < -0.15:
                    if self.symbols["TQQQ"].Rsi_10.Current_Value > self.symbols["SOXL"].Rsi_10.Current_Value:
                        self.symbols["TQQQ"].Counter += TQQQFTLT_outer_allocation - (config.CASH_BUFFER * TQQQFTLT_outer_allocation)
                        self.symbols["TQQQ"].Function = "TQQQFTLT_Sub_Strategy_4 TQQQ.Rsi_10 > SOXL.Rsi_10 & QQQ.Return_70_days < -15%"
                        self.algorithm.Debug(TQQQFTLT_outer_allocation)
                        self.algorithm.Debug(self.algorithm.Time)
                    elif self.symbols["TQQQ"].Rsi_10.Current_Value <= self.symbols["SOXL"].Rsi_10.Current_Value:
                        self.symbols["SOXL"].Counter += TQQQFTLT_outer_allocation - (config.CASH_BUFFER * TQQQFTLT_outer_allocation)
                        self.symbols["SOXL"].Function = "TQQQFTLT_Sub_Strategy_4 TQQQ.Rsi_10 <= SOXL.Rsi & QQQ.Return_70_days < -15%"
                        self.algorithm.Debug(TQQQFTLT_outer_allocation)
                        self.algorithm.Debug(self.algorithm.Time)
                else:
                    return_list = [(self.symbols["SPY"], self.symbols["SPY"].Return_15.Current_Value), 
                                (self.symbols["QQQ"], self.symbols["QQQ"].Return_15.Current_Value), 
                                (self.symbols["DIA"], self.symbols["DIA"].Return_15.Current_Value),
                                (self.symbols["XLP"], self.symbols["XLP"].Return_15.Current_Value),
                                ]
                    
                    sorted_return_list = sorted(return_list, key=lambda x: x[1], reverse=True)
                    counter_ = 0
                    for symbol, symbol_return in sorted_return_list:
                        if counter_ >= 2:
                            break
                        symbol.Counter += TQQQFTLT_outer_allocation*0.5 - (config.CASH_BUFFER * (TQQQFTLT_outer_allocation*0.5)) #25% each allocation
                        symbol.Function = "TQQQFTLT_Sub_Strategy_4_pick_2_1x_ETF's" 
                        counter_ += 1
                        self.algorithm.Debug(TQQQFTLT_outer_allocation)
                        self.algorithm.Debug(self.algorithm.Time)


    def TQQQFTLT_Sub_Strategy_2(self, TQQQFTLT_outer_allocation): #25% allocation
        if self.algorithm.Securities["SPY"].Price > self.symbols["SPY"].SMA_20.Current_Value:
            self.symbols["SPY"].Counter += TQQQFTLT_outer_allocation - (config.CASH_BUFFER * TQQQFTLT_outer_allocation)
            self.symbols["SPY"].Function = "TQQQFTLT_Sub_Strategy_2 SPY_Price > SPY.SMA_20"
            self.algorithm.Debug(TQQQFTLT_outer_allocation)
            self.algorithm.Debug(self.algorithm.Time)
        else:
            if self.symbols["TLT"].Rsi_10.Current_Value > self.symbols["SQQQ"].Rsi_10.Current_Value:
                self.symbols["QQQ"].Counter += TQQQFTLT_outer_allocation - (config.CASH_BUFFER * TQQQFTLT_outer_allocation)
                self.symbols["QQQ"].Function = "TQQQFTLT_Sub_Strategy_2 TLT.Rsi_10 > SQQQ.Rsi_10"
                self.algorithm.Debug(TQQQFTLT_outer_allocation)
                self.algorithm.Debug(self.algorithm.Time)
            elif self.symbols["TLT"].Rsi_10.Current_Value <= self.symbols["SQQQ"].Rsi_10.Current_Value:
                self.symbols["PSQ"].Counter += TQQQFTLT_outer_allocation - (config.CASH_BUFFER * TQQQFTLT_outer_allocation)
                self.symbols["PSQ"].Function = "TQQQFTLT_Sub_Strategy_2 TLT.Rsi_10 <= SQQQ.Rsi_10"
                self.algorithm.Debug(TQQQFTLT_outer_allocation)
                self.algorithm.Debug(self.algorithm.Time)
      
    def TQQQFTLT_Sub_Strategy_3(self, TQQQFTLT_outer_allocation): #25% allocation
        if self.symbols["TLT"].Rsi_10.Current_Value > self.symbols["SQQQ"].Rsi_10.Current_Value:
            self.symbols["QQQ"].Counter += TQQQFTLT_outer_allocation - (config.CASH_BUFFER * TQQQFTLT_outer_allocation)
            self.symbols["QQQ"].Function = "TQQQFTLT_Sub_Strategy_3 TLT.Rsi_10 > SQQQ.Rsi_10"
            self.algorithm.Debug(TQQQFTLT_outer_allocation)
            self.algorithm.Debug(self.algorithm.Time)
        elif self.symbols["TLT"].Rsi_10.Current_Value <= self.symbols["SQQQ"].Rsi_10.Current_Value:
            self.symbols["PSQ"].Counter += TQQQFTLT_outer_allocation - (config.CASH_BUFFER * TQQQFTLT_outer_allocation)
            self.symbols["PSQ"].Function = "TQQQFTLT_Sub_Strategy_3 TLT.Rsi_10 <= SQQQ.Rsi_10"
            self.algorithm.Debug(TQQQFTLT_outer_allocation)
            self.algorithm.Debug(self.algorithm.Time)
from AlgorithmImports import *
import config

class AdaptiveBattleship():



    def __init__(self, algorithm, symbols):
        self.algorithm = algorithm
        self.symbols = symbols





    def AWP1_6B_BATTLESHIP(self, awp1_6b_battleship_outer_allocation):
        if self.symbols["VIXY"].Rsi_97.Current_Value > 54:
            # CASH PROTECTION
            if self.algorithm.Securities["QQQ"].Price > self.symbols["QQQ"].SMA_25.Current_Value:
                rsi_list = [(self.symbols["QLD"], self.symbols["QLD"].Rsi_21.Current_Value), 
                            (self.symbols["SPUU"], self.symbols["SPUU"].Rsi_21.Current_Value), 
                            (self.symbols["ERX"], self.symbols["ERX"].Rsi_21.Current_Value),
                            ]
                
                sorted_rsi_list = sorted(rsi_list, key=lambda x: x[1], reverse=False)
                counter_ = 0
                for symbol, symbol_return in sorted_rsi_list:
                    if counter_ >= 1:
                        break
                    symbol.Counter += awp1_6b_battleship_outer_allocation - (config.CASH_BUFFER * (awp1_6b_battleship_outer_allocation))
                    symbol.Function = "AWP1_6B_BATTLESHIP-1" 
                    counter_ += 1
                    

            else:
                self.symbols["SHY"].Counter += (awp1_6b_battleship_outer_allocation/4) - (config.CASH_BUFFER * (awp1_6b_battleship_outer_allocation/4))
                self.symbols["SHY"].Function = "AWP1_6B_BATTLESHIP-2"
                self.symbols["SUB"].Counter += (awp1_6b_battleship_outer_allocation/4) - (config.CASH_BUFFER * (awp1_6b_battleship_outer_allocation/4))
                self.symbols["SUB"].Function = "AWP1_6B_BATTLESHIP-2"
                self.symbols["BSV"].Counter += (awp1_6b_battleship_outer_allocation/4) - (config.CASH_BUFFER * (awp1_6b_battleship_outer_allocation/4))
                self.symbols["BSV"].Function = "AWP1_6B_BATTLESHIP-2"
                self.symbols["BIL"].Counter += (awp1_6b_battleship_outer_allocation/4) - (config.CASH_BUFFER * (awp1_6b_battleship_outer_allocation/4))
                self.symbols["BIL"].Function = "AWP1_6B_BATTLESHIP-2"
        else:
            self.AWP1_6B_BATTLESHIP_EQUITIES(0.30*awp1_6b_battleship_outer_allocation) # 30% Cash
            self.AWP1_6B_BATTLESHIP_BONDS(0.55*awp1_6b_battleship_outer_allocation) # 55% Cash
            self.AWP1_6B_BATTLESHIP_AGGRESSIVE_BATTLESHIP(0.15*awp1_6b_battleship_outer_allocation) # 15% Cash


    def AWP1_6B_BATTLESHIP_EQUITIES(self, awp1_6b_battleship_outer_allocation):
        if self.symbols["BND"].Return_60.Current_Value > self.symbols["BIL"].Return_60.Current_Value:
            self.risk_on_bullish(awp1_6b_battleship_outer_allocation)
        else:
            if self.symbols["TLT"].Return_MA_15.Current_Value < 0:
                self.risk_off_rising_rates_2(awp1_6b_battleship_outer_allocation)
            else:
                self.risk_on_falling_rates(awp1_6b_battleship_outer_allocation)

    def risk_on_bullish(self, awp1_6b_battleship_outer_allocation):
            if self.symbols["SPY"].Drawdown_10.Current_Value < 0.05: # MAX DRAWDOWN LESS THAN 5%
                self.x3_aggressive_equities(awp1_6b_battleship_outer_allocation)
            else:
                self.x1_defensive_equities(awp1_6b_battleship_outer_allocation)
    def x3_aggressive_equities(self, awp1_6b_battleship_outer_allocation):
        ma_retrun_list = [(self.symbols["UPRO"], self.symbols["UPRO"].Return_MA_10.Current_Value), 
                    (self.symbols["TECL"], self.symbols["TECL"].Return_MA_10.Current_Value), 
                    (self.symbols["TQQQ"], self.symbols["TQQQ"].Return_MA_10.Current_Value),
                    (self.symbols["SOXL"], self.symbols["SOXL"].Return_MA_10.Current_Value), 
                    (self.symbols["FAS"], self.symbols["FAS"].Return_MA_10.Current_Value)
                    ]
        
        sorted_ma_retrun_list = sorted(ma_retrun_list, key=lambda x: x[1], reverse=False)
        counter_ = 0
        for symbol, symbol_return in sorted_ma_retrun_list:
            if counter_ >= 4:
                break
            symbol.Counter += (awp1_6b_battleship_outer_allocation*0.25) - (config.CASH_BUFFER * (awp1_6b_battleship_outer_allocation*0.25))
            symbol.Function = "AWP1_6B_BATTLESHIP_EQUITIES-1" 
            counter_ += 1


    def x1_defensive_equities(self, awp1_6b_battleship_outer_allocation):
        # 1x DEFENSIVE EQUITIES
        self.symbols["SPY"].Counter += (awp1_6b_battleship_outer_allocation * 0.50) - (config.CASH_BUFFER * (awp1_6b_battleship_outer_allocation * 0.50))
        self.sub_strategy_7(awp1_6b_battleship_outer_allocation*0.5)

    def sub_strategy_7(self, awp1_6b_battleship_outer_allocation):
        return_list = [(self.symbols["RSP"], self.symbols["RSP"].Return_10.Current_Value), 
                    (self.symbols["RPV"], self.symbols["RPV"].Return_10.Current_Value), 
                    (self.symbols["QUAL"], self.symbols["QUAL"].Return_10.Current_Value),
                    (self.symbols["BTAL"], self.symbols["BTAL"].Return_10.Current_Value),
                    ]
        
        sorted_return_list = sorted(return_list, key=lambda x: x[1], reverse=True)
        counter_ = 0
        for symbol, symbol_return in sorted_return_list:
            if counter_ >= 1:
                break
            symbol.Counter += (awp1_6b_battleship_outer_allocation) - (config.CASH_BUFFER * (awp1_6b_battleship_outer_allocation))
            symbol.Function = "AWP1_6B_BATTLESHIP_EQUITIES-2"
            counter_ += 1


    def risk_off_rising_rates_2(self, awp1_6b_battleship_outer_allocation):
        self.symbols["USDU"].Counter += (awp1_6b_battleship_outer_allocation * 0.4) - (config.CASH_BUFFER * (awp1_6b_battleship_outer_allocation * 0.40))
        self.symbols["USDU"].Function = "AWP1_6B_BATTLESHIP-5"
        self.sub_strategy_8(awp1_6b_battleship_outer_allocation*0.6)


    def sub_strategy_8(self, awp1_6b_battleship_outer_allocation):
        if self.algorithm.Securities["SPY"].Price > self.symbols["SPY"].SMA_25.Current_Value:
            self.defensive_fund_surf(awp1_6b_battleship_outer_allocation)
        else:
            self.fund_surfing_shorting(awp1_6b_battleship_outer_allocation)


    
    def defensive_fund_surf(self, awp1_6b_battleship_outer_allocation):
              
        rsi_list = [(self.symbols["XLP"], self.symbols["XLP"].Rsi_10.Current_Value), 
                    (self.symbols["XLE"], self.symbols["XLE"].Rsi_10.Current_Value), 
                    (self.symbols["BTAL"], self.symbols["BTAL"].Rsi_10.Current_Value),
                    (self.symbols["XLY"], self.symbols["XLY"].Rsi_10.Current_Value),
                    (self.symbols["XLF"], self.symbols["XLF"].Rsi_10.Current_Value)
                    ]
    
        sorted_rsi_list = sorted(rsi_list, key=lambda x: x[1], reverse=False)
        counter_ = 0
        for symbol, symbol_return in sorted_rsi_list:
            if counter_ >= 3:
                break
            symbol.Counter += (awp1_6b_battleship_outer_allocation * 0.33) - (config.CASH_BUFFER * (awp1_6b_battleship_outer_allocation * 0.33))
            symbol.Function = "AWP1_6B_BATTLESHIP_EQUITIES-3"
            counter_ += 1
         
    
    def fund_surfing_shorting(self, awp1_6b_battleship_outer_allocation):
      
        rsi_list = [(self.symbols["XLP"], self.symbols["XLP"].Rsi_10.Current_Value), 
                    (self.symbols["XLE"], self.symbols["XLE"].Rsi_10.Current_Value), 
                    (self.symbols["SDS"], self.symbols["SDS"].Rsi_10.Current_Value),
                    (self.symbols["SH"], self.symbols["SH"].Rsi_10.Current_Value),
                    (self.symbols["BTAL"], self.symbols["BTAL"].Rsi_10.Current_Value)
                    ]
    
        sorted_rsi_list = sorted(rsi_list, key=lambda x: x[1], reverse=False)
        counter_ = 0
        for symbol, symbol_return in sorted_rsi_list:
            if counter_ >= 3:
                break
            symbol.Counter += (awp1_6b_battleship_outer_allocation * 0.33) - (config.CASH_BUFFER * (awp1_6b_battleship_outer_allocation * 0.33))
            symbol.Function = "AWP1_6B_BATTLESHIP_EQUITIES-4"
            counter_ += 1
                  
    def risk_on_falling_rates(self, awp1_6b_battleship_outer_allocation):
        if self.symbols["SPY"].Return_MA_25.Current_Value > self.symbols["QUAL"].Return_MA_25.Current_Value:
            self.modified_nasdaq_buy_dip(awp1_6b_battleship_outer_allocation)
        else:
            self.recessionary_defensive(awp1_6b_battleship_outer_allocation)

    def modified_nasdaq_buy_dip(self, awp1_6b_battleship_outer_allocation):
        if self.symbols["QQQ"].Return_5.Current_Value < -0.05:
            self.symbols["UPRO"].Counter += (awp1_6b_battleship_outer_allocation*0.5) - (config.CASH_BUFFER * (awp1_6b_battleship_outer_allocation*0.5))
            self.sub_strategy_9(awp1_6b_battleship_outer_allocation*0.5)
        else:
            self.symbols["SPY"].Counter += (awp1_6b_battleship_outer_allocation) - (config.CASH_BUFFER * (awp1_6b_battleship_outer_allocation))
            self.symbols["SPY"].Function = "AWP1_6B_BATTLESHIP-6"
    
    def sub_strategy_9(self, awp1_6b_battleship_outer_allocation):
        if self.symbols["TQQQ"].Return_1.Current_Value > 0.05:
            self.symbols["SSO"].Counter += (awp1_6b_battleship_outer_allocation) - (config.CASH_BUFFER * (awp1_6b_battleship_outer_allocation))
        else:
            rsi_list = [(self.symbols["TQQQ"], self.symbols["TQQQ"].Rsi_5.Current_Value), 
                        (self.symbols["TECL"], self.symbols["TECL"].Rsi_5.Current_Value)
                        ]                
            sorted_rsi_list = sorted(rsi_list, key=lambda x: x[1], reverse=False)
            counter_ = 0
            for symbol, symbol_return in sorted_rsi_list:
                if counter_ >= 1:
                    break
                symbol.Counter += (awp1_6b_battleship_outer_allocation) - (config.CASH_BUFFER * (awp1_6b_battleship_outer_allocation ))
                symbol.Function = "AWP1_6B_BATTLESHIP_EQUITIES-5"
                counter_ += 1
    

                    
    
    def recessionary_defensive(self, awp1_6b_battleship_outer_allocation):
        self.symbols["SCHD"].Counter += (awp1_6b_battleship_outer_allocation * 0.50) - (config.CASH_BUFFER * (awp1_6b_battleship_outer_allocation * 0.50))
        self.symbols["SCHD"].Function = "AWP1_6B_BATTLESHIP-7"
        self.sub_strategy_10(awp1_6b_battleship_outer_allocation*0.5)

    def sub_strategy_10(self, awp1_6b_battleship_outer_allocation):
        return_list = [(self.symbols["RSP"], self.symbols["RSP"].Return_10.Current_Value), 
                (self.symbols["RPV"], self.symbols["RPV"].Return_10.Current_Value), 
                (self.symbols["QUAL"], self.symbols["QUAL"].Return_10.Current_Value),
                (self.symbols["BTAL"], self.symbols["BTAL"].Return_10.Current_Value),
                (self.symbols["XLP"], self.symbols["XLP"].Return_10.Current_Value),
                (self.symbols["XLF"], self.symbols["XLF"].Return_10.Current_Value),
                ]
        
        sorted_return_list = sorted(return_list, key=lambda x: x[1], reverse=True)
        counter_ = 0
        for symbol, symbol_return in sorted_return_list:
            if counter_ >= 1:
                break
            symbol.Counter += (awp1_6b_battleship_outer_allocation) - (config.CASH_BUFFER * (awp1_6b_battleship_outer_allocation))
            

            symbol.Function = "AWP1_6B_BATTLESHIP_EQUITIES-8"
            counter_ += 1
    
    def AWP1_6B_BATTLESHIP_BONDS(self, awp1_6b_battleship_outer_allocation):
        if self.symbols["BND"].Return_60.Current_Value > self.symbols["BIL"].Return_60.Current_Value:
            self.normal_market_bonds(awp1_6b_battleship_outer_allocation)
        else:
            if self.symbols["TLT"].Return_MA_20.Current_Value < 0:
                self.rising_rates_usa_bond_surf(awp1_6b_battleship_outer_allocation)
            else:
                self.falling_rates_bonds(awp1_6b_battleship_outer_allocation)

    def normal_market_bonds(self, awp1_6b_battleship_outer_allocation):
        self.symbols["VGLT"].Counter += (awp1_6b_battleship_outer_allocation * 0.60) - (config.CASH_BUFFER * (awp1_6b_battleship_outer_allocation * 0.60))
        self.symbols["VGLT"].Function = "AWP1_6B_BATTLESHIP_BONDS-1"
        self.symbols["VGIT"].Counter += (awp1_6b_battleship_outer_allocation * 0.40) - (config.CASH_BUFFER * (awp1_6b_battleship_outer_allocation * 0.40))
        self.symbols["VGIT"].Function = "AWP1_6B_BATTLESHIP_BONDS-2"
    

    def rising_rates_usa_bond_surf(self, awp1_6b_battleship_outer_allocation):

        self.symbols["USDU"].Counter += (awp1_6b_battleship_outer_allocation * 0.50) - (config.CASH_BUFFER * (awp1_6b_battleship_outer_allocation * 0.50))
        self.symbols["USDU"].Function = "AWP1_6B_BATTLESHIP_BONDS-3"
        self.sub_strategy_11(awp1_6b_battleship_outer_allocation*0.5)

    def sub_strategy_11(self, awp1_6b_battleship_outer_allocation):
        rsi_list = [(self.symbols["TMV"], self.symbols["TMV"].Rsi_20.Current_Value),
                    (self.symbols["TYO"], self.symbols["TYO"].Rsi_20.Current_Value),
                    (self.symbols["UUP"], self.symbols["UUP"].Rsi_20.Current_Value),
                    ]
        sorted_rsi_list = sorted(rsi_list, key=lambda x: x[1], reverse=False)
        counter_ = 0
        for symbol, symbol_return in sorted_rsi_list:
            if counter_ >= 1:
                break
            symbol.Counter += (awp1_6b_battleship_outer_allocation) - (config.CASH_BUFFER * (awp1_6b_battleship_outer_allocation))
            symbol.Function = "AWP1_6B_BATTLESHIP_BONDS-4"
            counter_ += 1
    

    def falling_rates_bonds(self, awp1_6b_battleship_outer_allocation):
        # FALLING RATES - 3X BONDS
        self.symbols["TMF"].Counter += (awp1_6b_battleship_outer_allocation*0.6) - (config.CASH_BUFFER * (awp1_6b_battleship_outer_allocation*0.6))
        self.symbols["TMF"].Function = "AWP1_6B_BATTLESHIP_BONDS-5"
        self.symbols["TYD"].Counter += (awp1_6b_battleship_outer_allocation*0.4) - (config.CASH_BUFFER * (awp1_6b_battleship_outer_allocation*0.4))
        self.symbols["TYD"].Function = "AWP1_6B_BATTLESHIP_BONDS-6"

            
    def AWP1_6B_BATTLESHIP_AGGRESSIVE_BATTLESHIP(self,awp1_6b_battleship_outer_allocation):
        if self.symbols["VIXM"].Rsi_40.Current_Value > 68:
            self.symbols["SPXU"].Counter += (awp1_6b_battleship_outer_allocation) - (config.CASH_BUFFER * (awp1_6b_battleship_outer_allocation))
            self.symbols["SPXU"].Function = "AWP1_6B_BATTLESHIP_AGGRESSIVE_BATTLESHIP-1"
        else:
            if self.symbols["SPY"].Return_MA_400.Current_Value > self.symbols["DBC"].Return_MA_385.Current_Value:
                if self.symbols["SPY"].EMA_100.Current_Value > self.symbols["SPY"].EMA_400.Current_Value:
                    if self.symbols["SPY"].Rsi_10.Current_Value > 70:
                        self.symbols["UVXY"].Counter += (awp1_6b_battleship_outer_allocation) - (config.CASH_BUFFER * (awp1_6b_battleship_outer_allocation))
                        self.symbols["UVXY"].Function = "AWP1_6B_BATTLESHIP_AGGRESSIVE_BATTLESHIP-2"
                    else:
                        if self.symbols["SPY"].Return_5.Current_Value < -0.025:
                            if self.symbols["SPY"].Return_1.Current_Value > 0.04:
                                self.symbols["UVXY"].Counter += (awp1_6b_battleship_outer_allocation) - (config.CASH_BUFFER * (awp1_6b_battleship_outer_allocation))
                                self.symbols["UVXY"].Function = "AWP1_6B_BATTLESHIP_AGGRESSIVE_BATTLESHIP-3"
                            else:
                                self.symbols["UPRO"].Counter += (awp1_6b_battleship_outer_allocation) - (config.CASH_BUFFER * (awp1_6b_battleship_outer_allocation))
                                self.symbols["UPRO"].Function = "AWP1_6B_BATTLESHIP_AGGRESSIVE_BATTLESHIP-4"
                        else:
                            return_list = [(self.symbols["UPRO"], self.symbols["UPRO"].Return_16.Current_Value), 
                                (self.symbols["TQQQ"], self.symbols["TQQQ"].Return_16.Current_Value), 
                                ]
                
                            sorted_return_list = sorted(return_list, key=lambda x: x[1], reverse=True)
                            counter_ = 0
                            for symbol, symbol_return in sorted_return_list:
                                if counter_ >= 1:
                                    break
                                symbol.Counter += (awp1_6b_battleship_outer_allocation) - (config.CASH_BUFFER * (awp1_6b_battleship_outer_allocation))
                                symbol.Function = "AWP1_6B_BATTLESHIP_AGGRESSIVE_BATTLESHIP-5"
                                counter_ += 1

            else:
                self.dbc_others_bullish(awp1_6b_battleship_outer_allocation)
    def dbc_others_bullish(self, awp1_6b_battleship_outer_allocation):
        if self.symbols["DBC"].EMA_100.Current_Value > self.symbols["DBC"].EMA_400.Current_Value:
            return_list = [(self.symbols["DBC"], self.symbols["DBC"].Return_16.Current_Value), 
                (self.symbols["GLD"], self.symbols["GLD"].Return_16.Current_Value),
                (self.symbols["SLV"], self.symbols["SLV"].Return_16.Current_Value), 
                (self.symbols["USO"], self.symbols["USO"].Return_16.Current_Value), 
                (self.symbols["WEAT"], self.symbols["WEAT"].Return_16.Current_Value), 
                (self.symbols["CORN"], self.symbols["CORN"].Return_16.Current_Value), 
                (self.symbols["SH"], self.symbols["SH"].Return_16.Current_Value), 
                (self.symbols["DRN"], self.symbols["DRN"].Return_16.Current_Value), 
                (self.symbols["PDBC"], self.symbols["PDBC"].Return_16.Current_Value), 
                (self.symbols["COMT"], self.symbols["COMT"].Return_16.Current_Value), 
                (self.symbols["KOLD"], self.symbols["KOLD"].Return_16.Current_Value), 
                (self.symbols["BOIL"], self.symbols["BOIL"].Return_16.Current_Value),
                (self.symbols["ESPO"], self.symbols["ESPO"].Return_16.Current_Value), 
                (self.symbols["PEJ"], self.symbols["PEJ"].Return_16.Current_Value), 
                ]
                                
            sorted_return_list = sorted(return_list, key=lambda x: x[1], reverse=True)
            counter_ = 0
            for symbol, symbol_return in sorted_return_list:
                if counter_ >= 1:
                    break
                symbol.Counter += (awp1_6b_battleship_outer_allocation) - (config.CASH_BUFFER * (awp1_6b_battleship_outer_allocation))
                symbol.Function = "AWP1_6B_BATTLESHIP_AGGRESSIVE_BATTLESHIP-6"
                counter_ += 1 
        else:
            self.inflation_hedge(awp1_6b_battleship_outer_allocation)

    def inflation_hedge(self, awp1_6b_battleship_outer_allocation):
        # INFLATION HEDGE WHAT DID WELL SEP-NOV 20?
        self.symbols["UGL"].Counter += (awp1_6b_battleship_outer_allocation*0.2) - (config.CASH_BUFFER * (awp1_6b_battleship_outer_allocation*0.2))
        self.symbols["UGL"].Function = "AWP1_6B_BATTLESHIP_AGGRESSIVE_BATTLESHIP-1"
        self.symbols["URE"].Counter += (awp1_6b_battleship_outer_allocation*0.2) - (config.CASH_BUFFER * (awp1_6b_battleship_outer_allocation*0.2))
        self.symbols["URE"].Function = "AWP1_6B_BATTLESHIP_AGGRESSIVE_BATTLESHIP-2"
        self.symbols["VXX"].Counter += (awp1_6b_battleship_outer_allocation*0.2) - (config.CASH_BUFFER * (awp1_6b_battleship_outer_allocation*0.2))
        self.symbols["VXX"].Function = "AWP1_6B_BATTLESHIP_AGGRESSIVE_BATTLESHIP-3"
        self.symbols["UVXY"].Counter += (awp1_6b_battleship_outer_allocation*0.2) - (config.CASH_BUFFER * (awp1_6b_battleship_outer_allocation*0.2))
        self.symbols["UVXY"].Function = "AWP1_6B_BATTLESHIP_AGGRESSIVE_BATTLESHIP-4"
        self.symbols["UUP"].Counter += (awp1_6b_battleship_outer_allocation*0.2) - (config.CASH_BUFFER * (awp1_6b_battleship_outer_allocation*0.2))
        self.symbols["UUP"].Function = "AWP1_6B_BATTLESHIP_AGGRESSIVE_BATTLESHIP-5"

# region imports
from AlgorithmImports import *
import config
from beat_the_market_v3_copy import BEAT_THE_MARKET_LEV
from tqqq_for_the_longterm import TQQQForTheLongTerm
from tqqq_or_not import TQQQOrNot
from risk_on_off_hedgefundie import RiskOnOffHedgeFundie
from adaptive_battleship import AdaptiveBattleship
from symboldata import SymbolData

class ETF_Rebalancing(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(config.BACKTEST_START_YEAR, config.BACKTEST_START_MONTH, config.BACKTEST_START_DAY)   # Set Backtest Start Date
        self.SetEndDate(config.BACKTEST_END_YEAR, config.BACKTEST_END_MONTH, config.BACKTEST_END_DAY)           # Set Backtest End Date       
        self.SetCash(config.BACKTEST_ACCOUNT_CASH)                                                              # Set Backtest Strategy Cash

        self.Rebalance_Threshold = config.REBALANCE_THRESHOLD/100
        self.UniverseSettings.Leverage = 4
        self.SHY = None
        self.TECL = None
        self.SOXL = None
        self.FAS = None
        self.TQQQ = None
        self.UPRO = None
        self.TMF = None
        self.USDU = None
        self.SQQQ = None
        self.TBF = None
        self.BIL = None
        self.PHDG = None
        self.TMV = None
        self.UDOW = None
        self.MOAT = None
        self.BRKB = None
        self.USMV = None
        self.SPY = None
        self.QQQ = None
        self.SPXL = None
        self.DBC = None
        self.DBA = None
        self.ICSH = None
        self.TYD = None
        self.URTY = None
        self.XLU = None
        self.NUGT = None
        self.TYO = None
        self.VIXM = None
        self.IEF = None
        self.SOXS = None
        self.BND = None
        self.VTI = None
        self.TLT = None
        self.VXX = None
        self.PSQ = None
        self.VIXY = None
        self.BSV = None
        self.SPHB = None
        self.USD = None
        self.SPXU = None
        self.EWZ = None
        self.MVV = None
        self.UCO = None
        self.UUP = None
        self.QLD = None
        self.GLD = None
        self.YCS = None
        self.TECS = None
        self.SPXS = None
        self.ERX = None
        self.EPI = None
        self.TNA = None
        self.EUO = None
        self.AGG = None
        self.PUI = None
        self.EFA = None
        self.EEM = None
        self.SH = None
        self.SDS = None
        self.UVXY = None
        self.SMH = None
        self.DIA = None
        self.XLP = None
        self.XLV = None
        self.XLF = None
        self.LABU = None
        self.UTSL = None
        self.DRN = None
        self.SOXX = None
        self.QLD = None
        self.SPUU = None
        self.ERX = None
        self.TECL = None
        self.TQQQ = None
        self.SOXL = None
        self.FAS = None
        self.RSP = None
        self.QUAL = None
        self.BTAL = None
        self.XLP = None
        self.XLE = None
        self.XLY = None
        self.XLF = None
        self.SDS = None
        self.TQQQ = None
        self.RPV = None
        self.TMV = None
        self.TYO = None
        self.USDU = None
        self.UPRO = None
        self.DBC = None
        self.GLD = None
        self.SLV = None
        self.USO = None
        self.WEAT = None
        self.CORN = None
        self.SH = None
        self.DRN = None
        self.PDBC = None
        self.COMT = None
        self.KOLD = None
        self.BOIL = None
        self.ESPO = None
        self.PEJ = None
        self.VGLT = None
        self.VGIT = None
        self.ARKK = None
        self.SSO = None
        self.SCHD = None
        self.UGL = None
        self.URE = None
        self.VXX = None       
        self.XLK = None
        self.FNGS = None
        self.HIBL = None
        self.FNGU = None                        
        self.GUSH = None  
        self.TBX = None                  

        self.symbols = {}
        self.class_init_finished = False
     
        #Stratagey Cash Allocations Variable Define
        self.beat_the_market_outer_allocation = config.beat_the_market_outer_allocation 
        self.hedgefundie_outer_alloation = config.hedgefundie_outer_alloation 
        self.black_swan_outer_allocation = config.black_swan_outer_allocation
        self.TQQQFTLT_outer_allocation = config.TQQQFTLT_outer_allocation
        self.awp1_6b_battleship_outer_allocation = config.awp1_6b_battleship_outer_allocation      


        self.ARKK_4_List = []


        self.Symbol_List = []
        self.ETFs = config.ETFS
        self.Trade_Decision_Time = config.TRADE_DECISION_TIME
        self.Order_Delay = config.ORDER_DELAY # Enable this for Delayed orders
        self.Update_Intraday = False
        self.Trade_Decision = False

        for etf in self.ETFs:
            try:
                self.Symbol_List.append(self.AddEquity(etf, Resolution.Minute, dataNormalizationMode=DataNormalizationMode.Adjusted).Symbol)
            except:
                self.Debug(f"Unable to add stock {etf} to the algorithm")


        self.Symbol_Dictionary = {}

        for symbol in self.Symbol_List:
            self.Symbol_Dictionary[symbol] = SymbolData(self, symbol)

        self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.BeforeMarketClose("SPY", self.Trade_Decision_Time), self.Set_Update_Intraday)

        self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.BeforeMarketClose("SPY", self.Trade_Decision_Time - self.Order_Delay), self.Delayed_Orders)

    def Set_Update_Intraday(self):
        self.Update_Intraday = True
        self.Trade_Decision = True



    def Orders(self):
        for symbol, Symbol_Object in self.symbols.items():
            if Symbol_Object.Current_Position < (Symbol_Object.Counter - self.Rebalance_Threshold) or Symbol_Object.Current_Position > (Symbol_Object.Counter + self.Rebalance_Threshold):
                if Symbol_Object.Counter <= 0:
                    self.Liquidate(symbol)
        counter_ = 0    
        
        if not config.USE_ORDER_DELAY:
            for symbol, Symbol_Object in self.symbols.items():
                if Symbol_Object.Current_Position < (Symbol_Object.Counter - self.Rebalance_Threshold) or Symbol_Object.Current_Position > (Symbol_Object.Counter + self.Rebalance_Threshold):     
                    if Symbol_Object.Counter >= 0:
                        counter_  += Symbol_Object.Counter
                        self.SetHoldings(symbol, Symbol_Object.Counter, tag=f"{Symbol_Object.Counter} {Symbol_Object.Function}") # Disable this for Delayed orders
                elif not self.Portfolio[symbol].Invested and Symbol_Object.Current_Position == 0 and Symbol_Object.Counter > 0:
                    self.SetHoldings(symbol, Symbol_Object.Counter, tag=f"{Symbol_Object.Function}")
                    Symbol_Object.Counter
        self.Debug(f"TOTAL COUNTER = {counter_} {self.Time}")

    # Enable this for Delayed orders
    def Delayed_Orders(self):
        if config.USE_ORDER_DELAY:
            for symbol, Symbol_Object in self.symbols.items():
                
                if Symbol_Object.Current_Position < (Symbol_Object.Counter - self.Rebalance_Threshold) or Symbol_Object.Current_Position > (Symbol_Object.Counter + self.Rebalance_Threshold):     
                    if Symbol_Object.Counter >= 0:
                        self.SetHoldings(symbol, Symbol_Object.Counter, tag=f"{Symbol_Object.Function}") 
                elif not self.Portfolio[symbol].Invested and Symbol_Object.Current_Position == 0 and Symbol_Object.Counter > 0:
                    self.SetHoldings(symbol, Symbol_Object.Counter, tag=f"{Symbol_Object.Function}")
    


    def Main_Strategy(self):
        self.beat_the_mkt.BEAT_THE_MARKET(self.beat_the_market_outer_allocation)
        #self.risk_oo_hedge.Refined_Hedgefundie(self.hedgefundie_outer_alloation)
        #self.tqqq_on.BlackSwan_MeanRev_BondSignal(self.black_swan_outer_allocation)
        #self.tqqq_ftl.TQQQFTLT(self.TQQQFTLT_outer_allocation)
        #self.adapt_ship.AWP1_6B_BATTLESHIP(self.awp1_6b_battleship_outer_allocation)



    def Clear_Weightings(self):
        portfolio_value = self.Portfolio.TotalPortfolioValue
        for symbol, Symbol_Object in self.symbols.items():
            Symbol_Object.Counter = 0
            symbol_holding = self.Portfolio[symbol].AbsoluteHoldingsValue
            allocation_per = symbol_holding / portfolio_value
            Symbol_Object.Current_Position = allocation_per


    def OnData(self, data: Slice):
        for symbol in self.Symbol_Dictionary.keys():
            Symbol_Object = self.Symbol_Dictionary[symbol]

            if Symbol_Object is None:continue
            
        
            if self.SHY is None and symbol == "SHY":
                self.SHY = Symbol_Object
                self.symbols["SHY"] = Symbol_Object
            if self.TECL is None and symbol == "TECL":
                self.TECL = Symbol_Object
                self.symbols["TECL"] = Symbol_Object
            if self.SOXL is None and symbol == "SOXL":
                self.SOXL = Symbol_Object
                self.symbols["SOXL"] = Symbol_Object
            if self.FAS is None and symbol == "FAS":
                self.FAS = Symbol_Object
                self.symbols["FAS"] = Symbol_Object
            if self.TQQQ is None and symbol == "TQQQ":
                self.TQQQ = Symbol_Object
                self.symbols["TQQQ"] = Symbol_Object
            if self.UPRO is None and symbol == "UPRO":
                self.UPRO = Symbol_Object
                self.symbols["UPRO"] = Symbol_Object
            if self.TMF is None and symbol == "TMF":
                self.TMF = Symbol_Object
                self.symbols["TMF"] = Symbol_Object
            if self.USDU is None and symbol == "USDU":
                self.USDU = Symbol_Object
                self.symbols["USDU"] = Symbol_Object
            if self.SQQQ is None and symbol == "SQQQ":
                self.SQQQ = Symbol_Object
                self.symbols["SQQQ"] = Symbol_Object
            if self.TBF is None and symbol == "TBF":
                self.TBF = Symbol_Object
                self.symbols["TBF"] = Symbol_Object
            if self.BIL is None and symbol == "BIL":
                self.BIL = Symbol_Object
                self.symbols["BIL"] = Symbol_Object
            if self.PHDG is None and symbol == "PHDG":
                self.PHDG = Symbol_Object
                self.symbols["PHDG"] = Symbol_Object
            if self.TMV is None and symbol == "TMV":
                self.TMV = Symbol_Object
                self.symbols["TMV"] = Symbol_Object
            if self.UDOW is None and symbol == "UDOW":
                self.UDOW = Symbol_Object
                self.symbols["UDOW"] = Symbol_Object
            if self.MOAT is None and symbol == "MOAT":
                self.MOAT = Symbol_Object
                self.symbols["MOAT"] = Symbol_Object
            if self.BRKB is None and symbol == "BRK.B":
                self.BRKB = Symbol_Object
                self.symbols["BRK.B"] = Symbol_Object
            if self.USMV is None and symbol == "USMV":
                self.USMV = Symbol_Object
                self.symbols["USMV"] = Symbol_Object
            if self.SPY is None and symbol == "SPY":
                self.SPY = Symbol_Object
                self.symbols["SPY"] = Symbol_Object
            if self.QQQ is None and symbol == "QQQ":
                self.QQQ = Symbol_Object
                self.symbols["QQQ"] = Symbol_Object
            if self.SPXL is None and symbol == "SPXL":
                self.SPXL = Symbol_Object
                self.symbols["SPXL"] = Symbol_Object
            if self.DBC is None and symbol == "DBC":
                self.DBC = Symbol_Object
                self.symbols["DBC"] = Symbol_Object
            if self.DBA is None and symbol == "DBA":
                self.DBA = Symbol_Object
                self.symbols["DBA"] = Symbol_Object
            if self.ICSH is None and symbol == "ICSH":
                self.ICSH = Symbol_Object
                self.symbols["ICSH"] = Symbol_Object
            if self.TYD is None and symbol == "TYD":
                self.TYD = Symbol_Object
                self.symbols["TYD"] = Symbol_Object
            if self.URTY is None and symbol == "URTY":
                self.URTY = Symbol_Object
                self.symbols["URTY"] = Symbol_Object
            if self.XLU is None and symbol == "XLU":
                self.XLU = Symbol_Object
                self.symbols["XLU"] = Symbol_Object
            if self.NUGT is None and symbol == "NUGT":
                self.NUGT = Symbol_Object
                self.symbols["NUGT"] = Symbol_Object
            if self.TYO is None and symbol == "TYO":
                self.TYO = Symbol_Object
                self.symbols["TYO"] = Symbol_Object
            if self.VIXM is None and symbol == "VIXM":
                self.VIXM = Symbol_Object
                self.symbols["VIXM"] = Symbol_Object
            if self.IEF is None and symbol == "IEF":
                self.IEF = Symbol_Object
                self.symbols["IEF"] = Symbol_Object
            if self.SOXS is None and symbol == "SOXS":
                self.SOXS = Symbol_Object
                self.symbols["SOXS"] = Symbol_Object
            if self.BND is None and symbol == "BND":
                self.BND = Symbol_Object
                self.symbols["BND"] = Symbol_Object
            if self.VTI is None and symbol == "VTI":
                self.VTI = Symbol_Object
                self.symbols["VTI"] = Symbol_Object
            if self.TLT is None and symbol == "TLT":
                self.TLT = Symbol_Object
                self.symbols["TLT"] = Symbol_Object
            if self.VXX is None and symbol == "VXX":
                self.VXX = Symbol_Object
                self.symbols["VXX"] = Symbol_Object
            if self.PSQ is None and symbol == "PSQ":
                self.PSQ = Symbol_Object
                self.symbols["PSQ"] = Symbol_Object
            if self.VIXY is None and symbol == "VIXY":
                self.VIXY = Symbol_Object
                self.symbols["VIXY"] = Symbol_Object
            if self.BSV is None and symbol == "BSV":
                self.BSV = Symbol_Object
                self.symbols["BSV"] = Symbol_Object
            if self.SPHB is None and symbol == "SPHB":
                self.SPHB = Symbol_Object
                self.symbols["SPHB"] = Symbol_Object
            if self.USD is None and symbol == "USD":
                self.USD = Symbol_Object
                self.symbols["USD"] = Symbol_Object
            if self.SPXU is None and symbol == "SPXU":
                self.SPXU = Symbol_Object
                self.symbols["SPXU"] = Symbol_Object
            if self.EWZ is None and symbol == "EWZ":
                self.EWZ = Symbol_Object
                self.symbols["EWZ"] = Symbol_Object
            if self.MVV is None and symbol == "MVV":
                self.MVV = Symbol_Object
                self.symbols["MVV"] = Symbol_Object
            if self.UCO is None and symbol == "UCO":
                self.UCO = Symbol_Object
                self.symbols["UCO"] = Symbol_Object
            if self.UUP is None and symbol == "UUP":
                self.UUP = Symbol_Object
                self.symbols["UUP"] = Symbol_Object
            if self.QLD is None and symbol == "QLD":
                self.QLD = Symbol_Object
                self.symbols["QLD"] = Symbol_Object
            if self.GLD is None and symbol == "GLD":
                self.GLD = Symbol_Object
                self.symbols["GLD"] = Symbol_Object
            if self.YCS is None and symbol == "YCS":
                self.YCS = Symbol_Object
                self.symbols["YCS"] = Symbol_Object
            if self.TECS is None and symbol == "TECS":
                self.TECS = Symbol_Object
                self.symbols["TECS"] = Symbol_Object
            if self.SPXS is None and symbol == "SPXS":
                self.SPXS = Symbol_Object
                self.symbols["SPXS"] = Symbol_Object
            if self.ERX is None and symbol == "ERX":
                self.ERX = Symbol_Object
                self.symbols["ERX"] = Symbol_Object
            if self.EPI is None and symbol == "EPI":
                self.EPI = Symbol_Object
                self.symbols["EPI"] = Symbol_Object
            if self.TNA is None and symbol == "TNA":
                self.TNA = Symbol_Object
                self.symbols["TNA"] = Symbol_Object
            if self.EUO is None and symbol == "EUO":
                self.EUO = Symbol_Object
                self.symbols["EUO"] = Symbol_Object
            if self.AGG is None and symbol == "AGG":
                self.AGG = Symbol_Object
                self.symbols["AGG"] = Symbol_Object
            if self.PUI is None and symbol == "PUI":
                self.PUI = Symbol_Object
                self.symbols["PUI"] = Symbol_Object
            if self.EFA is None and symbol == "EFA":
                self.EFA = Symbol_Object
                self.symbols["EFA"] = Symbol_Object
            if self.EEM is None and symbol == "EEM":
                self.EEM = Symbol_Object
                self.symbols["EEM"] = Symbol_Object
            if self.SH is None and symbol == "SH":
                self.SH = Symbol_Object
                self.symbols["SH"] = Symbol_Object
            if self.SDS is None and symbol == "SDS":
                self.SDS = Symbol_Object
                self.symbols["SDS"] = Symbol_Object
            if self.UVXY is None and symbol == "UVXY":
                self.UVXY = Symbol_Object
                self.symbols["UVXY"] = Symbol_Object
            if self.SMH is None and symbol == "SMH":
                self.SMH = Symbol_Object
                self.symbols["SMH"] = Symbol_Object
            if self.DIA is None and symbol == "DIA":
                self.DIA = Symbol_Object
                self.symbols["DIA"] = Symbol_Object
            if self.XLP is None and symbol == "XLP":
                self.XLP = Symbol_Object
                self.symbols["XLP"] = Symbol_Object
            if self.XLV is None and symbol == "XLV":
                self.XLV = Symbol_Object
                self.symbols["XLV"] = Symbol_Object
            if self.XLF is None and symbol == "XLF":
                self.XLF = Symbol_Object
                self.symbols["XLF"] = Symbol_Object
            if self.LABU is None and symbol == "LABU":
                self.LABU = Symbol_Object
                self.symbols["LABU"] = Symbol_Object
            if self.UTSL is None and symbol == "UTSL":
                self.UTSL = Symbol_Object
                self.symbols["UTSL"] = Symbol_Object
            if self.DRN is None and symbol == "DRN":
                self.DRN = Symbol_Object
                self.symbols["DRN"] = Symbol_Object
            if self.SOXX is None and symbol == "SOXX":
                self.SOXX = Symbol_Object
                self.symbols["SOXX"] = Symbol_Object
            if self.SPUU is None and symbol == "SPUU":
                self.SPUU = Symbol_Object
                self.symbols["SPUU"] = Symbol_Object
            if self.RSP is None and symbol == "RSP":
                self.RSP = Symbol_Object
                self.symbols["RSP"] = Symbol_Object
            if self.QUAL is None and symbol == "QUAL":
                self.QUAL = Symbol_Object
                self.symbols["QUAL"] = Symbol_Object
            if self.BTAL is None and symbol == "BTAL":
                self.BTAL = Symbol_Object
                self.symbols["BTAL"] = Symbol_Object
            if self.XLE is None and symbol == "XLE":
                self.XLE = Symbol_Object
                self.symbols["XLE"] = Symbol_Object
            if self.XLY is None and symbol == "XLY":
                self.XLY = Symbol_Object
                self.symbols["XLY"] = Symbol_Object
            if self.RPV is None and symbol == "RPV":
                self.RPV = Symbol_Object
                self.symbols["RPV"] = Symbol_Object
            if self.SLV is None and symbol == "SLV":
                self.SLV = Symbol_Object
                self.symbols["SLV"] = Symbol_Object
            if self.USO is None and symbol == "USO":
                self.USO = Symbol_Object
                self.symbols["USO"] = Symbol_Object
            if self.WEAT is None and symbol == "WEAT":
                self.WEAT = Symbol_Object
                self.symbols["WEAT"] = Symbol_Object
            if self.CORN is None and symbol == "CORN":
                self.CORN = Symbol_Object
                self.symbols["CORN"] = Symbol_Object
            if self.PDBC is None and symbol == "PDBC":
                self.PDBC = Symbol_Object
                self.symbols["PDBC"] = Symbol_Object
            if self.COMT is None and symbol == "COMT":
                self.COMT = Symbol_Object
                self.symbols["COMT"] = Symbol_Object
            if self.KOLD is None and symbol == "KOLD":
                self.KOLD = Symbol_Object
                self.symbols["KOLD"] = Symbol_Object
            if self.BOIL is None and symbol == "BOIL":
                self.BOIL = Symbol_Object
                self.symbols["BOIL"] = Symbol_Object
            if self.ESPO is None and symbol == "ESPO":
                self.ESPO = Symbol_Object
                self.symbols["ESPO"] = Symbol_Object
            if self.PEJ is None and symbol == "PEJ":
                self.PEJ = Symbol_Object
                self.symbols["PEJ"] = Symbol_Object
            if self.VGLT is None and symbol == "VGLT":
                self.VGLT = Symbol_Object
                self.symbols["VGLT"] = Symbol_Object
            if self.VGIT is None and symbol == "VGIT":
                self.VGIT = Symbol_Object
                self.symbols["VGIT"] = Symbol_Object
            if self.ARKK is None and symbol == "ARKK":
                self.ARKK = Symbol_Object
                self.symbols["ARKK"] = Symbol_Object
            if self.SSO is None and symbol == "SSO":
                self.SSO = Symbol_Object
                self.symbols["SSO"] = Symbol_Object
            if self.SCHD is None and symbol == "SCHD":
                self.SCHD = Symbol_Object
                self.symbols["SCHD"] = Symbol_Object
            if self.UGL is None and symbol == "UGL":
                self.UGL = Symbol_Object
                self.symbols["UGL"] = Symbol_Object
            if self.URE is None and symbol == "URE":
                self.URE = Symbol_Object 
                self.symbols["URE"] = Symbol_Object     
            if self.XLK is None and symbol == "XLK":
                self.XLK = Symbol_Object
                self.symbols["XLK"] = Symbol_Object
            if self.FNGS is None and symbol == "FNGS":
                self.FNGS = Symbol_Object
                self.symbols["FNGS"] = Symbol_Object
            if self.HIBL is None and symbol == "HIBL":
                self.HIBL = Symbol_Object
                self.symbols["HIBL"] = Symbol_Object
            if self.FNGU is None and symbol == "FNGU":
                self.FNGU = Symbol_Object
                self.symbols["FNGU"] = Symbol_Object
            if self.GUSH is None and symbol == "GUSH":
                self.GUSH = Symbol_Object
                self.symbols["GUSH"] = Symbol_Object                                                                                
            if self.TBX is None and symbol == "TBX":
                self.TBX = Symbol_Object
                self.symbols["TBX"] = Symbol_Object


        if self.Update_Intraday:
            if self.SHY is not None:
                self.symbols["SHY"].Update_With_Intraday_Bar(self.Securities["SHY"].Close)
            if self.TECL is not None:
                self.symbols["TECL"].Update_With_Intraday_Bar(self.Securities["TECL"].Close)
            if self.SOXL is not None:
                self.symbols["SOXL"].Update_With_Intraday_Bar(self.Securities["SOXL"].Close)
            if self.FAS is not None:
                self.symbols["FAS"].Update_With_Intraday_Bar(self.Securities["FAS"].Close)
            if self.TQQQ is not None:
                self.symbols["TQQQ"].Update_With_Intraday_Bar(self.Securities["TQQQ"].Close)
            if self.UPRO is not None:
                self.symbols["UPRO"].Update_With_Intraday_Bar(self.Securities["UPRO"].Close)
            if self.TMF is not None:
                self.symbols["TMF"].Update_With_Intraday_Bar(self.Securities["TMF"].Close)
            if self.USDU is not None:
                self.symbols["USDU"].Update_With_Intraday_Bar(self.Securities["USDU"].Close)
            if self.SQQQ is not None:
                self.symbols["SQQQ"].Update_With_Intraday_Bar(self.Securities["SQQQ"].Close)
            if self.TBF is not None:
                self.symbols["TBF"].Update_With_Intraday_Bar(self.Securities["TBF"].Close)
            if self.BIL is not None:
                self.symbols["BIL"].Update_With_Intraday_Bar(self.Securities["BIL"].Close)
            if self.PHDG is not None:
                self.symbols["PHDG"].Update_With_Intraday_Bar(self.Securities["PHDG"].Close)
            if self.TMV is not None:
                self.symbols["TMV"].Update_With_Intraday_Bar(self.Securities["TMV"].Close)
            if self.UDOW is not None:
                self.symbols["UDOW"].Update_With_Intraday_Bar(self.Securities["UDOW"].Close)
            if self.MOAT is not None:
                self.symbols["MOAT"].Update_With_Intraday_Bar(self.Securities["MOAT"].Close)
            if self.BRKB is not None:
                self.symbols["BRK.B"].Update_With_Intraday_Bar(self.Securities["BRK.B"].Close)
            if self.USMV is not None:
                self.symbols["USMV"].Update_With_Intraday_Bar(self.Securities["USMV"].Close)
            if self.SPY is not None:
                self.symbols["SPY"].Update_With_Intraday_Bar(self.Securities["SPY"].Close)
            if self.QQQ is not None:
                self.symbols["QQQ"].Update_With_Intraday_Bar(self.Securities["QQQ"].Close)
            if self.SPXL is not None:
                self.symbols["SPXL"].Update_With_Intraday_Bar(self.Securities["SPXL"].Close)
            if self.DBC is not None:
                self.symbols["DBC"].Update_With_Intraday_Bar(self.Securities["DBC"].Close)
            if self.DBA is not None:
                self.symbols["DBA"].Update_With_Intraday_Bar(self.Securities["DBA"].Close)
            if self.ICSH is not None:
                self.symbols["ICSH"].Update_With_Intraday_Bar(self.Securities["ICSH"].Close)
            if self.TYD is not None:
                self.symbols["TYD"].Update_With_Intraday_Bar(self.Securities["TYD"].Close)
            if self.URTY is not None:
                self.symbols["URTY"].Update_With_Intraday_Bar(self.Securities["URTY"].Close)
            if self.XLU is not None:
                self.symbols["XLU"].Update_With_Intraday_Bar(self.Securities["XLU"].Close)
            if self.NUGT is not None:
                self.symbols["NUGT"].Update_With_Intraday_Bar(self.Securities["NUGT"].Close)
            if self.TYO is not None:
                self.symbols["TYO"].Update_With_Intraday_Bar(self.Securities["TYO"].Close)
            if self.VIXM is not None:
                self.symbols["VIXM"].Update_With_Intraday_Bar(self.Securities["VIXM"].Close)
            if self.IEF is not None:
                self.symbols["IEF"].Update_With_Intraday_Bar(self.Securities["IEF"].Close)
            if self.SOXS is not None:
                self.symbols["SOXS"].Update_With_Intraday_Bar(self.Securities["SOXS"].Close)
            if self.BND is not None:
                self.symbols["BND"].Update_With_Intraday_Bar(self.Securities["BND"].Close)
            if self.VTI is not None:
                self.symbols["VTI"].Update_With_Intraday_Bar(self.Securities["VTI"].Close)
            if self.TLT is not None:
                self.symbols["TLT"].Update_With_Intraday_Bar(self.Securities["TLT"].Close)
            if self.VXX is not None:
                self.symbols["VXX"].Update_With_Intraday_Bar(self.Securities["VXX"].Close)
            if self.PSQ is not None:
                self.symbols["PSQ"].Update_With_Intraday_Bar(self.Securities["PSQ"].Close)
            if self.VIXY is not None:
                self.symbols["VIXY"].Update_With_Intraday_Bar(self.Securities["VIXY"].Close)
            if self.BSV is not None:
                self.symbols["BSV"].Update_With_Intraday_Bar(self.Securities["BSV"].Close)
            if self.SPHB is not None:
                self.symbols["SPHB"].Update_With_Intraday_Bar(self.Securities["SPHB"].Close)
            if self.USD is not None:
                self.symbols["USD"].Update_With_Intraday_Bar(self.Securities["USD"].Close)
            if self.SPXU is not None:
                self.symbols["SPXU"].Update_With_Intraday_Bar(self.Securities["SPXU"].Close)
            if self.EWZ is not None:
                self.symbols["EWZ"].Update_With_Intraday_Bar(self.Securities["EWZ"].Close)
            if self.MVV is not None:
                self.symbols["MVV"].Update_With_Intraday_Bar(self.Securities["MVV"].Close)
            if self.UCO is not None:
                self.symbols["UCO"].Update_With_Intraday_Bar(self.Securities["UCO"].Close)
            if self.UUP is not None:
                self.symbols["UUP"].Update_With_Intraday_Bar(self.Securities["UUP"].Close)
            if self.QLD is not None:
                self.symbols["QLD"].Update_With_Intraday_Bar(self.Securities["QLD"].Close)
            if self.GLD is not None:
                self.symbols["GLD"].Update_With_Intraday_Bar(self.Securities["GLD"].Close)
            if self.YCS is not None:
                self.symbols["YCS"].Update_With_Intraday_Bar(self.Securities["YCS"].Close)
            if self.TECS is not None:
                self.symbols["TECS"].Update_With_Intraday_Bar(self.Securities["TECS"].Close)
            if self.SPXS is not None:
                self.symbols["SPXS"].Update_With_Intraday_Bar(self.Securities["SPXS"].Close)
            if self.ERX is not None:
                self.symbols["ERX"].Update_With_Intraday_Bar(self.Securities["ERX"].Close)
            if self.EPI is not None:
                self.symbols["EPI"].Update_With_Intraday_Bar(self.Securities["EPI"].Close)
            if self.TNA is not None:
                self.symbols["TNA"].Update_With_Intraday_Bar(self.Securities["TNA"].Close)
            if self.EUO is not None:
                self.symbols["EUO"].Update_With_Intraday_Bar(self.Securities["EUO"].Close)
            if self.AGG is not None:
                self.symbols["AGG"].Update_With_Intraday_Bar(self.Securities["AGG"].Close)
            if self.PUI is not None:
                self.symbols["PUI"].Update_With_Intraday_Bar(self.Securities["PUI"].Close)
            if self.EFA is not None:
                self.symbols["EFA"].Update_With_Intraday_Bar(self.Securities["EFA"].Close)
            if self.EEM is not None:
                self.symbols["EEM"].Update_With_Intraday_Bar(self.Securities["EEM"].Close)
            if self.SH is not None:
                self.symbols["SH"].Update_With_Intraday_Bar(self.Securities["SH"].Close)
            if self.SDS is not None:
                self.symbols["SDS"].Update_With_Intraday_Bar(self.Securities["SDS"].Close)
            if self.UVXY is not None:
                self.symbols["UVXY"].Update_With_Intraday_Bar(self.Securities["UVXY"].Close)
            if self.SMH is not None:
                self.symbols["SMH"].Update_With_Intraday_Bar(self.Securities["SMH"].Close)
            if self.DIA is not None:
                self.symbols["DIA"].Update_With_Intraday_Bar(self.Securities["DIA"].Close)
            if self.XLP is not None:
                self.symbols["XLP"].Update_With_Intraday_Bar(self.Securities["XLP"].Close)
            if self.XLV is not None:
                self.symbols["XLV"].Update_With_Intraday_Bar(self.Securities["XLV"].Close)
            if self.XLF is not None:
                self.symbols["XLF"].Update_With_Intraday_Bar(self.Securities["XLF"].Close)
            if self.LABU is not None:
                self.symbols["LABU"].Update_With_Intraday_Bar(self.Securities["LABU"].Close)
            if self.UTSL is not None:
                self.symbols["UTSL"].Update_With_Intraday_Bar(self.Securities["UTSL"].Close)
            if self.DRN is not None:
                self.symbols["DRN"].Update_With_Intraday_Bar(self.Securities["DRN"].Close)
            if self.SOXX is not None:
                self.symbols["SOXX"].Update_With_Intraday_Bar(self.Securities["SOXX"].Close)
            if self.SPUU is not None:
                self.symbols["SPUU"].Update_With_Intraday_Bar(self.Securities["SPUU"].Close)
            if self.RSP is not None:
                self.symbols["RSP"].Update_With_Intraday_Bar(self.Securities["RSP"].Close)
            if self.QUAL is not None:
                self.symbols["QUAL"].Update_With_Intraday_Bar(self.Securities["QUAL"].Close)
            if self.BTAL is not None:
                self.symbols["BTAL"].Update_With_Intraday_Bar(self.Securities["BTAL"].Close)
            if self.XLE is not None:
                self.symbols["XLE"].Update_With_Intraday_Bar(self.Securities["XLE"].Close)
            if self.XLY is not None:
                self.symbols["XLY"].Update_With_Intraday_Bar(self.Securities["XLY"].Close)
            if self.RPV is not None:
                self.symbols["RPV"].Update_With_Intraday_Bar(self.Securities["RPV"].Close)
            if self.SLV is not None:
                self.symbols["SLV"].Update_With_Intraday_Bar(self.Securities["SLV"].Close)
            if self.USO is not None:
                self.symbols["USO"].Update_With_Intraday_Bar(self.Securities["USO"].Close)
            if self.WEAT is not None:
                self.symbols["WEAT"].Update_With_Intraday_Bar(self.Securities["WEAT"].Close)
            if self.CORN is not None:
                self.symbols["CORN"].Update_With_Intraday_Bar(self.Securities["CORN"].Close)
            if self.PDBC is not None:
                self.symbols["PDBC"].Update_With_Intraday_Bar(self.Securities["PDBC"].Close)
            if self.COMT is not None:
                self.symbols["COMT"].Update_With_Intraday_Bar(self.Securities["COMT"].Close)
            if self.KOLD is not None:
                self.symbols["KOLD"].Update_With_Intraday_Bar(self.Securities["KOLD"].Close)
            if self.BOIL is not None:
                self.symbols["BOIL"].Update_With_Intraday_Bar(self.Securities["BOIL"].Close)
            if self.ESPO is not None:
                self.symbols["ESPO"].Update_With_Intraday_Bar(self.Securities["ESPO"].Close)
            if self.PEJ is not None:
                self.symbols["PEJ"].Update_With_Intraday_Bar(self.Securities["PEJ"].Close)
            if self.VGLT is not None:
                self.symbols["VGLT"].Update_With_Intraday_Bar(self.Securities["VGLT"].Close)
            if self.VGIT is not None:
                self.symbols["VGIT"].Update_With_Intraday_Bar(self.Securities["VGIT"].Close)
            if self.ARKK is not None:
                self.symbols["ARKK"].Update_With_Intraday_Bar(self.Securities["ARKK"].Close)
            if self.SSO is not None:
                self.symbols["SSO"].Update_With_Intraday_Bar(self.Securities["SSO"].Close)
            if self.SCHD is not None:
                self.symbols["SCHD"].Update_With_Intraday_Bar(self.Securities["SCHD"].Close)
            if self.UGL is not None:
                self.symbols["UGL"].Update_With_Intraday_Bar(self.Securities["UGL"].Close)
            if self.URE is not None:
                self.symbols["URE"].Update_With_Intraday_Bar(self.Securities["URE"].Close)                
            if self.XLK is not None:
                self.symbols["XLK"].Update_With_Intraday_Bar(self.Securities["XLK"].Close)   
            if self.FNGS is not None:
                self.symbols["FNGS"].Update_With_Intraday_Bar(self.Securities["FNGS"].Close)   
            if self.HIBL is not None:
                self.symbols["HIBL"].Update_With_Intraday_Bar(self.Securities["HIBL"].Close)   
            if self.FNGU is not None:
                self.symbols["FNGU"].Update_With_Intraday_Bar(self.Securities["FNGU"].Close)   
            if self.GUSH is not None:
                self.symbols["GUSH"].Update_With_Intraday_Bar(self.Securities["GUSH"].Close)                                                                                     
            if self.TBX is not None:
                self.symbols["TBX"].Update_With_Intraday_Bar(self.Securities["TBX"].Close)  
       
            
#        if   self.TECL is None or self.SOXL is None or self.FAS is None or self.TQQQ is None or self.UPRO is None or self.TMF is None or self.USDU is None or self.SQQQ is None or self.TBF is None or self.BIL is None or self.PHDG is None or self.TMV is None or self.UDOW is None or self.MOAT is None or self.BRKB is None or self.USMV is None or self.SPY is None or self.QQQ is None or self.SPXL is None or self.DBC is None or self.DBA is None or self.ICSH is None or self.TYD is None or self.URTY is None or self.XLU is None or self.NUGT is None or self.TYO is None or self.VIXM is None or self.IEF is None or self.SOXS is None or self.BND is None or self.VTI is None or self.TLT is None or self.VXX is None or self.PSQ is None or self.VIXY is None or self.BSV is None or self.SPHB is None or self.USD is None or self.SPXU is None or self.EWZ is None or self.MVV is None or self.UCO is None or self.QLD is None or self.GLD is None or self.YCS is None or self.TECS is None or self.SPXS is None or self.ERX is None or self.EPI is None or self.TNA is None or self.EUO is None or self.AGG is None or self.PUI is None or self.EFA is None or self.EEM is None or self.SHY is None or self.UVXY is None or self.SDS is None or self.SH is None or self.SMH is None or self.DIA is None or self.XLP is None or self.XLV is None or self.XLF is None or self.LABU is None or self.UTSL is None or self.DRN is None or self.SOXX is None or self.XLK is None or self.FNGS is None or self.HIBL is None or self.FNGU is None or self.GUSH is None or self.TBX is None:
#            return

        if self.SHY is None or self.TECL is None or self.SOXL is None or self.FAS is None or self.TQQQ is None or self.UPRO is None or self.TMF is None or self.USDU is None or self.SQQQ is None or self.TBF is None or self.BIL is None or self.PHDG is None or self.TMV is None or self.UDOW is None or self.MOAT is None or self.BRKB is None or self.USMV is None or self.SPY is None or self.QQQ is None or self.SPXL is None or self.DBC is None or self.DBA is None or self.ICSH is None or self.TYD is None or self.URTY is None or self.XLU is None or self.NUGT is None or self.TYO is None or self.VIXM is None or self.IEF is None or self.SOXS is None or self.BND is None or self.VTI is None or self.TLT is None or self.VXX is None or self.PSQ is None or self.VIXY is None or self.BSV is None or self.SPHB is None or self.USD is None or self.SPXU is None or self.EWZ is None or self.MVV is None or self.UCO is None or self.UUP is None or self.QLD is None or self.GLD is None or self.YCS is None or self.TECS is None or self.SPXS is None or self.ERX is None or self.EPI is None or self.TNA is None or self.EUO is None or self.AGG is None or self.PUI is None or self.EFA is None or self.EEM is None or self.SH is None or self.SDS is None or self.UVXY is None or self.SMH is None or self.DIA is None or self.XLP is None or self.XLV is None or self.XLF is None or self.LABU is None or self.UTSL is None or self.DRN is None or self.SOXX is None or self.SPUU is None or self.RSP is None or self.QUAL is None or self.BTAL is None or self.XLE is None or self.XLY is None or self.RPV is None or self.SLV is None or self.USO is None or self.WEAT is None or self.CORN is None or self.PDBC is None or self.COMT is None or self.KOLD is None or self.BOIL is None or self.ESPO is None or self.PEJ is None or self.VGLT is None or self.VGIT is None or self.ARKK is None or self.SSO is None or self.SCHD is None or self.UGL is None or self.URE is None or self.URE is None or self.XLK is None or self.FNGS is None or self.HIBL is None or self.FNGU is None or self.GUSH is None or self.TBX is None:
            return
        
        self.Update_Intraday = False
        

        if not self.class_init_finished:
            self.beat_the_mkt = BEAT_THE_MARKET_LEV(self, self.symbols)
            self.tqqq_ftl = TQQQForTheLongTerm(self, self.symbols)
            self.tqqq_on = TQQQOrNot(self, self.symbols)
            self.risk_oo_hedge = RiskOnOffHedgeFundie(self, self.symbols)
            self.adapt_ship = AdaptiveBattleship(self, self.symbols)
            self.class_init_finished = True

        if self.Trade_Decision and self.class_init_finished:
            self.Clear_Weightings()
            self.Main_Strategy()
            self.Orders()

            self.Trade_Decision = False
from AlgorithmImports import *
import config

class TQQQOrNot():



    def __init__(self, algorithm, symbols):
        self.algorithm = algorithm
        self.symbols = symbols
    



    def BlackSwan_MeanRev_BondSignal(self, black_swan_outer_allocation):
        if self.symbols["TQQQ"].Rsi_10.Current_Value > 79:
            self.symbols["VXX"].Counter += black_swan_outer_allocation - (config.CASH_BUFFER * black_swan_outer_allocation)
        else:
            # BlackSwan_Huge_Volatility
            if self.symbols["TQQQ"].Return_6.Current_Value < -0.12:
                if self.symbols["TQQQ"].Return_1.Current_Value > 0.05:
                    self.symbols["VXX"].Counter += black_swan_outer_allocation - (config.CASH_BUFFER * black_swan_outer_allocation)
                else:
                    # BlackSwan_MEAN_REVISION
                    if self.symbols["TQQQ"].Rsi_10.Current_Value < 32:
                        self.symbols["TQQQ"].Counter += black_swan_outer_allocation - (config.CASH_BUFFER * black_swan_outer_allocation)
                    else:
                        if self.symbols["TMF"].Drawdown_10.Current_Value < 0.07:
                            self.symbols["TQQQ"].Counter += black_swan_outer_allocation - (config.CASH_BUFFER * black_swan_outer_allocation)
                        else:
                            self.symbols["BIL"].Counter += black_swan_outer_allocation - (config.CASH_BUFFER * black_swan_outer_allocation)
            else:
                # BlackSwan_NORMAL_MARKET
                if self.symbols["QQQ"].Drawdown_10.Current_Value > 0.06:
                    self.symbols["BIL"].Counter += black_swan_outer_allocation - (config.CASH_BUFFER * black_swan_outer_allocation)
                else:
                    if self.symbols["TMF"].Drawdown_10.Current_Value > 0.07:
                        self.symbols["BIL"].Counter += black_swan_outer_allocation - (config.CASH_BUFFER * black_swan_outer_allocation)
                    else:
                        if self.algorithm.Securities["QQQ"].Price > self.symbols["QQQ"].SMA_25.Current_Value:
                            self.symbols["TQQQ"].Counter += black_swan_outer_allocation - (config.CASH_BUFFER * black_swan_outer_allocation)
                        else:
                            if self.symbols["SPY"].Rsi_60.Current_Value > 50:
                                # BlackSwan BOND > STOCK
                                if self.symbols["BND"].Rsi_45.Current_Value > self.symbols["SPY"].Rsi_45.Current_Value:
                                    self.symbols["TQQQ"].Counter += black_swan_outer_allocation - (config.CASH_BUFFER * black_swan_outer_allocation)
                                else:
                                    self.symbols["BIL"].Counter += black_swan_outer_allocation - (config.CASH_BUFFER * black_swan_outer_allocation)
                            else:
                                # BlackSwan BOND MID_TERM < LONG_TERM
                                if self.symbols["IEF"].Rsi_200.Current_Value < self.symbols["TLT"].Rsi_200.Current_Value:
                                    if self.symbols["BND"].Rsi_45.Current_Value > self.symbols["SPY"].Rsi_45.Current_Value:
                                        self.symbols["TQQQ"].Counter += black_swan_outer_allocation - (config.CASH_BUFFER * black_swan_outer_allocation)
                                   

                                    else:
                                        self.symbols["BIL"].Counter += black_swan_outer_allocation - (config.CASH_BUFFER * black_swan_outer_allocation)
                                else:
                                    self.symbols["BIL"].Counter += black_swan_outer_allocation - (config.CASH_BUFFER * black_swan_outer_allocation)
#region imports
from AlgorithmImports import *
#endregion
"""


"""




BACKTEST_START_YEAR = 2023        # Set start Year of the Backtest
BACKTEST_START_MONTH = 1           # Set start Month of the Backtest
BACKTEST_START_DAY = 1              # Set start Day of the Backtest

BACKTEST_END_YEAR = 2023            # Set end Year of the Backtest
BACKTEST_END_MONTH = 4             # Set end Month of the Backtest       
BACKTEST_END_DAY = 20              # Set end Day of the Backtest

BACKTEST_ACCOUNT_CASH = 10000        # Set Backtest Strategy Cash


# List of ETF's that the algorithm will trade,
# !!! Changing these will break the code without the adequate changes in the main file
# If you need help in changing these please contact me
ETFS = ['SHY', 'TECL', 'SOXL', 'FAS', 'TQQQ', 'UPRO', 'TMF', 'USDU', 'SQQQ', 'TBF',
        'BIL', 'PHDG', 'TMV', 'UDOW', 'MOAT', 'BRK.B', 'USMV', 'SPY', 'QQQ', 'SPXL', 'DBC', 
        'DBA', 'ICSH', 'TYD', 'URTY', 'XLU', 'NUGT', 'TYO', 'VIXM', 'IEF', 'SOXS', 'BND', 
        'VTI', 'TLT', 'VXX', 'PSQ', 'VIXY', 'BSV', 'SPHB', 'USD', 'SPXU', 'EWZ', 'MVV', 
        'UCO', 'UUP', 'QLD', 'GLD', 'YCS', 'TECS', 'SPXS', 'ERX', 'EPI', 'TNA', 'EUO', 
        'AGG', 'PUI', 'EFA', 'EEM', 'SH', 'SDS', 'UVXY', 'SMH', 'DIA', 'XLP', 'XLV', 'XLF', 
        'LABU', 'UTSL', 'DRN', 'SOXX', 'SPUU', 'RSP', 'QUAL', 'BTAL', 'XLE', 'XLY', 'RPV',
        'SLV', 'USO', 'WEAT', 'CORN', 'PDBC', 'COMT', 'KOLD', 'BOIL', 'ESPO', 'PEJ', 'VGLT',
        'VGIT', 'ARKK', 'SSO', 'SCHD', 'UGL', 'URE' , 'XLK', 'FNGS','HIBL', 'FNGU', 'GUSH','TBX' ]
REBALANCE_THRESHOLD = 5



# Cash Buffer in Percent, 0.05 = 5%
CASH_BUFFER = 0  #0.05

# Cash allocation for each Stratagey 0.05 = 5% , 0.1 = 10%
beat_the_market_outer_allocation = 1
TQQQFTLT_outer_allocation = 0
black_swan_outer_allocation = 0
awp1_6b_battleship_outer_allocation = 0


hedgefundie_outer_alloation = 0
hedged_lev_bnd_outer_alloation = 0
SOXX_RSI_outer_allocation = 0
lev_sec_mkt_fwork_allocation = 0




# In minutes before market close
TRADE_DECISION_TIME = 2


USE_ORDER_DELAY = False
ORDER_DELAY = 1


# In minutes after original order
# If scheduled call is set to BeforeMarketClose keep the minus in the main file
# If scheduled call is set to AfterMarketOpen change the minus to a plus
# E.g. self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.BeforeMarketClose("SPY", self.Trade_Decision_Time - self.Order_Delay), self.Delayed_Orders)
# E.g. self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.AfterMarketOpen("SPY", self.Trade_Decision_Time + self.Order_Delay), self.Delayed_Orders)
# ORDER_DELAY = 1



# Period for the Daily RSI


RSI_2_PERIOD = 2
RSI_5_PERIOD = 5
RSI_6_PERIOD = 6
RSI_7_PERIOD = 7
RSI_8_PERIOD = 8
RSI_10_PERIOD = 10
RSI_11_PERIOD = 11
RSI_12_PERIOD = 12
RSI_14_PERIOD = 14
RSI_15_PERIOD = 15
RSI_16_PERIOD = 16
RSI_17_PERIOD = 17
RSI_18_PERIOD = 18
RSI_20_PERIOD = 20
RSI_21_PERIOD = 21
RSI_40_PERIOD = 40
RSI_42_PERIOD = 42
RSI_45_PERIOD = 45
RSI_60_PERIOD = 60
RSI_70_PERIOD = 70
RSI_97_PERIOD = 97
RSI_200_PERIOD = 200



# Period for the Daily Cumulative Return

RETURN_1_PERIOD = 1
RETURN_2_PERIOD = 2
RETURN_3_PERIOD = 3
RETURN_4_PERIOD = 4
RETURN_5_PERIOD = 5
RETURN_6_PERIOD = 6
RETURN_10_PERIOD = 10
RETURN_15_PERIOD = 15
RETURN_16_PERIOD = 16
RETURN_20_PERIOD = 20
RETURN_30_PERIOD = 30
RETURN_60_PERIOD = 60
RETURN_70_PERIOD = 70
RETURN_200_PERIOD = 200



# Period for the Daily Return MA

RETURN_MA_3_PERIOD = 3
RETURN_MA_4_PERIOD = 4
RETURN_MA_5_PERIOD = 5
RETURN_MA_7_PERIOD = 7
RETURN_MA_10_PERIOD = 10
RETURN_MA_12_PERIOD = 12
RETURN_MA_15_PERIOD = 15
RETURN_MA_20_PERIOD = 20
RETURN_MA_22_PERIOD = 22
RETURN_MA_25_PERIOD = 25
RETURN_MA_210_PERIOD = 210
RETURN_MA_360_PERIOD = 360
RETURN_MA_385_PERIOD = 385
RETURN_MA_400_PERIOD = 400




# Period for the Daily EMA

EMA_100_PERIOD = 100
EMA_210_PERIOD = 210
EMA_360_PERIOD = 360
EMA_400_PERIOD = 400




# Period for the Standard Deviation of Return

STDEV_RETURN_5_PERIOD = 5
STDEV_RETURN_10_PERIOD = 10
STDEV_RETURN_20_PERIOD = 20




# Period for the Daily SMA

SMA_2_PERIOD = 2
SMA_10_PERIOD = 10
SMA_12_PERIOD = 12
SMA_20_PERIOD = 20
SMA_21_PERIOD = 21
SMA_23_PERIOD = 23
SMA_25_PERIOD = 25
SMA_30_PERIOD = 30
SMA_31_PERIOD = 31
SMA_50_PERIOD = 50
SMA_150_PERIOD = 150
SMA_200_PERIOD = 200
SMA_210_PERIOD = 210
SMA_360_PERIOD = 360
SMA_1000_PERIOD = 1000


CUSTOM_DRAWDOWN_5_PERIOD = 5
CUSTOM_DRAWDOWN_8_PERIOD = 8
CUSTOM_DRAWDOWN_10_PERIOD = 10
from AlgorithmImports import *
from collections import deque


class Intraday_Updating_Cumulative_Return():



    def __init__(self, algorithm, return_period):
        self.algorithm = algorithm
        self.return_period = return_period +1
        self.Bar_Queue = deque(maxlen=self.return_period)

        self.Current_Value = None
      

        self.First_Intraday_Update_Of_Day = True
        self.Last_Main_Bar = None
     

        self.New_Main_Bar = True
        self.WarmUP = True
        self.Updated_Today = False
        

    
    def Receive_Main_Bar(self, bar):
        if not self.First_Intraday_Update_Of_Day:
            del self.Bar_Queue[0]
            self.New_Main_Bar = True
        self.Bar_Queue.appendleft(bar.Close)
       
        


        if len(self.Bar_Queue) == self.return_period:
            self.Calculate_Cumulative_Return()
    

    def Receive_Sub_Bar(self, bar):
        if bar is not None:
            if len(self.Bar_Queue) == self.return_period:
                if self.First_Intraday_Update_Of_Day:
                    self.Last_Main_Bar = self.Bar_Queue[0]
            
                    self.Bar_Queue.appendleft(bar)
                    self.First_Intraday_Update_Of_Day = False
                else:
                    del self.Bar_Queue[0]
                    self.Bar_Queue.appendleft(bar)


                self.Calculate_Cumulative_Return()
                    


    

    def Calculate_Cumulative_Return(self):
       
        
  
    
        if len(self.Bar_Queue) == self.return_period:
         
            self.Current_Value = (self.Bar_Queue[0] - self.Bar_Queue[self.return_period-1]) / self.Bar_Queue[self.return_period-1]
           
           
            if self.New_Main_Bar:
                self.First_Intraday_Update_Of_Day = True
                

            self.New_Main_Bar = False
from AlgorithmImports import *
from collections import deque


class CustomDrawdown():



    def __init__(self, algorithm, symbol, drawdown_period):
        self.algorithm = algorithm
        self.drawdown_period = drawdown_period 
        self.High_Queue = deque(maxlen=self.drawdown_period)
        self.Low_Queue = deque(maxlen=self.drawdown_period)
        self.symbol = symbol
        self.Current_Value = None
      

        self.First_Intraday_Update_Of_Day = True
        self.Last_Main_Bar = None
     

        self.New_Main_Bar = True
        self.WarmUP = True
        self.Updated_Today = False
        

    
    def Receive_Main_Bar(self, bar):
        if not self.First_Intraday_Update_Of_Day:
            del self.High_Queue[0]
            del self.Low_Queue[0]
            self.New_Main_Bar = True
        self.High_Queue.appendleft(bar.High)
        self.Low_Queue.appendleft(bar.Low)
       
        


        if len(self.High_Queue) == self.drawdown_period and len(self.Low_Queue) == self.drawdown_period:
            self.Calculate_Drawdown()
    

    def Receive_Sub_Bar(self, high, low):
        if high is not None and low is not None:
            if len(self.High_Queue) == self.drawdown_period and len(self.Low_Queue) == self.drawdown_period:
                if self.First_Intraday_Update_Of_Day:
                    self.Last_Main_High = self.High_Queue[0]
                    self.Last_Main_Low = self.Low_Queue[0]

                    if high is not None:
                        self.High_Queue.appendleft(high)
                    if low is not None:
                        self.Low_Queue.appendleft(low)
                    self.First_Intraday_Update_Of_Day = False
                else:
                    del self.High_Queue[0]
                    del self.Low_Queue[0]
                    if high is not None:
                        self.High_Queue.appendleft(high)
                    if low is not None:
                        self.Low_Queue.appendleft(low)


                self.Calculate_Drawdown()
                


    

    def Calculate_Drawdown(self):
       
        
     
       
        if len(self.High_Queue) == self.drawdown_period and len(self.Low_Queue) == self.drawdown_period:
            
            high = 0
            idx = None

            for i in range(0, len(self.High_Queue)):
                if self.High_Queue[i] > high:
                    high = self.High_Queue[i]
                    idx = i

            low = 100000000
            for i in range(0, idx+1):
                if self.Low_Queue[i] < low:
                    low = self.Low_Queue[i]
         
            hh = high
            ll = low
            self.Current_Value = abs((ll - hh) / hh)
           
           
            if self.New_Main_Bar:
                self.First_Intraday_Update_Of_Day = True
                

            self.New_Main_Bar = False
from AlgorithmImports import *
from collections import deque


class CustomEMA():



    def __init__(self, algorithm, ema_period):
        self.algorithm = algorithm
        self.ema_period = ema_period
        self.Bar_Queue = deque(maxlen=self.ema_period)

        self.IsFirst_Value = True

        self.Current_Value = None
      

        self.First_Intraday_Update_Of_Day = True
        self.Last_Main_Bar = None
        
        self.Updated_Today = False

        self.New_Main_Bar = True
        self.WarmUP = True
        

    
    def Receive_Main_Bar(self, bar):
        if not self.First_Intraday_Update_Of_Day:
            del self.Bar_Queue[0]
            self.New_Main_Bar = True
        self.Bar_Queue.appendleft(bar.Close)
       
        


        if len(self.Bar_Queue) == self.ema_period:
            self.Calculate_EMA()
    

    def Receive_Sub_Bar(self, bar):
        if bar is not None:
            if len(self.Bar_Queue) == self.ema_period:
                if self.First_Intraday_Update_Of_Day:
                    self.Last_Main_Bar = self.Bar_Queue[0]
            
                    self.Bar_Queue.appendleft(bar)
                    self.First_Intraday_Update_Of_Day = False
                else:
                    del self.Bar_Queue[0]
                    self.Bar_Queue.appendleft(bar)


                self.Calculate_EMA()
                    


    

    def Calculate_EMA(self):
       
        
  
    
        if len(self.Bar_Queue) == self.ema_period:
            

            if self.IsFirst_Value:
                self.Current_Value = sum(self.Bar_Queue) / len(self.Bar_Queue)
                self.IsFirst_Value = False
                return


            k = 2 / (self.ema_period + 1)
            self.Current_Value = self.Bar_Queue[0] * k + self.Current_Value * (1-k)

            if self.New_Main_Bar:
                self.First_Intraday_Update_Of_Day = True
                

            self.New_Main_Bar = False
from AlgorithmImports import *
from collections import deque



class CustomReturnMA():



    def __init__(self, algorithm, symbol, return_ma_period):
        self.algorithm = algorithm
        self.symbol = symbol
        self.return_ma_period = return_ma_period + 1


        self.Return_Queue = deque(maxlen=self.return_ma_period)     


        self.Current_Value = None
      

        self.First_Intraday_Update_Of_Day = True
        self.Last_Main_Return = None
     

        self.New_Main_Bar = True
        self.WarmUP = True
        self.Updated_Today = False   

        

    def Receive_Main_Bar(self, bar):
        if not self.First_Intraday_Update_Of_Day:
            del self.Return_Queue[0]
            self.New_Main_Bar = True
        
        return_ = (bar.Close - bar.Open) / bar.Open
        self.Return_Queue.appendleft(return_)
        
       
      

        if len(self.Return_Queue) == self.return_ma_period:
            self.Calculate_Return_MA()
    

    def Receive_Sub_Bar(self, open_, close):
        if open_ is not None and close is not None:
            if len(self.Return_Queue) == self.return_ma_period:
                if self.First_Intraday_Update_Of_Day:
                    self.Last_Main_Return = self.Return_Queue[0]
                    return_ = (close - open_) / open_
                    self.Return_Queue.appendleft(return_)
                    self.First_Intraday_Update_Of_Day = False
                else:
                    del self.Return_Queue[0]
                    return_ = (close - open_) / open_
                    self.Return_Queue.appendleft(return_)
                


                self.Calculate_Return_MA()
                


    

    def Calculate_Return_MA(self):
       
        
        #Drawdown needs to check that the LOW occurs AFTER the high! LOL
        
        if len(self.Return_Queue) == self.return_ma_period:
            
            

            


            self.Current_Value = sum(self.Return_Queue) / len(self.Return_Queue)
           
           
            if self.New_Main_Bar:
                self.First_Intraday_Update_Of_Day = True
                

            self.New_Main_Bar = False
from AlgorithmImports import *
from collections import deque
import statistics as stats


class CustomReturnStdev():



    def __init__(self, algorithm, symbol, return_stdev_period):
        self.algorithm = algorithm
        self.symbol = symbol
        self.return_stdev_period = return_stdev_period + 1


        self.Return_Queue = deque(maxlen=self.return_stdev_period)     


        self.Current_Value = None
      

        self.First_Intraday_Update_Of_Day = True
        self.Last_Main_Return = None
     

        self.New_Main_Bar = True
        self.WarmUP = True
        self.Updated_Today = False   

    

    def Receive_Main_Bar(self, bar):
        if not self.First_Intraday_Update_Of_Day:
            del self.Return_Queue[0]
            self.New_Main_Bar = True
        
        return_ = (bar.Close - bar.Open) / bar.Open
        self.Return_Queue.appendleft(return_)
        
       
        


        if len(self.Return_Queue) == self.return_stdev_period:
            self.Calculate_Return_Stdev()
    

    def Receive_Sub_Bar(self, open_, close):
        if open_ is not None and close is not None:
            if len(self.Return_Queue) == self.return_stdev_period:
                if self.First_Intraday_Update_Of_Day:
                    self.Last_Main_Return = self.Return_Queue[0]
                    return_ = (close - open_) / open_
                    self.Return_Queue.appendleft(return_)
                    self.First_Intraday_Update_Of_Day = False
                else:
                    del self.Return_Queue[0]
                    return_ = (close - open_) / open_
                    self.Return_Queue.appendleft(return_)
                    


                self.Calculate_Return_Stdev()
                    


    

    def Calculate_Return_Stdev(self):
       
        
        #Drawdown needs to check that the LOW occurs AFTER the high! LOL
       
        if len(self.Return_Queue) == self.return_stdev_period:
            
            

            


            self.Current_Value = stats.stdev(self.Return_Queue)
           
           
            if self.New_Main_Bar:
                self.First_Intraday_Update_Of_Day = True
                

            self.New_Main_Bar = False
from AlgorithmImports import *
from collections import deque


class Intraday_Updating_RSI():



    def __init__(self, algorithm, rsi_period):
        self.algorithm = algorithm
        self.rsi_period = rsi_period
        self.Bar_Queue = deque(maxlen=2)
        self.Average_Gain_Queue = deque(maxlen=self.rsi_period)
        self.Average_Gain = None
        self.Average_Loss_Queue = deque(maxlen=self.rsi_period)
        self.Average_Loss = None
        self.Change = None
        self.Relative_Strength = None
        self.Current_Value = None
        self.Alpha = None
        self.Sum_Gain = 0
        self.Sum_Loss = 0

        self.First_Intraday_Update_Of_Day = True
        self.Last_Main_Bar = None
        self.Last_Sum_Gain = None
        self.Last_Sum_Loss = None

        self.New_Main_Bar = True
        self.WarmUP = True
        self.Previous_Value = None
        self.Updated_Today = False

    
    def Receive_Main_Bar(self, bar):
        if not self.First_Intraday_Update_Of_Day:
            del self.Bar_Queue[0]
            self.New_Main_Bar = True
        self.Bar_Queue.appendleft(bar.Close)
       
        


        if len(self.Bar_Queue) == 2:
            self.Calculate_Change()
    

    def Receive_Sub_Bar(self, bar):
        if bar is not None:
            if len(self.Bar_Queue) == 2:
                if self.First_Intraday_Update_Of_Day:
                    self.Last_Main_Bar = self.Bar_Queue[1]
                    self.Last_Sum_Gain = self.Sum_Gain
                    self.Last_Sum_Loss = self.Sum_Loss
                    self.Bar_Queue.appendleft(bar)
                    self.First_Intraday_Update_Of_Day = False
                else:
                    del self.Bar_Queue[0]
                    self.Bar_Queue.appendleft(bar)


                self.Calculate_Change()
                


    def Calculate_Change(self):
        self.Change = self.Bar_Queue[0] - self.Bar_Queue[1]

        self.Calculate_Average_Gain_Loss()
    

    def Calculate_Average_Gain_Loss(self):
        if self.Change >= 0:
            gain = self.Change
        else:
            gain = 0
        
        if self.Change < 0:
            loss = abs(self.Change)
        else:
            loss = 0
        
        self.Average_Gain_Queue.appendleft(gain)
        self.Average_Loss_Queue.appendleft(loss)
    
        if len(self.Average_Gain_Queue) == self.rsi_period and len(self.Average_Loss_Queue) == self.rsi_period:
            self.Alpha = 1/self.rsi_period
            self.Average_Gain = sum(self.Average_Gain_Queue) / len(self.Average_Gain_Queue)
            self.Average_Loss = sum(self.Average_Loss_Queue) / len(self.Average_Loss_Queue)
            if self.First_Intraday_Update_Of_Day:
                if self.Sum_Gain == 0:
                    if self.WarmUP:
                        self.Sum_Gain = self.Average_Gain
                    else:
                        self.Sum_Gain = self.Last_Sum_Gain
                else:
                    self.Sum_Gain = self.Alpha * gain + (1-self.Alpha) * self.Sum_Gain
                
                if self.Sum_Loss == 0:
                    if self.WarmUP:
                        self.Sum_Loss = self.Average_Loss
                    else:
                        self.Sum_Loss = self.Last_Sum_Loss
                else:
                    self.Sum_Loss = self.Alpha * loss + (1-self.Alpha) * self.Sum_Loss


            else:
                if self.Sum_Gain == 0:
                    self.Sum_Gain = self.Average_Gain
                else:
                    self.Sum_Gain = self.Alpha * gain + (1-self.Alpha) * self.Last_Sum_Gain
                
                if self.Sum_Loss == 0:
                    self.Sum_Loss = self.Average_Loss
                else:
                    self.Sum_Loss = self.Alpha * loss + (1-self.Alpha) * self.Last_Sum_Loss

                if self.New_Main_Bar:
                    self.First_Intraday_Update_Of_Day = True
                

            self.Calculate_Relative_Strength()

    def Calculate_Relative_Strength(self):
        if self.Sum_Gain != 0 and self.Sum_Loss != 0:
            self.Relative_Strength = self.Sum_Gain / self.Sum_Loss

            if self.Relative_Strength is not None:
                self.Current_Value = 100 - (100 / (1 + self.Relative_Strength))
        if self.Sum_Gain == 0:
            self.Current_Value = 0
        elif self.Sum_Loss == 0:
            self.Current_Value = 100


        self.New_Main_Bar = False
from AlgorithmImports import *
from collections import deque


class Intraday_Updating_SMA():



    def __init__(self, algorithm, sma_period):
        self.algorithm = algorithm
        self.sma_period = sma_period
        self.Bar_Queue = deque(maxlen=self.sma_period)

        self.Current_Value = None
      

        self.First_Intraday_Update_Of_Day = True
        self.Last_Main_Bar = None
        
        self.Updated_Today = False

        self.New_Main_Bar = True
        self.WarmUP = True
        

    
    def Receive_Main_Bar(self, bar):
        if not self.First_Intraday_Update_Of_Day:
            del self.Bar_Queue[0]
            self.New_Main_Bar = True
        self.Bar_Queue.appendleft(bar.Close)
       
        


        if len(self.Bar_Queue) == self.sma_period:
            self.Calculate_SMA()
    

    def Receive_Sub_Bar(self, bar):
        if bar is not None:
            if len(self.Bar_Queue) == self.sma_period:
                if self.First_Intraday_Update_Of_Day:
                    self.Last_Main_Bar = self.Bar_Queue[0]
            
                    self.Bar_Queue.appendleft(bar)
                    self.First_Intraday_Update_Of_Day = False
                else:
                    del self.Bar_Queue[0]
                    self.Bar_Queue.appendleft(bar)


                self.Calculate_SMA()
                


    

    def Calculate_SMA(self):
       
        
  
    
        if len(self.Bar_Queue) == self.sma_period:
         
            self.Current_Value = sum(self.Bar_Queue) / len(self.Bar_Queue)
           
           
            if self.New_Main_Bar:
                self.First_Intraday_Update_Of_Day = True
                

            self.New_Main_Bar = False
from AlgorithmImports import *
from custom_sma import Intraday_Updating_SMA
from custom_cumulative_return import Intraday_Updating_Cumulative_Return
from custom_rsi import Intraday_Updating_RSI
from custom_drawdown import CustomDrawdown
from custom_return_ma import CustomReturnMA
from custom_return_stdev import CustomReturnStdev
from custom_ema import CustomEMA
import config




class SymbolData():


    def __init__(self, algorithm, symbol):
        self.algorithm = algorithm
        self.symbol = symbol

        self.Daily_Open = None
        self.Daily_High = None
        self.Daily_Low = None
        
        self.Counter = 0

        self.Rsi_2_Period = config.RSI_2_PERIOD
        self.Rsi_5_Period = config.RSI_5_PERIOD
        self.Rsi_6_Period = config.RSI_6_PERIOD
        self.Rsi_7_Period = config.RSI_7_PERIOD
        self.Rsi_8_Period = config.RSI_8_PERIOD
        self.Rsi_10_Period = config.RSI_10_PERIOD
        self.Rsi_11_Period = config.RSI_11_PERIOD
        self.Rsi_12_Period = config.RSI_12_PERIOD
        self.Rsi_14_Period = config.RSI_14_PERIOD        
        self.Rsi_15_Period = config.RSI_15_PERIOD
        self.Rsi_16_Period = config.RSI_16_PERIOD
        self.Rsi_17_Period = config.RSI_17_PERIOD
        self.Rsi_18_Period = config.RSI_18_PERIOD
        self.Rsi_20_Period = config.RSI_20_PERIOD
        self.Rsi_21_Period = config.RSI_21_PERIOD          
        self.Rsi_40_Period = config.RSI_40_PERIOD      
        self.Rsi_42_Period = config.RSI_42_PERIOD  
        self.Rsi_45_Period = config.RSI_45_PERIOD          
        self.Rsi_60_Period = config.RSI_60_PERIOD
        self.Rsi_70_Period = config.RSI_70_PERIOD 
        self.Rsi_97_Period = config.RSI_97_PERIOD         
        self.Rsi_200_Period = config.RSI_200_PERIOD

        self.Return_1_Period = config.RETURN_1_PERIOD
        self.Return_2_Period = config.RETURN_2_PERIOD
        self.Return_3_Period = config.RETURN_3_PERIOD
        self.Return_4_Period = config.RETURN_4_PERIOD
        self.Return_5_Period = config.RETURN_5_PERIOD
        self.Return_6_Period = config.RETURN_6_PERIOD
        self.Return_10_Period = config.RETURN_10_PERIOD
        self.Return_15_Period = config.RETURN_15_PERIOD
        self.Return_16_Period = config.RETURN_16_PERIOD        
        self.Return_20_Period = config.RETURN_20_PERIOD
        self.Return_30_Period = config.RETURN_30_PERIOD
        self.Return_60_Period = config.RETURN_60_PERIOD
        self.Return_70_Period = config.RETURN_70_PERIOD
        self.Return_200_Period = config.RETURN_200_PERIOD


        self.Return_MA_3_Period = config.RETURN_MA_3_PERIOD
        self.Return_MA_4_Period = config.RETURN_MA_4_PERIOD
        self.Return_MA_5_Period = config.RETURN_MA_5_PERIOD
        self.Return_MA_7_Period = config.RETURN_MA_7_PERIOD
        self.Return_MA_10_Period = config.RETURN_MA_10_PERIOD        
        self.Return_MA_12_Period = config.RETURN_MA_12_PERIOD
        self.Return_MA_15_Period = config.RETURN_MA_15_PERIOD        
        self.Return_MA_20_Period = config.RETURN_MA_20_PERIOD
        self.Return_MA_22_Period = config.RETURN_MA_22_PERIOD
        self.Return_MA_25_Period = config.RETURN_MA_25_PERIOD        
        self.Return_MA_210_Period = config.RETURN_MA_210_PERIOD
        self.Return_MA_360_Period = config.RETURN_MA_360_PERIOD
        self.Return_MA_385_Period = config.RETURN_MA_385_PERIOD        
        self.Return_MA_400_Period = config.RETURN_MA_400_PERIOD 


        self.STDEV_Return_20_Period = config.STDEV_RETURN_20_PERIOD
        self.STDEV_Return_10_Period = config.STDEV_RETURN_10_PERIOD
        self.STDEV_Return_5_Period = config.STDEV_RETURN_5_PERIOD


        self.EMA_100_Period = config.EMA_100_PERIOD
        self.EMA_210_Period = config.EMA_210_PERIOD        
        self.EMA_360_Period = config.EMA_360_PERIOD
        self.EMA_400_Period = config.EMA_400_PERIOD

        self.SMA_2_Period = config.SMA_2_PERIOD
        self.SMA_10_Period = config.SMA_10_PERIOD
        self.SMA_12_Period = config.SMA_12_PERIOD
        self.SMA_20_Period = config.SMA_20_PERIOD    
        self.SMA_21_Period = config.SMA_21_PERIOD                
        self.SMA_23_Period = config.SMA_23_PERIOD
        self.SMA_25_Period = config.SMA_25_PERIOD
        self.SMA_30_Period = config.SMA_30_PERIOD
        self.SMA_31_Period = config.SMA_31_PERIOD        
        self.SMA_50_Period = config.SMA_50_PERIOD        
        self.SMA_150_Period = config.SMA_150_PERIOD
        self.SMA_200_Period = config.SMA_200_PERIOD
        self.SMA_210_Period = config.SMA_210_PERIOD        
        self.SMA_360_Period = config.SMA_360_PERIOD
        self.SMA_1000_Period = config.SMA_1000_PERIOD

        self.Custom_Drawdown_5_Period = config.CUSTOM_DRAWDOWN_5_PERIOD
        self.Custom_Drawdown_8_Period = config.CUSTOM_DRAWDOWN_8_PERIOD
        self.Custom_Drawdown_10_Period = config.CUSTOM_DRAWDOWN_10_PERIOD

        self.Function = ""
        self.Current_Position = 0

        
        self.Rsi_2 = Intraday_Updating_RSI(algorithm, self.Rsi_2_Period)
        self.Rsi_5 = Intraday_Updating_RSI(algorithm, self.Rsi_5_Period)
        self.Rsi_6 = Intraday_Updating_RSI(algorithm, self.Rsi_6_Period)
        self.Rsi_7 = Intraday_Updating_RSI(algorithm, self.Rsi_7_Period)
        self.Rsi_8 = Intraday_Updating_RSI(algorithm, self.Rsi_8_Period)
        self.Rsi_10 = Intraday_Updating_RSI(algorithm, self.Rsi_10_Period)
        self.Rsi_11 = Intraday_Updating_RSI(algorithm, self.Rsi_11_Period)
        self.Rsi_12 = Intraday_Updating_RSI(algorithm, self.Rsi_12_Period)
        self.Rsi_14 = Intraday_Updating_RSI(algorithm, self.Rsi_14_Period)        
        self.Rsi_15 = Intraday_Updating_RSI(algorithm, self.Rsi_15_Period)
        self.Rsi_16 = Intraday_Updating_RSI(algorithm, self.Rsi_16_Period)
        self.Rsi_17 = Intraday_Updating_RSI(algorithm, self.Rsi_17_Period)
        self.Rsi_18 = Intraday_Updating_RSI(algorithm, self.Rsi_18_Period)
        self.Rsi_20 = Intraday_Updating_RSI(algorithm, self.Rsi_20_Period)  
        self.Rsi_21 = Intraday_Updating_RSI(algorithm, self.Rsi_21_Period)          
        self.Rsi_40 =  Intraday_Updating_RSI(algorithm, self.Rsi_40_Period) 
        self.Rsi_42 =  Intraday_Updating_RSI(algorithm, self.Rsi_42_Period)   
        self.Rsi_45 =  Intraday_Updating_RSI(algorithm, self.Rsi_45_Period)         
        self.Rsi_60 = Intraday_Updating_RSI(algorithm, self.Rsi_60_Period)
        self.Rsi_70 =  Intraday_Updating_RSI(algorithm, self.Rsi_70_Period)
        self.Rsi_97 =  Intraday_Updating_RSI(algorithm, self.Rsi_97_Period)
        self.Rsi_200 = Intraday_Updating_RSI(algorithm, self.Rsi_200_Period)

        self.Return_1 = Intraday_Updating_Cumulative_Return(algorithm, self.Return_1_Period)
        self.Return_2 = Intraday_Updating_Cumulative_Return(algorithm, self.Return_2_Period)
        self.Return_3 = Intraday_Updating_Cumulative_Return(algorithm, self.Return_3_Period)
        self.Return_4 = Intraday_Updating_Cumulative_Return(algorithm, self.Return_4_Period)
        self.Return_5 = Intraday_Updating_Cumulative_Return(algorithm, self.Return_5_Period)
        self.Return_6 = Intraday_Updating_Cumulative_Return(algorithm, self.Return_6_Period)
        self.Return_10 = Intraday_Updating_Cumulative_Return(algorithm, self.Return_10_Period)
        self.Return_15 = Intraday_Updating_Cumulative_Return(algorithm, self.Return_15_Period)
        self.Return_16 = Intraday_Updating_Cumulative_Return(algorithm, self.Return_16_Period)        
        self.Return_20 = Intraday_Updating_Cumulative_Return(algorithm, self.Return_20_Period)
        self.Return_30 = Intraday_Updating_Cumulative_Return(algorithm, self.Return_30_Period)
        self.Return_60 = Intraday_Updating_Cumulative_Return(algorithm, self.Return_60_Period)
        self.Return_70 = Intraday_Updating_Cumulative_Return(algorithm, self.Return_70_Period)
        self.Return_200 = Intraday_Updating_Cumulative_Return(algorithm, self.Return_200_Period)
        

        self.SMA_2 = Intraday_Updating_SMA(algorithm, self.SMA_2_Period)
        self.SMA_10 = Intraday_Updating_SMA(algorithm, self.SMA_10_Period)
        self.SMA_12 = Intraday_Updating_SMA(algorithm, self.SMA_12_Period)  
        self.SMA_20 = Intraday_Updating_SMA(algorithm, self.SMA_20_Period)        
        self.SMA_21 = Intraday_Updating_SMA(algorithm, self.SMA_21_Period)        
        self.SMA_23 = Intraday_Updating_SMA(algorithm, self.SMA_23_Period)
        self.SMA_25 = Intraday_Updating_SMA(algorithm, self.SMA_25_Period)           
        self.SMA_30 = Intraday_Updating_SMA(algorithm, self.SMA_30_Period)
        self.SMA_31 = Intraday_Updating_SMA(algorithm, self.SMA_31_Period)        
        self.SMA_50 = Intraday_Updating_SMA(algorithm, self.SMA_50_Period)        
        self.SMA_150 = Intraday_Updating_SMA(algorithm, self.SMA_150_Period)
        self.SMA_200 = Intraday_Updating_SMA(algorithm, self.SMA_200_Period)
        self.SMA_210 = Intraday_Updating_SMA(algorithm, self.SMA_210_Period)        
        self.SMA_360 = Intraday_Updating_SMA(algorithm, self.SMA_360_Period)
        self.SMA_1000 = Intraday_Updating_SMA(algorithm, self.SMA_1000_Period)

        self.Drawdown_5 = CustomDrawdown(algorithm, symbol, self.Custom_Drawdown_5_Period)
        self.Drawdown_8 = CustomDrawdown(algorithm, symbol, self.Custom_Drawdown_8_Period)
        self.Drawdown_10 = CustomDrawdown(algorithm, symbol, self.Custom_Drawdown_10_Period)


        self.Return_MA_3 = CustomReturnMA(algorithm, symbol, self.Return_MA_3_Period)
        self.Return_MA_4 = CustomReturnMA(algorithm, symbol, self.Return_MA_4_Period)
        self.Return_MA_5 = CustomReturnMA(algorithm, symbol, self.Return_MA_5_Period)
        self.Return_MA_7 = CustomReturnMA(algorithm, symbol, self.Return_MA_7_Period)
        self.Return_MA_10 = CustomReturnMA(algorithm, symbol, self.Return_MA_10_Period)        
        self.Return_MA_12 = CustomReturnMA(algorithm, symbol, self.Return_MA_12_Period)
        self.Return_MA_15 = CustomReturnMA(algorithm, symbol, self.Return_MA_15_Period)        
        self.Return_MA_20 = CustomReturnMA(algorithm, symbol, self.Return_MA_20_Period)
        self.Return_MA_22 = CustomReturnMA(algorithm, symbol, self.Return_MA_22_Period)
        self.Return_MA_25 = CustomReturnMA(algorithm, symbol, self.Return_MA_25_Period)        
        self.Return_MA_210 = CustomReturnMA(algorithm, symbol, self.Return_MA_210_Period)
        self.Return_MA_360 = CustomReturnMA(algorithm, symbol, self.Return_MA_360_Period)
        self.Return_MA_385 = CustomReturnMA(algorithm, symbol, self.Return_MA_385_Period)
        self.Return_MA_400 = CustomReturnMA(algorithm, symbol, self.Return_MA_400_Period)


        self.Return_STDEV_5 = CustomReturnStdev(algorithm, symbol, self.STDEV_Return_5_Period)
        self.Return_STDEV_10 = CustomReturnStdev(algorithm, symbol, self.STDEV_Return_10_Period)
        self.Return_STDEV_20 = CustomReturnStdev(algorithm, symbol, self.STDEV_Return_20_Period)


        self.EMA_100 = CustomEMA(algorithm, self.EMA_100_Period)
        self.EMA_210 = CustomEMA(algorithm, self.EMA_210_Period)
        self.EMA_360 = CustomEMA(algorithm, self.EMA_360_Period)
        self.EMA_400 = CustomEMA(algorithm, self.EMA_400_Period)


        self.WarmUp = True


        self.Daily_Bar_Consolidator = TradeBarConsolidator(timedelta(days=1))
        self.algorithm.SubscriptionManager.AddConsolidator(self.symbol, self.Daily_Bar_Consolidator)
        self.Daily_Bar_Consolidator.DataConsolidated += self.OnDataConsolidated

        history = self.algorithm.History[TradeBar](self.symbol, 1100, Resolution.Daily)


        for bar in history:
            self.Daily_Bar_Consolidator.Update(bar)
        

        self.WarmUp = False



        self.Minute_Bar_Consolidator = TradeBarConsolidator(timedelta(minutes=1))
        self.algorithm.SubscriptionManager.AddConsolidator(self.symbol, self.Minute_Bar_Consolidator)
        self.Minute_Bar_Consolidator.DataConsolidated += self.OnMinuteConsolidated


        self.algorithm.Schedule.On(self.algorithm.DateRules.EveryDay(), self.algorithm.TimeRules.AfterMarketOpen(self.symbol, -10), self.Reset_HL)


    def Reset_HL(self):
        self.Daily_Open = None
        self.Daily_High = None
        self.Daily_Low = None

    def OnMinuteConsolidated(self, sender, bar):
        if self.Daily_Open is None:
            self.Daily_Open = bar.Open

        if self.Daily_High is None:
            self.Daily_High = bar.High
        if self.Daily_Low is None:
            self.Daily_Low = bar.Low
        
        if self.Daily_High is not None and bar.High > self.Daily_High:
            self.Daily_High = bar.High
        if self.Daily_Low is not None and bar.Low < self.Daily_Low:
            self.Daily_Low = bar.Low


    def OnDataConsolidated(self, sender, bar):
        self.Rsi_2.Receive_Main_Bar(bar)        
        self.Rsi_5.Receive_Main_Bar(bar)
        self.Rsi_6.Receive_Main_Bar(bar)
        self.Rsi_7.Receive_Main_Bar(bar)
        self.Rsi_8.Receive_Main_Bar(bar)
        self.Rsi_10.Receive_Main_Bar(bar)
        self.Rsi_11.Receive_Main_Bar(bar)
        self.Rsi_12.Receive_Main_Bar(bar)
        self.Rsi_14.Receive_Main_Bar(bar)        
        self.Rsi_15.Receive_Main_Bar(bar)
        self.Rsi_16.Receive_Main_Bar(bar)
        self.Rsi_17.Receive_Main_Bar(bar)
        self.Rsi_18.Receive_Main_Bar(bar)
        self.Rsi_20.Receive_Main_Bar(bar)
        self.Rsi_21.Receive_Main_Bar(bar)        
        self.Rsi_40.Receive_Main_Bar(bar)
        self.Rsi_42.Receive_Main_Bar(bar)
        self.Rsi_45.Receive_Main_Bar(bar)        
        self.Rsi_60.Receive_Main_Bar(bar)
        self.Rsi_70.Receive_Main_Bar(bar)
        self.Rsi_97.Receive_Main_Bar(bar)        
        self.Rsi_200.Receive_Main_Bar(bar)

        self.Return_1.Receive_Main_Bar(bar)
        self.Return_2.Receive_Main_Bar(bar)
        self.Return_3.Receive_Main_Bar(bar)
        self.Return_4.Receive_Main_Bar(bar)
        self.Return_5.Receive_Main_Bar(bar)
        self.Return_6.Receive_Main_Bar(bar)
        self.Return_10.Receive_Main_Bar(bar)
        self.Return_15.Receive_Main_Bar(bar)
        self.Return_16.Receive_Main_Bar(bar)        
        self.Return_20.Receive_Main_Bar(bar)
        self.Return_30.Receive_Main_Bar(bar)
        self.Return_60.Receive_Main_Bar(bar)
        self.Return_70.Receive_Main_Bar(bar)
        self.Return_200.Receive_Main_Bar(bar)
        
        self.SMA_2.Receive_Main_Bar(bar)
        self.SMA_10.Receive_Main_Bar(bar)
        self.SMA_12.Receive_Main_Bar(bar)
        self.SMA_20.Receive_Main_Bar(bar)        
        self.SMA_21.Receive_Main_Bar(bar)         
        self.SMA_23.Receive_Main_Bar(bar)
        self.SMA_25.Receive_Main_Bar(bar)        
        self.SMA_30.Receive_Main_Bar(bar)
        self.SMA_31.Receive_Main_Bar(bar)
        self.SMA_50.Receive_Main_Bar(bar)        
        self.SMA_150.Receive_Main_Bar(bar)
        self.SMA_200.Receive_Main_Bar(bar)
        self.SMA_210.Receive_Main_Bar(bar)        
        self.SMA_360.Receive_Main_Bar(bar)
        self.SMA_1000.Receive_Main_Bar(bar)

        self.Drawdown_5.Receive_Main_Bar(bar)
        self.Drawdown_8.Receive_Main_Bar(bar)
        self.Drawdown_10.Receive_Main_Bar(bar)

        self.Return_MA_3.Receive_Main_Bar(bar)
        self.Return_MA_4.Receive_Main_Bar(bar)        
        self.Return_MA_5.Receive_Main_Bar(bar)
        self.Return_MA_7.Receive_Main_Bar(bar)
        self.Return_MA_10.Receive_Main_Bar(bar)        
        self.Return_MA_12.Receive_Main_Bar(bar)
        self.Return_MA_15.Receive_Main_Bar(bar)        
        self.Return_MA_20.Receive_Main_Bar(bar)
        self.Return_MA_22.Receive_Main_Bar(bar)
        self.Return_MA_25.Receive_Main_Bar(bar)        
        self.Return_MA_210.Receive_Main_Bar(bar)
        self.Return_MA_360.Receive_Main_Bar(bar)
        self.Return_MA_385.Receive_Main_Bar(bar)        
        self.Return_MA_400.Receive_Main_Bar(bar)          

        self.Return_STDEV_5.Receive_Main_Bar(bar)
        self.Return_STDEV_10.Receive_Main_Bar(bar)
        self.Return_STDEV_20.Receive_Main_Bar(bar)

        self.EMA_100.Receive_Main_Bar(bar)
        self.EMA_210.Receive_Main_Bar(bar)
        self.EMA_360.Receive_Main_Bar(bar)
        self.EMA_400.Receive_Main_Bar(bar)

        self.Rsi_2.Updated_Today = True 
        self.Rsi_5.Updated_Today = True 
        self.Rsi_6.Updated_Today = True
        self.Rsi_7.Updated_Today = True
        self.Rsi_8.Updated_Today = True      
        self.Rsi_10.Updated_Today = True
        self.Rsi_11.Updated_Today = True
        self.Rsi_12.Updated_Today = True
        self.Rsi_14.Updated_Today = True        
        self.Rsi_15.Updated_Today = True
        self.Rsi_16.Updated_Today = True
        self.Rsi_17.Updated_Today = True
        self.Rsi_18.Updated_Today = True
        self.Rsi_20.Updated_Today = True  
        self.Rsi_21.Updated_Today = True         
        self.Rsi_40.Updated_Today = True   
        self.Rsi_42.Updated_Today = True           
        self.Rsi_45.Updated_Today = True          
        self.Rsi_60.Updated_Today = True
        self.Rsi_70.Updated_Today = True
        self.Rsi_97.Updated_Today = True        
        self.Rsi_200.Updated_Today = True

        self.Return_1.Updated_Today = True
        self.Return_2.Updated_Today = True
        self.Return_3.Updated_Today = True
        self.Return_4.Updated_Today = True
        self.Return_5.Updated_Today = True
        self.Return_6.Updated_Today = True
        self.Return_10.Updated_Today = True
        self.Return_15.Updated_Today = True
        self.Return_16.Updated_Today = True        
        self.Return_20.Updated_Today = True
        self.Return_30.Updated_Today = True
        self.Return_60.Updated_Today = True
        self.Return_70.Updated_Today = True
        self.Return_200.Updated_Today = True

        self.SMA_2.Updated_Today = True
        self.SMA_10.Updated_Today = True
        self.SMA_12.Updated_Today = True
        self.SMA_20.Updated_Today = True        
        self.SMA_21.Updated_Today = True              
        self.SMA_23.Updated_Today = True
        self.SMA_25.Updated_Today = True        
        self.SMA_30.Updated_Today = True   
        self.SMA_31.Updated_Today = True        
        self.SMA_50.Updated_Today = True        
        self.SMA_150.Updated_Today = True
        self.SMA_200.Updated_Today = True
        self.SMA_210.Updated_Today = True        
        self.SMA_360.Updated_Today = True
        self.SMA_1000.Updated_Today = True

        self.Drawdown_5.Updated_Today = True
        self.Drawdown_8.Updated_Today = True
        self.Drawdown_10.Updated_Today = True

        self.Return_MA_3.Updated_Today = True
        self.Return_MA_4.Updated_Today = True
        self.Return_MA_5.Updated_Today = True
        self.Return_MA_7.Updated_Today = True
        self.Return_MA_10.Updated_Today = True        
        self.Return_MA_12.Updated_Today = True
        self.Return_MA_15.Updated_Today = True        
        self.Return_MA_20.Updated_Today = True
        self.Return_MA_22.Updated_Today = True
        self.Return_MA_25.Updated_Today = True        
        self.Return_MA_210.Updated_Today = True
        self.Return_MA_360.Updated_Today = True
        self.Return_MA_385.Updated_Today = True        
        self.Return_MA_400.Updated_Today = True        

        self.Return_STDEV_5.Updated_Today = True
        self.Return_STDEV_10.Updated_Today = True
        self.Return_STDEV_20.Updated_Today = True

        self.EMA_100.Updated_Today = True
        self.EMA_210.Updated_Today = True
        self.EMA_360.Updated_Today = True
        self.EMA_400.Updated_Today = True


    def Update_With_Intraday_Bar(self, price):
        if not self.WarmUp:
            if self.Rsi_2.Updated_Today:
                self.Rsi_2.Receive_Sub_Bar(price)            
            if self.Rsi_5.Updated_Today:
                self.Rsi_5.Receive_Sub_Bar(price)
            if self.Rsi_6.Updated_Today:
                self.Rsi_6.Receive_Sub_Bar(price)
            if self.Rsi_7.Updated_Today:
                self.Rsi_7.Receive_Sub_Bar(price)
            if self.Rsi_8.Updated_Today:
                self.Rsi_8.Receive_Sub_Bar(price)
            if self.Rsi_10.Updated_Today:
                self.Rsi_10.Receive_Sub_Bar(price)
            if self.Rsi_11.Updated_Today:
                self.Rsi_11.Receive_Sub_Bar(price)
            if self.Rsi_12.Updated_Today:
                self.Rsi_12.Receive_Sub_Bar(price)
            if self.Rsi_14.Updated_Today:
                self.Rsi_14.Receive_Sub_Bar(price)                 
            if self.Rsi_15.Updated_Today:
                self.Rsi_15.Receive_Sub_Bar(price)
            if self.Rsi_16.Updated_Today:
                self.Rsi_16.Receive_Sub_Bar(price)
            if self.Rsi_17.Updated_Today:
                self.Rsi_17.Receive_Sub_Bar(price)
            if self.Rsi_18.Updated_Today:
                self.Rsi_18.Receive_Sub_Bar(price)
            if self.Rsi_20.Updated_Today:
                self.Rsi_20.Receive_Sub_Bar(price)    
            if self.Rsi_21.Updated_Today:
                self.Rsi_21.Receive_Sub_Bar(price)
            if self.Rsi_40.Updated_Today:
                self.Rsi_40.Receive_Sub_Bar(price)
            if self.Rsi_42.Updated_Today:
                self.Rsi_42.Receive_Sub_Bar(price)                  
            if self.Rsi_45.Updated_Today:
                self.Rsi_45.Receive_Sub_Bar(price)                        
            if self.Rsi_60.Updated_Today:
                self.Rsi_60.Receive_Sub_Bar(price)
            if self.Rsi_70.Updated_Today:
                self.Rsi_70.Receive_Sub_Bar(price)
            if self.Rsi_97.Updated_Today:
                self.Rsi_97.Receive_Sub_Bar(price)                
            if self.Rsi_200.Updated_Today:
                self.Rsi_200.Receive_Sub_Bar(price)

            if self.Return_1.Updated_Today:
                self.Return_1.Receive_Sub_Bar(price)
            if self.Return_2.Updated_Today:
                self.Return_2.Receive_Sub_Bar(price)
            if self.Return_3.Updated_Today:
                self.Return_3.Receive_Sub_Bar(price)
            if self.Return_4.Updated_Today:
                self.Return_4.Receive_Sub_Bar(price)
            if self.Return_5.Updated_Today:
                self.Return_5.Receive_Sub_Bar(price)
            if self.Return_6.Updated_Today:
                self.Return_6.Receive_Sub_Bar(price)
            if self.Return_10.Updated_Today:
                self.Return_10.Receive_Sub_Bar(price)
            if self.Return_15.Updated_Today:
                self.Return_15.Receive_Sub_Bar(price)
            if self.Return_16.Updated_Today:
                self.Return_16.Receive_Sub_Bar(price)                
            if self.Return_20.Updated_Today:
                self.Return_20.Receive_Sub_Bar(price)
            if self.Return_30.Updated_Today:
                self.Return_30.Receive_Sub_Bar(price)
            if self.Return_60.Updated_Today:
                self.Return_60.Receive_Sub_Bar(price)
            if self.Return_70.Updated_Today:
                self.Return_70.Receive_Sub_Bar(price)
            if self.Return_200.Updated_Today:
                self.Return_200.Receive_Sub_Bar(price)

            if self.SMA_2.Updated_Today:
                self.SMA_2.Receive_Sub_Bar(price)
            if self.SMA_10.Updated_Today:
                self.SMA_10.Receive_Sub_Bar(price)                
            if self.SMA_12.Updated_Today:
                self.SMA_12.Receive_Sub_Bar(price)
            if self.SMA_20.Updated_Today:
                self.SMA_20.Receive_Sub_Bar(price) 
            if self.SMA_21.Updated_Today:
                self.SMA_21.Receive_Sub_Bar(price)                                   
            if self.SMA_23.Updated_Today:
                self.SMA_23.Receive_Sub_Bar(price)
            if self.SMA_25.Updated_Today:
                self.SMA_25.Receive_Sub_Bar(price)                
            if self.SMA_30.Updated_Today:
                self.SMA_30.Receive_Sub_Bar(price)   
            if self.SMA_31.Updated_Today:
                self.SMA_31.Receive_Sub_Bar(price)                
            if self.SMA_50.Updated_Today:
                self.SMA_50.Receive_Sub_Bar(price)                         
            if self.SMA_150.Updated_Today:
                self.SMA_150.Receive_Sub_Bar(price)
            if self.SMA_200.Updated_Today:
                self.SMA_200.Receive_Sub_Bar(price)
            if self.SMA_210.Updated_Today:
                self.SMA_210.Receive_Sub_Bar(price)                
            if self.SMA_360.Updated_Today:
                self.SMA_360.Receive_Sub_Bar(price)
            if self.SMA_1000.Updated_Today:
                self.SMA_1000.Receive_Sub_Bar(price)                

            if self.Drawdown_5.Updated_Today:
                self.Drawdown_5.Receive_Sub_Bar(self.Daily_High, self.Daily_Low)
            if self.Drawdown_8.Updated_Today:
                self.Drawdown_8.Receive_Sub_Bar(self.Daily_High, self.Daily_Low)
            if self.Drawdown_10.Updated_Today:
                self.Drawdown_10.Receive_Sub_Bar(self.Daily_High, self.Daily_Low)
            
            if self.Return_MA_3.Updated_Today:
                self.Return_MA_3.Receive_Sub_Bar(self.Daily_Open, price)
            if self.Return_MA_4.Updated_Today:
                self.Return_MA_4.Receive_Sub_Bar(self.Daily_Open, price)                
            if self.Return_MA_5.Updated_Today:
                self.Return_MA_5.Receive_Sub_Bar(self.Daily_Open, price)
            if self.Return_MA_7.Updated_Today:
                self.Return_MA_7.Receive_Sub_Bar(self.Daily_Open, price)
            if self.Return_MA_10.Updated_Today:
                self.Return_MA_10.Receive_Sub_Bar(self.Daily_Open, price)            
            if self.Return_MA_12.Updated_Today:
                self.Return_MA_12.Receive_Sub_Bar(self.Daily_Open, price)
            if self.Return_MA_15.Updated_Today:
                self.Return_MA_15.Receive_Sub_Bar(self.Daily_Open, price)                
            if self.Return_MA_20.Updated_Today:
                self.Return_MA_20.Receive_Sub_Bar(self.Daily_Open, price)
            if self.Return_MA_22.Updated_Today:
                self.Return_MA_22.Receive_Sub_Bar(self.Daily_Open, price)
            if self.Return_MA_25.Updated_Today:
                self.Return_MA_25.Receive_Sub_Bar(self.Daily_Open, price)                
            if self.Return_MA_210.Updated_Today:
                self.Return_MA_210.Receive_Sub_Bar(self.Daily_Open, price)
            if self.Return_MA_360.Updated_Today:
                self.Return_MA_360.Receive_Sub_Bar(self.Daily_Open, price)
            if self.Return_MA_385.Updated_Today:
                self.Return_MA_385.Receive_Sub_Bar(self.Daily_Open, price)                
            if self.Return_MA_400.Updated_Today:
                self.Return_MA_400.Receive_Sub_Bar(self.Daily_Open, price)  

            if self.Return_STDEV_5.Updated_Today:
                self.Return_STDEV_5.Receive_Sub_Bar(self.Daily_Open, price)
            if self.Return_STDEV_10.Updated_Today:
                self.Return_STDEV_10.Receive_Sub_Bar(self.Daily_Open, price)
            if self.Return_STDEV_20.Updated_Today:
                self.Return_STDEV_20.Receive_Sub_Bar(self.Daily_Open, price)
            
            if self.EMA_100.Updated_Today:
                self.EMA_100.Receive_Sub_Bar(price)
            if self.EMA_210.Updated_Today:
                self.EMA_210.Receive_Sub_Bar(price)
            if self.EMA_360.Updated_Today:
                self.EMA_360.Receive_Sub_Bar(price)
            if self.EMA_400.Updated_Today:
                self.EMA_400.Receive_Sub_Bar(price)                

    def Reset(self):
        self.Rsi_2.Updated_Today = False        
        self.Rsi_5.Updated_Today = False
        self.Rsi_6.Updated_Today = False
        self.Rsi_7.Updated_Today = False
        self.Rsi_8.Updated_Today = False
        self.Rsi_10.Updated_Today = False
        self.Rsi_11.Updated_Today = False
        self.Rsi_12.Updated_Today = False
        self.Rsi_14.Updated_Today = False        
        self.Rsi_15.Updated_Today = False
        self.Rsi_16.Updated_Today = False
        self.Rsi_17.Updated_Today = False
        self.Rsi_18.Updated_Today = False
        self.Rsi_20.Updated_Today = False
        self.Rsi_21.Updated_Today = False        
        self.Rsi_40.Updated_Today = False
        self.Rsi_42.Updated_Today = False
        self.Rsi_45.Updated_Today = False        
        self.Rsi_60.Updated_Today = False
        self.Rsi_70.Updated_Today = False
        self.Rsi_97.Updated_Today = False        
        self.Rsi_200.Updated_Today = False
        

        self.Return_1.Updated_Today = False
        self.Return_2.Updated_Today = False
        self.Return_3.Updated_Today = False
        self.Return_4.Updated_Today = False
        self.Return_5.Updated_Today = False
        self.Return_6.Updated_Today = False
        self.Return_10.Updated_Today = False
        self.Return_15.Updated_Today = False
        self.Return_16.Updated_Today = False        
        self.Return_20.Updated_Today = False
        self.Return_30.Updated_Today = False
        self.Return_60.Updated_Today = False
        self.Return_70.Updated_Today = False
        self.Return_200.Updated_Today = False

        self.SMA_2.Updated_Today = False
        self.SMA_10.Updated_Today = False        
        self.SMA_12.Updated_Today = False
        self.SMA_20.Updated_Today = False      
        self.SMA_21.Updated_Today = False           
        self.SMA_23.Updated_Today = False
        self.SMA_25.Updated_Today = False        
        self.SMA_30.Updated_Today = False 
        self.SMA_31.Updated_Today = False        
        self.SMA_50.Updated_Today = False               
        self.SMA_150.Updated_Today = False
        self.SMA_200.Updated_Today = False
        self.SMA_210.Updated_Today = False        
        self.SMA_360.Updated_Today = False
        self.SMA_1000.Updated_Today = False        

        self.Drawdown_5.Updated_Today = False
        self.Drawdown_8.Updated_Today = False
        self.Drawdown_10.Updated_Today = False

        self.Return_MA_3.Updated_Today = False
        self.Return_MA_4.Updated_Today = False       
        self.Return_MA_5.Updated_Today = False
        self.Return_MA_7.Updated_Today = False
        self.Return_MA_10.Updated_Today = False        
        self.Return_MA_12.Updated_Today = False
        self.Return_MA_15.Updated_Today = False        
        self.Return_MA_20.Updated_Today = False
        self.Return_MA_22.Updated_Today = False
        self.Return_MA_25.Updated_Today = False        
        self.Return_MA_210.Updated_Today = False
        self.Return_MA_360.Updated_Today = False
        self.Return_MA_385.Updated_Today = False        
        self.Return_MA_400.Updated_Today = False         

        self.Return_STDEV_5.Updated_Today = False
        self.Return_STDEV_10.Updated_Today = False
        self.Return_STDEV_20.Updated_Today = False

        self.EMA_100.Updated_Today = False        
        self.EMA_210.Updated_Today = False
        self.EMA_360.Updated_Today = False
        self.EMA_400.Updated_Today = False
from AlgorithmImports import *
import config

class RiskOnOffHedgeFundie():



    def __init__(self, algorithm, symbols):
        self.algorithm = algorithm
        self.symbols = symbols

    def Refined_Hedgefundie(self, hedgefundie_outer_alloation):
       
        if self.symbols["VIXM"].Rsi_40.Current_Value > 69:
            self.Risk_Off_Market_Crash(0.34)
        else:
            if self.symbols["BND"].Return_60.Current_Value > self.symbols["BIL"].Return_60.Current_Value:
                self.Risk_On_Normal_Market_Conditions(0.34)
            else:
                if self.symbols["TLT"].Return_20.Current_Value < self.symbols["BIL"].Return_20.Current_Value:
                    self.Risk_Off_Rising_Rates(0.34)
                else:
                    self.Risk_On_Falling_Rates_HFEA(0.34)

      
    def Risk_Off_Market_Crash(self, hedgefundie_outer_alloation):
        self.symbols["SHY"].Counter += hedgefundie_outer_alloation * 0 - (config.CASH_BUFFER * hedgefundie_outer_alloation * 0) 
    

    def Risk_On_Normal_Market_Conditions(self, hedgefundie_outer_alloation):
        self.Sub_Strategy_1(0.55*hedgefundie_outer_alloation)
        self.symbols["TMF"].Counter += (hedgefundie_outer_alloation * 0.45) - (config.CASH_BUFFER * (hedgefundie_outer_alloation * 0.45))
    

    def Sub_Strategy_1(self, hedgefundie_outer_alloation):
      
        rsi_list = [(self.symbols["TECL"], self.symbols["TECL"].Rsi_10.Current_Value), 
                    (self.symbols["SOXL"], self.symbols["SOXL"].Rsi_10.Current_Value), 
                    (self.symbols["FAS"], self.symbols["FAS"].Rsi_10.Current_Value),
                    (self.symbols["TQQQ"], self.symbols["TQQQ"].Rsi_10.Current_Value),
                    (self.symbols["UPRO"], self.symbols["UPRO"].Rsi_10.Current_Value)
                    ]
                
        sorted_rsi_list = sorted(rsi_list, key=lambda x: x[1], reverse=False)
        counter_ = 0
        for symbol, symbol_return in sorted_rsi_list:
            if counter_ >= 3:
                break
            symbol.Counter += hedgefundie_outer_alloation/3 - (config.CASH_BUFFER * (hedgefundie_outer_alloation/3))
            counter_ += 1



    def Risk_Off_Rising_Rates(self, hedgefundie_outer_alloation):
        self.symbols["USDU"].Counter += hedgefundie_outer_alloation * 0.5 - (config.CASH_BUFFER * (hedgefundie_outer_alloation * 0.5)) 
        self.Sub_Strategy_2(0.5*hedgefundie_outer_alloation)

    def Sub_Strategy_2(self, hedgefundie_outer_alloation):
        if self.symbols["SQQQ"].Rsi_20.Current_Value < self.symbols["TBF"].Rsi_20.Current_Value:
            self.symbols["SQQQ"].Counter += hedgefundie_outer_alloation - (config.CASH_BUFFER * hedgefundie_outer_alloation) 
        else:
            self.symbols["TBF"].Counter += hedgefundie_outer_alloation- (config.CASH_BUFFER * hedgefundie_outer_alloation)

    def Risk_On_Falling_Rates_HFEA(self, hedgefundie_outer_alloation):
        if self.symbols["SPY"].Drawdown_10.Current_Value < 0.05:
            self.HFEA_Refined_Risk_On(hedgefundie_outer_alloation)
        else:
            self.HFEA_Refined_Risk_Off(hedgefundie_outer_alloation)

    def HFEA_Refined_Risk_On(self, hedgefundie_outer_alloation):
        self.symbols["UPRO"].Counter += 0.55*hedgefundie_outer_alloation - (config.CASH_BUFFER * (0.55*hedgefundie_outer_alloation))
        self.symbols["TMF"].Counter += 0.45*hedgefundie_outer_alloation - (config.CASH_BUFFER * (0.45*hedgefundie_outer_alloation))

    def HFEA_Refined_Risk_Off(self, hedgefundie_outer_alloation):
        self.symbols["BIL"].Counter += hedgefundie_outer_alloation * 0 - (config.CASH_BUFFER * hedgefundie_outer_alloation * 0)