Overall Statistics
Total Trades
749
Average Win
0.65%
Average Loss
-1.45%
Compounding Annual Return
5.953%
Drawdown
32.000%
Expectancy
0.218
Net Profit
237.126%
Sharpe Ratio
0.509
Probabilistic Sharpe Ratio
0.566%
Loss Rate
16%
Win Rate
84%
Profit-Loss Ratio
0.45
Alpha
0.055
Beta
0
Annual Standard Deviation
0.108
Annual Variance
0.012
Information Ratio
-0.073
Tracking Error
0.208
Treynor Ratio
-757.478
Total Fees
$1884.32
# 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.

class AssetClassTrendFollowing(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2000, 1, 1)  
        self.SetCash(100000)  
        
        self.data = {}
        period = 10 * 21
        self.SetWarmUp(period)
        
        self.symbols = ["SPY", "EFA", "IEF", "VNQ", "GSG"]
        
        for symbol in self.symbols:
            self.AddEquity(symbol, Resolution.Daily)
            self.data[symbol] = self.SMA(symbol, period, Resolution.Daily)

        self.Schedule.On(self.DateRules.MonthStart(self.symbols[0]), self.TimeRules.AfterMarketOpen(self.symbols[0]), self.Rebalance)
        
    def Rebalance(self):
        long = [x[0] for x in self.data.items() if self.Securities.ContainsKey(x[0]) and self.Securities[x[0]].Price > x[1].Current.Value]
        
        # Trade execution.
        invested = [x.Key.Value for x in self.Portfolio if x.Value.Invested]
        for symbol in invested:
            if symbol not in long:
                self.Liquidate(symbol)
    
        for symbol in long:
            self.SetHoldings(symbol, 1 / len(long))