Overall Statistics
Total Trades
742
Average Win
0.65%
Average Loss
-1.40%
Compounding Annual Return
6.307%
Drawdown
32.000%
Expectancy
0.231
Net Profit
267.114%
Sharpe Ratio
0.544
Probabilistic Sharpe Ratio
0.873%
Loss Rate
16%
Win Rate
84%
Profit-Loss Ratio
0.47
Alpha
0.058
Beta
-0.002
Annual Standard Deviation
0.106
Annual Variance
0.011
Information Ratio
-0.069
Tracking Error
0.206
Treynor Ratio
-25.352
Total Fees
$1825.29
Estimated Strategy Capacity
$4200000.00
 
 
# 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"]
        
        self.tracked_symbol = None
        for symbol in self.symbols:
            self.AddEquity(symbol, Resolution.Daily)
            self.data[symbol] = self.SMA(symbol, period, Resolution.Daily)
            self.tracked_symbol = symbol

        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 x[1].IsReady 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:
            if symbol == self.tracked_symbol:
                self.Log(self.data[symbol].Current.Value)
                
            self.SetHoldings(symbol, 1 / len(long))