INNER CODE UNIT · Python
targetDTE
ldt9/PyOptionTrader · models/futures/short_strangle.py:211
targetDTE = datetime.date.today() + datetime.timedelta(days=days)
# convert chain.expirations to datetime.date
expire = [datetime.datetime.strptime(exp, '%Y%m%d').date() for exp in chain.expirations]
print(expire)
# find the nearest monthly expiration in chain.expirations to targetDTE
self.nearestDTE = min(expire, key=lambda x: abs(x - targetDTE))
# find the number of days until the nearest monthly expiration
self.daysToexp = (self.nearestDTE - datetime.date.today()).days / 365
print(f"{self.get_timestamp()} Days to expiration: ", round(self.daysToexp * 365), " days")
print(f"{self.get_timestamp()} Expiration date: ", self.nearestDTE)
except Exception as e:
print(str(e))
print(f"{self.get_timestamp()} Could not update target expiration.")
def get_strike(self, delta=0.16, option_type='C', call_strike_rounding='up', put_strike_rounding='down'):