Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Runtime exception: TypeError: int() argument must be a string or a number, not 'NoneType'

Hi,

I'm getting runtime exception with this simple code:

def initialize(context):  
    pass

def handle_data(context, data):  
    prices = history(bar_count=2, frequency='1d', field='price')  
    print prices[symbol('AAPL')] + prices[symbol('SPY')] + prices[symbol('SPY')]  

Is it a bug in Quantopian code? I can't reproduce it with Zipline and Pandas.

Regards,
Ed

6 responses

Hi Richard,

You can try to call symbol or sid in initialize. I believe you'll have the same result.
This issue happens only when more than 3 TimeSeries added to each other, i.e.this code works just fine:

def initialize(context):  
    pass

def handle_data(context, data):  
    prices = history(bar_count=2, frequency='1d', field='price')  
    print prices[symbol('AAPL')] + prices[symbol('SPY')]  

Ed,

Not sure what you're trying to do, but this should be equivalent:

def initialize(context):  
    pass

def handle_data(context, data):  
    prices = history(bar_count=2, frequency='1d', field='price')  
    print prices[symbol('AAPL')] + 2*prices[symbol('SPY')]  

I suspect that the build is choking on the duplicate SPY reference.

Grant

At first I assumed you were a newbie however now I think you were just calling attention to some weirdness. Nevertheless here's what I wrote...

Try print str( prices[symbol('AAPL')]) + ' ' + str(prices[symbol('SPY')]) )

Maybe you can find some of this interesting...

def initialize(context):  
    context.stocks = symbols('AAPL', 'SPY')

def handle_data(context, data):  
    prices = history(bar_count=3, frequency='1d', field='price')  
    print '_____'  
    for sec in data:  
        sym = sec.symbol  
        print sym + ' ' + str(prices[sec].values[-1])  
        print sym + ' ' + str(prices[sec].values.tolist())  
        print sym + '\n' + str(prices[sec])  
        print '.'  

Example of the output:

2011-01-04 PRINT _____  
2011-01-04 PRINT AAPL 47.33  
2011-01-04 PRINT AAPL [46.064, 47.104, 47.33]  
2011-01-04 PRINT AAPL  
2010-12-31 00:00:00+00:00 46.064  
2011-01-03 00:00:00+00:00 47.104  
2011-01-04 00:00:00+00:00 47.330  
Name: Security(24 [AAPL]), dtype: float64  
2011-01-04 PRINT .  
2011-01-04 PRINT SPY 126.93  
2011-01-04 PRINT SPY [125.8, 127.05, 126.93]  
2011-01-04 PRINT SPY  
2010-12-31 00:00:00+00:00 125.80  
2011-01-03 00:00:00+00:00 127.05  
2011-01-04 00:00:00+00:00 126.93  
Name: Security(8554 [SPY]), dtype: float64  
2011-01-04 PRINT .  

(I changed your 2 to a 3).
If you really want to concatenate more than two in a print statement with the plus signs you can wrap them in str() first and that would work.
In all of programming, many error messages are a disappointment, vague or misleading. In that case I think it is Python.

Not sure what you're trying to do

I'm trying to get a sum of more than two time series objects and use result. It doesn't matter how to use it as it's not possible to get the sum in Quantopian. For example if I want to calculate mean and standard deviation of the result I'd do it like this using pandas:

from pandas import Series  
from datetime import datetime  
import numpy as np  
dates = [datetime(2012, 5, 1), datetime(2012, 5, 2), datetime(2012, 5, 3)]  
ts = Series(np.random.randn(3), dates) + Series(np.random.randn(3), dates) + Series(np.random.randn(3), dates)  
print ts.mean(), ts.std()  
0.478376431986 0.507340148737  

However, Quantopian raises weird exception on the similar code.

Here is another Quantopian example, hopefully better one:

def initialize(context):  
    context.qqq = symbol('QQQ')  
    context.spy = symbol('SPY')  
    context.iwm = symbol('IWM')

def handle_data(context, data):  
    prices = history(bar_count=2, frequency='1d', field='price')  
    ts = prices[context.qqq] + prices[context.spy] + prices[context.iwm]  
    print ts.mean(), ts.std()  

Any code, containing adding more than two different time series raises runtime exception: TypeError: int() argument must be a string or a number, not 'NoneType'

Regards,
Ed

Hello Ed,

I tinkered with it a bit, but couldn't sort out the problem. However, this works:

import numpy as np

def initialize(context):  
    context.qqq = symbol('QQQ')  
    context.spy = symbol('SPY')  
    context.iwm = symbol('IWM')

def handle_data(context, data):  
    prices = history(bar_count=2, frequency='1d', field='price')  
    ts = prices[context.qqq].values + prices[context.spy].values + prices[context.iwm].values  
    print np.mean(ts)  
    print np.std(ts)  

Grant

Grant, thank you for the idea!

I took this a bit further and created Series object out of values and index:

from pandas import Series

def initialize(context):  
    context.qqq = symbol('QQQ')  
    context.spy = symbol('SPY')  
    context.iwm = symbol('IWM')

def handle_data(context, data):  
    prices = history(bar_count=2, frequency='1d', field='price')  
    values = prices[context.qqq].values + prices[context.spy].values + prices[context.iwm].values  
    ts = Series(values,   prices[context.qqq].index)  
    print ts.mean()  
    print ts.std()