Hi All,
I am trying to get ATM option contracts which are closest to expiration. Where am I going wrong? Also how can I change the nearest to 2 days before expiry?
Thanks
def Initialize(self):
self.SetStartDate(2022, 6, 1) # Set Start Date
self.SetEndDate(2022,6, 29) #Set End Date
self.SetCash(500000) # Set Strategy Cash
self.Settings.FreePortfolioValuePercentage = 0.3 #Free portfolio percentage to avoid margin calls %
option = self.AddOption("QQQ");
option.SetFilter(-2, 2, timedelta(0), timedelta(182))
self.oi_contract = None
self.macd = None
self.takeProfit = None
self.stopLoss = None
def OnData(self, slice):
for chain in slice.OptionChains.Values:
contracts = [contract for contract in chain.Value]
if len(contracts) == 0:
self.qqq_contract = None
break
contracts = sorted(sorted(chain, \
key = lambda x: abs(chain.Underlying.Price - x.Strike)), \
key = lambda x: x.Expiry, reverse=False)
if self.qqq_contract is not None and contract.Symbol == self.qqq_contract.Symbol:
break
Louis Szeto
Hi Donato
Separate the 2 conditions will do:
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
I am still having issues now that I have added more conditions, am I not coding correctly the long and short buying mechanisms?
Thanks
Louis Szeto
Hi Donato
We cannot debug with this amount of information. Could you please attach a backtest?
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!