Created with Highcharts 12.1.2EquityJan 2019Jan…Jul 2019Jan 2020Jul 2020Jan 2021Jul 2021Jan 2022Jul 2022Jan 2023Jul 2023Jan 2024Jul 2024Jan 20250500k1,000k-500010120200M400M05M10M405060
Overall Statistics
Total Orders
125
Average Win
22.26%
Average Loss
-6.87%
Compounding Annual Return
92.212%
Drawdown
45.800%
Expectancy
1.395
Start Equity
10000
End Equity
495042.85
Net Profit
4850.429%
Sharpe Ratio
1.538
Sortino Ratio
1.43
Probabilistic Sharpe Ratio
72.325%
Loss Rate
44%
Win Rate
56%
Profit-Loss Ratio
3.24
Alpha
0.652
Beta
0.666
Annual Standard Deviation
0.469
Annual Variance
0.22
Information Ratio
1.345
Tracking Error
0.459
Treynor Ratio
1.082
Total Fees
$555.92
Estimated Strategy Capacity
$240000000.00
Lowest Capacity Asset
MSTR RBGP9S2961YD
Portfolio Turnover
5.70%
from AlgorithmImports import *
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import numpy as np
import pandas as pd

class MLTradingAlgorithm(QCAlgorithm):
    
    def Initialize(self):
        # 1. Algorithm Parameters
        self.SetStartDate(2019, 1, 1)         # Start date
        self.SetEndDate(2024, 12, 31)         # End date
        self.SetCash(10000)                   # Initial capital

        # 2. Add MSTR Equity
        self.symbol = self.AddEquity("MSTR", Resolution.Daily).Symbol

        # 3. RollingWindow to Store 200 Days of TradeBar Data
        self.data = RollingWindow[TradeBar](200)

        # 4. Warm-Up Period
        self.SetWarmUp(200)

        # 5. Initialize Linear Regression Model
        self.model = LinearRegression()
        self.training_count = 0
        self.is_model_trained = False  # Tracks if the model is trained

        # 6. Schedule Training Every Monday at 10:00 AM
        self.Schedule.On(self.DateRules.Every(DayOfWeek.Monday), 
                         self.TimeRules.At(10, 0), 
                         self.TrainModel)
    
    def OnData(self, data):
        # 7. Ensure Data Exists
        if not data.ContainsKey(self.symbol):
            return
        
        trade_bar = data[self.symbol]
        if trade_bar is None:
            return
        
        # 8. Add TradeBar to Rolling Window
        self.data.Add(trade_bar)

        # 9. Check if RollingWindow is Ready
        if not self.data.IsReady or self.data.Count < 200:
            return
        
        # Ensure Model is Fitted Before Using It
        if not self.is_model_trained:
            self.Debug("Model is not trained yet. Skipping prediction.")
            return

        # 10. Extract Features for Prediction
        df = self.GetFeatureDataFrame()
        if df is None or len(df) < 1:
            return
        
        latest_features = df.iloc[-1, :-1].values.reshape(1, -1)
        
        # 11. Make Predictions (Convert Continuous Output to Binary)
        try:
            prediction_value = self.model.predict(latest_features)[0]
            # Threshold at 0.5 => If > 0.5 → Buy; Otherwise → Sell
            prediction = 1 if prediction_value > 0.5 else 0
        except:
            self.Debug("Error: Model prediction failed.")
            return
        
        # Trading Logic
        holdings = self.Portfolio[self.symbol].Quantity
        
        # Buy if prediction = 1 and not currently invested
        if prediction == 1 and holdings <= 0:
            self.SetHoldings(self.symbol, 1) 
        # Sell if prediction = 0 and currently invested
        elif prediction == 0 and holdings > 0:
            self.Liquidate(self.symbol)

    def TrainModel(self):
        # 12. Prepare Training Data
        df = self.GetFeatureDataFrame()
        if df is None or len(df) < 50:  # Require enough data to train
            self.Debug("Insufficient data for training.")
            return

        # 13. Split Data
        X = df.iloc[:, :-1]  # Features
        y = df.iloc[:, -1]   # Target (0 or 1)
        
        # Use chronological split (no shuffle)
        X_train, X_test, y_train, y_test = train_test_split(
            X, y, test_size=0.2, shuffle=False, random_state=42
        )

        # 14. Train Linear Regression Model
        self.model.fit(X_train, y_train)
        self.is_model_trained = True

        # 15. Evaluate Model Performance (Convert Regression Output → Binary for Accuracy)
        y_train_pred = self.model.predict(X_train)
        y_train_pred_binary = [1 if val > 0.5 else 0 for val in y_train_pred]
        train_accuracy = accuracy_score(y_train, y_train_pred_binary)

        y_test_pred = self.model.predict(X_test)
        y_test_pred_binary = [1 if val > 0.5 else 0 for val in y_test_pred]
        test_accuracy = accuracy_score(y_test, y_test_pred_binary)
        
        self.training_count += 1
        self.Debug(f"Training #{self.training_count}: "
                   f"Train Accuracy: {train_accuracy:.2%}, "
                   f"Test Accuracy: {test_accuracy:.2%}")

    def GetFeatureDataFrame(self):
        # Wait until we have 200 data points in the rolling window
        if self.data.Count < 200:
            return None
        
        # Convert rolling window data (TradeBars) to a DataFrame
        close_prices = [bar.Close for bar in self.data]
        df = pd.DataFrame(close_prices, columns=["Close"])
        
        # Feature Engineering
        df["SMA_10"] = df["Close"].rolling(window=10).mean()
        df["SMA_50"] = df["Close"].rolling(window=50).mean()
        
        # RSI Calculation
        delta = df["Close"].diff()
        gain = (delta.where(delta > 0, 0)).rolling(14).mean()
        loss = (-delta.where(delta < 0, 0)).rolling(14).mean()
        rs = gain / loss
        df["RSI"] = 100 - (100 / (1 + rs))
        
        # MACD Calculation
        df["MACD"] = df["Close"].ewm(span=12, adjust=False).mean() - df["Close"].ewm(span=26, adjust=False).mean()
        df["MACD_Signal"] = df["MACD"].ewm(span=9, adjust=False).mean()

        # Historical Volatility (HV_30)
        df["HV_30"] = df["Close"].pct_change().rolling(window=30).std() * np.sqrt(252)

        # Define Target: 1 if next day's Close > today's Close, else 0
        df["Target"] = (df["Close"].shift(-1) > df["Close"]).astype(int)
        
        # Remove rows with NaN values from rolling calculations
        df.dropna(inplace=True)

        return df