Package: yfinance-cache
yfinance-cachePersistent caching wrapper foryfinancemodule. Intelligent caching, not dumb caching of web requests - only update cache where missing/outdated and new data expected. Idea is to minimise fetch frequency and quantity - Yahoo API officially only cares about frequency, but I'm guessing they also care about server load from scrapers.Cache auto-update implemented for:pricescalendarsharesinfoEverything else cached once but never updated (unless you delete their files).
Financials auto-update will be implemented soon ...Persistent cache stored in your user cache folder:Windows = C:/Users/<USER>/AppData/Local/py-yfinance-cacheLinux = /home/<USER>/.cache/py-yfinance-cacheMacOS = /Users/<USER>/Library/Caches/py-yfinance-cacheInterfaceInteraction almost identical to yfinance, listed is attributes with auto-update:importyfinance_cacheasyfcmsft=yfc.Ticker("MSFT")msft.infomsft.calendarmsft.get_shares(start='2024-01-01')msft.history(period="1wk")yfc.download("MSFT AMZN",period="1wk")# See yfinance documentation for full APIPrice data differencesOther people have implemented price caches, but none adjust cached data for new stock splits or dividends.
YFC does. Price can be adjusted for stock splits, dividends, or both:msft.history(...,adjust_splits=True,adjust_divs=True)Price repair is force-enabled, to prevent bad Yahoo data corrupting cache.
Seeyfinance Wikifor detail.Returned table has 2 new columns:FetchDate= when data was fetchedFinal?=trueif don't expect future fetches to changeAgingConcept ofmax agecontrols when cached data is updated.
Ifmax agetime has passed since last fetch then cache is updated.
Value must beTimedeltaor equivalentstr.Price data agingdf=msft.history(interval="1d",max_age="1h",trigger_at_market_close=False,...)With price data, YFC also considers how long exchange been open since last fetch,
usingexchange_calendars.
Only if market been open long enough since last fetch,
or iftrigger_at_market_close=Trueand market since closed,
is cache refreshed.max_agedefaults to half of interval.Shares agingdf=msft.shares(...,max_age='60d')Property agingFor data obtained fromTickerproperties not functions, max age set in YFC options.
Implemented to behave likepandas.options, except YFC options are persistent.>>>importyfinance_cacheasyfc>>>yfc.options{"max_ages":{"calendar":"7d","info":"180d"}}>>>yfc.options.max_ages.calendar='30d'>>>yfc.options{"max_ages":{"calendar":"30d","info":"180d"}}Verifying cacheCached prices can be compared against latest Yahoo Finance data, and correct differences:# Verify prices of one ticker symbolmsft.verify_cached_prices(rtol=0.0001,# relative tolerance for differencesvol_rtol=0.005,# relative tolerance specifically for Volumecorrect=False,# delete incorrect cached data?discard_old=False,# if cached data too old to check (e.g. 30m), assume incorrect and delete?quiet=True,# enable to print nothing, disable to print summary detail of why cached data wrongdebug=False,# enable even more detail for debuggingdebug_interval=None)# only verify this interval (note: 1d always verified)# Verify prices of entire cache, ticker symbols processed alphabetically. Recommend using `requests_cache` session.yfc.verify_cached_tickers_prices(session=None,# recommend you provide a requests_cache herertol=0.0001,vol_rtol=0.005,correct=False,halt_on_fail=True,# stop verifying on first failresume_from_tkr=None,# in case you aborted verification, can jump ahead to this ticker symbol. Append '+1' to start AFTER the tickerdebug_tkr=None,# only verify this ticker symboldebug_interval=None)With latest version the only genuine differences you should see are tiny Volume differences (~0.5%). Seems Yahoo is still adjusting Volume over 24 hours after that day ended, e.g. updating Monday Volume on Wednesday.If you see big differences in the OHLC price of recent intervals (last few days), probably Yahoo is wrong! Since fetching that price data on day / day after, Yahoo has messed up their data - at least this is my experience. Cross-check against TradingView or stock exchange website.PerformanceFor each ticker, YFC basically performs 2 tasks:1 - check if fetch needed2 - fetch data and integrate into cacheThroughput on 1 thread decent CPU: task 1 @ ~60/sec, task 2 @ ~5/sec.InstallationAvailable on PIP:pip install yfinance_cacheLimitationsonly price data is checked if refresh neededintraday pre/post price data not available