Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
TypeError for stock 'MYLNG' : data[sec].price: TypeError: exceptions must be old-style classes or derived from BaseException, not NoneType

I am new to Quantopian and have very limited knowledge. I am trying to learn new things and Quantopian caught my attention.
I was writing a very simple algo using the examples available on the community for my learning purpose.
The algo is using pipeline to get top 5 stocks with 10 day sma, 30 day sma and latest close with a filter of sma_10 < latest_close.
Then for each stock in this updated universe, I am calculating 5 day average price and buying if current price is more than 1% of 5 day average or selling if current price is less than 5 day average.
And I record all this with current price.
I get the below error for a stock 'MYLNG' on 2011-01-13
data[sec].price: TypeError: exceptions must be old-style classes or derived from BaseException, not NoneType

This stock comes up in my universe twice before on 2011-01-06 and 2011-01-07 and gives no error.
I am not able to figure out what is causing this issue.

2011-01-06
sec: Equity(38107, symbol=u'MYLNG', asset_name=u'MYLAN INC 1.25% 3/15/12 CONV', exchange=u'NASDAQ CAPITAL MARKET', start_date=Timestamp('2007-03-05 00:00:00+0000', tz='UTC'), end_date=Timestamp('2012-12-19 00:00:00+0000', tz='UTC'), first_traded=None)
sec.symbol: u'MYLNG'
average_price: 1105.6
current_price: 1110.0
cash: -437628.250318
1.01*average_price: 1116.656
number_of_shares: NameError: name 'number_of_shares' is not defined
number_of_shares*current_price: NameError: name 'number_of_shares' is not defined
data[sec].price: 1082.124
int(cash/current_price): -394

2011-01-07
sec: Equity(38107, symbol=u'MYLNG', asset_name=u'MYLAN INC 1.25% 3/15/12 CONV', exchange=u'NASDAQ CAPITAL MARKET', start_date=Timestamp('2007-03-05 00:00:00+0000', tz='UTC'), end_date=Timestamp('2012-12-19 00:00:00+0000', tz='UTC'), first_traded=None)
sec.symbol: u'MYLNG'
average_price: 1079.152
current_price: 1082.124
cash: -459659.331104
1.01*average_price: 1089.94352
number_of_shares: NameError: name 'number_of_shares' is not defined
number_of_shares*current_price: NameError: name 'number_of_shares' is not defined
data[sec].price: 1082.124
int(cash/current_price): -424

2011-01-13
sec: Equity(38107, symbol=u'MYLNG', asset_name=u'MYLAN INC 1.25% 3/15/12 CONV', exchange=u'NASDAQ CAPITAL MARKET', start_date=Timestamp('2007-03-05 00:00:00+0000', tz='UTC'), end_date=Timestamp('2012-12-19 00:00:00+0000', tz='UTC'), first_traded=None)
sec.symbol: u'MYLNG'
average_price: 1104.966
current_price: 1105.0
cash: -104578.69921
1.01*average_price: 1116.01566
number_of_shares: NameError: name 'number_of_shares' is not defined
number_of_shares*current_price: NameError: name 'number_of_shares' is not defined
data[sec].price: TypeError: exceptions must be old-style classes or derived from BaseException, not NoneType
int(cash/current_price): -94

1 response

Hi Faheem,

Thank you for the very detailed post! I took a look and I believe the problem is in the line:

record(stock_price = data[sec].price)  

The issue here is that data[sec] is being called but sec isn't in data. This can happen if a security doesn't have a price in a given bar, and didn't have a price from a previous bar to forward fill from - it gets removed from data in this case. All you have to do is add one more indent before your call to record() and things should work. This is an issue that has caused confusion in the past and is something that we are working on improving.

I am curious though, you mentioned that the MYLNG stock comes up in your universe in earlier days of the simulation, but I don't see it showing up in the logs when I cloned the algo you shared. Was there a different version where this was the case?

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.