Overall Statistics
Total Trades
741
Average Win
0.63%
Average Loss
-1.46%
Compounding Annual Return
5.590%
Drawdown
32.000%
Expectancy
0.207
Net Profit
210.793%
Sharpe Ratio
0.48
Probabilistic Sharpe Ratio
0.392%
Loss Rate
16%
Win Rate
84%
Profit-Loss Ratio
0.43
Alpha
0.052
Beta
-0.001
Annual Standard Deviation
0.108
Annual Variance
0.012
Information Ratio
-0.06
Tracking Error
0.208
Treynor Ratio
-97.155
Total Fees
$1830.87
# 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))