Overall Statistics
Total Trades
390
Average Win
4.24%
Average Loss
-0.75%
Compounding Annual Return
67.983%
Drawdown
49.000%
Expectancy
3.274
Net Profit
4063.340%
Sharpe Ratio
1.788
Probabilistic Sharpe Ratio
91.919%
Loss Rate
36%
Win Rate
64%
Profit-Loss Ratio
5.67
Alpha
0.486
Beta
-0.073
Annual Standard Deviation
0.271
Annual Variance
0.074
Information Ratio
1.672
Tracking Error
0.283
Treynor Ratio
-6.678
Total Fees
$70466.19
Estimated Strategy Capacity
$12000.00
Lowest Capacity Asset
ETHUSD XJ
# region imports
from AlgorithmImports import *
# endregion

class AdaptableYellowLeopard(QCAlgorithm):

    def Initialize(self):
        start = datetime(2016, 1, 1)   # Start date
        end = datetime(2024, 12, 30)    # End date
        cash = 1000000                  # Starting capital
        self.period = 200               # rolling period to consider for drawup and drawdown calculations
        self.SetWarmUp(200)

        self.SetStartDate(start)  # Set Start Date
        self.SetEndDate(end)
        self.SetCash(cash)  # Set Strategy Cash


        self.AddEquity("LQD", Resolution.Minute)

        #GOLD
        self.AddEquity("GLD", Resolution.Minute)
        self.AddEquity("IAU", Resolution.Minute)
        self.AddEquity("IAUM", Resolution.Minute)

        #TREASURY
        self.AddEquity("VTIP", Resolution.Minute)
        self.AddEquity("EDV", Resolution.Minute)
        self.AddEquity("IEI", Resolution.Minute)
        self.AddEquity("TIP", Resolution.Minute)
        self.AddEquity("LTPZ", Resolution.Minute)

        self.AddEquity("SPY", Resolution.Minute)
        self.AddEquity("IEF", Resolution.Minute)
        self.AddEquity("QQQ", Resolution.Minute)



        self.AddEquity("KR", Resolution.Minute)
        self.AddEquity("WMT", Resolution.Minute)
        self.AddEquity("TSCO", Resolution.Minute)
        self.AddEquity("HD", Resolution.Minute)
        self.AddEquity("GIS", Resolution.Minute)
        self.AddEquity("NEE", Resolution.Minute)
        self.AddEquity("AAPL", Resolution.Minute)
        self.AddEquity("AJG", Resolution.Minute)
        self.AddEquity("ORCL", Resolution.Minute)
        self.AddEquity("MSFT", Resolution.Minute)
        self.AddEquity("RJF", Resolution.Minute)
        self.AddEquity("SCHW", Resolution.Minute)
        # self.AddEquity("ODFL", Resolution.Minute)
        self.AddEquity("CVBF", Resolution.Minute)
        self.AddEquity("CB", Resolution.Minute)
        
        #BLOCK
        self.AddCrypto("BTCUSD", Resolution.Minute)
        self.AddCrypto("ETHUSD", Resolution.Minute)


        #NO DIV
        self.AddEquity("TSLA", Resolution.Minute)
        # self.AddEquity("NFLX", Resolution.Minute)
        # self.AddEquity("TMUS", Resolution.Minute)

        # self.AddEquity("VBR", Resolution.Minute)
        # self.AddEquity("SHY", Resolution.Minute)
        # self.AddEquity("BIL", Resolution.Minute)
        # self.AddEquity("TLT", Resolution.Minute)
        # self.AddEquity("HYG", Resolution.Minute)
        # self.AddEquity("VCLT", Resolution.Minute)
        # self.AddEquity("ETHE", Resolution.Minute)
        # self.AddEquity("LQD", Resolution.Minute).SetDataNormalizationMode(DataNormalizationMode.Raw)
        # self.AddEquity("VWEHX", Resolution.Minute)
        # self.btc = self.AddCrypto("BTCUSD", Resolution.Minute).Symbol



        self.high = RollingWindow[float](self.period)
        self.low = RollingWindow[float](self.period)
        self.close = RollingWindow[float](self.period)

        self.state1 = False
        self.state2 = False
        self.state3 = False

        # self.spy
        # self.Schedule.On(self.DateRules.MonthEnd("SPY", 1), 
        self.Schedule.On(self.DateRules.EveryDay("SPY"),
            self.TimeRules.BeforeMarketClose("SPY", 2),
            self.FunctionBeforeMarketClose)

    def OnData(self, slice: Slice):
        pass

    def LiquidateExisting(self, symbols):

        for symbol in symbols:

            quantity = self.Portfolio[symbol].Quantity 

            if quantity != 0:
                self.MarketOrder(symbol, -quantity, tag=f"Rebalancing {symbol}")

    def FunctionBeforeMarketClose(self):

        self.close = self.Securities['LQD'].Close
        self.high.Add(self.Securities['LQD'].High)  
        self.low.Add(self.Securities['LQD'].Low)

        # self.close = self.Securities['HYG'].Close
        # self.high.Add(self.Securities['HYG'].High)  
        # self.low.Add(self.Securities['HYG'].Low)

        if self.high.IsReady:
            dd = abs((self.close / max(self.high)) - 1) * 100
            du = (1 - (min(self.low) / self.close)) * 100

            # if dd == 0:
            #     dd = .01    
                # dd = 10000
            if du == 0:
                du = 1000
                # du = .0001   

        # if dd != 0:
            # ddd = dd
            ddd = dd / du

            if ddd < 1 and not self.state1:
                # 100% SPY
                self.LiquidateExisting([ 'IAU', 'IEF','GLD', 'EDV', 'VTIP', 'IEI'])

                # quantity = self.CalculateOrderQuantity("GLD", .30)
                # self.MarketOrder('GLD', quantity, tag='.2% GLD')

                quantity = self.CalculateOrderQuantity("BTCUSD", .1)
                self.MarketOrder('BTCUSD', quantity, tag='.05% BTCUSD')
                quantity = self.CalculateOrderQuantity("ETHUSD", .1)
                self.MarketOrder('ETHUSD', quantity, tag='.05% ETHUSD')


                quantity = self.CalculateOrderQuantity("SPY", .2)
                self.MarketOrder('SPY', quantity, tag='.05% SPY')

                # quantity = self.CalculateOrderQuantity("KR", .05)
                # self.MarketOrder('KR', quantity, tag='100% KR')
                # quantity = self.CalculateOrderQuantity("WMT", .05)
                # self.MarketOrder('WMT', quantity, tag='100% WMT')
                # quantity = self.CalculateOrderQuantity("TSCO", .05)
                # self.MarketOrder('TSCO', quantity, tag='100% TSCO')
                # quantity = self.CalculateOrderQuantity("HD", .05)
                # self.MarketOrder('HD', quantity, tag='100% HD')
                # quantity = self.CalculateOrderQuantity("GIS", .05)
                # self.MarketOrder('GIS', quantity, tag='100% GIS')
                # quantity = self.CalculateOrderQuantity("AJG", .05)
                # self.MarketOrder('AJG', quantity, tag='100% AJG')
                # quantity = self.CalculateOrderQuantity("RJF", .05)
                # self.MarketOrder('RJF', quantity, tag='100% RJF')
                # quantity = self.CalculateOrderQuantity("SCHW", .05)
                # self.MarketOrder('SCHW', quantity, tag='100% SCHW')
                # quantity = self.CalculateOrderQuantity("CVBF", .05)
                # self.MarketOrder('CVBF', quantity, tag='100% CVBF')
                # quantity = self.CalculateOrderQuantity("AAPL", .05)
                # self.MarketOrder('AAPL', quantity, tag='100% AAPL')
                # quantity = self.CalculateOrderQuantity("NEE", .05)
                # self.MarketOrder('NEE', quantity, tag='100% NEE')
                # quantity = self.CalculateOrderQuantity("ORCL", .05)
                # self.MarketOrder('ORCL', quantity, tag='100% ORCL')
                # quantity = self.CalculateOrderQuantity("CB", .05)
                # self.MarketOrder('CB', quantity, tag='100% CB')

                    # 'KR','WMT',  'TSCO'  ,                                      \
                    # 'HD', 'GIS', 'ODFL'                                   \
                    # 'AJG',  'RJF', 'SCHW', 'CVBF'                                                   \
                    # 'AAPL', 'NEE', 'ORCL'  


                # quantity = self.CalculateOrderQuantity("HD", .16)
                # self.MarketOrder('HD', quantity, tag='100% HD')
                # quantity = self.CalculateOrderQuantity("AAPL", .17)
                # self.MarketOrder('AAPL', quantity, tag='100% AAPL')
                # quantity = self.CalculateOrderQuantity("TSCO", .17)
                # self.MarketOrder('TSCO', quantity, tag='100% TSCO')

                quantity = self.CalculateOrderQuantity("AAPL", .2)
                self.MarketOrder('AAPL', quantity, tag='100% AAPL')
                quantity = self.CalculateOrderQuantity("GLD", .2)
                self.MarketOrder('GLD', quantity, tag='100% GLD')
                # # quantity = self.CalculateOrderQuantity("WMT", .20)
                # # self.MarketOrder('WMT', quantity, tag='100% WMT')
                # quantity = self.CalculateOrderQuantity("TSCO", .15)
                # self.MarketOrder('WMT', quantity, tag='100% WMT')
                # # quantity = self.CalculateOrderQuantity("AMZN", .30)
                # # self.MarketOrder('AMZN', quantity, tag='100% AMZN')
                # # quantity = self.CalculateOrderQuantity("NFLX", .25)
                # # self.MarketOrder('NFLX', quantity, tag='100% NFLX')
                quantity = self.CalculateOrderQuantity("TSLA", .2)
                self.MarketOrder('TSLA', quantity, tag='100% TSLA')





                # quantity = self.CalculateOrderQuantity("SPY", .8)
                # self.MarketOrder('SPY', quantity, tag='100% SPY')
                # quantity = self.CalculateOrderQuantity("QQQ", .67)
                # self.MarketOrder('QQQ', quantity, tag='100% QQQ')

                # quantity = self.CalculateOrderQuantity("VBR", 0.1)
                # self.MarketOrder('VBR', quantity, tag='40% VBR')
                # quantity = self.CalculateOrderQuantity("IEF", 0.1)
                # self.MarketOrder('IEF', quantity, tag='40% IEF')

                self.state1 = True
                self.state2 = False
                self.state3 = False

            if ddd >= 1 and not self.state2:
                # 50% BIL, 40% TLT, 10% IAU
                self.LiquidateExisting([  'IAU','GLD',        \
                    'SPY','QQQ', \
                    'ETHUSD', \
                    'BTCUSD',    \

                    # 'KR','WMT',  'TSCO'  ,                                      \
                    # 'HD', 'GIS',                                  \
                    # 'AJG',  'RJF', 'SCHW', 'CVBF',              \
                    # 'AAPL', 'NEE', 'ORCL',           \
                    # 'CB',                    \
                    'TSLA', 'AAPL' \
                    # 'DLTR', 'NFLX',  'TMUS',  'TUP', 'AMZN', 'TSLA',  'AAPL'               \
                        ])
                # self.LiquidateExisting(['BTCUSD'])
                # self.LiquidateExisting(['QQQ'])

                # quantity = self.CalculateOrderQuantity("EDV", .5)
                # self.MarketOrder('IEF', quantity, tag='40% IEF')

                quantity = self.CalculateOrderQuantity("GLD", 0.5)
                self.MarketOrder('GLD', quantity, tag='10% GLD')


                # quantity = self.CalculateOrderQuantity("BIL", .45)
                # self.MarketOrder('BIL', quantity, tag='50% BIL')
                
                quantity = self.CalculateOrderQuantity("VTIP", .5)
                self.MarketOrder('VTIP', quantity, tag='50% VTIP')
                # quantity = self.CalculateOrderQuantity("TLT", 0.5)
                # self.MarketOrder('TLT', quantity, tag='40% TLT')


                # quantity = self.CalculateOrderQuantity("IAU", 0.5)
                # self.MarketOrder('IAU', quantity, tag='10% IAU')

                # quantity = self.CalculateOrderQuantity("VBR", 0.33)
                # self.MarketOrder('VBR', quantity, tag='10% VBR')

                # quantity = self.CalculateOrderQuantity("BIL", 0.25)
                # self.MarketOrder('BIL', quantity, tag='50% BIL')

                # quantity = self.CalculateOrderQuantity("TLT", 0.2)
                # self.MarketOrder('TLT', quantity, tag='40% TLT')



                self.state2 = True
                self.state1 = False
                self.state3 = False

            # if 1.2 >= ddd >= 0.8 and not self.state3:
            #     # 50% SPY, 50% BIL
            #     self.LiquidateExisting(['TLT', 'IAU'])

            #     quantity = self.CalculateOrderQuantity("SPY", 0.25)
            #     self.MarketOrder('SPY', quantity, tag='50% SPY')

            #     quantity = self.CalculateOrderQuantity("BIL", 0.25)
            #     self.MarketOrder('BIL', quantity, tag='50% BIL')
            #     self.state3 = True
            #     self.state1 = False
            #     self.state2 = False