Overall Statistics
Total Orders
59
Average Win
0.08%
Average Loss
-0.01%
Compounding Annual Return
56.116%
Drawdown
10.000%
Expectancy
8.009
Start Equity
1000000
End Equity
1074656.58
Net Profit
7.466%
Sharpe Ratio
1.916
Sortino Ratio
1.61
Probabilistic Sharpe Ratio
64.950%
Loss Rate
12%
Win Rate
88%
Profit-Loss Ratio
9.24
Alpha
0.086
Beta
0.185
Annual Standard Deviation
0.174
Annual Variance
0.03
Information Ratio
-3.079
Tracking Error
0.325
Treynor Ratio
1.798
Total Fees
$69.60
Estimated Strategy Capacity
$220000000.00
Lowest Capacity Asset
ADBE R735QTJ8XC9X
Portfolio Turnover
2.18%
"""
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(2024, 6, 1)
        self.SetEndDate(2024, 7, 29)
        self.SetCash(1000000)
        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 = ["ADBE", "AAPL", "NFLX", "MSFT","NVDA", "TSLA", "META", "JPM", "LLY"]

        [self.AddEquity(t, self.resolution) for t in self.tickers]
        self.vix = self.AddData(CBOE, "VIX", 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