Try this way:
# Help with Linear Regression
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline.filters import StaticAssets
from quantopian.pipeline.factors import Returns
from quantopian.pipeline import Pipeline
import numpy as np
# ---------------------------------------------------------
stocks, ret_wnd, wnd = symbols('SHAK','TRIP','TSLA'), 1, 10
# ---------------------------------------------------------
def initialize(context):
schedule_function(rebalance, date_rules.every_day(), time_rules.market_open(minutes = 65))
m = StaticAssets(stocks)
stocks_returns = Returns(window_length = ret_wnd + 1, mask = m)
market_returns = Returns(window_length = ret_wnd + 1, mask = StaticAssets(symbols('SPY')))[symbol('SPY')]
ret_regr = stocks_returns.linear_regression(target = market_returns, regression_length = wnd, mask = m)
alpha = ret_regr.alpha
beta = ret_regr.beta
attach_pipeline(Pipeline(columns = {'alpha': alpha, 'beta': beta}, screen = m), 'pipeline')
def before_trading_start(context, data):
bars = ret_wnd + wnd
context.output = pipeline_output('pipeline')
record(b1 = context.output.beta[symbol('SHAK')])
record(a1 = context.output.alpha[symbol('SHAK')])
stock = symbol('SHAK')
bench = symbol('SPY')
bench_history = data.history(bench, 'close', bars, '1d')
asset_history = data.history(stock, 'close', bars, '1d')
y = asset_history.pct_change(ret_wnd)[ret_wnd:]
x = bench_history.pct_change(ret_wnd)[ret_wnd:]
A = np.vstack([x, np.ones(len(x))]).T
m, c = np.linalg.lstsq(A, y)[0]
record(b2 = m)
record(a2 = c)
def rebalance(context, data):
order_target_percent(symbol('SHAK'), 1.0)