Hi there,
I am trying to find the best way to select option contracts by greeks and trade the ones that match my threshhold on the same dates. Does anyone know how to do this effectively in Python?
Basically I want my list to only contain contracts where deltas are closest to 0.25 and 0.15. From there I want to traded those contracts as long as the Expiry dates are the same.
I tried doing some sorting and getting the min values to match but doesn't seem to work. It also doesn't ensure that the Expiry dates are the same
def getContracts(self, slice):
shortContract = None
longContract = None
# Get Contracts
for i in slice.OptionChains:
chain = i.Value
contracts = [x for x in chain]
contracts = sorted(contracts, key=lambda x: (x.Expiry, x.Greeks.Delta))
shortContract = min(contracts, key=lambda x: abs(x.Greeks.Delta-self.shortDelta))
longContract = min(contracts, key=lambda x: abs(x.Greeks.Delta-self.longDelta))
self.Debug(f"shortcontract: Strike: {shortContract.Strike} Expiry: {shortContract.Expiry} Delta: {shortContract.Greeks.Delta}")
self.Debug(f"longContract: Strike: {longContract.Strike} Expiry: {longContract.Expiry} Delta: {longContract.Greeks.Delta}")
# Check that contracts are not the same
if shortContract != longContract:
self.placeOrder(shortContract, longContract)
Shile Wen
Hi Rajats179,
We can use list comprehension to make sure the possible options are filtered down to the ones that have the same expiry as the first option contract. I've shown this in the attached backtest.
Best,
Shile Wen
Rajats179
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!