Overall Statistics
Total Trades
50
Average Win
0.97%
Average Loss
-0.74%
Compounding Annual Return
-14.233%
Drawdown
9.700%
Expectancy
-0.098
Net Profit
-2.232%
Sharpe Ratio
-0.39
Probabilistic Sharpe Ratio
26.259%
Loss Rate
61%
Win Rate
39%
Profit-Loss Ratio
1.30
Alpha
-0.106
Beta
0.936
Annual Standard Deviation
0.211
Annual Variance
0.044
Information Ratio
-0.571
Tracking Error
0.188
Treynor Ratio
-0.088
Total Fees
$557.26
Estimated Strategy Capacity
$390000.00
Lowest Capacity Asset
TMV UBTUG7D0B7TX
Portfolio Turnover
39.16%
from AlgorithmImports import *
import math
import pandas as pd
from cmath import sqrt
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Common")

from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Data.Custom import *
from QuantConnect.Python import PythonData


class IntelligentSkyDoge(QCAlgorithm):
   
    def Initialize(self):

        self.cash = 100000
        self.buffer_pct = 0.06 # CHANGE YOUR THRESHOLD HERE
        self.SetStartDate(2023, 4, 1)
        self.SetEndDate(2023, 5, 24)
        self.SetCash(self.cash)
        self.equities = ['VCIT', 'UDN', 'SARK','AMD','FNGU','TSLL','AEHR','MSTR','TARK','XLY','QQQE','VOOG','VOOV','VTV','HIBL','XLK','XLP','SVXY','QID','TBF','TSLA','LQD','VTIP','EDV','STIP','SPTL','IEI','USDU','SQQQ','VIXM','SPXU','QQQ','BSV','TQQQ','SPY','DBC','SHV','IAU','VEA','UTSL','UVXY','UPRO','EFA','EEM','TLT','SHY','GLD','SLV','USO','WEAT','CORN','SH','DRN','PDBC','COMT','KOLD','BOIL','ESPO','PEJ','UGL','URE','VXX','UUP','BND','BIL','DUST','JDST','JNUG','GUSH','DBA','DBB','COM','PALL','AGQ','BAL','WOOD','URA','SCO','UCO','DBO','TAGS','CANE','REMX','COPX','IEF','SPDN','CHAD','DRIP','SPUU','INDL','BRZU','ERX','ERY','CWEB','CHAU','KORU','MEXX','EDZ','EURL','YINN','YANG','TNA','TZA','SPXL','SPXS','MIDU','TYD','TYO','TMF','TMV','TECL','TECS','SOXL','SOXS','LABU','LABD','RETL','DPST','DRV','PILL','CURE','FAZ','FAS','EWA','EWGS','EWG','EWP','EWQ','EWU','EWJ','EWI','EWN','ECC','NURE','VNQI','VNQ','VDC','VIS','VGT','VAW','VPU','VOX','VFH','VHT','VDE','SMH','DIA','UDOW','PSQ','SOXX','VTI','COST','UNH','SPHB','BTAL','VIXY','WEBL','WEBS','UBT','PST','TLH','QLD','SQM','SSO','SD','DGRO','SCHD','SGOL','TIP','DUG','EWZ','TBX','VGIT','VGLT','CCOR','LBAY','NRGD','PHDG','SPHD','COWZ','CTA','DBMF','GDMA','VIGI','AGG','NOBL','FAAR','BITO','FTLS','MORT','FNDX','GLL','NTSX','RWL','VLUE','IJR','SPYG','VXUS','AAL','AEP','AFL','C','CMCSA','DUK','EXC','F','GM','GOOGL','INTC','JNJ','KO','MET','NWE','OXY','PFE','RTX','SNY','SO','T','TMUS','VZ','WFC','WMT','AMZN','MSFT','NVDA','TSM','BA','CB','COKE','FDX','GE','LMT','MRK','NVEC','ORCL','PEP','V','DBE','BRK-B','CRUS','INFY','KMLM','NSYS','SCHG','SGML','SLDP','ARKQ','XLU','XLV','ULTA','AAPL','AMZU','BAD','DDM','IYH','JPM','PM','XOM']

        self.MKT = self.AddEquity("SPY", Resolution.Daily).Symbol
        self.mkt = []
        for equity in self.equities:
            self.AddEquity(equity, Resolution.Minute)
            self.Securities[equity].SetDataNormalizationMode(DataNormalizationMode.Adjusted)
        
        self.PT1 = 0.72
        self.PT2 = 0.0
        self.PT3 = 0.22

        self.HT50 = {str(i).zfill(2): 0 for i in range(1, 21)}
        self.HTS50 = {str(i).zfill(2): [] for i in range(1, 21)}

        self.Schedule.On(self.DateRules.EveryDay("SPY"),
                self.TimeRules.BeforeMarketClose("SPY", 17),
                self.FunctionBeforeMarketClose)

    def RSI(self, equity, period):
        extension = min(period*5, 250)
        r_w = RollingWindow[float](extension)
        history = self.History(equity, extension - 1, Resolution.Daily)
        for historical_bar in history:
            r_w.Add(historical_bar.Close)
        while r_w.Count < extension:
            current_price = self.Securities[equity].Price
            r_w.Add(current_price)
        if r_w.IsReady:
            average_gain = 0
            average_loss = 0
            gain = 0
            loss = 0
            for i in range(extension - 1, extension - period -1, -1):
                gain += max(r_w[i-1] - r_w[i], 0)
                loss += abs(min(r_w[i-1] - r_w[i], 0))
            average_gain = gain/period
            average_loss = loss/period
            for i in range(extension - period - 1, 0, -1):
                average_gain = (average_gain*(period-1) + max(r_w[i-1] - r_w[i], 0))/period
                average_loss = (average_loss*(period-1) + abs(min(r_w[i-1] - r_w[i], 0)))/period
            if average_loss == 0:
                return 100
            else:
                rsi = 100 - (100/(1 + average_gain / average_loss))
                return rsi
        else:
            return None

    def CumReturn(self, equity, period):
        history = self.History(equity, period, Resolution.Daily)
        closing_prices = pd.Series([bar.Close for bar in history])
        current_price = self.Securities[equity].Price
        closing_prices = closing_prices.append(pd.Series([current_price]))
        first_price = closing_prices.iloc[0]
        if first_price == 0:
            return None
        else:
            return_val = (current_price / first_price) - 1
            return return_val


    def STD(self, equity, period):
        r_w = RollingWindow[float](period + 1)
        r_w_return = RollingWindow[float](period)
        history = self.History(equity, period, Resolution.Daily)
        for historical_bar in history:
            r_w.Add(historical_bar.Close)
        while r_w.Count < period + 1:
            current_price = self.Securities[equity].Price
            r_w.Add(current_price)
        for i in range (period, 0, -1):
            daily_return = (r_w[i-1]/r_w[i] - 1)
            r_w_return.Add(daily_return)
        dfstd = pd.DataFrame({'r_w_return':r_w_return})       
        if r_w.IsReady:
            std = dfstd['r_w_return'].std()
            if std == 0:
                return 0
            else:
                return std
        else:
            return 0

    def MaxDD(self, equity, period):

        history = self.History(equity, period - 1, Resolution.Daily)     
        closing_prices = pd.Series([bar.Close for bar in history])
        current_price = self.Securities[equity].Price
        closing_prices = closing_prices.append(pd.Series([current_price]))
        rolling_max = closing_prices.cummax()
        drawdowns = (rolling_max - closing_prices) / rolling_max
        max_dd = drawdowns.min()
        return max_dd


    def SMA(self, equity, period):
        r_w = RollingWindow[float](period)
        history = self.History(equity, period - 1, Resolution.Daily)
        for historical_bar in history:
            r_w.Add(historical_bar.Close)
        while r_w.Count < period:
            current_price = self.Securities[equity].Price
            r_w.Add(current_price)        
        if r_w.IsReady:
            sma = sum(r_w) / period
            return sma
        else:
            return 0
           
    def IV(self, equity, period):
        r_w = RollingWindow[float](period + 1)
        r_w_return = RollingWindow[float](period)
        history = self.History(equity, period, Resolution.Daily)
        for historical_bar in history:
            r_w.Add(historical_bar.Close)
        while r_w.Count < period + 1:
            current_price = self.Securities[equity].Price
            r_w.Add(current_price)
        for i in range (period, 0, -1):
            if r_w[i] == 0:
                return 0
            else:
                daily_return = (r_w[i-1]/r_w[i] - 1)
                r_w_return.Add(daily_return)
        dfinverse = pd.DataFrame({'r_w_return':r_w_return})       
        if r_w.IsReady:
            std = dfinverse['r_w_return'].std()
            if std == 0:
                return 0
            else:
                inv_vol = 1 / std
                return inv_vol
        else:
            return 0

    def SMADayRet(self, equity, period):
        r_w = RollingWindow[float](period + 1)
        r_w_return = RollingWindow[float](period)
        history = self.History(equity, period, Resolution.Daily)
        for historical_bar in history:
            r_w.Add(historical_bar.Close)
        while r_w.Count < period + 1:
            current_price = self.Securities[equity].Price
            r_w.Add(current_price)
        for i in range (period, 0, -1):
            if r_w[i] == 0:
                return None
            daily_return = (r_w[i-1]/r_w[i] - 1)
            r_w_return.Add(daily_return)
        if r_w.IsReady:
            smareturn = sum(r_w_return) / period
            return smareturn
        else:
            return 0

    def EMA(self, equity, period):
        extension = period + 50
        r_w = RollingWindow[float](extension)
        history = self.History(equity, extension - 1, Resolution.Daily)
        for historical_bar in history:
            r_w.Add(historical_bar.Close)
        while r_w.Count < extension:
            current_price = self.Securities[equity].Price
            r_w.Add(current_price)
        if r_w.IsReady:
            total_price = 0
            for i in range(extension - 1, extension - period - 2, -1):
                total_price += r_w[i]
            average_price = total_price/period
            for i in range(extension - period - 2, -1, -1):
                average_price = r_w[i]*2/(period+1) + average_price*(1-2/(period+1))

            return average_price
        else:
            return None

    def OnData (self, data):
        pass
   
    def FunctionBeforeMarketClose(self):

        mkt_price = self.History(self.MKT, 2, Resolution.Daily)['close'].unstack(level= 0).iloc[-1]
        self.mkt.append(mkt_price)
        mkt_perf = self.cash * self.mkt[-1] / self.mkt[0]
        self.Plot('Strategy Equity', self.MKT, mkt_perf)

        self.HolyGrailSimplified()
        #self.V15A()
        self.v41Pops()
        self.ExecuteTrade()

    def HolyGrailSimplified(self):

        if self.Securities['SPY'].Price > self.SMA('SPY', 200):
            if self.RSI('QQQ', 10) > 79:
                equities = ["UVXY", "SQQQ"]
                returns = {}
                for equity in equities:
                    returns[equity] = self.RSI(equity, 13)
                s_e = sorted([item for item in returns.items() if item[1] is not None], key = lambda x: x[1], reverse = True)
                t3e = s_e[:1]
                for i in self.HT50.keys():
                    if self.HT50[i] == 0:
                        self.HT50[i] = self.PT1
                        self.HTS50[i].append(t3e[0][0])
                        break
            else:
                if self.RSI('SPY', 10) > 79:
                    equities = ["UVXY", "SPXS"]
                    returns = {}
                    for equity in equities:
                        returns[equity] = self.RSI(equity, 13)
                    s_e = sorted([item for item in returns.items() if item[1] is not None], key = lambda x: x[1], reverse = True)
                    t3e = s_e[:1]
                    for i in self.HT50.keys():
                        if self.HT50[i] == 0:
                            self.HT50[i] = self.PT1
                            self.HTS50[i].append(t3e[0][0])
                            break
                else:
                    equities = ["TECL", "STIP"]
                    returns = {}
                    for equity in equities:
                        returns[equity] = self.RSI(equity, 11)
                    s_e = sorted([item for item in returns.items() if item[1] is not None], key = lambda x: x[1], reverse = False)
                    t3e = s_e[:1]
                    for i in self.HT50.keys():
                        if self.HT50[i] == 0:
                            self.HT50[i] = self.PT1
                            self.HTS50[i].append(t3e[0][0])
                            break
        else:
            if self.RSI('TQQQ', 10) < 31:
                equities = ["TECL", "SHY"]
                returns = {}
                for equity in equities:
                    returns[equity] = self.RSI(equity, 11)
                s_e = sorted([item for item in returns.items() if item[1] is not None], key = lambda x: x[1], reverse = False)
                t3e = s_e[:1]
                for i in self.HT50.keys():
                    if self.HT50[i] == 0:
                        self.HT50[i] = self.PT1
                        self.HTS50[i].append(t3e[0][0])
                        break
            else:
                if self.RSI('UPRO', 10) < 31:
                    equities = ["UPRO", "SHY"]
                    returns = {}
                    for equity in equities:
                        returns[equity] = self.RSI(equity, 11)
                    s_e = sorted([item for item in returns.items() if item[1] is not None], key = lambda x: x[1], reverse = False)
                    t3e = s_e[:1]
                    for i in self.HT50.keys():
                        if self.HT50[i] == 0:
                            self.HT50[i] = self.PT1
                            self.HTS50[i].append(t3e[0][0])
                            break
                else:
                    if self.CumReturn('TQQQ', 6) < -0.11:
                        self.BuythedipsV2()
                    else:
                        if self.Securities['QLD'].Price > self.SMA('QLD', 20):
                            equities = ["TECL", "STIP"]
                            returns = {}
                            for equity in equities:
                                returns[equity] = self.RSI(equity, 11)
                            s_e = sorted([item for item in returns.items() if item[1] is not None], key = lambda x: x[1], reverse = False)
                            t3e = s_e[:1]
                            for i in self.HT50.keys():
                                if self.HT50[i] == 0:
                                    self.HT50[i] = self.PT1
                                    self.HTS50[i].append(t3e[0][0])
                                    break
                        else:
                            self.Substrategy5()
                            self.Substrategy6()

    def BuythedipsV2(self):

        if self.CumReturn('TQQQ', 5) > 0.055:
            equities = ["UVXY", "SQQQ"]
            returns = {}
            for equity in equities:
                returns[equity] = self.RSI(equity, 11)
            s_e = sorted([item for item in returns.items() if item[1] is not None], key = lambda x: x[1], reverse = True)
            t3e = s_e[:1]
            for i in self.HT50.keys():
                if self.HT50[i] == 0:
                    self.HT50[i] = self.PT1
                    self.HTS50[i].append(t3e[0][0])
                    break
        if self.CumReturn('SQQQ', 1) > 0.028:
            self.Substrategy1()
            self.Substrategy2()
        else:
            self.HoldStocksBondsSOXLSHVTECLSTIP()

    def Substrategy1(self):

        equities = ["TECL", "UDN"]
        returns = {}
        for equity in equities:
            returns[equity] = self.RSI(equity, 11)
        s_e = sorted([item for item in returns.items() if item[1] is not None], key = lambda x: x[1], reverse = False)
        t3e = s_e[:1]
        for i in self.HT50.keys():
            if self.HT50[i] == 0:
                self.HT50[i] = self.PT1 * 0.5
                self.HTS50[i].append(t3e[0][0])
                break        
    
    def Substrategy2(self):

        equities = ["TECL", "TMV"]
        returns = {}
        for equity in equities:
            returns[equity] = self.RSI(equity, 11)
        s_e = sorted([item for item in returns.items() if item[1] is not None], key = lambda x: x[1], reverse = True)
        t3e = s_e[:1]
        for i in self.HT50.keys():
            if self.HT50[i] == 0:
                self.HT50[i] = self.PT1 * 0.5
                self.HTS50[i].append(t3e[0][0])
                break     

    def HoldStocksBondsSOXLSHVTECLSTIP(self):

            self.Substrategy3()
            self.Substrategy4()
        
    def Substrategy3(self):

        equities = ["SOXL", "SHV"]
        returns = {}
        for equity in equities:
            returns[equity] = self.RSI(equity, 11)
        s_e = sorted([item for item in returns.items() if item[1] is not None], key = lambda x: x[1], reverse = False)
        t3e = s_e[:1]
        for i in self.HT50.keys():
            if self.HT50[i] == 0:
                self.HT50[i] = self.PT1 * 0.68
                self.HTS50[i].append(t3e[0][0])
                break        

    def Substrategy4(self):

        equities = ["TECL", "STIP"]
        returns = {}
        for equity in equities:
            returns[equity] = self.RSI(equity, 11)
        s_e = sorted([item for item in returns.items() if item[1] is not None], key = lambda x: x[1], reverse = False)
        t3e = s_e[:1]
        for i in self.HT50.keys():
            if self.HT50[i] == 0:
                self.HT50[i] = self.PT1 * 0.32
                self.HTS50[i].append(t3e[0][0])
                break     

    def Substrategy5(self):

        if self.SMADayRet('TLT', 20) > self.SMADayRet('UDN', 20):
            equities = ["TLT", "SQQQ"]
            returns = {}
            for equity in equities:
                returns[equity] = self.RSI(equity, 11)
            s_e = sorted([item for item in returns.items() if item[1] is not None], key = lambda x: x[1], reverse = True)
            t3e = s_e[:1]
            for i in self.HT50.keys():
                if self.HT50[i] == 0:
                    self.HT50[i] = self.PT1 * 0.5
                    self.HTS50[i].append(t3e[0][0])
                    break    
        else:
            equities = ["UUP", "SQQQ"]
            returns = {}
            for equity in equities:
                returns[equity] = self.RSI(equity, 10)
            s_e = sorted([item for item in returns.items() if item[1] is not None], key = lambda x: x[1], reverse = False)
            t3e = s_e[:1]
            for i in self.HT50.keys():
                if self.HT50[i] == 0:
                    self.HT50[i] = self.PT1 * 0.5
                    self.HTS50[i].append(t3e[0][0])
                    break   

    def Substrategy6(self):

        equities = ["UGL", "SQQQ"]
        returns = {}
        for equity in equities:
            returns[equity] = self.RSI(equity, 12)
        s_e = sorted([item for item in returns.items() if item[1] is not None], key = lambda x: x[1], reverse = False)
        t3e = s_e[:1]
        for i in self.HT50.keys():
            if self.HT50[i] == 0:
                self.HT50[i] = self.PT1 * 0.5
                self.HTS50[i].append(t3e[0][0])
                break   

    def V15A(self):
        if self.RSI('SPY', 6) > 75:
            equities = ["UVXY", "VIXY"]
            returns = {}
            for equity in equities:
                returns[equity] = self.RSI(equity, 5)
            s_e = sorted([item for item in returns.items() if item[1] is not None], key = lambda x: x[1], reverse = False)
            t3e = s_e[:1]
            for i in self.HT50.keys():
                if self.HT50[i] == 0:
                    self.HT50[i] = self.PT2
                    self.HTS50[i].append(t3e[0][0])
                    break
        else:
            if self.RSI('BIL', 5) < self.RSI('VCIT', 4):
                for i in self.HT50.keys():
                    if self.HT50[i] == 0:
                        self.HT50[i] = self.PT2
                        self.HTS50[i].append('SOXL')
                        break  
            else:
                if self.SMADayRet('VIXY', 5) > self.SMADayRet('BND', 5):
                    equities = ["UVXY", "VIXY", "IEF"]
                    returns = {}
                    for equity in equities:
                        returns[equity] = self.MaxDD(equity, 3)
                    s_e = sorted([item for item in returns.items() if item[1] is not None], key = lambda x: x[1], reverse = True)
                    t3e = s_e[:1]
                    for i in self.HT50.keys():
                        if self.HT50[i] == 0:
                            self.HT50[i] = self.PT2
                            self.HTS50[i].append(t3e[0][0])
                            break       
                else:
                    if self.SMADayRet('EDV', 11) < 0:
                        equities = ["SVXY", "TMF"]
                        returns = {}
                        for equity in equities:
                            returns[equity] = self.RSI(equity, 21)
                        s_e = sorted([item for item in returns.items() if item[1] is not None], key = lambda x: x[1], reverse = False)
                        t3e = s_e[:1]
                        for i in self.HT50.keys():
                            if self.HT50[i] == 0:
                                self.HT50[i] = self.PT2
                                self.HTS50[i].append(t3e[0][0])
                                break                                
                    else:
                        equities = ["SVXY", "TMV"]
                        returns = {}
                        for equity in equities:
                            returns[equity] = self.RSI(equity, 21)
                        s_e = sorted([item for item in returns.items() if item[1] is not None], key = lambda x: x[1], reverse = False)
                        t3e = s_e[:1]
                        for i in self.HT50.keys():
                            if self.HT50[i] == 0:
                                self.HT50[i] = self.PT2
                                self.HTS50[i].append(t3e[0][0])
                                break      
    def v41Pops(self):

        if self.RSI('QQQE', 10) > 79:
            equities = ["UVXY", "VIXY"]
            returns = {}
            for equity in equities:
                returns[equity] = self.RSI(equity, 13)
            s_e = sorted([item for item in returns.items() if item[1] is not None], key = lambda x: x[1], reverse = False)
            t3e = s_e[:1]
            for i in self.HT50.keys():
                if self.HT50[i] == 0:
                    self.HT50[i] = self.PT3
                    self.HTS50[i].append(t3e[0][0])
                    break        
        else:
            if self.RSI('VTV', 10) > 79:
                equities = ["UVXY", "VIXY"]
                returns = {}
                for equity in equities:
                    returns[equity] = self.RSI(equity, 13)
                s_e = sorted([item for item in returns.items() if item[1] is not None], key = lambda x: x[1], reverse = False)
                t3e = s_e[:1]
                for i in self.HT50.keys():
                    if self.HT50[i] == 0:
                        self.HT50[i] = self.PT3
                        self.HTS50[i].append(t3e[0][0])
                        break         
            else:
                if self.RSI('VOX', 10) > 79:
                    equities = ["UVXY", "VIXY"]
                    returns = {}
                    for equity in equities:
                        returns[equity] = self.RSI(equity, 13)
                    s_e = sorted([item for item in returns.items() if item[1] is not None], key = lambda x: x[1], reverse = False)
                    t3e = s_e[:1]
                    for i in self.HT50.keys():
                        if self.HT50[i] == 0:
                            self.HT50[i] = self.PT3
                            self.HTS50[i].append(t3e[0][0])
                            break     
                else:
                    if self.MaxDD('SPY', 9) < 0:
                        equities = ["UVXY", "VIXY"]
                        returns = {}
                        for equity in equities:
                            returns[equity] = self.RSI(equity, 13)
                        s_e = sorted([item for item in returns.items() if item[1] is not None], key = lambda x: x[1], reverse = False)
                        t3e = s_e[:1]
                        for i in self.HT50.keys():
                            if self.HT50[i] == 0:
                                self.HT50[i] = self.PT3
                                self.HTS50[i].append(t3e[0][0])
                                break
                    else:
                        if self.RSI('TECL', 10) > 79:
                            equities = ["UVXY", "VIXY"]
                            returns = {}
                            for equity in equities:
                                returns[equity] = self.RSI(equity, 13)
                            s_e = sorted([item for item in returns.items() if item[1] is not None], key = lambda x: x[1], reverse = False)
                            t3e = s_e[:1]
                            for i in self.HT50.keys():
                                if self.HT50[i] == 0:
                                    self.HT50[i] = self.PT3
                                    self.HTS50[i].append(t3e[0][0])
                                    break   
                        else:
                            if self.RSI('VOOG', 10) > 79:
                                equities = ["UVXY", "VIXY"]
                                returns = {}
                                for equity in equities:
                                    returns[equity] = self.RSI(equity, 13)
                                s_e = sorted([item for item in returns.items() if item[1] is not None], key = lambda x: x[1], reverse = False)
                                t3e = s_e[:1]
                                for i in self.HT50.keys():
                                    if self.HT50[i] == 0:
                                        self.HT50[i] = self.PT3
                                        self.HTS50[i].append(t3e[0][0])
                                        break     
                            else:
                                if self.RSI('VOOV', 10) > 79:
                                    equities = ["UVXY", "VIXY"]
                                    returns = {}
                                    for equity in equities:
                                        returns[equity] = self.RSI(equity, 13)
                                    s_e = sorted([item for item in returns.items() if item[1] is not None], key = lambda x: x[1], reverse = False)
                                    t3e = s_e[:1]
                                    for i in self.HT50.keys():
                                        if self.HT50[i] == 0:
                                            self.HT50[i] = self.PT3
                                            self.HTS50[i].append(t3e[0][0])
                                            break
                                else:
                                    if self.RSI('XLP', 10) > 75:
                                        equities = ["UVXY", "VIXY"]
                                        returns = {}
                                        for equity in equities:
                                            returns[equity] = self.RSI(equity, 13)
                                        s_e = sorted([item for item in returns.items() if item[1] is not None], key = lambda x: x[1], reverse = False)
                                        t3e = s_e[:1]
                                        for i in self.HT50.keys():
                                            if self.HT50[i] == 0:
                                                self.HT50[i] = self.PT3
                                                self.HTS50[i].append(t3e[0][0])
                                                break       
                                    else:
                                        if self.RSI('TQQQ', 10) > 79:
                                            equities = ["UVXY", "VIXY"]
                                            returns = {}
                                            for equity in equities:
                                                returns[equity] = self.RSI(equity, 13)
                                            s_e = sorted([item for item in returns.items() if item[1] is not None], key = lambda x: x[1], reverse = False)
                                            t3e = s_e[:1]
                                            for i in self.HT50.keys():
                                                if self.HT50[i] == 0:
                                                    self.HT50[i] = self.PT3
                                                    self.HTS50[i].append(t3e[0][0])
                                                    break          
                                        else:
                                            if self.RSI('XLY', 10) > 80:
                                                equities = ["UVXY", "VIXY"]
                                                returns = {}
                                                for equity in equities:
                                                    returns[equity] = self.RSI(equity, 13)
                                                s_e = sorted([item for item in returns.items() if item[1] is not None], key = lambda x: x[1], reverse = False)
                                                t3e = s_e[:1]
                                                for i in self.HT50.keys():
                                                    if self.HT50[i] == 0:
                                                        self.HT50[i] = self.PT3
                                                        self.HTS50[i].append(t3e[0][0])
                                                        break   
                                            else:
                                                if self.RSI('FAS', 10) > 80:
                                                    equities = ["UVXY", "VIXY"]
                                                    returns = {}
                                                    for equity in equities:
                                                        returns[equity] = self.RSI(equity, 13)
                                                    s_e = sorted([item for item in returns.items() if item[1] is not None], key = lambda x: x[1], reverse = False)
                                                    t3e = s_e[:1]
                                                    for i in self.HT50.keys():
                                                        if self.HT50[i] == 0:
                                                            self.HT50[i] = self.PT3
                                                            self.HTS50[i].append(t3e[0][0])
                                                            break  
                                                else:
                                                    if self.RSI('SPY', 10) > 80:
                                                        equities = ["UVXY", "VIXY"]
                                                        returns = {}
                                                        for equity in equities:
                                                            returns[equity] = self.RSI(equity, 13)
                                                        s_e = sorted([item for item in returns.items() if item[1] is not None], key = lambda x: x[1], reverse = False)
                                                        t3e = s_e[:1]
                                                        for i in self.HT50.keys():
                                                            if self.HT50[i] == 0:
                                                                self.HT50[i] = self.PT3
                                                                self.HTS50[i].append(t3e[0][0])
                                                                break     
                                                    else:
                                                        if self.CumReturn('TQQQ', 6) < -0.12:
                                                            if self.CumReturn('QQQ', 1) > 0.018: 
                                                                equities = ["UVXY", "VIXY"]
                                                                returns = {}
                                                                for equity in equities:
                                                                    returns[equity] = self.RSI(equity, 13)
                                                                s_e = sorted([item for item in returns.items() if item[1] is not None], key = lambda x: x[1], reverse = False)
                                                                t3e = s_e[:1]
                                                                for i in self.HT50.keys():
                                                                    if self.HT50[i] == 0:
                                                                        self.HT50[i] = self.PT3
                                                                        self.HTS50[i].append(t3e[0][0])
                                                                        break   
                                                            else:
                                                                self.V15B()
                                                        else:
                                                            self.V15B()

    def V15B(self):

        if self.RSI('SPY', 6) > 75:
            equities = ["UVXY", "VIXY"]
            returns = {}
            for equity in equities:
                returns[equity] = self.RSI(equity, 5)
            s_e = sorted([item for item in returns.items() if item[1] is not None], key = lambda x: x[1], reverse = False)
            t3e = s_e[:1]
            for i in self.HT50.keys():
                if self.HT50[i] == 0:
                    self.HT50[i] = self.PT3
                    self.HTS50[i].append(t3e[0][0])
                    break
        else:
            if self.RSI('BIL', 5) < self.RSI('VCIT', 5):
                for i in self.HT50.keys():
                    if self.HT50[i] == 0:
                        self.HT50[i] = self.PT3
                        self.HTS50[i].append('SOXL')
                        break  
            else:
                if self.RSI('SVXY', 11) > 60:
                    equities = ["TMF", "BIL"]
                    returns = {}
                    for equity in equities:
                        returns[equity] = self.RSI(equity, 10)
                    s_e = sorted([item for item in returns.items() if item[1] is not None], key = lambda x: x[1], reverse = False)
                    t3e = s_e[:1]
                    for i in self.HT50.keys():
                        if self.HT50[i] == 0:
                            self.HT50[i] = self.PT3
                            self.HTS50[i].append(t3e[0][0])
                            break                    
                else:
                    if self.SMADayRet('VIXM', 10) > self.SMADayRet('SVXY', 5):
                        equities = ["UVXY", "VIXY", "IEF"]
                        returns = {}
                        for equity in equities:
                            returns[equity] = self.MaxDD(equity, 3)
                        s_e = sorted([item for item in returns.items() if item[1] is not None], key = lambda x: x[1], reverse = True)
                        t3e = s_e[:1]
                        for i in self.HT50.keys():
                            if self.HT50[i] == 0:
                                self.HT50[i] = self.PT3
                                self.HTS50[i].append(t3e[0][0])
                                break       
                    else:
                        if self.SMADayRet('EDV', 11) < 0:
                            equities = ["SVXY", "TMF"]
                            returns = {}
                            for equity in equities:
                                returns[equity] = self.RSI(equity, 21)
                            s_e = sorted([item for item in returns.items() if item[1] is not None], key = lambda x: x[1], reverse = False)
                            t3e = s_e[:1]
                            for i in self.HT50.keys():
                                if self.HT50[i] == 0:
                                    self.HT50[i] = self.PT3
                                    self.HTS50[i].append(t3e[0][0])
                                    break                                
                        else:
                            equities = ["SVXY", "TMV"]
                            returns = {}
                            for equity in equities:
                                returns[equity] = self.RSI(equity, 21)
                            s_e = sorted([item for item in returns.items() if item[1] is not None], key = lambda x: x[1], reverse = False)
                            t3e = s_e[:1]
                            for i in self.HT50.keys():
                                if self.HT50[i] == 0:
                                    self.HT50[i] = self.PT3
                                    self.HTS50[i].append(t3e[0][0])
                                    break                                                                                                                                                                                                                                                                                                                                                                  


    def ExecuteTrade(self):
        group = {
            'HTS': [self.HTS50[i][0] if len(self.HTS50[i]) == 1 else self.HTS50[i] for i in self.HTS50],
            'HT': [self.HT50[i] for i in self.HT50]
        }
        df = pd.DataFrame(group)
        df = pd.concat([df])
        df['HTS'] = df['HTS'].astype(str)
        result = df.groupby(['HTS']).sum().reset_index()

        for equity in self.equities:
            if all(not pd.isnull(result.iloc[i, 0]) and not equity == result.iloc[i, 0] for i in range(len(result))):
                if self.Portfolio[equity].HoldStock:
                    self.Liquidate(equity)
        output = "*****"

        for i in range(len(result)):
            if result.iloc[i, 0]:
                percentage = round(result.iloc[i, 1] * 100, 2)
                output += "{}: {}% - ".format(result.iloc[i, 0], percentage)
        output = output.rstrip(" - ")
        self.Log(output)

        for i in range(len(result)):
            if not result.iloc[i, 1] == 0 and not result.iloc[i, 0] == 'BIL':
                percentage_equity = self.Portfolio[result.iloc[i, 0]].HoldingsValue / self.Portfolio.TotalPortfolioValue
                quantity = (result.iloc[i, 1] - percentage_equity) * self.Portfolio.TotalPortfolioValue / self.Securities[result.iloc[i, 0]].Price
                if result.iloc[i, 1] < percentage_equity and abs(result.iloc[i, 1] / percentage_equity - 1) > self.buffer_pct:
                    #self.SetHoldings(result.iloc[i, 0], result.iloc[i, 1])
                    self.MarketOnCloseOrder(result.iloc[i, 0], quantity)
                else:
                    pass

        for i in range(len(result)):
            if not result.iloc[i, 1] == 0 and not result.iloc[i, 0] == 'BIL':
                percentage_equity = self.Portfolio[result.iloc[i, 0]].HoldingsValue / self.Portfolio.TotalPortfolioValue
                quantity = (result.iloc[i, 1] - percentage_equity) * self.Portfolio.TotalPortfolioValue / self.Securities[result.iloc[i, 0]].Price
                if result.iloc[i, 1] > percentage_equity and abs(percentage_equity / result.iloc[i, 1] - 1) > self.buffer_pct:
                    #self.SetHoldings(result.iloc[i, 0], result.iloc[i, 1])
                    self.MarketOnCloseOrder(result.iloc[i, 0], quantity)
                else:
                    pass
    
        self.HT50 = {str(i).zfill(2): 0 for i in range(1, 21)}
        self.HTS50 = {str(i).zfill(2): [] for i in range(1, 21)}