Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
build error help

When I try to run the code below, I get the build error "35 Error Runtime exception: ValueError: cannot convert float NaN to integer". Any idea why?

If I comment out the last two lines, the code runs.

Grant

import numpy as np

def initialize(context):  
    context.stocks =   [ sid(19662),  # XLY Consumer Discrectionary SPDR Fund  
                       sid(19656),  # XLF Financial SPDR Fund  
                       sid(19658),  # XLK Technology SPDR Fund  
                       sid(19655),  # XLE Energy SPDR Fund  
                       sid(19661),  # XLV Health Care SPRD Fund  
                       sid(19657),  # XLI Industrial SPDR Fund  
                       sid(19659),  # XLP Consumer Staples SPDR Fund  
                       sid(19654),  # XLB Materials SPDR Fund  
                       sid(19660) ] # XLU Utilities SPRD Fund

def handle_data(context, data):  

    prices = history(31,'1d','price')[0:-1]  

    pct_change_sum = []  

    for k in range(len(prices)-1):  
        pct_change_sum.append(prices.pct_change(periods=k+1).sum())  

    prod = pct_change_sum[0]  
    for k in range(len(pct_change_sum)-1):  
        prod = np.multiply(prod,pct_change_sum[k+1])  

    prod[prod>0] = 0  
    prod[prod  
8 responses

Not to be a stickler, but that is a runtime error not a build error. Build errors never let a program get to run time. Build errors are often syntax problems while runtime errors happen due to unintended consequences of the code itself.

moving on to be a bit more helpful, I am not sure that "pct_change_sum.append(prices.pct_change(periods=k+1).sum()) " is doing what you think it is doing. If you run my code with some simple log statements in it you can see that NaN is being returned by numpy often. If the problem is ignore-able you could use numpy.isnan(yourItemHere) to find out if there was a problem. My code actually runs without this error as long as I cared to run it, so in the end I couldn't replicate your error to help you fix the problem, but I hope I have found the origin for you.

Happy Exploring.
-Jeff

import numpy as np

def initialize(context):  
    context.stocks =   [ sid(19662),  # XLY Consumer Discrectionary SPDR Fund  
                       sid(19656),  # XLF Financial SPDR Fund  
                       sid(19658),  # XLK Technology SPDR Fund  
                       sid(19655),  # XLE Energy SPDR Fund  
                       sid(19661),  # XLV Health Care SPRD Fund  
                       sid(19657),  # XLI Industrial SPDR Fund  
                       sid(19659),  # XLP Consumer Staples SPDR Fund  
                       sid(19654),  # XLB Materials SPDR Fund  
                       sid(19660) ] # XLU Utilities SPRD Fund

def handle_data(context, data):  

    prices = history(31,'1d','price')[0:-1]  

    pct_change_sum = []  

    for k in range(len(prices)-1):  
        pct_change_sum.append(prices.pct_change(periods=k+1).sum())  
        log.info(prices.pct_change(periods=k+1))  
        log.info(prices.pct_change(periods=k+1).sum())

    prod = pct_change_sum[0]  
    for k in range(len(pct_change_sum)-1):  
        prod = np.multiply(prod,pct_change_sum[k+1])  
        log.info(prod)  
        log.info("-----")

    prod[prod>0] = 0  
    prod[prod]  

Thanks Jeff,

When I flip through the logs for the code below, I don't see any NaNs. Quantopian executes a pre-run (i.e. "build") screen for errors, which has caused problems for me before. I suspect that may be the culprit here. Usually, I can sort out a work-around, but in this case, I'm having trouble.

Grant

import numpy as np

def initialize(context):  
    context.stocks =   [ sid(19662),  # XLY Consumer Discrectionary SPDR Fund  
                       sid(19656),  # XLF Financial SPDR Fund  
                       sid(19658),  # XLK Technology SPDR Fund  
                       sid(19655),  # XLE Energy SPDR Fund  
                       sid(19661),  # XLV Health Care SPRD Fund  
                       sid(19657),  # XLI Industrial SPDR Fund  
                       sid(19659),  # XLP Consumer Staples SPDR Fund  
                       sid(19654),  # XLB Materials SPDR Fund  
                       sid(19660) ] # XLU Utilities SPRD Fund  
def handle_data(context, data):  
    prices = history(31,'1d','price')[0:-1]  
    pct_change_sum = []  
    for k in range(len(prices)-1):  
        pct_change_sum.append(prices.pct_change(periods=k+1).sum())  
    prod = pct_change_sum[0]  
    for k in range(len(pct_change_sum)-1):  
        prod = np.multiply(prod,pct_change_sum[k+1])  
        log.info(prod)  
        log.info("-----")  

Why did you delete the 2 log.info's in the loop that does "pct_change_sum.append(prices.pct_change(periods=k+1).sum())"

That was were the important NaN's came from before.

I just woke up, so I may be missing something you said.

-Jeff

Thanks Jeff,

Yes, that may be a clue. The sum should ignore NaNs, unless all values in a column are NaNs. This may be what is hanging up the build check. I'll try inserting a check for NaNs.

Grant

This fixes the problem:

    if np.any(~np.isfinite(percent)):  
        return  
    for stock in context.stocks:  
        order_target_percent(stock,percent[stock])  

Awesome! I hope I was helpful!

-Jeff

Thanks. Yes, you were helpful. --Grant

May be the following change might also help

prices = history(31,'1d','price', True)  

unless you need fill to be false. You do not need [0:-1] as this is the full DF anyway as I understand Python.