Hi All,
In the code below I have it set up to be trading on Wednesday the 1st of June and i would like it to trade contract on Friday the 3rd of June (2 days) where am i going wrong in the code for it to not give me any options at all to trade. Do i need to subscribe to my own interactive brokers data for better data with equity options? Seems to not make a difference which dates I set in the .Expiration() it never brings me back the contracts i would want to trade.
Thanks
from AlgorithmImports import *
from math import floor
class NSDQOPTIONSSTRAT1(QCAlgorithm):
UnderlyingTicker = "QQQ"
def Initialize(self):
self.SetStartDate(2022, 6, 1)
self.SetEndDate(2022, 6, 1)
self.SetCash(5000)
equity = self.AddEquity(self.UnderlyingTicker)
equity.VolatilityModel = StandardDeviationOfReturnsVolatilityModel(30)
option = self.AddOption(self.UnderlyingTicker, Resolution.Minute)
self.option_symbol = option.Symbol
# set our strike/expiry filter for this option chain
option.SetFilter(lambda u: (u.Strikes(-2, +2)
# Expiration method accepts TimeSpan objects or integer for days.
# The following statements yield the same filtering criteria
.Expiration(0, 2)))
#Benchmark
self.SetBenchmark(equity.Symbol)
#Warm Up
self.SetWarmup(30, Resolution.Daily)
def OnData(self,slice):
if self.Portfolio.Invested or not self.IsMarketOpen(self.option_symbol): return
chain = slice.OptionChains.GetValue(self.option_symbol)
if chain is None:
return
# 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=False)
# if found, trade it
if len(contracts) == 0: return
symbol = contracts[0].Symbol
self.MarketOrder(symbol, 1)
self.MarketOnCloseOrder(symbol, -1)
def OnOrderEvent(self, orderEvent):
self.Log(str(orderEvent))
Louis Szeto
Hi Donato
Weekly options are not in the option universe by default. Please add .IncludeWeeklys() in your filter. Refer to option universe filter docs for more details.
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.
Nicholas Pearce
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!