Coder Social home page Coder Social logo

yfinance-cache's Introduction

yfinance-cache

Persistent caching wrapper for yfinance module. 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:

  • prices
  • financials
  • calendar & earnings_dates
  • shares
  • info

Everything else cached once but never updated (unless you delete their files).

Persistent cache stored in your user cache folder:

  • Windows = C:/Users/<USER>/AppData/Local/py-yfinance-cache
  • Linux = /home/<USER>/.cache/py-yfinance-cache
  • MacOS = /Users/<USER>/Library/Caches/py-yfinance-cache

Install

Available via PIP: pip install yfinance_cache

Interface

Interaction almost identical to yfinance, listed is attributes with auto-update:

import yfinance_cache as yfc

msft = yfc.Ticker("MSFT")
msft.info
msft.calendar
msft.cashflow ; msft.quarterly_cashflow  # or: balance_sheet, financials
msft.get_earnings_dates(4)
msft.get_shares(start='2024-01-01')
msft.history(period="1wk")
yfc.download("MSFT AMZN", period="1wk")

Price data differences

Other 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. See yfinance Wiki for detail.

Returned table has 2 new columns:

  • FetchDate = when data was fetched
  • Final? = true if don't expect future fetches to change

Aging

Concept of max age controls when cached data is updated. If max age time has passed since last fetch then cache is updated. Value must be Timedelta or equivalent str.

Price data aging

df = 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, using exchange_calendars. Only if market been open long enough since last fetch, or if trigger_at_market_close=True and market since closed, is cache refreshed. max_age defaults to half of interval.

Shares aging

df = msft.shares(..., max_age='60d')

Property aging

For data obtained from Ticker properties not functions, max age set in YFC options. Implemented to behave like pandas.options, except YFC options are persistent.

>>> import yfinance_cache as yfc
>>> yfc.options
{
    "max_ages": {
        "calendar": "7d",
        "info": "180d"
    }
}
>>> yfc.options.max_ages.calendar = '30d'
>>> yfc.options
{
    "max_ages": {
        "calendar": "30d",
        "info": "180d"
    }
}

Financials

Financials updates are handled different because they don't age. Instead, YFC analyses earnings dates to determine exactly when next earnings will be, or if Yahoo data is incomplete then YFC will predict. You can inspect this schedule in new function dat.get_release_dates().

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(
	rtol=0.0001,  # relative tolerance for differences
	vol_rtol=0.005,  # relative tolerance specifically for Volume
	correct=[False|'one'|'all'],  # delete incorrect cached data? 'one' = stop after correcting first incorrect prices table ; 'all' = correct all tickers & intervals
	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 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. Recommend using `requests_cache` session.
yfc.verify_cached_tickers_prices(
	session=None,  # recommend you provide a requests_cache here if debugging
	rtol=0.0001,
	vol_rtol=0.005,
	correct=[False|'one'|'all'],
	halt_on_fail=True,  # stop verifying on first fail
	resume_from_tkr=None,  # in case you aborted verification, can jump ahead to this ticker symbol. Append '+1' to start AFTER the ticker
	debug_tkr=None,  # only verify this ticker symbol
	debug_interval=None)

These return False if difference detected else True, regardless of if difference was corrected.

  • to scan for first data mismatch but not correct: yfc.verify_cached_tickers_prices().

  • to fix all data issues: yfc.verify_cached_tickers_prices(correct='all', halt_on_fail=False)

I hope latest version 0.6.2 fixed the last bugs in applying new dividend-adjustments and splits to cached prices (Adj Close etc). Only genuine differences in not-adjusted prices are Volume differences (~0.5%) - Yahoo sometimes changes Volume over 24 hours after that day ended e.g. updating Monday Volume on Wednesday, sometimes weeks later!

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.

Performance

For each ticker, YFC basically performs 2 tasks:

1 - check if fetch needed

2 - fetch data and integrate into cache

Throughput on 1 thread decent CPU: task 1 @ ~60/sec, task 2 @ ~5/sec.

Limitations

  • intraday pre/post price data not available

yfinance-cache's People

Contributors

sonique6784 avatar valueraider avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

yfinance-cache's Issues

Some Feedback

How do you know when your cache is out-of-date?
For instance - yfinance-cache was run on day n and the data wasn't in cache so the cache was filled-in.

Scenario 1:

Now it is day n+2 and during day n+1 yahoo finance updated its records such that a quarterly element was updated. you must cache that in as your cache is out of date.

Scenario 2:

Now it is day n+2 and during day n+1 yahoo finance did not update its records such that you can use your cache - but how do you know that without checking - rendering the cache obsolete (unless same operations per day or unless you allow some time to not be updated). I use caching in my stock scanner and screener (https://github.com/asafravid/sss) using json, but thats just for crash and continue or reruns - is that your intention with this caching tool?

Scenario 3:

As we saw in a few issues (and as I saw as well) - sometimes two different calls to yfinance can yield different data as yahoo picks the data randomly from several websites which may or may not be updated, yielding different (sometimes less updated by one quarter) results

Bare that in mind while you develop your tool, maybe add usecases in the Readme, etc.

macOS: Unable to run yfinance-cache: python multiprocessing errors

I'm not able to run yfinance-cache. I get the following python multiprocessing errors:

I tried with Python 3.12 and 3.9, macOS Ventura

What Python version do you recommend?

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/Cellar/[email protected]/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "/usr/local/Cellar/[email protected]/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/spawn.py", line 125, in _main
    prepare(preparation_data)
  File "/usr/local/Cellar/[email protected]/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/spawn.py", line 236, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "/usr/local/Cellar/[email protected]/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/spawn.py", line 287, in _fixup_main_from_path
    main_content = runpy.run_path(main_path,
  File "/usr/local/Cellar/[email protected]/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 288, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "/usr/local/Cellar/[email protected]/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/usr/local/Cellar/[email protected]/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/path/to/venv/script.py", line 376, in <module>
    best_performing_asset(snp500)
  File "/path/to/venv/script.py", line 250, in best_performing_asset
    stock_data = get_asset_data(asset_list)
  File "/path/to/venv/script.py", line 219, in get_asset_data
    stock_data = yf.download(asset_list_joined, start=start, end=end, interval="1mo")["Close"]  # Download closing monthly.


  File "/path/to/venv/lib/python3.9/site-packages/yfinance_cache/yfc_multi.py", line 48, in download
    queue = multiprocessing.Manager().Queue()
  File "/usr/local/Cellar/[email protected]/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/context.py", line 57, in Manager
    m.start()
  File "/usr/local/Cellar/[email protected]/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/managers.py", line 554, in start
    self._process.start()
  File "/usr/local/Cellar/[email protected]/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/process.py", line 121, in start
    self._popen = self._Popen(self)
  File "/usr/local/Cellar/[email protected]/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/context.py", line 284, in _Popen
    return Popen(process_obj)
  File "/usr/local/Cellar/[email protected]/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/popen_spawn_posix.py", line 32, in __init__
    super().__init__(process_obj)
  File "/usr/local/Cellar/[email protected]/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/popen_fork.py", line 19, in __init__
    self._launch(process_obj)
  File "/usr/local/Cellar/[email protected]/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/popen_spawn_posix.py", line 42, in _launch
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "/usr/local/Cellar/[email protected]/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/spawn.py", line 154, in get_preparation_data
    _check_not_importing_main()
  File "/usr/local/Cellar/[email protected]/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/spawn.py", line 134, in _check_not_importing_main
    raise RuntimeError('''
RuntimeError: 
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.
Traceback (most recent call last):
  File "/path/to/venv/script.py", line 5, in <module>
    import yfinance_cache as yf
  File "/path/to/venv/lib/python3.9/site-packages/yfinance_cache/__init__.py", line 3, in <module>
    from .yfc_dat import Period, Interval, AmbiguousComparisonException
  File "/path/to/venv/lib/python3.9/site-packages/yfinance_cache/yfc_dat.py", line 266, in <module>
    manager = Manager()
  File "/usr/local/Cellar/[email protected]/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/context.py", line 57, in Manager
    m.start()
  File "/usr/local/Cellar/[email protected]/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/managers.py", line 558, in start
    self._address = reader.recv()
  File "/usr/local/Cellar/[email protected]/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/connection.py", line 250, in recv
    buf = self._recv_bytes()
  File "/usr/local/Cellar/[email protected]/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/connection.py", line 414, in _recv_bytes
    buf = self._recv(4)
  File "/usr/local/Cellar/[email protected]/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/connection.py", line 383, in _recv
    raise EOFError
EOFError

KeyError when getting the history of a ticker

Just do a yfc.Ticker("1T5.DU").history(period="max", interval="1d")

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[9], [line 1](vscode-notebook-cell:?execution_count=9&line=1)
----> [1](vscode-notebook-cell:?execution_count=9&line=1) history = ticker.history(period="max", interval="1d");
      [2](vscode-notebook-cell:?execution_count=9&line=2) history

File ~/dev/personal/marketdata/.venv/lib/python3.10/site-packages/yfinance_cache/yfc_ticker.py:225, in Ticker.history(self, interval, max_age, period, start, end, prepost, actions, adjust_splits, adjust_divs, keepna, proxy, rounding, debug, quiet, trigger_at_market_close)
    [223](https://file+.vscode-resource.vscode-cdn.net/home/yyy/dev/personal/marketdata/notebooks/~/dev/personal/marketdata/.venv/lib/python3.10/site-packages/yfinance_cache/yfc_ticker.py:223) hist = self._histories_manager.GetHistory(interval)
    [224](https://file+.vscode-resource.vscode-cdn.net/home/yyy/dev/personal/marketdata/notebooks/~/dev/personal/marketdata/.venv/lib/python3.10/site-packages/yfinance_cache/yfc_ticker.py:224) if period is not None:
--> [225](https://file+.vscode-resource.vscode-cdn.net/home/yyy/dev/personal/marketdata/notebooks/~/dev/personal/marketdata/.venv/lib/python3.10/site-packages/yfinance_cache/yfc_ticker.py:225)     h = hist.get(start=None, end=None, period=period, max_age=max_age, trigger_at_market_close=trigger_at_market_close, quiet=quiet)
    [226](https://file+.vscode-resource.vscode-cdn.net/home/yyy/dev/personal/marketdata/notebooks/~/dev/personal/marketdata/.venv/lib/python3.10/site-packages/yfinance_cache/yfc_ticker.py:226) elif interday:
    [227](https://file+.vscode-resource.vscode-cdn.net/home/yyy/dev/personal/marketdata/notebooks/~/dev/personal/marketdata/.venv/lib/python3.10/site-packages/yfinance_cache/yfc_ticker.py:227)     h = hist.get(start_d, end_d, period=None, max_age=max_age, trigger_at_market_close=trigger_at_market_close, quiet=quiet)

File ~/dev/personal/marketdata/.venv/lib/python3.10/site-packages/yfinance_cache/yfc_prices_manager.py:516, in PriceHistory.get(self, start, end, period, max_age, trigger_at_market_close, repair, prepost, adjust_splits, adjust_divs, quiet)
    [513](https://file+.vscode-resource.vscode-cdn.net/home/yyy/dev/personal/marketdata/notebooks/~/dev/personal/marketdata/.venv/lib/python3.10/site-packages/yfinance_cache/yfc_prices_manager.py:513) self._applyNewEvents()
    [515](https://file+.vscode-resource.vscode-cdn.net/home/yyy/dev/personal/marketdata/notebooks/~/dev/personal/marketdata/.venv/lib/python3.10/site-packages/yfinance_cache/yfc_prices_manager.py:515) try:
--> [516](https://file+.vscode-resource.vscode-cdn.net/home/yyy/dev/personal/marketdata/notebooks/~/dev/personal/marketdata/.venv/lib/python3.10/site-packages/yfinance_cache/yfc_prices_manager.py:516)     yf_lag = yfcd.exchangeToYfLag[self.exchange]
    [517](https://file+.vscode-resource.vscode-cdn.net/home/yyy/dev/personal/marketdata/notebooks/~/dev/personal/marketdata/.venv/lib/python3.10/site-packages/yfinance_cache/yfc_prices_manager.py:517) except:
    [518](https://file+.vscode-resource.vscode-cdn.net/home/yyy/dev/personal/marketdata/notebooks/~/dev/personal/marketdata/.venv/lib/python3.10/site-packages/yfinance_cache/yfc_prices_manager.py:518)     print(f"- ticker = {self.ticker}")

KeyError: 'DUS'

yfinance seems to be working just fine, so my only guess is that there is an issue trying to repair the data

Too many "FutureWarning"

my test code is like this #43 (when setting threads=False)

the warning logs:

C: \miniconda3\envs\yfinance\lib\site-packages\yfinance_cache\yfc_time.py:482: FutureWarning: Series.getitem treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use ser.iloc[pos]
last_open_day = sched["open"][-1].date()

C: \miniconda3\envs\yfinance\lib\site-packages\yfinance_cache\yfc_prices_manager.py:647: FutureWarning: Series.getitem treating keys as positions is deprecated.
In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use ser.iloc[pos]
fetch_dt = yfct.ConvertToDatetime(self.h["FetchDate"][idx], tz=tz_exchange)

C: \miniconda3\envs\yfinance\lib\site-packages\yfinance_cache\yfc_time.py:816: FutureWarning: Series.getitem treating keys as positions is deprecated.
In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use ser.iloc[pos]
if ts < sched["open"][i]:

C: \miniconda3\envs\yfinance\lib\site-packages\yfinance_cache\yfc_time.py:818: FutureWarning: Series.getitem treating keys as positions is deprecated.
In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use ser.iloc[pos]
return {"market_open": sched["open"][i].to_pydatetime().astimezone(tz), "market_close": sched["close"][i].to_pydatetime().astimezone(tz)}

C: \miniconda3\envs\yfinance\lib\site-packages\yfinance_cache\yfc_time.py:1087: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise in a future error of pandas.
Value 'DatetimeIndex(['2022-10-14 09:30:00-04:00', '2022-10-14 10:30:00-04:00',
'2022-10-14 11:30:00-04:00', '2022-10-14 12:30:00-04:00',
'2022-10-14 13:30:00-04:00', '2022-10-14 14:30:00-04:00',
'2022-10-14 15:30:00-04:00', '2022-10-17 09:30:00-04:00',
'2022-10-17 10:30:00-04:00', '2022-10-17 11:30:00-04:00',
...
'2023-10-12 13:30:00-04:00', '2023-10-12 14:30:00-04:00',
'2023-10-12 15:30:00-04:00', '2023-10-13 09:30:00-04:00',
'2023-10-13 10:30:00-04:00', '2023-10-13 11:30:00-04:00',
'2023-10-13 12:30:00-04:00', '2023-10-13 13:30:00-04:00',
'2023-10-13 14:30:00-04:00', '2023-10-13 15:30:00-04:00'],
dtype='datetime64[ns, America/New_York]', length=1751, freq=None)' has dtype incompatible with datetime64[ns], please explicitly cast to a compatible dtype first.
intervals.loc[intervals.index[f], "interval_open"] = tis.left[idx[f]].tz_convert(tz)

C: \miniconda3\envs\yfinance\lib\site-packages\yfinance_cache\yfc_time.py:1088: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise in a future error of pandas.
Value 'DatetimeIndex(['2022-10-14 10:30:00-04:00', '2022-10-14 11:30:00-04:00',
'2022-10-14 12:30:00-04:00', '2022-10-14 13:30:00-04:00',
'2022-10-14 14:30:00-04:00', '2022-10-14 15:30:00-04:00',
'2022-10-14 16:00:00-04:00', '2022-10-17 10:30:00-04:00',
'2022-10-17 11:30:00-04:00', '2022-10-17 12:30:00-04:00',
...
'2023-10-12 14:30:00-04:00', '2023-10-12 15:30:00-04:00',
'2023-10-12 16:00:00-04:00', '2023-10-13 10:30:00-04:00',
'2023-10-13 11:30:00-04:00', '2023-10-13 12:30:00-04:00',
'2023-10-13 13:30:00-04:00', '2023-10-13 14:30:00-04:00',
'2023-10-13 15:30:00-04:00', '2023-10-13 16:00:00-04:00'],
dtype='datetime64[ns, America/New_York]', length=1751, freq=None)' has dtype incompatible with datetime64[ns], please explicitly cast to a compatible dtype first.
intervals.loc[intervals.index[f], "interval_close"] = tis.right[idx[f]].tz_convert(tz)

C: \miniconda3\envs\yfinance\lib\site-packages\yfinance_cache\yfc_prices_manager.py:805: FutureWarning: Series.getitem treating keys as positions is deprecated.
In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use ser.iloc[pos]
if (not h_intervals.empty) and isinstance(h_intervals["interval_open"][0], datetime):

C: \miniconda3\envs\yfinance\lib\site-packages\yfinance_cache\yfc_time.py:482: FutureWarning: Series.getitem treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use ser.iloc[pos]
last_open_day = sched["open"][-1].date()


I'm using Window 11 OS.
my Python version:
Python 3.9.18 (main, Sep 11 2023, 14:09:26) [MSC v.1916 64 bit (AMD64)] on win32

see dependences' version:
yfance_cache (0.4.7)
yfance (0.2.31)
pandas (2.1.1)
exchange-calendars (4.5)
scipy (1.10.1)
click (8.1.7)
numpy (1.26.0)
pyluach (2.2.0)
python-dateutil (2.8.2)
toolz (0.12.0)
tzdata (2023.3)
korean-lunar-calendar (0.3.1)
pytz (2023.3.post1)
requests (2.31.0)
multitaskg(0.0.11)
lxml (4.9.3)
appdirs (1.4.4)
frozendict(2.3.8)
peewee (3.16.3)
beautifulsoup4 (4.12.2)
html5lib (1.1)
colorama (0.4.4)
soupsieve>1.2 (2.5)
six>=1.9 (1.16.0)
webencodgs (0.5.1)
charset-normalizer (3.3.0)
idna(3.4)
urllib3 (1.26.17)
certifi (2023.7.22)

The behavior of parameter “period” is inconsistent with yfinance

in yfinance, parameter “period” means the only "trading" days,
while in yfinance_cache, it means all the ordinary days (including weekends and holidays)

so, please you can check.

By the way, this is a great module offering cache capabilities, really appreciate that, keep going!

Issue with exchange_calendars and currencies (CCY)

Hey everyone,
first of all thanks for the great library.

I have just switched from yfinance to this lib and am trying to get currency data such as 'EUR=X'.
YFC gets the exchange as CCY, which is not known by exchange_calendars. However, creating a custom calendar / creating an alias and linking to another calendar is not helping.

I am always receiving this exception from yfc.time.

_raise Exception("Need to add mapping of exchange {} to xcal (ticker={})".format(exchange, self.ticker))

Exception: Need to add mapping of exchange CCY to xcal (ticker=EUR=X)_

I am quite new here.. how are you dealing with currency conversion and gettig currencies?

cannot download (historical) currency data

This example example fails with an error

import pandas as pd
import yfinance as yf
import yfinance_cache as yfc
import datetime as dt

start_date = dt.datetime(2023,11,1)
end_date = dt.datetime.today()

EUR_USD = yfc.Ticker("EURUSD=X")
df_eur_usd = EUR_USD.history(start=start_date, end=end_date)

The full error is

Traceback (most recent call last):
  File "/home/biddisco/src/grox/transactions/get-currencies.py", line 12, in <module>
    df_eur_usd = EUR_USD.history(start=start_date, end=end_date)
  File "/home/biddisco/.local/lib/python3.10/site-packages/yfinance_cache/yfc_ticker.py", line 230, in history
    h = hist.get(start_d, end_d, period=None, max_age=max_age, trigger_at_market_close=trigger_at_market_close, quiet=quiet)
  File "/home/biddisco/.local/lib/python3.10/site-packages/yfinance_cache/yfc_prices_manager.py", line 889, in get
    self._fetchAndAddRanges_contiguous(ranges_to_fetch, prepost, debug_yf, quiet=quiet)
  File "/home/biddisco/.local/lib/python3.10/site-packages/yfinance_cache/yfc_prices_manager.py", line 1067, in _fetchAndAddRanges_contiguous
    h2_pre = self._fetchYfHistory(r[0], r[1], prepost, debug)
  File "/home/biddisco/.local/lib/python3.10/site-packages/yfinance_cache/yfc_prices_manager.py", line 1764, in _fetchYfHistory
    df = self._fetchYfHistory_dateRange(fetch_start_pad, fetch_end, prepost, debug)
  File "/home/biddisco/.local/lib/python3.10/site-packages/yfinance_cache/yfc_prices_manager.py", line 2417, in _fetchYfHistory_dateRange
    raise e
  File "/home/biddisco/.local/lib/python3.10/site-packages/yfinance_cache/yfc_prices_manager.py", line 2392, in _fetchYfHistory_dateRange
    df = self.dat.history(**history_args)
  File "/home/biddisco/.local/lib/python3.10/site-packages/yfinance/utils.py", line 104, in wrapper
    result = func(*args, **kwargs)
  File "/home/biddisco/.local/lib/python3.10/site-packages/yfinance/base.py", line 78, in history
    return self._lazy_load_price_history().history(*args, **kwargs)
  File "/home/biddisco/.local/lib/python3.10/site-packages/yfinance/utils.py", line 104, in wrapper
    result = func(*args, **kwargs)
  File "/home/biddisco/.local/lib/python3.10/site-packages/yfinance/scrapers/history.py", line 343, in history
    df = self._fix_zeroes(df, interval, tz_exchange, prepost)
  File "/home/biddisco/.local/lib/python3.10/site-packages/yfinance/utils.py", line 104, in wrapper
    result = func(*args, **kwargs)
  File "/home/biddisco/.local/lib/python3.10/site-packages/yfinance/scrapers/history.py", line 1074, in _fix_zeroes
    df2.loc[f_prices_bad.any(axis=1) & f_vol_zero_or_nan, "Volume"] = tag
ValueError: operands could not be broadcast together with shapes (183,) (184,) 

Any idea what's wrong?

Support memcache

Would be great if it could support memcache to support distributed cloud based applications.

Exception: Not tested. Does this make sense as cache dir in Windows?

----> 6 import yfinance_cache as yf

File c:\...\venv\lib\site-packages\yfinance_cache\__init__.py:4
      1 #!/usr/bin/env python
      3 from .yfc_dat import Period, Interval
----> 4 from .yfc_ticker import Ticker
      6 # __all__ = ['Ticker', 'Period', 'Interval']

File c:\...\venv\lib\site-packages\yfinance_cache\yfc_ticker.py:3
      1 import yfinance as yf
----> 3 from . import yfc_cache_manager as yfcm
      4 from . import yfc_dat as yfcd
      5 from . import yfc_utils as yfcu

File c:\...\venv\lib\site-packages\yfinance_cache\yfc_cache_manager.py:404
    400 	with open(fp, 'wb') as outData:
    401 		pickle.dump(pkData, outData, 4)
--> 404 ResetCacheDirpath()

File c:\...\venv\lib\site-packages\yfinance_cache\yfc_cache_manager.py:35, in ResetCacheDirpath()
     33 def ResetCacheDirpath():
     34 	global cacheDirpath
---> 35 	cacheDirpath = os.path.join(yfcu.GetUserCacheDirpath(), "yfinance-cache")

File c:\...\venv\lib\site-packages\yfinance_cache\yfc_utils.py:27, in GetUserCacheDirpath()
     25 if _os == OperatingSystem.Windows:
     26 	dp = os.getenv("APPDATA")
---> 27 	raise Exception("Not tested. Does this make sense as cache dir in Windows? - {0}".format(dp))
     28 elif _os == OperatingSystem.Linux:
     29 	dp = os.path.join(os.getenv("HOME"), ".cache")

Exception: Not tested. Does this make sense as cache dir in Windows? - C:\Users\(my username)\AppData\Roaming

import error

Exception has occurred: ValueError
numpy.dtype size changed, may indicate binary incompatibility. Expected 96 from C header, got 88 from PyObject
File "C:\Users\zt\project\testyfc\test.py", line 2, in
import yfinance_cache as yfc
ValueError: numpy.dtype size changed, may indicate binary incompatibility. Expected 96 from C header, got 88 from PyObject

Support S3

Would be great if it could support AWS S3 (including Googles implementation) to support distributed cloud-based applications.

Potential bug in yfc: Typeerror

Tried to use yfc as plugin-replacement for yf. However, on one call I got a typeerror, apparently coming from yfc_prices_manager:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

** Stuff deleted due to personal information **
** Last call in my code:  parthist = ticker.history(period="7d", interval="1m") **

File [/usr/local/lib/python3.11/site-packages/yfinance_cache/yfc_ticker.py:232](https://untitled+.vscode-resource.vscode-cdn.net/usr/local/lib/python3.11/site-packages/yfinance_cache/yfc_ticker.py:232), in Ticker.history(self, interval, max_age, period, start, end, prepost, actions, adjust_splits, adjust_divs, keepna, proxy, rounding, debug, quiet, trigger_at_market_close)
    [230](file:///usr/local/lib/python3.11/site-packages/yfinance_cache/yfc_ticker.py?line=229) hist = self._histories_manager.GetHistory(interval)
    [231](file:///usr/local/lib/python3.11/site-packages/yfinance_cache/yfc_ticker.py?line=230) if period is not None:
--> [232](file:///usr/local/lib/python3.11/site-packages/yfinance_cache/yfc_ticker.py?line=231)     h = hist.get(start=None, end=None, period=period, max_age=max_age, trigger_at_market_close=trigger_at_market_close, quiet=quiet)
    [233](file:///usr/local/lib/python3.11/site-packages/yfinance_cache/yfc_ticker.py?line=232) elif interday:
    [234](file:///usr/local/lib/python3.11/site-packages/yfinance_cache/yfc_ticker.py?line=233)     h = hist.get(start_d, end_d, period=None, max_age=max_age, trigger_at_market_close=trigger_at_market_close, quiet=quiet)

File [/usr/local/lib/python3.11/site-packages/yfinance_cache/yfc_prices_manager.py:609](https://untitled+.vscode-resource.vscode-cdn.net/usr/local/lib/python3.11/site-packages/yfinance_cache/yfc_prices_manager.py:609), in PriceHistory.get(self, start, end, period, max_age, trigger_at_market_close, repair, prepost, adjust_splits, adjust_divs, quiet)
    [605](file:///usr/local/lib/python3.11/site-packages/yfinance_cache/yfc_prices_manager.py?line=604) f_nfinal = ~f_final
    [606](file:///usr/local/lib/python3.11/site-packages/yfinance_cache/yfc_prices_manager.py?line=605) # - also treat repaired data as non-final, if fetched near to interval timepoint
    [607](file:///usr/local/lib/python3.11/site-packages/yfinance_cache/yfc_prices_manager.py?line=606) #   because Yahoo might now have correct data
    [608](file:///usr/local/lib/python3.11/site-packages/yfinance_cache/yfc_prices_manager.py?line=607) #   TODO: test!
--> [609](file:///usr/local/lib/python3.11/site-packages/yfinance_cache/yfc_prices_manager.py?line=608) f_repair = (self.h["Repaired?"].to_numpy() & (self.h["FetchDate"] < (self.h.index + self.itd + td_7d)).to_numpy())
    [610](file:///usr/local/lib/python3.11/site-packages/yfinance_cache/yfc_prices_manager.py?line=609) f_nfinal = f_nfinal | f_repair
    [611](file:///usr/local/lib/python3.11/site-packages/yfinance_cache/yfc_prices_manager.py?line=610) for idx in np.where(f_nfinal)[0]:
    [612](file:///usr/local/lib/python3.11/site-packages/yfinance_cache/yfc_prices_manager.py?line=611)     # repaired = False

TypeError: unsupported operand type(s) for &: 'float' and 'bool'

same code with same arguments runs for yfinance. But I can not exclude the possibility of incorrect handling, hence, could also be on my side, but I suppose incorrect arguments to an "&" should be addressed within a library.

TypeError: get_calendar() got an unexpected keyword argument 'cache'

exchange_calendars package doesn't support parameter 'cache'

Error:
Traceback (most recent call last):
File "/Users/mike/Projects/eiten/test_yfc.py", line 3, in
hist = msft.history(period="max")
File "/Users/mike/Projects/eiten/venv/lib/python3.9/site-packages/yfinance_cache/yfc_ticker.py", line 178, in history
sched = yfct.GetExchangeSchedule(exchange, d_now-(7*td_1d), d_now+td_1d)
File "/Users/mike/Projects/eiten/venv/lib/python3.9/site-packages/yfinance_cache/yfc_time.py", line 180, in GetExchangeSchedule
cal = GetCalendar(exchange)
File "/Users/mike/Projects/eiten/venv/lib/python3.9/site-packages/yfinance_cache/yfc_time.py", line 126, in GetCalendar
cal = xcal.get_calendar(cal_name, start=start, cache=True)
TypeError: get_calendar() got an unexpected keyword argument 'cache'

Sample code:
import yfinance_cache as yf
msft = yf.Ticker("MSFT")
hist = msft.history(period="max")
print(hist)

Failed to map these Yahoo intervals to xcal

I have a simple application with fastapi with single endpoint it fetches historical data and return it in webpack format. I run it few days ago and didn't used it. Now I have tried to fetch data and got error below. Restart has cleared the issue. Have used the latest library installed from git.

ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/var/mail/.local/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 411, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/mail/.local/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 69, in __call__
    return await self.app(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/mail/.local/lib/python3.11/site-packages/fastapi/applications.py", line 1054, in __call__
    await super().__call__(scope, receive, send)
  File "/var/mail/.local/lib/python3.11/site-packages/starlette/applications.py", line 123, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/var/mail/.local/lib/python3.11/site-packages/starlette/middleware/errors.py", line 186, in __call__
    raise exc
  File "/var/mail/.local/lib/python3.11/site-packages/starlette/middleware/errors.py", line 164, in __call__
    await self.app(scope, receive, _send)
  File "/var/mail/.local/lib/python3.11/site-packages/msgpack_asgi/_middleware.py", line 29, in __call__
    await responder(scope, receive, send)
  File "/var/mail/.local/lib/python3.11/site-packages/msgpack_asgi/_middleware.py", line 72, in __call__
    await self.app(scope, self.receive_with_msgpack, self.send_with_msgpack)
  File "/var/mail/.local/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 62, in __call__
    await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
  File "/var/mail/.local/lib/python3.11/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app
    raise exc
  File "/var/mail/.local/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
    await app(scope, receive, sender)
  File "/var/mail/.local/lib/python3.11/site-packages/starlette/routing.py", line 758, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/var/mail/.local/lib/python3.11/site-packages/starlette/routing.py", line 778, in app
    await route.handle(scope, receive, send)
  File "/var/mail/.local/lib/python3.11/site-packages/starlette/routing.py", line 299, in handle
    await self.app(scope, receive, send)
  File "/var/mail/.local/lib/python3.11/site-packages/starlette/routing.py", line 79, in app
    await wrap_app_handling_exceptions(app, request)(scope, receive, send)
  File "/var/mail/.local/lib/python3.11/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app
    raise exc
  File "/var/mail/.local/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
    await app(scope, receive, sender)
  File "/var/mail/.local/lib/python3.11/site-packages/starlette/routing.py", line 74, in app
    response = await func(request)
               ^^^^^^^^^^^^^^^^^^^
  File "/var/mail/.local/lib/python3.11/site-packages/fastapi/routing.py", line 278, in app
    raw_response = await run_endpoint_function(
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/mail/.local/lib/python3.11/site-packages/fastapi/routing.py", line 191, in run_endpoint_function
    return await dependant.call(**values)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/ESP-TickerGraph.py", line 21, in get_history
    hist = getHist(ticker, points)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/ESP-TickerGraph.py", line 28, in getHist
    hist = t.history(period=period, interval=interval)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/mail/.local/lib/python3.11/site-packages/yfinance_cache/yfc_ticker.py", line 233, in history
    h = hist.get(start=None, end=None, period=period, max_age=max_age, trigger_at_market_close=trigger_at_market_close, quiet=quiet)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/mail/.local/lib/python3.11/site-packages/yfinance_cache/yfc_prices_manager.py", line 897, in get
    self._fetchAndAddRanges_sparse(pstr, ranges_to_fetch, prepost, debug_yf, quiet=quiet)
  File "/var/mail/.local/lib/python3.11/site-packages/yfinance_cache/yfc_prices_manager.py", line 1233, in _fetchAndAddRanges_sparse
    h2 = self._fetchYfHistory(pstr, fetch_start, fetch_end, prepost, debug)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/mail/.local/lib/python3.11/site-packages/yfinance_cache/yfc_prices_manager.py", line 2311, in _fetchYfHistory
    accept = click.confirm(msg, default=False)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/mail/.local/lib/python3.11/site-packages/click/termui.py", line 235, in confirm
    raise Abort() from None
click.exceptions.Abort
esp-tickergraph_server-esp-tickergraph-1  |
Failed to map these Yahoo intervals to xcal: (tkr=FIVN, exchange=NGM, xcal=NASDAQ). Normally happens when 'exchange_calendars' is wrong so inform developers.
                               Close  Volume  Dividends  Stock Splits
Datetime
2024-04-03 15:30:00-04:00  60.505001   21274        0.0           0.0
Accept into cache anyway? [y/N]: INFO:     192.168.1.2:51284 - "GET /hist?ticker=FIVN&points=200 HTTP/1.0" 500 Internal Server Error

AssertionError: daemonic processes are not allowed to have children

bulk download failure occurred when setting "threads=True" default

see my code below:
def test_yfc_download():
tickers = ["INTC", "MSFT"]

  df = yfc.download(tickers, 
              interval='1h', 
              period=TIME_PERIOD_60M, 
              max_age='4h', 
              group_by='ticker', 
              # threads=False
              )


  print(df['MSFT'][['High', 'Low', 'Close', 'Volume']])

  return

if __name__ == '__main__':

  import yfinance_cache as yfc

  TIME_PERIOD_60M = '250d'
  test_yfc_download()`

The failure log is as below:

Process SpawnPoolWorker-3:
Traceback (most recent call last):
File "C:\miniconda3\envs\yfinance\lib\multiprocessing\process.py", line 315, in _bootstrap
self.run()
Traceback (most recent call last):
File "C:\miniconda3\envs\yfinance\lib\multiprocessing\process.py", line 108, in run
self._target(*self._args, **self._kwargs)
File "C:\miniconda3\envs\yfinance\lib\multiprocessing\process.py", line 315, in _bootstrap
self.run()
File "C:\miniconda3\envs\yfinance\lib\multiprocessing\process.py", line 108, in run
self._target(*self._args, **self._kwargs)
File "C:\miniconda3\envs\yfinance\lib\multiprocessing\pool.py", line 114, in worker
task = get()
File "C:\miniconda3\envs\yfinance\lib\multiprocessing\pool.py", line 114, in worker
task = get()
File "C:\miniconda3\envs\yfinance\lib\multiprocessing\queues.py", line 367, in get
return ForkingPickler.loads(res)
File "C:\miniconda3\envs\yfinance\lib\multiprocessing\queues.py", line 367, in get
return ForkingPickler.loads(res)
File "C:\miniconda3\envs\yfinance\lib\site-packages\yfinance_cache_init
.py", line 4, in
from .yfc_ticker import Ticker, verify_cached_tickers_prices
File "C:\miniconda3\envs\yfinance\lib\site-packages\yfinance_cache_init
.py", line 4, in
from .yfc_ticker import Ticker, verify_cached_tickers_prices
File "C:\miniconda3\envs\yfinance\lib\site-packages\yfinance_cache\yfc_ticker.py", line 7, in
from . import yfc_time as yfct
File "C:\miniconda3\envs\yfinance\lib\site-packages\yfinance_cache\yfc_ticker.py", line 7, in
from . import yfc_time as yfct
File "C:\miniconda3\envs\yfinance\lib\site-packages\yfinance_cache\yfc_time.py", line 22, in
manager = Manager()
File "C:\miniconda3\envs\yfinance\lib\multiprocessing\context.py", line 57, in Manager
m.start()
File "C:\miniconda3\envs\yfinance\lib\multiprocessing\managers.py", line 554, in start
self._process.start()
File "C:\miniconda3\envs\yfinance\lib\multiprocessing\process.py", line 118, in start assert not _current_process._config.get('daemon'),
File "C:\miniconda3\envs\yfinance\lib\site-packages\yfinance_cache\yfc_time.py", line 22, in
manager = Manager()
File "C:\miniconda3\envs\yfinance\lib\multiprocessing\context.py", line 57, in Manager
m.start()
AssertionError: daemonic processes are not allowed to have children
File "C:\miniconda3\envs\yfinance\lib\multiprocessing\managers.py", line 554, in start
self._process.start()
File "C:\miniconda3\envs\yfinance\lib\multiprocessing\process.py", line 118, in start assert not _current_process._config.get('daemon'),
AssertionError: daemonic processes are not allowed to have children

I'm using Window 11 OS.

my Python version:
Python 3.9.18 (main, Sep 11 2023, 14:09:26) [MSC v.1916 64 bit (AMD64)] on win32

see dependences' version:
yfance_cache (0.4.7)
yfance (0.2.31)
pandas (2.1.1)
exchange-calendars (4.5)
scipy (1.10.1)
click (8.1.7)
numpy (1.26.0)
pyluach (2.2.0)
python-dateutil (2.8.2)
toolz (0.12.0)
tzdata (2023.3)
korean-lunar-calendar (0.3.1)
pytz (2023.3.post1)
requests (2.31.0)
multitaskg(0.0.11)
lxml (4.9.3)
appdirs (1.4.4)
frozendict(2.3.8)
peewee (3.16.3)
beautifulsoup4 (4.12.2)
html5lib (1.1)
colorama (0.4.4)
soupsieve>1.2 (2.5)
six>=1.9 (1.16.0)
webencodgs (0.5.1)
charset-normalizer (3.3.0)
idna(3.4)
urllib3 (1.26.17)
certifi (2023.7.22)

Bug: Progress bar enumerator not defined

Using yf.download(ticker_list, progress=True) results in an error due to the variable i not being defined:

 File ".conda/lib/python3.12/site-packages/yfinance_cache/yfc_multi.py", line 93, in download
    yfcu.display_progress_bar(i + 1, len(tickers))
                              ^
UnboundLocalError: cannot access local variable 'i' where it is not associated with a value

Feature request: download()

Implement download() function to batch download many ticker price histories.

Should just need to make GetCalendarViaCache and SetExchangeTzName thread-safe with per-exchange mutexes.

yfinance-cache not compatible with numpy 2.0, python >= 3.9 and appdirs dependencies

Not compatible with NumPy 2.0

yfinance-cache not compatible with numpy 2.0

triggering error:

ValueError: numpy.dtype size changed, may indicate binary incompatibility. Expected 96 from C header, got 88 from PyObject

solution

install numpy 1.26.4 with:

pip install numpy==1.26.4 

Missing Appdirs dependency

yfinance-cache also requires appdirs:

ModuleNotFoundError: No module named 'appdirs'

solution:

pip install appdirs

Requires python 3.9 (zoneinfo)

with python 3.8 following error is shown

ModuleNotFoundError: No module named 'zoneinfo'

zoneinfo was added to python 3.9 hence this yfinance-cache requires 3.9 minimum

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.