Hey Community! Need a bit of help. 

Making an algorithm to trade options, however, I am using a consolidation function instead of OnData (The reason is I am trying to get 5-minute prices instead of 1-minute). In order for me to add options, the API states we need to use a Slice data structure. I tried using it in my consolidation function however I am getting the error…

Runtime Error: 'TradeBar' object has no attribute 'OptionChains'
  at consolidation_handler
    chain = slice.OptionChains.get(self.option_symbol) 

Here is a snippet of the Code. Would love the help!

 

def Initialize(self):

self.SetStartDate(2022, 2, 17)

self.SetEndDate(2023, 5, 5)

self.SetCash(50000) # Set an initial cash balance

self.spy = self.AddEquity("SPY", Resolution.Minute).Symbol

self.consolidator = self.Consolidate(self.spy, timedelta(minutes=5), self.consolidation_handler)

#Options Part

option = self.AddOption("SPY")

#Options Filters

self.option_symbol = option.Symbol

option.SetFilter(minStrike=-1, maxStrike=1, minExpiry=timedelta(days=0), maxExpiry=timedelta(days=30))

self.atm_put = None

self.atm_call = None

 

def consolidation_handler(self, slice: Slice) -> None:
 

chain = slice.OptionChains.get(self.option_symbol)

if chain:

#Find the Call Options

call = [x for x in chain if x.Right == OptionRight.Call]

# we sort the contracts to find at the money (ATM) contract with farthest expiration

contracts = sorted(sorted(sorted(chain, \

key = lambda x: abs(chain.Underlying.Price - x.Strike)), \

key = lambda x: x.Expiry, reverse=True), \

key = lambda x: x.Right, reverse=True)

if len(contracts) == 0:

pass


symbol = contracts[0].Symbol

# Enter Position

if (slice.Price <= ema_10) and (slice.Price >= ema_10 - 2) and (self.track_long != 1):
self.MarketOrder(symbol, 1)

# Exit Position

if (slice.Price > ema_10) and (slice.Price <= ema_10 + 2) and (self.track_long != 1):
self.MarketOrder(symbol, -1)