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)
Louis Szeto
Hi Aryan
The consolidator only returns the n-period TradeBar/QuoteBar/Tick data of the particular symbol you've assigned to it. In your case, you may use this to get the current slice:
Best
Louis
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 QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect 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. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Aryan Sharma
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 QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect 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. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!