Need help with RollingLinearRegressionOfReturns and Computing Weights

Hi I am trying to make to make a strategy where I will get a bucket of Top 50 (or some other number) stocks as per dollar volume through pipeline.

Now, I will compute their respective Beta's (wrt. SPY maybe?) and then compute weights in-order to minimise the overall Beta.

I've recently started working with Quantopian and I am really struggling with implementing few basics, I would really appreciate some help.

Things where I'm stuck:

1. How do I get Beta's of respective stocks? I was trying to implement it by using 'RollingLinearRegressionOfReturns' in make_pipeline() and I couldn't figure out what target should be given to RollingLinearRegressionOfReturns.

1. Can you give me few leads on how to compute weights such that it minimises the overall portfolio beta?

Attaching my code here


from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.factors import AverageDollarVolume,RollingLinearRegressionOfReturns
from quantopian.pipeline.filters.morningstar import Q500US
from quantopian.pipeline.filters import Filter
def initialize(context):
schedule_function(allot, date_rules.week_start(), time_rules.market_open(minutes=30))
attach_pipeline(make_pipeline(), 'my_pipeline')

def make_pipeline():
dollar_volume = AverageDollarVolume(window_length=5)
top_dollar_volume = dollar_volume.top(50)
pipe = Pipeline(
screen = top_dollar_volume,
)
return pipe

context.output = pipeline_output('my_pipeline')
context.security_list = context.output.index.tolist()
context.beta_list = []
for se in context.security_list:
reg = RollingLinearRegressionOfReturns(
target=se,
returns_length=2,
regression_length=30,
)
context.beta_list.append(reg.beta)
print context.beta_list #This is not printing the Beta value

def allot(context,data):
pass


Thanks

1 response

for beta you need to update the target to SPY

   factors.RollingLinearRegressionOfReturns(
target=sid(8554), # sid for SPY
returns_length=2,
regression_length=30,
)


you can pass this factor to the pipe and the beta will be calculated in the pipe output.
this is a cpu intensive operation so use less than 20 symbols to get results within a reasonable time.