Hi Everyone,

Goal: For a filtered universe of stocks, each symbol is checked in OnData to verify that it isn't already invested and that it doesn't have open orders of any type (unfilled, partially filled, filled, etc.) because I don't want to accidently attempt to double place an order. 

In an attempt to check for if I'm invested in a symbol, I tried using ‘and not Portfolio[symbol].Invested’ from another community post but it's not working, giving me an error, 'name 'Portfolio' is not defined at OnData'

Guidance on this is appreciated!

    def Initialize(self):

#other stuff
self.Data = {}

def OnData(self, data):
for symbol in self.Data.keys():
symbolData = self.Data[symbol]
if not symbolData.IsReady:
if symbolData.Bars[0].Open < symbolData.Bars[0].Close and not Portfolio[symbol].Invested: #error here
self.MarketOrder(symbol, 100)
self.Log("MarketOrder was placed for symbol " + str(symbol))

def CoarseSelectionFilter(self, coarse):
if self.Time <= self.rebalanceTime: #checks if it has been 1 day since rebalancing
return self.Universe.Unchanged #if not do not change the universe
self.rebalanceTime = self.Time + timedelta(1) #set rebalance time to 1 day

sortedByDollarVolume = sorted(coarse, key=lambda c: c.DollarVolume, reverse=True)
symbols_by_price = [c.Symbol for c in sortedByDollarVolume if c.Price > 50 and c.Price < 100 and c.HasFundamentalData and c.Volume > 100000]
self.filteredByPrice = symbols_by_price[:100]
return self.filteredByPrice

def FineSelectionFilter(self, fine):
sortedByMarketCap = sorted(fine, key=lambda c: c.MarketCap)
symbols_by_marketcap = [c.Symbol for c in sortedByMarketCap if c.MarketCap > 0]
self.filteredBymarketcap = symbols_by_marketcap[:100]
return self.filteredBymarketcap

def OnSecuritiesChanged(self, changes):
for security in changes.AddedSecurities:
symbol = security.Symbol
if symbol not in self.Data:
self.Data[symbol] = SymbolData(self, symbol)

for security in changes.RemovedSecurities:
symbol = security.Symbol
if symbol in self.Data:
symbolData = self.Data.pop(symbol, None)
self.SubscriptionManager.RemoveConsolidator(symbol, symbolData.consolidator)

class SymbolData:
def __init__(self, algorithm, symbol):
self.algorithm = algorithm
self.symbol = symbol
self.Bars = RollingWindow[TradeBar](10) # Rolling window for data bars
self.consolidator = TradeBarConsolidator(timedelta(days=1))
self.consolidator.DataConsolidated += self.OnDataConsolidated
algorithm.SubscriptionManager.AddConsolidator(symbol, self.consolidator)

def OnDataConsolidated(self, sender, bar):

def IsReady(self):
return self.Bars.IsReady