Overall Statistics
Total Trades
103
Average Win
4.72%
Average Loss
-4.13%
Compounding Annual Return
7.594%
Drawdown
36.700%
Expectancy
0.303
Net Profit
66.942%
Sharpe Ratio
0.352
Probabilistic Sharpe Ratio
1.836%
Loss Rate
39%
Win Rate
61%
Profit-Loss Ratio
1.14
Alpha
0
Beta
0
Annual Standard Deviation
0.224
Annual Variance
0.05
Information Ratio
0.352
Tracking Error
0.224
Treynor Ratio
0
Total Fees
$907.91
Estimated Strategy Capacity
$7800000.00
Lowest Capacity Asset
XLI RGRPZX100F39
#region imports
from AlgorithmImports import *
#endregion
class VerticalNadionShield(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2016, 1, 1)  # Set Start Date
        self.SetEndDate(2022,12,31)    # set end date
        self.SetCash(100000)  # Set Strategy Cash
        self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Margin)
        self.leverage = 1
        
  
        #self.equities = ["SPY", "SCZ"]
        #self.equities = ["SPY", "SCZ", "XLV", "XLK", "XLI", "XLU", "XLF", "XLY", "XLP", "XLB", "XLE", "PSR", "IYZ", "USO"]
        self.equities = ["XLV", "XLK", "XLI", "XLU", "XLF", "XLY", "XLP", "XLB", "XLE", "PSR", "IYZ", "USO"]

        self.equityCombinedMomentum = {}
        
        #self.bonds = ["TLT", "TIP"]
        self.bonds = ["TLT", "TIP", "BIL", "AGG"]
        self.bondCombinedMomentum = {}

            
        for equity in self.equities:
            self.AddEquity(equity, Resolution.Hour)
            self.Securities[equity].SetDataNormalizationMode(DataNormalizationMode.TotalReturn)
            self.equityCombinedMomentum[equity] = CombinedMomentum(self, equity)

        for bond in self.bonds:
            self.AddEquity(bond, Resolution.Hour)
            self.Securities[bond].SetDataNormalizationMode(DataNormalizationMode.TotalReturn)
            self.bondCombinedMomentum[bond] = CombinedMomentum(self, bond)
            
        self.SetWarmUp(125)

    def shiftAssets(self, target):
        if not (self.Portfolio[target].Invested):
            for symbol in self.Portfolio.Keys:
                self.Liquidate(symbol)
            if not self.Portfolio.Invested:
                self.MarketOnCloseOrder(target, self.CalculateOrderQuantity(target, 1 * self.leverage))


    def getMonthLastTradingDay(self):
        month_last_day = DateTime(self.Time.year, self.Time.month, DateTime.DaysInMonth(self.Time.year, self.Time.month))
        tradingDays = self.TradingCalendar.GetDaysByType(TradingDayType.BusinessDay, DateTime(self.Time.year, self.Time.month, 1), month_last_day)
        tradingDays = [day.Date.date() for day in tradingDays]
        return tradingDays[-1]
        

    def OnData(self, data):
        if self.IsWarmingUp:
            return

        if (self.Time.date() == self.getMonthLastTradingDay()) and (self.Time.hour == 15):
            topEquities = sorted(self.equityCombinedMomentum.items(), key=lambda x: x[1].getValue(), reverse=True)
            topBonds = sorted(self.bondCombinedMomentum.items(), key=lambda x: x[1].getValue(), reverse=True)
            if (topEquities[0][1].getValue() > 0):
                self.shiftAssets(topEquities[0][0])
            else:
                self.shiftAssets(topBonds[0][0])
        

class CombinedMomentum():
    def __init__(self, algo, symbol):
        self.fst = algo.MOMP(symbol,  21, Resolution.Daily)     # 1 mos
        self.med = algo.MOMP(symbol,  63, Resolution.Daily)     # 3 mos
        self.slw = algo.MOMP(symbol,  126, Resolution.Daily)    # 6 mos
        
    def getValue(self):
        value = (self.fst.Current.Value + self.med.Current.Value + self.slw.Current.Value) / 3
        return value