When I run this from Jan 1 2013 to May 19 2013, and get the prices with
data[sid(#)].price
an AttributeError occurs.
Can anybody enlighten me?
When I run this from Jan 1 2013 to May 19 2013, and get the prices with
data[sid(#)].price
an AttributeError occurs.
Can anybody enlighten me?
You are explicitly setting context.dictionary[stock] = None
Then later you pull that None and try to get a price using "data[None].price" and voila an exception occurs.
What did you expect to happen?
Thank you for your reply, Dennis.
By setting context.dictionary[stock] = None, I am making a dictionary of stock, mapping them to None, e.g. dictionary[sid(apple)] == None
When I loop through dictionary in line 38, I am looping through the keys, i.e. the stocks, so this should not be a problem.
Moreover, data[None] will raise a key error, not an attribute error.
What puzzles me more is that the error doesn't happen with every data bar, it just occurs randomly, to random stocks.
Okay I misunderstood. You are right about using the key not the value from the dictionary. So the "None" value is unimportant.
However I doubt the result is random. When I run the backtest it clearly stops working after the first quarter of the year. So your universe has simply been repopulated.
If you want to ensure that those securities stay in your universe then buy at least 1 share and maintain a position during the end/start of each quarter (e.g. April 1st, etc.)