Merge pull request #1606 from DanielGoldfarb/main

option_chain() return underlying data that comes with the options data
pull/1640/head
ValueRaider 2023-07-27 15:10:13 +01:00 committed by GitHub
commit d5282967ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 3 deletions

View File

@ -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 f'yfinance.Ticker object <{self.ticker}>'
@ -48,8 +49,13 @@ class Ticker(TickerBase):
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=[
@ -87,9 +93,10 @@ class Ticker(TickerBase):
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']
})
# ------------------------