| Overall Statistics |
|
Total Orders 152 Average Win 23.11% Average Loss -7.69% Compounding Annual Return 73.703% Drawdown 67.600% Expectancy 0.950 Start Equity 10000 End Equity 270839.95 Net Profit 2608.399% Sharpe Ratio 1.245 Sortino Ratio 1.25 Probabilistic Sharpe Ratio 49.586% Loss Rate 51% Win Rate 49% Profit-Loss Ratio 3.01 Alpha 0.557 Beta 0.747 Annual Standard Deviation 0.511 Annual Variance 0.261 Information Ratio 1.067 Tracking Error 0.497 Treynor Ratio 0.851 Total Fees $4176.53 Estimated Strategy Capacity $250000000.00 Lowest Capacity Asset MSTR RBGP9S2961YD Portfolio Turnover 7.01% |
from AlgorithmImports import *
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import numpy as np
import pandas as pd
# Custom fee model for 0.1% per trade
class PercentageFeeModel(FeeModel):
def GetOrderFee(self, parameters):
security = parameters.Security
order = parameters.Order
fee = 0.001 * security.Price * abs(order.Quantity)
currency = security.QuoteCurrency.Symbol # Correctly get the currency code
return OrderFee(CashAmount(fee, currency))
class MLTradingAlgorithm(QCAlgorithm):
def Initialize(self):
# Algorithm Parameters
self.SetStartDate(2019, 1, 1) # Start date
self.SetEndDate(2024, 12, 31) # End date
self.SetCash(10000) # Initial capital
# Configurable ticker symbols with defaults
self.trading_ticker = self.GetParameter("trading_ticker", "MSTR")
self.benchmark_ticker = self.GetParameter("benchmark_ticker", "SPY")
# Add trading equity with custom fee and slippage models
trading_security = self.AddEquity(self.trading_ticker, Resolution.Daily)
trading_security.SetFeeModel(PercentageFeeModel())
trading_security.SetSlippageModel(ConstantSlippageModel(0))
self.symbol = trading_security.Symbol
# Add benchmark equity with custom fee and slippage models
benchmark_security = self.AddEquity(self.benchmark_ticker, Resolution.Daily)
benchmark_security.SetFeeModel(PercentageFeeModel())
benchmark_security.SetSlippageModel(ConstantSlippageModel(0))
self.benchmark_symbol = benchmark_security.Symbol
# RollingWindow to store 200 days of TradeBar data for trading asset
self.data = RollingWindow[TradeBar](200)
# Warm-up period
self.SetWarmUp(200)
# Initialize Random Forest model
self.model = RandomForestClassifier(random_state=42)
self.training_count = 0
self.is_model_trained = False # Tracks if the model is trained
# Schedule training every Monday at 10:00 AM
self.Schedule.On(self.DateRules.Every(DayOfWeek.Monday),
self.TimeRules.At(10, 0),
self.TrainModel)
# Initialize variables for benchmarking
self.previous_portfolio_value = None
self.previous_benchmark_close = None
self.beat_benchmark_count = 0
def OnData(self, data):
# Ensure data exists for trading symbol
if not data.ContainsKey(self.symbol):
return
trade_bar = data[self.symbol]
if trade_bar is None:
return
# Add TradeBar to Rolling Window
self.data.Add(trade_bar)
# Check if RollingWindow is ready
if not self.data.IsReady or self.data.Count < 200:
return
# Ensure model is trained before making predictions
if not self.is_model_trained:
self.Debug("Model is not trained yet. Skipping prediction.")
return
# 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)
# Make predictions using probability threshold
try:
prob_class = self.model.predict_proba(latest_features)[0][1] # Probability of class 1
prediction = 1 if prob_class > 0.5 else 0
except Exception as e:
self.Debug(f"Error: Model prediction failed. {e}")
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)
# Benchmarking against benchmark symbol
if self.benchmark_symbol in data and data[self.benchmark_symbol] is not None:
current_benchmark_close = data[self.benchmark_symbol].Close
current_portfolio_value = self.Portfolio.TotalPortfolioValue
# Calculate daily returns if previous values are available
if self.previous_portfolio_value is not None and self.previous_benchmark_close is not None:
strategy_return = (current_portfolio_value - self.previous_portfolio_value) / self.previous_portfolio_value
benchmark_return = (current_benchmark_close - self.previous_benchmark_close) / self.previous_benchmark_close
if strategy_return > benchmark_return:
self.beat_benchmark_count += 1
# Update previous values
self.previous_portfolio_value = current_portfolio_value
self.previous_benchmark_close = current_benchmark_close
def TrainModel(self):
# 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
# Split data chronologically (no shuffle)
X = df.iloc[:, :-1] # Features
y = df.iloc[:, -1] # Target (0 or 1)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, shuffle=False, random_state=42
)
# Train Random Forest model
self.model.fit(X_train, y_train)
self.is_model_trained = True
# Evaluate model performance
y_train_prob = self.model.predict_proba(X_train)[:, 1]
y_train_pred_binary = [1 if val > 0.5 else 0 for val in y_train_prob]
train_accuracy = accuracy_score(y_train, y_train_pred_binary)
y_test_prob = self.model.predict_proba(X_test)[:, 1]
y_test_pred_binary = [1 if val > 0.5 else 0 for val in y_test_prob]
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 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
df.dropna(inplace=True)
return df
def OnEndOfAlgorithm(self):
# Print the number of times the strategy beat the benchmark
self.Log(f"Number of times strategy beat {self.benchmark_ticker}: {self.beat_benchmark_count}")