Skip to content

Commit

Permalink
Merge branch 'main' of github.com:ranaroussi/yfinance
Browse files Browse the repository at this point in the history
  • Loading branch information
ranaroussi committed Oct 19, 2021
2 parents 0edce6b + 4b98515 commit 25efeac
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 4 deletions.
5 changes: 4 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ I've since renamed it to ``yfinance`` as I no longer consider it a mere "fix".
For reasons of backward-compatibility, ``fix-yahoo-finance`` now import and
uses ``yfinance``, but you should install and use ``yfinance`` directly.

`Changelog » <./CHANGELOG.rst>`__
`Changelog » <https://github.com/ranaroussi/yfinance/edit/main/README.rst>`__

-----

Expand Down Expand Up @@ -128,6 +128,9 @@ Note: yahoo finance datetimes are received as UTC.
# show options expirations
msft.options
# show news
msft.news
# get option chain for specific expiration
opt = msft.option_chain('YYYY-MM-DD')
# data available via: opt.calls, opt.puts
Expand Down
5 changes: 4 additions & 1 deletion docs/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ msft.isin
# show options expirations
msft.options
# show news
msft.news
# get option chain for specific expiration
opt = msft.option_chain('YYYY-MM-DD')
# data available via: opt.calls, opt.puts
Expand Down Expand Up @@ -195,4 +198,4 @@ yf.pdr_override() # <== that's all it takes :-)
# download dataframe
data = pdr.get_data_yahoo("SPY", start="2017-01-01", end="2017-04-30")
```
```
1 change: 1 addition & 0 deletions test_yfinance.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def test_attributes(self):
ticker.quarterly_cashflow
ticker.sustainability
ticker.options
ticker.news

def test_holders(self):
for ticker in tickers:
Expand Down
42 changes: 40 additions & 2 deletions yfinance/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def __init__(self, ticker, session=None):
self._institutional_holders = None
self._mutualfund_holders = None
self._isin = None
self._news = []

self._calendar = None
self._expirations = {}
Expand Down Expand Up @@ -275,8 +276,7 @@ def history(self, period="1mo", interval="1d",

# duplicates and missing rows cleanup
df.dropna(how='all', inplace=True)
df.drop_duplicates(inplace=True)
df = df.groupby(df.index).last()
df = df[~df.index.duplicated(keep='first')]

self._history = df.copy()

Expand Down Expand Up @@ -386,6 +386,10 @@ def cleanup(data):
except Exception:
pass

# For ETFs, provide this valuable data: the top holdings of the ETF
if data.get('topHoldings'):
self._info.update(data['topHoldings'])

if not isinstance(data.get('summaryDetail'), dict):
# For some reason summaryDetail did not give any results. The price dict usually has most of the same info
self._info.update(data.get('price', {}))
Expand All @@ -397,6 +401,12 @@ def cleanup(data):
except Exception:
pass

try:
self._info['preMarketPrice'] = data.get('price', {}).get(
'preMarketPrice', self._info.get('preMarketPrice', None))
except Exception:
pass

self._info['logo_url'] = ""
try:
domain = self._info['website'].split(
Expand Down Expand Up @@ -626,3 +636,31 @@ def get_isin(self, proxy=None):

self._isin = data.split(search_str)[1].split('"')[0].split('|')[0]
return self._isin

def get_news(self, proxy=None):
if self._news:
return self._news

# setup proxy in requests format
if proxy is not None:
if isinstance(proxy, dict) and "https" in proxy:
proxy = proxy["https"]
proxy = {"https": proxy}

# Getting data from json
url = "{}/v1/finance/search?q={}".format(self._base_url, self.ticker)
data = self.session.get(
url=url,
proxies=proxy,
headers=utils.user_agent_headers
)
if "Will be right back" in data.text:
raise RuntimeError("*** YAHOO! FINANCE IS CURRENTLY DOWN! ***\n"
"Our engineers are working quickly to resolve "
"the issue. Thank you for your patience.")
data = data.json()

# parse news
self._news = data.get("news", [])
return self._news

4 changes: 4 additions & 0 deletions yfinance/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,7 @@ def options(self):
if not self._expirations:
self._download_options()
return tuple(self._expirations.keys())

@property
def news(self):
return self.get_news()

0 comments on commit 25efeac

Please sign in to comment.