Hello everyone, can anyone help me with one line of the code here? Below is my options filter to obtain DTE0 covered call option on QQQ. I've run this for puts and it works but for some reason on the call side, (AND only when running live) it is pulling expired options from 1 month ago, which aren't tradeable. The backtesting works fine. I've tried to change the following line of code but I keep getting errors. Probably a simple fix and my lack of coding skills is making it more difficult.
Thanks in advance!
Here is the section I need to change:
qqqotm_calls = [i for i in callcontractsqqq if i.ID.OptionRight == OptionRight.Call and
i.ID.StrikePrice > self.qqqCoveredCallOTM * self.underlyingPriceqqq and
(i.ID.Date - data.Time).days < self.qqqCoveredCallDTE]
Its this part that I'm messing with. Mine line self.qqqCoveredCallDTE equals zero.
(i.ID.Date - data.Time).days < self.qqqCoveredCallDTE]
I tried the following which just gives an error:
i.ID.Date == data.Time.Date
Here is the whole options filter:
def Buyqqqcoveredcall(self, data):
if self.coveredcallcontractqqq == str():
self.Log("Obtaining QQQ covered call contract")
self.coveredcallcontractqqq = self.OptionsFilterqqqcoveredcall(data)
self.Log("Covered QQQ Call contract: {0}".format(self.coveredcallcontractqqq))
return
if not self.Portfolio[self.coveredcallcontractqqq].Invested and data.ContainsKey(self.coveredcallcontractqqq):
Contractstosell = self.qqqnumberofcontracts[0]
self.Log("Covered QQQ Call contract: {0}, Number of contracts to sell: {1}".format(self.coveredcallcontractqqq, Contractstosell))
self.MarketOrder(self.coveredcallcontractqqq, -Contractstosell)
self.qqqcoveredcalllist[0] = 1
self.Log("Sold QQQ covered calls: {0}".format(Contractstosell))
def OptionsFilterqqqcoveredcall(self, data):
callcontractsqqq = self.OptionChainProvider.GetOptionContractList(self.symbolqqq, data.Time)
self.underlyingPriceqqq = self.Securities[self.symbolqqq].Price
qqqotm_calls = [i for i in callcontractsqqq if i.ID.OptionRight == OptionRight.Call and
i.ID.StrikePrice > self.qqqCoveredCallOTM * self.underlyingPriceqqq and
(i.ID.Date - data.Time).days < self.qqqCoveredCallDTE]
if len(qqqotm_calls) > 0:
callqqqcontract = sorted(sorted(qqqotm_calls, key = lambda x: abs(x.ID.StrikePrice - self.underlyingPriceqqq)))[0]
if callqqqcontract not in self.coveredcallcontractsAddedqqq:
self.coveredcallcontractsAddedqqq.add(callqqqcontract)
self.AddOptionContract(callqqqcontract, Resolution.Minute)
contractarray = sorted(sorted(qqqotm_calls, key = lambda x: abs(x.ID.StrikePrice - self.underlyingPriceqqq)))
strikes = [x.ID.StrikePrice for x in contractarray]
self.qqqdtezerostrike[0] = strikes[0]
self.Log("QQQ Opt filter Call contract {0}".format(callqqqcontract))
self.Log("contract strike price: {0}".format(self.qqqdtezerostrike[0]))
return callqqqcontract
else:
return str()
David Eldringhoff
Please close this post out. I figured it out. Super simple. Dont know why I was making it more complicated before.
Interestingly if I do == 0, it doesn't do same day but rather 1 day forward. IF I do -1 it will do same day. At least on the backtesting. I'll check live tomorrow.
David Eldringhoff
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!