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

Speed up execution suggestion. #3

Closed
a904guy opened this issue Feb 7, 2021 · 4 comments
Closed

Speed up execution suggestion. #3

a904guy opened this issue Feb 7, 2021 · 4 comments

Comments

@a904guy
Copy link

a904guy commented Feb 7, 2021

Cache the calls to Yahoo Finance to speed up execution.

You should replace all the calls to yf.Ticker(symbol) with this function:

_symbols = {}
def yfTicker(symbol):
  if(symbol in _symbols):
    return _symbols[symbol]
  _symbols[symbol] = yf.Ticker(symbol)
  return _symbols[symbol]

Additionally when using the notebook you can just restart the kernel or run _symbols = {} again to clear the caching.

Why?

Because every call is being executed when yf.Ticker(symbol) is called, it's a ton of redundant calls to YF especially when your calling the same ticker repeatedly.

Additional possible speedup would to overload the history functions in a similar manner as above as well.

@iam-abbas
Copy link
Owner

That's a great idea @a904guy! In addition to this, I am also going to add a local static JSON/CSV containing all the tickers to verify the Tickers because api calls could be time taking. Suggestion by @Denbergvanthijs

@a904guy
Copy link
Author

a904guy commented Feb 7, 2021

Noticed I had a bug in the function, was calling tic not symbol. I've updated the example

@a904guy
Copy link
Author

a904guy commented Feb 7, 2021

History Caching to speed up Yahoo Financial section.

_symbols_history = {}
def yfTickerHistory(symbol, period = '1d'):
  if(symbol in _symbols_history and period in _symbols_history[symbol]):
     return _symbols_history[symbol][period]
  if(symbol not in _symbols_history):
    _symbols_history[symbol] = {}
  _symbols_history[symbol][period] = yfTicker(symbol).history(period)
  return _symbols_history[symbol][period]

@iam-abbas
Copy link
Owner

Fixed in #12

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

No branches or pull requests

2 participants