Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions yfinance/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Ticker(TickerBase):
def __init__(self, ticker, session=None):
super(Ticker, self).__init__(ticker, session=session)
self._expirations = {}
self._underlying = {}

def __repr__(self):
return 'yfinance.Ticker object <%s>' % self.ticker
Expand All @@ -50,8 +51,13 @@ def _download_options(self, date=None, proxy=None):
for exp in r['optionChain']['result'][0]['expirationDates']:
self._expirations[_datetime.datetime.utcfromtimestamp(
exp).strftime('%Y-%m-%d')] = exp

self._underlying = r['optionChain']['result'][0].get('quote', {})

opt = r['optionChain']['result'][0].get('options', [])
return opt[0] if len(opt) > 0 else []

return dict(**opt[0],underlying=self._underlying) if len(opt) > 0 else {}
return {}

def _options2df(self, opt, tz=None):
data = _pd.DataFrame(opt).reindex(columns=[
Expand Down Expand Up @@ -90,9 +96,10 @@ def option_chain(self, date=None, proxy=None, tz=None):
date = self._expirations[date]
options = self._download_options(date, proxy=proxy)

return _namedtuple('Options', ['calls', 'puts'])(**{
return _namedtuple('Options', ['calls', 'puts', 'underlying'])(**{
"calls": self._options2df(options['calls'], tz=tz),
"puts": self._options2df(options['puts'], tz=tz)
"puts": self._options2df(options['puts'], tz=tz),
"underlying": options['underlying']
})

# ------------------------
Expand Down