New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve performance of fetching Ticker timezone #1112
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9315f7b
Add Ticker._fetch_ticker_tz() for faster tz fetch
ValueRaider fe1c705
Simplify Ticker._fetch_ticker_tz()
ValueRaider 2623ba9
Simplify Ticker._fetch_ticker_tz() - 2
ValueRaider df11fcd
Improve Ticker._fetch_ticker_tz() ; Change timeout default to 10
ValueRaider 2dce6a7
Remove debug code
ValueRaider File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -159,7 +159,7 @@ def history(self, period="1mo", interval="1d", | |
|
||
if start or period is None or period.lower() == "max": | ||
# Check can get TZ. Fail => probably delisted | ||
tz = self._get_ticker_tz() | ||
tz = self._get_ticker_tz(proxy, timeout) | ||
if tz is None: | ||
# Every valid ticker has a timezone. Missing = problem | ||
err_msg = "No timezone found, symbol certainly delisted" | ||
|
@@ -533,7 +533,7 @@ def _fix_unit_mixups(self, df, interval, tz_exchange): | |
return df | ||
|
||
|
||
def _get_ticker_tz(self): | ||
def _get_ticker_tz(self, proxy=None, timeout=None): | ||
if not self._tz is None: | ||
return self._tz | ||
|
||
|
@@ -545,10 +545,7 @@ def _get_ticker_tz(self): | |
tz = None | ||
|
||
if tz is None: | ||
try: | ||
tz = self.info["exchangeTimezoneName"] | ||
except KeyError: | ||
return None | ||
tz = self._fetch_ticker_tz(proxy, timeout) | ||
|
||
if utils.is_valid_timezone(tz): | ||
# info fetch is relatively slow so cache timezone | ||
|
@@ -559,6 +556,28 @@ def _get_ticker_tz(self): | |
self._tz = tz | ||
return tz | ||
|
||
def _fetch_ticker_tz(self, proxy=None, timeout=None): | ||
# Query Yahoo for basic price data just to get returned timezone | ||
|
||
params = {"range":"1wk", "interval":"1d"} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think range can be "1d" as well to get even less data. |
||
|
||
# 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 = "{}/v8/finance/chart/{}".format(self._base_url, self.ticker) | ||
|
||
session = self.session or _requests | ||
try: | ||
data = session.get(url=url, params=params, proxies=proxy, headers=utils.user_agent_headers, timeout=timeout) | ||
data = data.json() | ||
return data["chart"]["result"][0]["meta"]["exchangeTimezoneName"] | ||
except: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe if debug is True print the exception
|
||
return None | ||
|
||
|
||
def _get_info(self, proxy=None): | ||
# setup proxy in requests format | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is _get_ticker_tz called without proxy and timeout in any other place?
As proxy and timeout is passed to the history method and not Ticker constructor, any calls to this from another methods might not have the correct proxy. (However I do not fully understand the use of proxies in yfinance so this might not be an issue).
Also if timeout is None the request will hang indefinitely, so maybe default to something reasonable.
https://requests.readthedocs.io/en/latest/user/quickstart/#timeouts
Maybe make sure any new code do not call it without setting proxy and timeout so do not make them optional. And if timeout is set to None use a default timeout .