Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
No Return From Pipeline?

I have a pipeline in which is attached and producing output (as I've printed the context.output[context.output['longs']].index but when I try to create a weight, it errors out (error to come after code). The code I use looks like this:

def my_rebalance(context,data):  
    """  
    Execute orders according to our schedule_function() timing.  
    """  
    long_secs = context.output[context.output['longs']].index

    port_weight = 1/len(long_secs)  
    for stock in long_secs:  
        if data.can_trade(stock) & (context.account.buying_power > 0):  
            order_target_percent(stock, port_weight)  
    for stock in context.portfolio.positions:  
        if data.can_trade(stock) & (stock not in long_secs):  
            order_target_percent(stock, 0)  
    record(leverage = context.account.leverage)  

The problem I'm getting is at the 'port_weight' because the error is a "division by zero" error in which there shouldn't be a 0 in the first place. Anyone understand why len(long_secs) is returning 0 when print long_secs prints multiple equities to the console?

4 responses

Maybe try

   port_weight = 1.0/len(long_secs)  

Note the '1.0' in place of '1'. Using the integer '1' , Python does integer math and ends up rounding your fraction to 0. Using '1.0' nudges Python to do floating point math and gives you the floating point number you are looking for.

That solves the problem of port_weight equaling 0 but I'm getting a "Division by 0" error meaning my

len(long_secs)

is returning 0. When I do a print on long_secs, it lists several securities in my long_secs that were returned from the pipeline but trying to get all the elements in the total amount of the ones that passed the pipeline parameters is the problem. It keeps saying there is 0 securities when I'm getting the length which it shouldn't be.

A few things...

It would help a lot if you could attach a backtest. You can't attach a backtest with an error , but if you could either comment out the offending line or method or find a timeframe which doesn't error, that could facilitate more help.

Also, your code should in general account for no securities being returned from the pipeline and handle it gracefully. Maybe do a check something like

try:  
    port_weight = 1.0/len(long_secs)  
except:  
    port_weight = 0.0

# or maybe a simple 'if' statement  
if len(long_secs) <> 0:  
    port_weight = 1.0/len(long_secs)  
else:  
    port_weight = 0.0

And, also remember that you can use the built in debugger (https://www.quantopian.com/help#debugger) and potentailly set a breakpoint (ie len(long_secs) <> 0) to help narrow down the problem.

Oh. I found out that it's because my longs column is returning false for all of the securities in the list and it shouldn't. I rank securities in

final_rank

and then try to get the 5th quantile with

longs = final_rank.percentile_between(80,100)

Not sure why that would be returning False for every single security when it should be true for the 5th quantile in the list.