INNER CODE UNIT · Python
update_target_expiration
ldt9/PyOptionTrader · models/futures/short_strangle.py:197
def update_target_expiration(self, days):
'''
Look for the chain with the nearest DTE expiration to days
:param days: number of days until expiration
'''
try:
# chain = next(c for c in self.chains if c.tradingClass == 'SPX' and c.exchange == 'SMART') # for index
# chain = next(c for c in self.chains if c.exchange == 'SMART') # for stock
print(self.chains)
chain = next(c for c in self.chains if c.exchange == 'CME') # for futures
# get the nearest monthly expiration
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]