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)