Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Having trouble runnig Pipeline

There was an error (line 55) running the below code. Could someone help me on the error? Thanks.

from quantopian.algorithm import attach_pipeline,pipeline_output
from quantopian.pipeline.data import USEquityPricing
from quantopian.pipeline.factors import SimpleMovingAverage
from quantopian.pipeline import Pipeline
from quantopian.pipeline.filters.morningstar import Q1500US

def initialize(context):
schedule_function(my_rebalance,date_rules.week_start(),time_rules.market_open(minutes = 1))
schedule_function(my_record_vars,date_rules.every_day(),time_rules.market_close())

my_pipe = make_pipeline()  
attach_pipeline(my_pipe,"my_pipeline")  

def make_pipeline():
base_universe = Q1500US()
mean_10 = SimpleMovingAverage(inputs = [USEquityPricing.close],window_length = 10, mask = base_universe)
mean_30 = SimpleMovingAverage(inputs = [USEquityPricing.close],window_length = 30, mask = base_universe)
percent_difference = (mean_10 - mean_30)/ mean_10
shorts = percent_difference.top(25)
longs = percent_difference.bottom(25)
securities_to_trade = (shorts | longs)

pipe = Pipeline(  
    {"longs":longs,  
     "shorts":shorts  
},  
    screen = securities_to_trade  
)  
return pipe

def my_compute_weight(context):
long_weight = 0.5/len(context.longs) if len(context.longs) > 0 else 0
short_weight = 0.5/len(context.shorts) if len(context.shorts) > 0 else 0

return (long_weight,short_weight) 

def before_trading_start(context, data):
context.output = pipeline_output("my_pipeline")

context.longs = context.output[context.output["longs"]].index.tolist()  
context.shorts = context.output[context.output["shorts"]].index.tolist()

def my_rebalance(context,data):
for security in context.portfolio.positions:
if security not in context.longs and security not in context.shorts and data.can_trade(security):
order_target_percent(security,0)

for security in context.longs:  
    if data.can_trade(security):  
        order_target_percent(security,context.long_weight)  

for security in context.shorts:  
    if data.can_trade(security):  
        order_target_percent(security,context.short_weight)  

def my_record_vars(context,data):
longs=shorts=0
for position in context.portfolio.positions.itervalues():
if position.amount > 0:
longs +=1
elif positions.amount < 0:
shorts +=1

record(leverage = context.account.leverage, long_count = longs, short_count= shorts)  
1 response

It seems like the long and short weights were never assigned which caused a key error. Works by adding this line to before_trading_start

    context.long_weight, context.short_weight = my_compute_weight(context)

See attached algo. BTW, it's more convenient for folks here on the forums to help if code is attached as a backtest rather than simply pasting. May be difficult however if the algo errors.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.