Trying iterate through the open positions in my portfolio and get the current price for each security using the following code:

for security in self.Portfolio.Values:     price = self.Securities[security].Price

I get the following error:

The error was NullReferenceException : Object reference not set to an instance of an object

Even if the portfolio is empty, this should not happen.

Here is simplified, but full code: 

class algorithm(QCAlgorithm): def Initialize(self): self.SetStartDate(2012,1,1) self.SetEndDate(2012,2,5) self.SetCash(100000) self.symbols = ['ABT', 'ACN', 'ACE', 'ADBE'] for symbol in self.symbols: self.AddEquity(symbol, Resolution.Daily) self.AddEquity('SPY', Resolution.Daily) self.Schedule.On(self.DateRules.MonthStart("SPY"), self.TimeRules.AfterMarketOpen("SPY", 60), Action(self.Rebalance)) def Rebalance(self): for security in self.Portfolio.Values: price = self.Securities[security].Price

Author