def __init__(self, quote, opt_type, d=date.today().day, m=date.today().month,
y=date.today().year, strict=False, source='google'):
self.source = source.lower()
self.underlying = Stock(quote, source=self.source)
if self.source == 'google':
self._google(quote, d, m, y)
elif self.source == 'yahoo':
self._yahoo(quote, d, m, y)
self._exp = [exp for exp in self._exp if exp not in self._skip_dates[opt_type]]
self.expirations = [exp.strftime(DATE_FORMAT) for exp in self._exp]
self.expiration = date(y, m, d)
try:
if opt_type == 'Call':
self.data = self.data['calls']
elif opt_type == 'Put':
self.data = self.data['puts']
assert self.data
except (KeyError, AssertionError):
if self._expiration in self._exp: # Date is in expirations list but no data for it
self._skip_dates[opt_type].add(self._expiration)
self._exp.remove(self._expiration)
self._has_run = False
if all((d, m, y)) and not self._has_run and not strict:
closest_date = min(self._exp, key=lambda x: abs(x - self._expiration))
print('No options listed for given date, using %s instead' % closest_date.strftime(DATE_FORMAT))
self._has_run = True
self.__init__(quote, closest_date.day, closest_date.month, closest_date.year, source=source)
else:
raise ValueError('Possible expiration dates for this option are:', self.expirations) from None
评论列表
文章目录