Smart caching wrapper for 'yfinance' module
Project description
yfinance-cache
Caching wrapper for yfinance module. Intelligent caching, not dumb caching of web requests:
- If requested data not in cache,
yfinance
is called - If all requested data is in cache, return that
- If some in cache but some missing, ask
yfinance
for the missing data
Additional logic decides if cached data needs refresh.
Interface
Interaction is almost identical to yfinance. Differences are highlighted underneath code:
import yfinance_cache as yfc
msft = yfc.Ticker("MSFT")
# get stock info
msft.info
# get historical market data
hist = msft.history(period="max")
...
# etc. See yfinance documentation for full API
Refreshing cache
msft.history(interval="1d", max_age=datetime.timedelta(hours=1), ...)
max_age
controls when to refresh cached data to avoid spam. If market is still open and max_age
time has passed since last fetch, then today's cached price data will be refreshed.
Defaults to half of interval. Refresh also triggered if market closed since last fetch.
Adjusting price
Price can be adjusted for stock splits, dividends, or both. yfinance
only allows control of dividends adjustment via auto_adjust
. How Yahoo adjusts for dividends is slightly mysterious so djusted prices are slightly different to Yahoo (tiny relative error ~1e-7)
msft.history(..., adjust_splits=True, adjust_divs=True)
Verifying cache
Cached prices can be compared against latest Yahoo Finance data, and correct differences:
# Verify prices of one ticker symbol
msft.verify_cached_prices(
correct=False, # delete incorrect cached data?
discard_old=False, # if cached data too old to check (e.g. 30m), assume incorrect and delete?
quiet=True, # disable to print summary detail of why cached data wrong
debug=False, # enable even more detail for debugging
debug_interval=None) # only verify this interval (note: 1d always verified)
# Verify prices of entire cache, ticker symbols processed alphabetically
yfc.verify_cached_tickers_prices(
session=None,
resume_from_tkr=None, # in case you aborted verification, can jump ahead
debug_tkr=None, # only verify this ticker symbol
debug_interval=None)
Installation
Available on PIP: pip install yfinance_cache
Known issues / pending tasks
- Considering adding a 'verify' function, checking all cached data against Yahoo.
- Add refresh check to financials data, then to earnings dates.
Limitations
Code is being actively developed so some features missing:
- only price data is checked if refresh needed
Tickers
class anddownload()
not available - useTicker.history()
- pre/post price data not available
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for yfinance_cache-0.4.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2dd5fdeb26e1f377c8f56f7025e3475188eee8a4a2db0f99f5753802f0b1dc82 |
|
MD5 | 49c8cdf240aca5785b67449e771b779f |
|
BLAKE2b-256 | 9e057be20a5c7920f874f9a6f0892bafec17a5eee25cca811497a0f0fd06157c |