Overall Statistics |
Total Trades 106 Average Win 0.10% Average Loss -0.05% Compounding Annual Return 49.367% Drawdown 9.200% Expectancy 1.458 Net Profit 9.548% Sharpe Ratio 1.816 Probabilistic Sharpe Ratio 62.539% Loss Rate 22% Win Rate 78% Profit-Loss Ratio 2.14 Alpha -1.562 Beta 0.309 Annual Standard Deviation 0.191 Annual Variance 0.037 Information Ratio -16.561 Tracking Error 0.352 Treynor Ratio 1.124 Total Fees $106.35 Estimated Strategy Capacity $33000000.00 Lowest Capacity Asset LUV R735QTJ8XC9X Portfolio Turnover 1.90% |
""" ML-based algorithm with regime features """ from AlgorithmImports import * import numpy as np import pandas as pd pd.set_option('mode.use_inf_as_na', True) from sklearn.ensemble import GradientBoostingClassifier class MLRegimes(QCAlgorithm): def Initialize(self): self.SetStartDate(2023, 1, 1) self.SetCash(100000) self.lookbacks = [1, 4, 13, 26, 52] # Different lookbacks used for indicators self.training_len = 52 * 10 # Total weeks of data used for training self.period = 5 # Trading frequency in days self.resolution = Resolution.Daily self.tickers = ["NVDA","XOM","AMZN","GOOGL","JPM","NEE","AMD","LUV","HD","DIS"] [self.AddEquity(t, self.resolution) for t in self.tickers] self.vix = self.AddData(CBOE, "VIX", Resolution.Daily).Symbol self.tlt = self.AddData(CBOE, "TLT", Resolution.Daily).Symbol self.gld = self.AddData(CBOE, "GLD", Resolution.Daily).Symbol self.dbc = self.AddData(CBOE, "DBC", Resolution.Daily).Symbol self.qqq = self.AddData(CBOE, "QQQ", Resolution.Daily).Symbol self.SetBenchmark(self.tickers[0]) self.model = GradientBoostingClassifier(n_iter_no_change=3) every_week = self.DateRules.WeekStart(self.tickers[0]) self.Train(every_week, self.TimeRules.At(9, 0), self.train) at_market_open = self.TimeRules.AfterMarketOpen(self.tickers[0], 0) self.Schedule.On(every_week, at_market_open, self.trade) def train(self): features, log_returns = self.get_data(self.training_len) target = log_returns > 0 # Up/Down target self.model.fit(features, target, sample_weight=abs(log_returns)) self.Debug(f"{self.Time} Training - Pts.: {target.value_counts()}\n") def trade(self): self.Transactions.CancelOpenOrders() n_points = max(self.lookbacks) + 1 x_pred = self.get_data(n_points, include_y=False).groupby("symbol").last() proba_up = self.model.predict_proba(x_pred)[:, 1] # Selecting probability of 1 x_pred_symbols = x_pred.index.get_level_values("symbol") positions = pd.Series(proba_up, index=x_pred_symbols) if positions.sum() > 1: positions /= positions.sum() # Capping positions at 100% (no leverage) for sym, pos in positions.items(): self.SetHoldings(sym, pos) self.Debug(f"{self.Time} Trading\n{sym}\nPos: {pos:.1%}") self.Plot("Positions", sym, pos) def get_data(self, n_points, include_y=True): data = self.History(self.tickers, n_points * self.period, self.resolution) # *5 to convert weeks -> days x = pd.DataFrame() for l in self.lookbacks: roll_high = data["high"].rolling(l*self.period).max() roll_low = data["low"].rolling(l*self.period).min() x[f"range_{l}"] = (data["close"] - roll_low) / (roll_high - roll_low) vix_data = self.History(self.vix, n_points * self.period, self.resolution) # Getting current VIX value x = x.join(vix_data.droplevel("symbol")["close"].rename("vix")) x.dropna(inplace=True) if include_y: y = data["close"].groupby("symbol").pct_change(self.period) y = y.apply(np.log1p).shift(-self.period).reindex(index=x.index).dropna() return x.loc[y.index], y else: return x