Overall Statistics
Total Orders
918
Average Win
0.62%
Average Loss
-1.32%
Compounding Annual Return
6.202%
Drawdown
29.400%
Expectancy
0.230
Start Equity
100000
End Equity
448495.37
Net Profit
348.495%
Sharpe Ratio
0.249
Sortino Ratio
0.245
Probabilistic Sharpe Ratio
0.058%
Loss Rate
16%
Win Rate
84%
Profit-Loss Ratio
0.47
Alpha
0.012
Beta
0.306
Annual Standard Deviation
0.104
Annual Variance
0.011
Information Ratio
-0.126
Tracking Error
0.143
Treynor Ratio
0.085
Total Fees
$3151.16
Estimated Strategy Capacity
$6700000.00
Lowest Capacity Asset
VNQ T2FCD04TATET
Portfolio Turnover
1.08%
# https://quantpedia.com/strategies/asset-class-trend-following/
#
# Use 5 ETFs (SPY - US stocks, EFA - foreign stocks, IEF - bonds, VNQ - REITs, 
# GSG - commodities), equal weight the portfolio. Hold asset class ETF only when 
# it is over its 10 month Simple Moving Average, otherwise stay in cash.
#
# QC implementation changes:
#   - SMA with period of 210 days is used.

#region imports
from AlgorithmImports import *
#endregion

class AssetClassTrendFollowing(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2000, 1, 1)
        self.SetCash(100000)
        
        tickers: List[str] = ["SPY", "EFA", "IEF", "VNQ", "GSG"]
        period: int = 10 * 21

        self.sma: Dict[Symbol, SimpleMovingAverage] = { 
            self.AddEquity(ticker, Resolution.Minute).Symbol : self.SMA(ticker, period, Resolution.Daily) for ticker in tickers 
        }

        self.recent_month: int = -1
        self.SetWarmUp(period, Resolution.Daily)
        self.Settings.MinimumOrderMarginPortfolioPercentage = 0.
    
    def OnData(self, data: Slice) -> None:
        if self.IsWarmingUp: return

        if not (self.Time.hour == 9 and self.Time.minute == 31):
            return

        # rebalance once a month
        if self.Time.month == self.recent_month:
            return
        self.recent_month = self.Time.month
        
        long: List[Symbol] = [ symbol for symbol, sma in self.sma.items() 
            if symbol in data 
            and data[symbol] 
            and sma.IsReady 
            and data[symbol].Value > sma.Current.Value
        ]

        portfolio: List[PortfolioTarget] = [PortfolioTarget(symbol, 1. / len(long)) for symbol in long]
        self.SetHoldings(portfolio, True)