Skip to content
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 5 commits into from Oct 25, 2022

Conversation

ValueRaider
Copy link
Collaborator

Instead of fetching and building info, just fetch basic price data which contains timezone. ~1.7x speedup. Hopefully also reduces failures to get timezone (issue #1076)

@ValueRaider
Copy link
Collaborator Author

ValueRaider commented Oct 24, 2022

@fredrik-corneliusson - just giving you an opportunity to review in case you disagree anywhere

Copy link
Contributor

@fredrik-corneliusson fredrik-corneliusson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, just had a few suggestions.

yfinance/base.py Outdated
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"}
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

yfinance/base.py Outdated
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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe if debug is True print the exception
example:

except Exception as e:
    if debug_mode:
        print("Failed to get ticker '{}' reason: {}".format(self.ticker, e))

yfinance/base.py Outdated
@@ -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):
Copy link
Contributor

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 .

@ValueRaider
Copy link
Collaborator Author

@fredrik-corneliusson All good suggestions.

@ValueRaider ValueRaider merged commit 6253e1d into dev Oct 25, 2022
@ValueRaider ValueRaider deleted the fix/get-tz-performance branch October 25, 2022 13:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants