Overall Statistics |
Total Trades 223 Average Win 1.64% Average Loss -0.70% Compounding Annual Return 13.087% Drawdown 19.400% Expectancy 0.618 Net Profit 85.058% Sharpe Ratio 0.836 Probabilistic Sharpe Ratio 29.585% Loss Rate 52% Win Rate 48% Profit-Loss Ratio 2.35 Alpha 0.097 Beta -0.013 Annual Standard Deviation 0.115 Annual Variance 0.013 Information Ratio -0.101 Tracking Error 0.196 Treynor Ratio -7.422 Total Fees $6560.17 Estimated Strategy Capacity $30000000.00 Lowest Capacity Asset SPY R735QTJ8XC9X |
9#region imports from AlgorithmImports import * from statsmodels.tsa.regime_switching.markov_regression import MarkovRegression #endregion class HMMDemo(QCAlgorithm): def Initialize(self): #1. Required: Five years of backtest history self.SetStartDate(2016, 1, 1) self.SetEndDate(2021, 1, 1) #2. Required: Alpha Streams Models: self.SetBrokerageModel(BrokerageName.AlphaStreams) #3. Required: Significant AUM Capacity self.SetCash(1000000) #4. Required: Benchmark to SPY self.SetBenchmark("SPY") self.assets = ["SPY", "TLT"] # "TLT" as fix income in out-of-market period (high volatility) # Add Equity ------------------------------------------------ for ticker in self.assets: self.AddEquity(ticker, Resolution.Minute) # Set Scheduled Event Method For Kalman Filter updating. self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.BeforeMarketClose("SPY", 5), self.EveryDayBeforeMarketClose) def EveryDayBeforeMarketClose(self): qb = self # Get history history = qb.History(["SPY"], datetime(2010, 1, 1), datetime.now(), Resolution.Daily) # Get the close price daily return. close = history['close'].unstack(level=0) # Call pct_change to obtain the daily return returns = close.pct_change().iloc[1:] # Initialize the HMM, then fit by the standard deviation data. model = MarkovRegression(returns, k_regimes=2, switching_variance=True).fit() # Obtain the market regime regime = model.smoothed_marginal_probabilities.values.argmax(axis=1)[-1] # ============================== if regime == 0: self.SetHoldings([PortfolioTarget("TLT", 0.), PortfolioTarget("SPY", 1.)]) self.SetHoldings else: self.SetHoldings([PortfolioTarget("TLT", 1.), PortfolioTarget("SPY", 0.)])