Overall Statistics |
Total Trades 165 Average Win 3.50% Average Loss -2.89% Compounding Annual Return 76.159% Drawdown 30.200% Expectancy 0.133 Net Profit 30.910% Sharpe Ratio 1.417 Probabilistic Sharpe Ratio 53.226% Loss Rate 49% Win Rate 51% Profit-Loss Ratio 1.21 Alpha 0.469 Beta 0.754 Annual Standard Deviation 0.449 Annual Variance 0.201 Information Ratio 0.942 Tracking Error 0.44 Treynor Ratio 0.844 Total Fees $3332.65 Estimated Strategy Capacity $25000.00 Lowest Capacity Asset MVV TJNNZWL5I4IT Portfolio Turnover 93.70% |
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 IntelligentSkyRodent(QCAlgorithm): def Initialize(self): self.cash = 100000 self.buffer_pct = 0.03 self.SetStartDate(2023, 1, 1) self.SetEndDate(2023, 6, 23) self.SetCash(self.cash) self.equities = ['XLY', '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', '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', 'VGI', 'XLU', 'XLV', 'EUO', 'YCS', 'MVV', 'USD', 'BIL', 'TMF', 'SPXL', 'EPI', 'IYK', 'CURE', 'DIG', 'AGG', 'XLU', 'PUI'] self.MKT = self.AddEquity("UPRO",Resolution.Daily).Symbol self.mkt = [] for equity in self.equities: self.AddEquity(equity,Resolution.Minute) self.Securities[equity].SetDataNormalizationMode(DataNormalizationMode.Adjusted) self.AddEquity('BIL',Resolution.Minute) self.Securities['BIL'].SetDataNormalizationMode(DataNormalizationMode.Adjusted) self.PT1 = 0.99 self.HT1 = {str(i).zfill(2): 0 for i in range(1,10)} self.HTS1 = {str(i).zfill(2): [] for i in range(1,10)} self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.BeforeMarketClose("SPY",0.1), 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 Sort(self,sort_type,equities,period,reverse,number,multiplier): self.PT = getattr(self,f"PT{number}") * multiplier returns = {} for equity in equities: returns[equity] = getattr(self,sort_type)(equity,period) s_e = sorted([item for item in returns.items() if item[1] is not None],key = lambda x: x[1],reverse = reverse) t3e = s_e[:1] ht = getattr(self,f"HT{number}") hts = getattr(self,f"HTS{number}") for i in ht.keys(): if ht[i] == 0: ht[i] = self.PT hts[i].append(t3e[0][0]) break setattr(self,f"HT{number}",ht) setattr(self,f"HTS{number}",hts) def AH(self, equities, PTnumber, multiplier): #AppendHolding if not isinstance(equities, list): equities = [equities] HT = getattr(self, f"HT{PTnumber}") HTS = getattr(self, f"HTS{PTnumber}") PT = getattr(self, f"PT{PTnumber}") * multiplier for equity in equities: for i in HT.keys(): if HT[i] == 0: HT[i] = PT HTS[i].append(equity) break 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.bbv3045() self.ExecuteTrade() def bbv3045(self): if self.RSI('SOXL', 8) > self.RSI('UPRO', 9): self.V3042aBetaBaller() else: self.ProposalV304eBetaBaller() def V3042aBetaBaller(self): if self.RSI('BIL', 10) < self.RSI('TLH', 10): if self.RSI('SPY', 6) > 75: self.OverboughtSPSellTheRipBuyVolatility() else: if self.RSI('SOXL', 5) <= 75: self.AH('SOXL', 1, 1) else: self.AH('SOXS', 1, 1) else: if self.RSI('SPY', 6) < 27: self.ExtremelyOversoldSPLowRSIDoubleCheckWithBondMktBeforeGoingLong() else: self.V021TCCCStopTheBleedDJKeyhole() def OverboughtSPSellTheRipBuyVolatility(self): self.Sort("RSI", ["UVXY", "VIXY"], 13, False, 1, 1) def ExtremelyOversoldSPLowRSIDoubleCheckWithBondMktBeforeGoingLong(self): if self.RSI('BSV', 7) < self.RSI('SPHB', 7): self.Sort("RSI", ["SOXS", "SQQQ"], 7, False, 1, 1) else: self.Sort("RSI", ["SOXL", "TECL"], 7, False, 1, 1) def V021TCCCStopTheBleedDJKeyhole(self): if self.RSI('SPY', 10) < 30: self.FiveAndBelowDJKeyholeNoLowVolumeLETFs() else: if self.RSI('UVXY', 10) > 74: if self.RSI('UVXY', 10) > 84: self.AH('SOXL', 1, 1) else: self.AH('UVXY', 1, 1) else: self.BearStockMarketHighInflationBILandTMV202b() def FiveAndBelowDJKeyholeNoLowVolumeLETFs(self): self.Sort("SMADayRet", ["TECL", "TQQQ", "SPXL", "SOXL", "UPRO", "QLD"], 5, False, 1, 1) def BearStockMarketHighInflationBILandTMV202b(self): if self.SMADayRet('SPTL', 100) > self.SMADayRet('BIL', 100): if self.SMADayRet('SPTL', 20) < 0: self.ABBARiskOffRisingRatesTMV() else: self.ABBBRiskOffFallingRatesTMF() else: self.BLongTermTLTTrendingDownSafety() def ABBARiskOffRisingRatesTMV(self): if self.EMA('SPY', 210) <= self.SMA('SPY', 360): if self.RSI('TQQQ', 10) < 30: self.Sort("SMADayRet", ["TECL", "TQQQ", "SOXL", "UPRO"], 5, False, 1, 1) else: if self.CumReturn('SPXU', 6) <= self.CumReturn('UPRO', 3): self.Sort("CumReturn", ["SQQQ", "EUO", "YCS"], 5, True, 1, 1) else: self.Sort("SMADayRet", ["TECL", "TQQQ", "SOXL", "CURE"], 5, False, 1, 1) else: if self.RSI('TQQQ', 11) > 77: self.AH('UVXY', 1, 1) else: self.Sort("SMADayRet", ["TECL", "TQQQ", "SOXL", "UPRO", "TMV"], 5, False, 1, 1) def ABBBRiskOffFallingRatesTMF(self): if self.EMA('SPY', 210) <= self.SMA('SPY', 360): if self.RSI('TQQQ', 10) < 30: self.Sort("SMADayRet", ["TQQQ", "SOXL", "TECL"], 5, False, 1, 1) else: if self.CumReturn('UUP', 2) > 0.01: self.Sort("CumReturn", ["TECS", "SOXS", "SQQQ"], 5, True, 1, 1) else: if self.SMA('SPY', 1) > -1: self.Sort("CumReturn", ["ERX", "EUO", "YCS"], 5, True, 1, 1) else: self.Sort("SMADayRet", ["SOXL", "EWZ", "MVV", "USD"], 5, False, 1, 1) else: if self.SMA('SPY', 210) > self.SMA('DBC', 360): if self.RSI('TQQQ', 11) > 77: self.AH('UVXY', 1, 1) else: if self.CumReturn('TQQQ', 6) < -0.1: if self.CumReturn('TQQQ', 1) > 0.055: self.AH('UVXY', 1, 1) else: if self.RSI('BIL', 7) < self.RSI('IEF', 7): self.AH('SOXL', 1, 1) else: self.Sort("CumReturn", ["EWZ", "UUP", "TMF", "UCO"], 5, True, 1, 1) else: if self.RSI('BIL', 7) < self.RSI('IEF', 7): self.Sort("SMADayRet", ["TECL", "TQQQ", "SPXL", "QLD", "USD"], 5, False, 1, 1) else: self.Sort("CumReturn", ["EWZ", "UUP", "TMF"], 5, True, 1, 1) else: self.DefenseModified() def DefenseModified(self): if self.STD('DBC', 20) > self.STD('SPY', 20): self.Sort("RSI", ["SHY", "EWZ", "GLD", "SPXS", "TECS", "SOXS", "UCO", "YCS"], 5, False, 1, 1) else: if self.RSI('BIL', 7) < self.RSI('IEF', 7): self.Sort("SMADayRet", ["SOXL", "USD", "TMF"], 5, False, 1, 1) else: self.Sort("CumReturn", ["EWZ", "SPXS", "SOXS", "UCO", "YCS"], 5, True, 1, 1) def BLongTermTLTTrendingDownSafety(self): if self.SMADayRet('SPTL', 20) < 0: self.BAARiskOffRisingRatesTMVLETFBasket() else: self.BABRiskOffFallingRatesTMFLETFBasket() def BAARiskOffRisingRatesTMVLETFBasket(self): if self.EMA('SPY', 210) <= self.SMA('SPY', 360): if self.RSI('TQQQ', 10) < 30: self.Sort("SMADayRet", ["TQQQ", "SOXL", "UPRO"], 5, True, 1, 1) else: if self.CumReturn('UUP', 2) >= 0.01: self.Sort("CumReturn", ["SPXS", "TECS", "SOXS", "SQQQ", "ERX"], 5, False, 1, 1) else: if self.CumReturn('SPXU', 5) >= self.CumReturn('UPRO', 4): self.Sort("CumReturn", ["SOXS", "SQQQ", "EPI", "TMV"], 5, True, 1, 1) else: if self.CumReturn('BIL', 3) >= self.CumReturn('TMV', 3): self.Sort("SMADayRet", ["TECL", "SOXL", "TNA"], 5, False, 1, 1) else: self.Sort("SMADayRet", ["TECL", "SOXL", "TMV", "TQQQ"], 3, False, 1, 1) else: if self.SMADayRet('SPY', 210) > self.SMADayRet('DBC', 360): if self.RSI('TQQQ', 11) > 77: self.AH('UVXY', 1, 1) else: if self.CumReturn('TQQQ', 6) < -0.1: if self.CumReturn('TQQQ', 1) > 0.055: self.AH('UVXY', 1, 1) else: self.Sort("SMADayRet", ["SOXL", "TMV"], 5, False, 1, 1) else: if self.RSI('BIL', 7) < self.RSI('IEF', 7): self.Sort("SMADayRet", ["TQQQ", "SOXL", "UPRO", "TMV", "TECL"], 5, True, 1, 1) else: self.Sort("SMADayRet", ["SOXL", "UPRO"], 22, False, 1, 1) else: self.DefenseModified2() def DefenseModified2(self): if self.STD('DBC', 20) > self.STD('SPY', 20): if self.STD('DBC', 10) >= 0.03: if self.STD('TMV', 5) <= self.STD('DBC', 5): self.AH('TMV', 1, 1) else: self.AH('DBC', 1, 1) else: if self.RSI('BIL', 7) < self.RSI('IEF', 7): self.Sort("SMADayRet", ["TMV", "SOXS", "SPXU"], 5, True, 1, 1) else: self.Sort("CumReturn", ["EFA", "EEM", "SPXS", "SOXS", "UCO", "TMV"], 5, False, 1, 1) else: if self.RSI('BIL', 7) < self.RSI('IEF', 7): self.Sort("SMADayRet", ["EPI", "SOXL", "UPRO"], 5, False, 1, 1) else: self.Sort("CumReturn", ["EWZ", "TECS", "SOXS", "EUO", "YCS", "TMV"], 5, True, 1, 1) def BABRiskOffFallingRatesTMFLETFBasket(self): if self.EMA('SPY', 210) <= self.SMA('SPY', 360): if self.CumReturn('SPY', 1) <= -0.02: self.Sort("CumReturn", ["SPXS", "TECS", "SOXS", "SQQQ"], 5, True, 1, 1) else: if self.CumReturn('SPXU', 6) >= self.CumReturn('UPRO', 3): self.Sort("CumReturn", ["TMF", "TMV", "SPXS", "SQQQ"], 10, True, 1, 1) else: self.Sort("SMADayRet", ["TECL", "TQQQ", "SOXL", "EWZ", "TMF"], 5, False, 1, 1) elif self.SMA('SPY', 210) > self.SMA('DBC', 360): if self.EMA('SPY', 210) > self.EMA('SPY', 360): if self.RSI('TQQQ', 11) > 77: self.AH('UVXY', 1, 1) else: if self.CumReturn('TQQQ', 6) < -0.1: if self.CumReturn('TQQQ', 1) > 0.055: self.AH('UVXY', 1, 1) else: self.Sort("SMADayRet", ["TECL", "TQQQ", "SPXL", "EPI", "SOXL", "UPRO", "QLD", "EWZ", "MVV", "PUI", "USD", "TMF"], 7, False, 1, 1) else: if self.RSI('BIL', 7) < self.RSI('IEF', 7): self.Sort("SMADayRet", ["TECL", "SPXL", "EPI", "SOXL", "UPRO", "MVV"], 7, False, 1, 1) else: self.Sort("CumReturn", ["SOXS", "TMF"], 5, True, 1, 1) else: self.Sort("RSI", ["SPXS", "SQQQ", "TECS", "SOXS"], 5, False, 1, 1) else: self.DefenseModified3() def DefenseModified3(self): if self.STD('DBC', 20) > self.STD('SPY', 20): self.Sort("RSI", ["SPXS", "EPI", "TECS", "SOXS", "SQQQ"], 5, False, 1, 1) else: self.Sort("SMADayRet", ["TECL", "TQQQ", "SOXL", "TMF"], 5, True, 1, 1) def ProposalV304eBetaBaller(self): if self.RSI('BIL', 42) < self.RSI('IEF', 70): if self.RSI('SPY', 7) > 75: self.OverboughtSPSellRipBuyVolatility2() else: if self.Securities['SOXL'].Price > self.SMADayRet('SOXL', 2): self.Sort("SMADayRet", ["SOXL", "UPRO"], 12, True, 1, 1) else: self.Sort("SMADayRet", ["SOXS", "PSQ"], 12, True, 1, 1) else: if self.RSI('SPY', 6) < 27: self.ExtremelyOversoldSPLowRSIDoubleCheckWithBondMktBeforeGoingLong2() else: self.V021TCCCStopTheBleedDJKeyholeHalf2() def OverboughtSPSellRipBuyVolatility2(self): self.Sort("RSI", ["UVXY", "VIXY"], 12, False, 1, 1) def ExtremelyOversoldSPLowRSIDoubleCheckWithBondMktBeforeGoingLong2(self): if self.RSI('BSV', 8) < self.RSI('SPHB', 8): self.Sort("RSI", ["SOXS", "SQQQ"], 7, False, 1, 1) else: self.Sort("RSI", ["SOXL", "SPXL", "TECL", "TMF", "UPRO", "USD", "TQQQ"], 18, False, 1, 1) def V021TCCCStopTheBleedDJKeyholeHalf2(self): if self.RSI('SPY', 10) < 30: self.V12FiveAndBelowDJKeyhole() else: if self.RSI('UVXY', 10) > 74: if self.RSI('UVXY', 10) > 84: self.AH('SOXL', 1, 1) else: self.AH('UVXY', 1, 1) else: self.BearStockMarketHighInflationStripped2() def V12FiveAndBelowDJKeyhole(self): self.Sort("RSI", ["SOXL", "TECL", "TMF", "UPRO"], 17, False, 1, 1) def BearStockMarketHighInflationStripped2(self): if self.Securities['TLT'].Price > self.SMA('TLT', 200): self.ABMediumTermTLTOverbought() else: self.BIfLongTermTLTTrendingDownSafetyLongTerm2() def ABMediumTermTLTOverbought(self): if self.SMADayRet('TLT', 20) < 0: self.ABBARiskOffRisingRatesTMV() else: self.ABBBRiskOffFallingRatesTMF2() def ABBBRiskOffFallingRatesTMF2(self): if self.EMA('SPY', 210) <= self.SMA('SPY', 360): if self.RSI('TQQQ', 10) < 30: self.Sort("SMADayRet", ["TECL", "TQQQ", "SOXL"], 5, False, 1, 1) elif self.CumReturn('UUP', 2) > 0.01: self.Sort("CumReturn", ["TECS", "SOXS", "SQQQ"], 5, True, 1, 1) elif self.CumReturn('SPXU', 5) >= self.CumReturn('UPRO', 4): self.Sort("CumReturn", ["ERX", "EUO", "YCS"], 5, True, 1, 1) else: self.Sort("SMADayRet", ["SOXL", "EWZ", "MVV", "USD"], 5, False, 1, 1) elif self.SMA('SPY', 210) > self.SMA('DBC', 360): if self.RSI('TQQQ', 11) > 77: self.AH('UVXY', 1, 1) elif self.CumReturn('TQQQ', 6) < -0.1: if self.CumReturn('TQQQ', 1) > 0.055: self.AH('UVXY', 1, 1) elif self.RSI('BIL', 7) < self.RSI('IEF', 7): self.AH('SOXL', 1, 1) else: self.Sort("CumReturn", ["EWZ", "UUP", "TMF", "UCO"], 5, True, 1, 1) elif self.RSI('BIL', 7) < self.RSI('IEF', 7): self.Sort("SMADayRet", ["TECL", "TQQQ", "SPXL", "QLD", "USD"], 5, False, 1, 1) else: self.Sort("CumReturn", ["EWZ", "UUP", "TMF"], 5, True, 1, 1) else: self.DefenseModified() def BIfLongTermTLTTrendingDownSafetyLongTerm2(self): if self.SMADayRet('TLT', 20) < 0: self.BAARiskOffRisingRatesTMVLETFBasket2() else: self.BABRiskOffFallingRatesTMFLETFBasket2() def BAARiskOffRisingRatesTMVLETFBasket2(self): if self.EMA('SPY', 210) <= self.SMA('SPY', 360): if self.RSI('TQQQ', 10) < 30: self.Sort("SMADayRet", ["TQQQ", "SOXL", "UPRO"], 5, True, 1, 1) elif self.CumReturn('UUP', 2) >= 0.01: self.Sort("CumReturn", ["SPXS", "TECS", "SOXS", "SQQQ", "ERX"], 5, False, 1, 1) elif self.CumReturn('SPXU', 5) >= self.CumReturn('UPRO', 4): self.Sort("CumReturn", ["SOXS", "SQQQ", "EPI"], 5, True, 1, 1) elif self.CumReturn('BIL', 3) >= self.CumReturn('TMV', 3): self.Sort("SMADayRet", ["TECL", "SOXL", "TNA"], 5, False, 1, 1) else: self.Sort("SMADayRet", ["TECL", "SOXL", "TMV", "TQQQ"], 3, False, 1, 1) elif self.SMADayRet('SPY', 210) > self.SMADayRet('DBC', 360): if self.RSI('TQQQ', 11) > 77: self.AH('UVXY', 1, 1) elif self.CumReturn('TQQQ', 6) < -0.1: if self.CumReturn('TQQQ', 1) > 0.055: self.AH('UVXY', 1, 1) else: self.Sort("SMADayRet", ["SOXL", "TMV"], 5, False, 1, 1) elif self.RSI('BIL', 7) < self.RSI('IEF', 7): self.Sort("SMADayRet", ["TQQQ", "SOXL", "UPRO", "TMV", "TECL"], 5, True, 1, 1) else: self.Sort("SMADayRet", ["SOXL", "UPRO"], 22, False, 1, 1) else: self.DefenseModified2() def BABRiskOffFallingRatesTMFLETFBasket2(self): if self.EMA('SPY', 210) <= self.SMA('SPY', 360): if self.CumReturn('SPY', 1) <= -0.02: self.Sort("CumReturn", ["SPXS", "TECS", "SOXS", "SQQQ"], 5, True, 1, 1) else: if self.CumReturn('SPXU', 6) >= self.CumReturn('UPRO', 3): self.Sort("CumReturn", ["BIL", "AGG", "TMF"], 5, False, 1, 1) else: self.Sort("SMADayRet", ["TECL", "TQQQ", "SOXL", "EWZ", "TMF"], 5, False, 1, 1) else: if self.SMA('SPY', 210) > self.SMA('DBC', 360): if self.EMA('SPY', 210) > self.EMA('SPY', 360): if self.RSI('TQQQ', 11) > 77: self.AH('UVXY', 1, 1) else: if self.CumReturn('TQQQ', 6) < -0.1: if self.CumReturn('TQQQ', 1) > 0.055: self.AH('UVXY', 1, 1) else: self.Sort("SMADayRet", ["TECL", "TQQQ", "SPXL", "EPI", "SOXL", "UPRO", "QLD", "EWZ", "MVV", "PUI", "USD", "TMF"], 7, False, 1, 1) else: if self.RSI('BIL', 7) < self.RSI('IEF', 7): self.Sort("SMADayRet", ["TECL", "SPXL", "EPI", "SOXL", "UPRO", "MVV"], 7, False, 1, 1) else: self.Sort("CumReturn", ["SOXS", "TMF"], 5, True, 1, 1) else: self.Sort("RSI", ["SPXS", "SQQQ", "TECS", "SOXS"], 5, False, 1, 1) else: self.DefenseModified3() def ExecuteTrade(self): group1 = { 'HTS': [self.HTS1[i][0] if len(self.HTS1[i]) == 1 else self.HTS1[i] for i in self.HTS1], 'HT': [self.HT1[i] for i in self.HT1] } df1 = pd.DataFrame(group1) df = pd.concat([df1]) 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 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]) 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 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]) else: pass self.HT1 = {str(i).zfill(2): 0 for i in range(1,10)} self.HTS1 = {str(i).zfill(2): [] for i in range(1,10)}