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

Hi, can anybody tell me why this code:

    price = data[context.spy].price  
    sd = data[context.spy].stddev(20)/price  

give me this runtime error:
TypeError: unsupported operand type(s) for /: 'NoneType' and 'float'

4 responses

Hello Victor,

This eliminates the error:

def initialize(context):  
    context.spy = sid(8554)  
def handle_data(context, data):  
    price = data[context.spy].price  
    std = data[context.spy].stddev(20)  
    if std != None:  
        sd = data[context.spy].stddev(20)/price  

You can dig deeper, but I believe what's going on is that the stddev transform needs more than one data point, so it returns None until the backtest has progressed. Note that if you run on minute data and use the history API (see the help page), the algorithm will be "warmed up" so that you can compute the statistics based on a trailing window of data that is available at the backtest start.

Grant

Hello Grant,

You just beat me to it:

def initialize(context):  
    context.spy = sid(8554)

def handle_data(context, data):  
    price = data[context.spy].price  
    if data[context.spy].stddev(20) == None:  
        sd = 0  
    else:  
        sd = data[context.spy].stddev(20)/price  

P.

Well, we can double-bill Quantopian for the help support! : )

Thanks to both of you!