Overall Statistics Total Trades20Average Win10.45%Average Loss-4.22%Compounding Annual Return6.416%Drawdown15.000%Expectancy1.086Net Profit45.244%Sharpe Ratio0.651Loss Rate40%Win Rate60%Profit-Loss Ratio2.48Alpha0.109Beta-2.074Annual Standard Deviation0.104Annual Variance0.011Information Ratio0.459Tracking Error0.104Treynor Ratio-0.033Total Fees\$63.32
```import numpy as np
from sklearn.linear_model import LinearRegression

class ScikitLearnLinearRegressionAlgorithm(QCAlgorithm):

def Initialize(self):
self.SetStartDate(2013, 1, 1)  # Set Start Date
self.SetEndDate(2019, 1, 1) # Set End Date

self.lookback = 30 # number of previous days for training

self.SetCash(100000)  # Set Strategy Cash

self.symbols = [ spy.Symbol ] # In the future, we can include more symbols to the list in this way

self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.AfterMarketOpen("SPY", 28), self.Regression)

def Regression(self):
# Daily historical data is used to train the machine learning model
history = self.History(self.symbols, self.lookback, Resolution.Daily)

# price dictionary:    key: symbol; value: historical price
self.prices = {}
# slope dictionary:    key: symbol; value: slope
self.slopes = {}

for symbol in self.symbols:
if not history.empty:
# get historical open price
self.prices[symbol] = list(history.loc[symbol.Value]['open'])

# A is the design matrix
A = range(self.lookback + 1)

for symbol in self.symbols:
if symbol in self.prices:
# response
Y = self.prices[symbol]
# features
X = np.column_stack([np.ones(len(A)), A])

# data preparation
length = min(len(X), len(Y))
X = X[-length:]
Y = Y[-length:]
A = A[-length:]

# fit the linear regression
reg = LinearRegression().fit(X, Y)

# run linear regression y = ax + b
b = reg.intercept_
a = reg.coef_

# store slopes for symbols
self.slopes[symbol] = a/b

# if there is no open price
if not self.prices:
return