Compare commits

...

125 Commits
0.2.59 ... 1.0

Author SHA1 Message Date
ValueRaider
d12c4b3c8c Version 1.0 2025-12-22 18:09:22 +00:00
ValueRaider
d1c69e1d38 Merge pull request #2637 from ranaroussi/dev
sync dev -> main
2025-12-22 18:06:49 +00:00
ValueRaider
6d07669ba1 Merge pull request #2653 from ranaroussi/fix/curl-cffi-0.14-breaking
Block curl_cffi version 0.14
2025-12-22 18:05:39 +00:00
ValueRaider
bed8959381 Merge pull request #2642 from biplavbarua/fix/issue-1436-nsei-30m
Fix: Correct 30m interval alignment and filtering for NSE/early-open markets
2025-12-22 18:05:06 +00:00
biplavbarua
7fba9cff58 fix: correct 30m interval alignment and filtering for NSE/early-open markets 2025-12-22 19:17:07 +05:30
ValueRaider
b0cae4dce1 Block curl_cffi version 0.14 2025-12-21 19:14:10 +00:00
ValueRaider
a6e26a8802 Merge pull request #2648 from ericpien/screener_const_update_2025
fix: Screener's sector industry mapping
2025-12-21 18:41:14 +00:00
Eric Pien
906d6f12c3 update Screener's querying logic and sector industry mapping 2025-12-21 08:16:29 -08:00
ValueRaider
d1aa6befe2 Merge pull request #2650 from evanreynolds9/fix/tickers-history-start-end
Fix: Set period default to None in multi
2025-12-20 19:17:32 +00:00
ValueRaider
7ce5bab562 ruff action: ignore E702 2025-12-20 19:16:41 +00:00
evanreynolds9
fcb951bf71 Set period default to None in Tickers.history and multi 2025-12-20 12:50:29 -05:00
ValueRaider
0e157d5928 Merge pull request #2651 from ranaroussi/fix/price-div-repair-dropping-nan-rows
Fix price-div-repair dropping NaN rows
2025-12-20 17:05:00 +00:00
ValueRaider
50bf706ce8 ruff action: ignore E702 2025-12-20 17:04:44 +00:00
ValueRaider
5d9f73222f Fix price-div-repair dropping NaN rows 2025-12-20 17:01:48 +00:00
ValueRaider
4f38ecfea0 Redesign YfConfig + small fixes
Fixes:
- earnings_dates caching
- _resample on period=ytd

New deprecations:
- yf.set_config()
- move enable_debug_mode() to YfConfig

Complete deprecations:
- proxy arguments
- download(auto_adjust)

Small test fixes
2025-12-20 16:23:20 +00:00
ValueRaider
d2ebd0e642 Refine issue-auto-close action. 2025-12-15 21:47:40 +00:00
ValueRaider
39c2f76c1a Merge pull request #2615 from ianmihura/feature/earnings-calendar
Feature: Earning Calendar (& other calendars)
2025-12-10 21:52:45 +00:00
Ian Mihura
428919b701 earning calendar and other calendars 2025-12-10 17:55:42 +01:00
ValueRaider
70d9b44af9 Merge pull request #2627 from axisrow/feature/retry-mechanism
Add optional retry mechanism for transient network errors
2025-12-02 20:58:34 +00:00
aivibe
ec5f1c2470 Add optional retry mechanism for transient network errors 2025-12-02 06:23:26 +07:00
ValueRaider
ebdfbb7d18 Remove 2x unnecessary exception raises in history() 2025-12-01 19:25:09 +00:00
ValueRaider
3d92f04f47 Merge pull request #2524 from ranaroussi/feature/expose-exceptions-v2
Improve exception handling
2025-11-23 12:05:37 +00:00
ValueRaider
f484539920 Merge branch 'dev' into feature/expose-exceptions-v2 2025-11-19 20:34:23 +00:00
ValueRaider
01c9bd13d8 Add missing class YfConfig 2025-11-19 20:31:03 +00:00
ValueRaider
d48230938d Merge pull request #2613 from danchev/main
fix: Correct column names
2025-10-26 11:41:10 +00:00
D. Danchev
146a2a3957 fix(industry): correct typo in column names
Fix incorrect column names in Industry._parse_top_growth_companies by removing an accidental leading space from 'growth estimate' and 'last price'
2025-10-25 10:14:23 -05:00
ValueRaider
fc0bde214a Issue auto-close: Improve message 2025-09-18 20:05:18 +01:00
ValueRaider
173ab2d811 Version 0.2.66 2025-09-17 12:20:05 +01:00
ValueRaider
8cca819fd6 Merge pull request #2597 from ranaroussi/dev
dev -> main
2025-09-17 12:16:50 +01:00
ValueRaider
bc67252d4b Screener: EquityQuery add industry field
And update docs to support
- fix duplication of class attributes in methods block
- repurpose 'SECTOR_INDUSTY_MAPPING' to back new EquityQuery field 'industry'
2025-09-17 12:02:43 +01:00
ValueRaider
f2d617b948 Merge pull request #2599 from Phil997/fix/missing-log-message
fix: add missing logging message
2025-09-16 22:04:34 +01:00
Philipp Jaschke
4e4b0af3ef fix: add missing logging message
Add the missing reson text from the response when logging the error of the request
2025-09-16 21:32:36 +02:00
ValueRaider
04ba66083e Merge pull request #2595 from ranaroussi/fix/prices-intraday-merge
Prices: bugfix for merging intraday with divs/splits
2025-09-12 23:45:00 +01:00
ValueRaider
bd327694c5 Prices: bugfix for merging intraday with divs/splits 2025-09-12 23:43:49 +01:00
ValueRaider
99b2db69e0 Merge pull request #2591 from hjlgood/new_get_earnings_dates
Add Ticker.get_earnings_dates_using_scrape() to fix ISSUE:#2566
2025-09-10 20:37:54 +01:00
ValueRaider
5a6efdf773 earnings_dates: Improve fix
Fix parsing
Fix function name
Restore original error when no earnings dates found
YfData: move consent-page check to get()
2025-09-10 20:27:57 +01:00
Ran Aroussi
4ff80a57d4 Update PyPi downloads badge refresh rate 2025-09-10 18:33:37 +01:00
jeong
441ae8d677 Add Ticker.get_earnings_dates() to fix ISSUE:#2566 2025-09-07 18:15:20 +09:00
ValueRaider
8b71e6cc05 Merge pull request #2585 from roberttidball/patch-1
Update index.rst - duplicate market reference
2025-08-23 13:34:22 +01:00
Robert Tidball
bc40e84461 Update index.rst
Also removing duplicate markets in table of contents tree so that the navbar on the left does not contain duplicates
2025-08-21 14:27:10 +10:00
Robert Tidball
ead4ed7ee7 Update index.rst - duplicate market reference
The market reference in the documentation (https://ranaroussi.github.io/yfinance/reference/api/yfinance.Market.html#) is duplicated.
2025-08-20 17:10:44 +10:00
ValueRaider
2e0ad642c1 Merge pull request #2579 from mxdev88/main
feat: add market_suffix
2025-08-17 16:20:16 +01:00
ValueRaider
54b37a403c reduce code diff 2025-08-17 16:09:00 +01:00
mxdev88
a04a4ea475 feat: add market_suffix 2025-08-06 09:56:39 +02:00
ValueRaider
9f0d3fd12d Merge pull request #2580 from skyblue6688/bug-fix-2576
Fix test_ticker unit test failure due to NEPT delisting
2025-08-04 19:02:06 +01:00
ValueRaider
3596924fe2 Merge pull request #2573 from jdmcclain47/fix/user-parse-epoch
Fix/user parse epoch
2025-08-04 19:00:25 +01:00
jdmcclain47
4259760bea Fix internal parsing of epochs and add tests 2025-08-03 21:19:08 -05:00
Shi Qin
9a6c82aea6 Fix test_ticker unit test failure due to NEPT delisting 2025-08-03 14:52:50 -07:00
ValueRaider
bdb2fecf1e Price repair: add a nan guard 2025-07-11 22:15:34 +01:00
ValueRaider
0b0b60e535 Screener: add Swiss exchange 2025-07-11 21:59:14 +01:00
ValueRaider
733091ad6c Merge pull request #2565 from ranaroussi/main
sync main -> dev
2025-07-06 17:25:30 +01:00
ValueRaider
ea4dc2d22a Version 0.2.65 2025-07-06 17:18:39 +01:00
ValueRaider
b29cc269c6 Merge pull request #2564 from ranaroussi/dev
sync dev -> main
2025-07-06 17:17:19 +01:00
ValueRaider
8db46f2b12 Fix a test 2025-07-06 17:16:37 +01:00
ValueRaider
f8e75b3aaf Merge pull request #2563 from ranaroussi/fix/financials-dtype
Financials: ensure dtype float
2025-07-06 16:36:44 +01:00
ValueRaider
8f04f91e2b Financials: ensure dtype float 2025-07-05 16:03:49 +01:00
ValueRaider
4a1e1a9fb2 Merge pull request #2562 from ranaroussi/fix/prices-period-mix-with-start-end
Prices: handle mixing period with start/end
2025-07-05 13:32:56 +01:00
ValueRaider
a05a759852 Merge pull request #2558 from ranaroussi/fix/price-repair-fx
Price repair: when changing FX, update metadata
2025-07-05 13:32:47 +01:00
ValueRaider
9b7f53689b Prices: handle mixing period with start/end 2025-07-03 21:48:58 +01:00
ValueRaider
31109b2a2a Merge pull request #2561 from ranaroussi/fix/prices-start-with-end-none
Prices: fix behaviour change on end=None
2025-07-02 21:39:10 +01:00
ValueRaider
8e88aec77d Prices: fix behaviour change on end=None 2025-07-02 21:37:27 +01:00
ValueRaider
068e6b04db Bug form: emphasise not affiliated with Yahoo, add reporting link 2025-06-27 21:05:34 +01:00
ValueRaider
90575a029f Price repair: when changing FX, update metadata 2025-06-27 19:31:22 +01:00
ValueRaider
5dbc2950e8 Version 0.2.64 2025-06-27 17:15:06 +01:00
ValueRaider
4707bc035e Merge pull request #2556 from ranaroussi/dev
sync dev -> main
2025-06-27 17:13:58 +01:00
ValueRaider
81011a7a75 Prices: fix 'period' when start/end set - extend to download() 2025-06-27 17:10:49 +01:00
ValueRaider
14ec3df9a4 Merge pull request #2555 from ranaroussi/fix/earnings-dates-type
earnings_dates: handle 'Event Type' properly
2025-06-27 16:57:38 +01:00
ValueRaider
1056487183 earnings_dates: handle 'Event Type' properly 2025-06-27 16:56:57 +01:00
ValueRaider
b7a425d683 Merge pull request #2550 from ranaroussi/fix/history-period-with-start-end
Prices: fix 'period' arg when start or end set
2025-06-26 19:24:25 +01:00
ValueRaider
a61e0a07b5 Merge pull request #2549 from ranaroussi/fix/dividends-with-currency
Handle dividends with FX, convert if repair=True
2025-06-26 19:24:21 +01:00
ValueRaider
e2150daf18 Prices: fix 'period' when start/end set, improve YFInvalidPeriodError 2025-06-26 11:09:46 +01:00
ValueRaider
69dbf74292 Handle dividends with FX, convert if repair=True 2025-06-26 10:55:34 +01:00
ValueRaider
b5c2160837 Merge pull request #2551 from ranaroussi/docs/price-repair
Add price repair to doc
2025-06-25 21:11:55 +01:00
ValueRaider
b8ab067caa Add price repair to doc 2025-06-25 21:10:21 +01:00
ValueRaider
c6429482ab Action to auto-close default issues 2025-06-24 10:23:29 +01:00
ValueRaider
4f9b6d6e9f Merge pull request #2540 from ranaroussi/main
sync main -> dev
2025-06-14 14:29:13 +01:00
ValueRaider
5812aa6a69 Version 0.2.63 2025-06-12 09:41:44 +01:00
ValueRaider
ea810b4b35 Merge pull request #2531 from ranaroussi/hotfix/download-isin
Fix download()+ISIN
2025-06-12 09:41:00 +01:00
ValueRaider
53bce46929 Fix download()+ISIN 2025-06-12 08:54:57 +01:00
ValueRaider
0eb11c56b0 Version 0.2.62 2025-06-08 16:08:37 +01:00
ValueRaider
2981893f30 Merge pull request #2525 from ranaroussi/dev
sync dev -> main
2025-06-08 16:02:33 +01:00
ValueRaider
3250386136 Merge pull request #2402 from cclauss/patch-1
GitHub Action: Replace archived ruff action with official action
2025-06-08 14:58:43 +01:00
ValueRaider
aa606642c0 Merge branch 'main' into patch-1 2025-06-08 14:58:10 +01:00
ValueRaider
cc0b03efd1 Merge pull request #2509 from cole-st-john/adjusting_max_period_logic2
adjusting for processing time in max period (reduced)
2025-06-08 12:52:55 +01:00
ValueRaider
5bbd3d096c fix ruff 2025-06-07 23:16:56 +01:00
ValueRaider
b8b04b5345 Add new config 'hide_exceptions 2025-06-07 23:14:04 +01:00
ValueRaider
39dd87080d Merge pull request #2523 from ranaroussi/feature/print_once_replace_with_warnings
Replace 'print_once' with warnings.
2025-06-07 22:27:18 +01:00
ValueRaider
72a2fd8955 Merge pull request #2516 from ranaroussi/feature/isin-cache
Feature: ISIN cache
2025-06-07 21:21:09 +01:00
ValueRaider
dd62ce510e Replace 'print_once' with warnings. Remove 'basic_info'. 2025-06-07 21:19:38 +01:00
ValueRaider
61ceb2b1a8 Merge branch 'dev' into feature/isin-cache 2025-06-07 18:03:47 +01:00
ValueRaider
5d7a298239 Merge pull request #2514 from ranaroussi/fix/isin-proxy-msg
Fix ISIN proxy
2025-05-27 21:05:46 +01:00
ValueRaider
0192d2e194 Prune old ISINs from cache, because Yahoo won't serve 2025-05-25 12:27:57 +01:00
ValueRaider
8c6eb1afeb ISIN->symbol cache 2025-05-25 12:09:51 +01:00
ValueRaider
ef60663bc2 Fix ISIN proxy 2025-05-25 11:48:42 +01:00
cole-st-john
d15cf378a1 Fix 'max' period
- add 5 sec buffer for processing time
- add '2m' interval
- increase '1m' max to 8 days
2025-05-21 20:28:15 +01:00
ValueRaider
a9282e5739 Fix ruff 2025-05-17 11:25:10 +01:00
ValueRaider
a506838c3c Merge pull request #2504 from ranaroussi/main
sync main -> dev
2025-05-17 11:19:36 +01:00
ValueRaider
f716eec5fe Little fixes for tests & proxy msg 2025-05-14 21:41:17 +01:00
ValueRaider
e769570f33 Tidy CONTRIBUTING.md 2025-05-14 21:15:16 +01:00
ValueRaider
3bc6bacf56 Replace requests.HTTPError with curl_cffi 2025-05-13 21:25:25 +01:00
ValueRaider
7db82f3496 Merge pull request #2491 from vsukhoml/crumb
Fix for rate limit during getting crumb.
2025-05-13 20:52:00 +01:00
ValueRaider
4ac5cd87b3 Docs: simplify dev guide 2025-05-13 20:48:33 +01:00
Vadim Sukhomlinov
4a91008c09 Fix for rate limit during getting crumb.
Address #2441, #2480
Don't check for cached crumb to be wrong since this shall not happen with a new flow.
2025-05-13 12:34:23 -07:00
ValueRaider
de0760eec8 Version 0.2.61 2025-05-12 09:21:21 +01:00
ValueRaider
55bc1bdced Merge pull request #2493 from ranaroussi/hotfix/live-type-hints-again
Fix ALL type hints in live.py
2025-05-12 09:19:39 +01:00
ValueRaider
b509dc9551 Fix ALL type hints in live.py 2025-05-12 09:17:16 +01:00
ValueRaider
bb6ebb4b84 Version 0.2.60 2025-05-11 21:06:20 +01:00
ValueRaider
d08afa21fc Merge pull request #2489 from ranaroussi/dev
sync dev -> main
2025-05-11 20:57:40 +01:00
ValueRaider
5bd805b3f6 Merge pull request #2488 from ranaroussi/fix/live-type-hints
Fix type hints in live.py
2025-05-11 20:39:16 +01:00
ValueRaider
54c6ac3ed7 Fix type hints in live.py 2025-05-11 19:52:10 +01:00
ValueRaider
68b7c16162 Merge pull request #2487 from ranaroussi/feature/deprecate-requests-cache
Deprecate using requests_cache etc, only curl_cffi works
2025-05-11 15:16:56 +01:00
ValueRaider
78ad990371 Enforce session be curl_cffi 2025-05-11 15:15:57 +01:00
ValueRaider
81d8737a25 Merge pull request #2440 from ranaroussi/fix/screen-offset
Fix screen + offset
2025-05-11 13:25:53 +01:00
ValueRaider
946a84bf20 Screen: fix predefined+offset by switching endpoint 2025-05-11 13:24:54 +01:00
ValueRaider
890026c862 Change docs trigger to main ; Improve contribute docs 2025-05-11 12:14:24 +01:00
ValueRaider
22e4219ec7 Remove 'Smarter Scraping' from docs 2025-05-11 11:07:53 +01:00
ValueRaider
ac9184bf18 Merge pull request #2485 from dhruvan2006/fix/live
Fix: Protobuf & Websockeets Requirement
2025-05-11 10:06:04 +01:00
ValueRaider
745d554aae Merge pull request #2483 from ranaroussi/fix/cookie-reuse
Fix cookie reuse/caching + logging
2025-05-11 10:05:40 +01:00
ValueRaider
ec5548bd85 Fix cookie reuse ; Handle DNS blocking fc.yahoo.com 2025-05-11 10:04:37 +01:00
Dhruvan Gnanadhandayuthapani
63b56a799a Relax protobuf version requirements 2025-05-11 00:12:41 +02:00
ValueRaider
d1dde1814d Merge pull request #2466 from ranaroussi/main
sync main -> dev
2025-05-08 19:05:42 +01:00
ValueRaider
9158d3c119 Merge pull request #2446 from ranaroussi/main
sync main -> dev
2025-05-05 14:20:33 +01:00
ValueRaider
75510557ea fix typo in setup.py 2025-05-03 12:16:09 +01:00
ValueRaider
c5209cad3b Merge pull request #2435 from ranaroussi/main
sync main -> dev
2025-05-03 11:20:10 +01:00
Christian Clauss
b8ae8f317f GitHub Action: Replace archived ruff action with official action
https://github.com/ChartBoost/ruff-action has been archived so replace it with the official https://github.com/astral-sh/ruff-action from the creators of ruff.
2025-04-15 17:16:22 +02:00
68 changed files with 2890 additions and 1210 deletions

View File

@@ -18,7 +18,15 @@ body:
Are you spelling symbol *exactly* same as Yahoo?
Then visit `finance.yahoo.com` and confirm they have the data you want. Maybe your symbol was delisted, or your expectations of `yfinance` are wrong.
Then visit `finance.yahoo.com` and confirm they have the data you want. Maybe your symbol was delisted.
### Data is wrong
**yfinance is not affiliated with Yahoo**. If Yahoo Finance website data is bad, tell Yahoo: https://help.yahoo.com/kb/finance-for-web/report-problems-feedback-ideas-yahoo-finance-sln28397.html.
But if yfinance is creating error during processing, then finish this form.
For price errors, try price repair: https://ranaroussi.github.io/yfinance/advanced/price_repair.html
### Are you spamming Yahoo?
@@ -63,7 +71,7 @@ body:
id: bad-data-proof
attributes:
label: "Bad data proof"
description: "If `yfinance` returning bad data, show proof of good data here. Best proof is screenshot of finance.yahoo.com"
description: "If yfinance returning bad data, show proof of good data on Yahoo Finance website here."
validations:
required: false

View File

@@ -0,0 +1,42 @@
name: Auto-close issues using default template
on:
issues:
types: [opened]
jobs:
check-template:
runs-on: ubuntu-latest
steps:
- name: Check if issue uses custom template
uses: actions/github-script@v7
with:
script: |
const issue = context.payload.issue;
const body = issue.body || '';
const title = issue.title || '';
const textToCheck = (title + ' ' + body).toLowerCase();
// Check for specific fields from your custom form
const hasCustomFields = body.includes('### Describe bug') ||
body.includes('### Simple code that reproduces');
// Check for bug-related keywords
const bugKeywords = ['bug', 'problem', 'broken'];
const looksBugReport = bugKeywords.some(keyword => textToCheck.includes(keyword));
// Only close if it looks like a bug report but doesn't use the template
if (!hasCustomFields && looksBugReport) {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue.number,
body: 'This issue appears to be a bug report but doesn\'t use our bug report template. Resubmit with our custom bug report form..'
});
await github.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue.number,
state: 'closed'
});
}

View File

@@ -3,7 +3,8 @@ name: Build and Deploy Sphinx Docs
on:
push:
branches:
- dev-documented
- main
# - dev-documented
workflow_dispatch:
jobs:

View File

@@ -9,5 +9,7 @@ jobs:
ruff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: chartboost/ruff-action@v1
- uses: actions/checkout@v4
- uses: astral-sh/ruff-action@v3
with:
args: check --ignore E702 . --exclude yfinance/pricing_pb2.py

3
.gitignore vendored
View File

@@ -9,7 +9,6 @@ yfinance.egg-info
build/
*.html
*.css
*.png
test.ipynb
# Environments
@@ -24,4 +23,4 @@ ENV/
/doc/_build/
/doc/source/reference/api
!yfinance.css
!/doc/source/development/assets/branches.png
!/doc/source/development/assets/branches.png

View File

@@ -1,9 +1,81 @@
Change Log
===========
1.0
---
yfinance been stable a long time now, time to grow up
No breaking changes, but some deprecation warnings.
Features:
- New config class yf.config / Improve exception handling #2524
- Feature: Earning Calendar (& other calendars) #2615 @ianmihura
- Add optional retry mechanism for transient network errors #2627 @axisrow
Fixes:
- 'Industry' column names #2613 @danchev
- Correct 30m interval alignment and filtering for NSE/early-open markets #2642 @biplavbarua
- Screener's sector industry mapping #2648 @ericpien
- Set period default to None in multi #2650 @evanreynolds9
- price-div-repair dropping NaN rows #2651
- Block curl_cffi version 0.14 #2653
0.2.66
------
Screener: add Swiss exchange and industry field
Support MIC #2579
Fixes:
- parse epoch dt #2573
- earnings_dates #2591
- merge intraday prices with divs/splits #2595
- exceptions missing detail #2599
0.2.65
------
Financials: ensure dtype float #2563
Prices: fix handling arguments start/end/period #2561 #2562
Price repair: when changing FX, update metadata #2558
0.2.64
------
Prices:
- handle dividends with FX, convert if repair=True #2549
- fix 'period' arg when start or end set #2550
earnings_dates: handle 'Event Type' properly #2555
0.2.63
------
Fix download(ISIN) # 2531
0.2.62
------
Fix prices 'period=max' sometimes failing # 2509
ISIN cache #2516
Proxy:
- fix false 'proxy deprecated' messages
- fix ISIN + proxy #2514
- replace print_once with warnings #2523
Error handling:
- detect rate-limit during crumb fetch #2491
- replace requests.HTTPError with curl_cffi
0.2.61
------
Fix ALL type hints in websocket #2493
0.2.60
------
Fix cookie reuse, and handle DNS blocking fc.yahoo.com #2483
Fixes for websocket:
- relax protobuf version #2485
- increase websockets version #2485
- fix type hints #2488
Fix predefined screen offset #2440
0.2.59
------
Fix the fix for rate-limit #2452
Feature: live price data websocket #2201
0.2.58
------

View File

@@ -1,37 +1,23 @@
# Contributing
> [!NOTE]
> This is a brief guide to contributing to yfinance.
> For more information See the [Developer Guide](https://ranaroussi.github.io/yfinance/development) for more information.
yfinance relies on the community to investigate bugs and contribute code.
## Changes
The list of changes can be found in the [Changelog](https://github.com/ranaroussi/yfinance/blob/main/CHANGELOG.rst)
## Running a branch
```bash
pip install git+ranaroussi/yfinance.git@dev # dev branch
```
For more information, see the [Developer Guide](https://ranaroussi.github.io/yfinance/development/running.html).
This is a quick short guide, full guide at https://ranaroussi.github.io/yfinance/development/index.html
## Branches
YFinance uses a two-layer branch model:
* **dev**: new features & some bug-fixes merged here, tested together, conflicts fixed, etc.
* **dev**: new features & most bug-fixes merged here, tested together, conflicts fixed, etc.
* **main**: stable branch where PIP releases are created.
> [!NOTE]
> By default, branches target **main**, but most contributions should target **dev**.
> Direct merges to **main** are allowed if:
> * `yfinance` is massively broken
> * Part of `yfinance` is broken, and the fix is simple and isolated
> * Not updating the code (e.g. docs)
## Running a branch
> [!NOTE]
> For more information, see the [Developer Guide](https://ranaroussi.github.io/yfinance/development/branches.html).
```bash
pip install git+ranaroussi/yfinance.git@dev # <- dev branch
```
https://ranaroussi.github.io/yfinance/development/running.html
### I'm a GitHub newbie, how do I contribute code?
@@ -39,39 +25,36 @@ YFinance uses a two-layer branch model:
2. Implement your change in your fork, ideally in a specific branch
3. Create a Pull Request, from your fork to this project. If addressing an Issue, link to it
3. Create a [Pull Request](https://github.com/ranaroussi/yfinance/pulls), from your fork to this project. If addressing an Issue, link to it
> [!NOTE]
> See the [Developer Guide](https://ranaroussi.github.io/yfinance/development/contributing.html) for more information.
### [How to download & run a GitHub version of yfinance](#Running-a-branch)
https://ranaroussi.github.io/yfinance/development/code.html
## Documentation website
The new docs website [ranaroussi.github.io/yfinance/index.html](https://ranaroussi.github.io/yfinance/index.html) is generated automatically from code.
The new docs website is generated automatically from code. https://ranaroussi.github.io/yfinance/index.html
> [!NOTE]
> See the [Developer Guide](https://ranaroussi.github.io/yfinance/development/documentation.html) for more information
> Including how to build and run the docs locally.
Remember to updates docs when you change code, and check docs locally.
https://ranaroussi.github.io/yfinance/development/documentation.html
## Git tricks
Help keep the Git commit history and [network graph](https://github.com/ranaroussi/yfinance/network) compact:
* got a long descriptive commit message? `git commit -m "short sentence summary" -m "full commit message"`
* combine multiple commits into 1 with `git squash`
* `git rebase` is your friend: change base branch, or "merge in" updates
https://ranaroussi.github.io/yfinance/development/code.html#git-stuff
## Unit tests
Tests have been written using the built-in Python module `unittest`. Examples:
#### Run all tests: `python -m unittest discover -s tests`
* Run all tests: `python -m unittest discover -s tests`
> [!NOTE]
>
> See the [Developer Guide](https://ranaroussi.github.io/yfinance/development/testing.html) for more information.
https://ranaroussi.github.io/yfinance/development/testing.html
## Git stuff
### commits
To keep the Git commit history and [network graph](https://github.com/ranaroussi/yfinance/network) compact please follow these two rules:
* For long commit messages use this: `git commit -m "short sentence summary" -m "full commit message"`
* `squash` tiny/negligible commits back with meaningful commits, or to combine successive related commits
> [!NOTE]
> See the [Developer Guide](https://ranaroussi.github.io/yfinance/development/contributing.html#GIT-STUFF) for more information.
> See the [Developer Guide](https://ranaroussi.github.io/yfinance/development/contributing.html#GIT-STUFF) for more information.

View File

@@ -6,11 +6,11 @@
<a target="new" href="https://pypi.python.org/pypi/yfinance"><img border=0 src="https://img.shields.io/badge/python-2.7,%203.6+-blue.svg?style=flat" alt="Python version"></a>
<a target="new" href="https://pypi.python.org/pypi/yfinance"><img border=0 src="https://img.shields.io/pypi/v/yfinance.svg?maxAge=60%" alt="PyPi version"></a>
<a target="new" href="https://pypi.python.org/pypi/yfinance"><img border=0 src="https://img.shields.io/pypi/status/yfinance.svg?maxAge=60" alt="PyPi status"></a>
<a target="new" href="https://pypi.python.org/pypi/yfinance"><img border=0 src="https://img.shields.io/pypi/dm/yfinance.svg?maxAge=2592000&label=installs&color=%2327B1FF" alt="PyPi downloads"></a>
<a target="new" href="https://pypi.python.org/pypi/yfinance"><img border=0 src="https://img.shields.io/pypi/dm/yfinance.svg?maxAge=86400&label=installs&color=%2327B1FF" alt="PyPi downloads"></a>
<a target="new" href="https://github.com/ranaroussi/yfinance"><img border=0 src="https://img.shields.io/github/stars/ranaroussi/yfinance.svg?style=social&label=Star&maxAge=60" alt="Star this repo"></a>
<a target="new" href="https://x.com/intent/follow?screen_name=aroussi"><img border=0 src="https://img.shields.io/twitter/follow/aroussi.svg?style=social&label=Follow&maxAge=60" alt="Follow me on twitter"></a>
<a href="https://trendshift.io/repositories/4578" target="_blank"><img src="https://trendshift.io/api/badge/repositories/4578" alt="ranaroussi%2Fyfinance | Trendshift" style="width: 250px; height: 55px;" width="250" height="55"/></a>
**yfinance** offers a Pythonic way to fetch financial & market data from [Yahoo!Ⓡ finance](https://finance.yahoo.com).
@@ -53,6 +53,8 @@ Install `yfinance` from PYPI using `pip`:
$ pip install yfinance
```
### [yfinance relies on the community to investigate bugs and contribute code. Here's how you can help.](CONTRIBUTING.md)
---
![Star History Chart](https://api.star-history.com/svg?repos=ranaroussi/yfinance)

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

View File

@@ -5,6 +5,7 @@
.. currentmodule:: {{ module }}
.. autoclass:: {{ objname }}
:exclude-members: {% for item in attributes %}{{ item }}{% if not loop.last %}, {% endif %}{% endfor %}
{% block attributes %}
{% if attributes %}

View File

@@ -1,47 +1,6 @@
Caching
=======
Smarter Scraping
----------------
Install the `nospam` package to cache API calls and reduce spam to Yahoo:
.. code-block:: bash
pip install yfinance[nospam]
To use a custom `requests` session, pass a `session=` argument to
the Ticker constructor. This allows for caching calls to the API as well as a custom way to modify requests via the `User-agent` header.
.. code-block:: python
import requests_cache
session = requests_cache.CachedSession('yfinance.cache')
session.headers['User-agent'] = 'my-program/1.0'
ticker = yf.Ticker('MSFT', session=session)
# The scraped response will be stored in the cache
ticker.actions
Combine `requests_cache` with rate-limiting to avoid triggering Yahoo's rate-limiter/blocker that can corrupt data.
.. code-block:: python
from requests import Session
from requests_cache import CacheMixin, SQLiteCache
from requests_ratelimiter import LimiterMixin, MemoryQueueBucket
from pyrate_limiter import Duration, RequestRate, Limiter
class CachedLimiterSession(CacheMixin, LimiterMixin, Session):
pass
session = CachedLimiterSession(
limiter=Limiter(RequestRate(2, Duration.SECOND*5)), # max 2 requests per 5 seconds
bucket_class=MemoryQueueBucket,
backend=SQLiteCache("yfinance.cache"),
)
Persistent Cache
----------------

View File

@@ -2,14 +2,55 @@
Config
******
`yfinance` has a new global config for sharing common values.
Proxy
-----
Set proxy once in config, affects all yfinance data fetches.
`yfinance` has a new global config for sharing common values:
.. code-block:: python
import yfinance as yf
yf.set_config(proxy="PROXY_SERVER")
>>> import yfinance as yf
>>> yf.config
{
"network": {
"proxy": null,
"retries": 0
},
"debug": {
"hide_exceptions": true,
"logging": false
}
}
>>> yf.config.network
{
"proxy": null,
"retries": 0
}
Network
-------
* **proxy** - Set proxy for all yfinance data fetches.
.. code-block:: python
yf.config.network.proxy = "PROXY_SERVER"
* **retries** - Configure automatic retry for transient network errors. The retry mechanism uses exponential backoff (1s, 2s, 4s...).
.. code-block:: python
yf.config.network.retries = 2
Debug
-----
* **hide_exceptions** - Set to `False` to stop yfinance hiding exceptions.
.. code-block:: python
yf.config.debug.hide_exceptions = False
* **logging** - Set to `True` to enable verbose debug logging.
.. code-block:: python
yf.config.debug.logging = True

View File

@@ -8,4 +8,5 @@ Advanced
logging
config
caching
multi_level_columns
multi_level_columns
price_repair

View File

@@ -0,0 +1,277 @@
************
Price Repair
************
The new argument ``repair=True`` in ``history()`` and ``download()`` will attempt to fix a variety of price errors caused by Yahoo. Only US market data appears perfect, I guess Yahoo doesn't care much about rest of world?
The returned table will have a new column ``Repaired?`` that specifies if row was repaired.
Price repair
============
Missing dividend adjustment
---------------------------
If dividend in data but preceding ``Adj Close`` = ``Close``, then manually apply dividend-adjustment to ``Adj Close``.
Note: ``Repaired?`` is NOT set to ``True`` because fix only changes ``Adj Close``
.. figure:: /_static/images/repair-prices-missing-div-adjust.png
:alt: 8TRA.DE: repair missing dividend adjustment
:width: 80%
:align: left
8TRA.DE
.. container:: clearer
..
Missing split adjustment
------------------------
If stock split in data but preceding price data is not adjusted, then manually apply stock split.
Requires date range include 1 day after stock split for calibration - sometimes Yahoo fails to adjust prices on stock split day.
.. figure:: /_static/images/repair-prices-missing-split-adjust.png
:alt: MOB.ST: repair missing split adjustment
:width: 80%
:align: left
MOB.ST
.. container:: clearer
..
Missing data
------------
If price data is clearly missing or corrupt, then reconstructed using smaller interval e.g. ``1h`` to fix ``1d`` data.
.. figure:: /_static/images/repair-prices-missing-row.png
:alt: 1COV.DE: repair missing row
:width: 80%
:align: left
1COV.DE missing row
.. container:: clearer
..
.. figure:: /_static/images/repair-prices-missing-volume-intraday.png
:alt: 1COV.DE: repair missing Volume, but intraday price changed
:width: 80%
:align: left
1COV.DE missing Volume, but intraday price changed
.. container:: clearer
..
.. figure:: /_static/images/repair-prices-missing-volume-daily.png
:alt: 0316.HK: repair missing Volume, but daily price changed
:width: 80%
:align: left
0316.HK missing Volume, but daily price changed
.. container:: clearer
..
100x errors
-----------
Sometimes Yahoo mixes up currencies e.g. $/cents or £/pence. So some prices are 100x wrong.
Sometimes they are spread randomly through data - these detected with ``scipy`` module.
Other times they are in a block, because Yahoo decided one day to permanently switch currency.
.. figure:: /_static/images/repair-prices-100x.png
:alt: AET.L: repair 100x
:width: 80%
:align: left
AET.L
Price reconstruction - algorithm notes
--------------------------------------
Spam minimised by grouping fetches. Tries to be aware of data limits e.g. ``1h`` cannot be fetched beyond 2 years.
If Yahoo eventually does fix the bad data that required reconstruction, you will see it's slightly different to reconstructed prices and volume often significantly different. Best I can do, and beats missing data.
Dividend repair (new)
=====================
Fix errors in dividends:
1. adjustment missing or 100x too small/big for the dividend
2. duplicate dividend (within 7 days)
3. dividend 100x too big/small for the ex-dividend price drop
4. ex-div date wrong (price drop is few days/weeks after)
Most errors I've seen are on London stock exchange (£/pence mixup), but no exchange is safe.
IMPORTANT - false positives
---------------------------
Because fixing (3) relies on price action, there is a chance of a "false positive" (FP) - thinking an error exists when data is good.
FP rate increases with longer intervals, so only 1d intervals are repaired. If you request repair on multiday intervals (weekly etc), then: 1d is fetched from Yahoo, repaired, then resampled - **this has nice side-effect of solving Yahoo's flawed way of div-adjusting multiday intervals.**
FP rate on 1d is tiny. They tend to happen with tiny dividends e.g. 0.5%, mistaking normal price volatility for an ex-div drop 100x bigger than the dividend, causing repair of the "too small" dividend (repair logic already tries to account for normal volatility by subtracting median). Either accept the risk, or fetch 6-12 months of prices with at least 2 dividends - then can analyse the dividends together to identify false positives.
Adjustment missing
------------------
1398.HK
.. code-block:: text
# ORIGINAL:
Close Adj Close Dividends
2024-07-08 00:00:00+08:00 4.33 4.33 0.335715
2024-07-04 00:00:00+08:00 4.83 4.83 0.000000
.. code-block:: text
# REPAIRED:
Close Adj Close Dividends
2024-07-08 00:00:00+08:00 4.33 4.330000 0.335715
2024-07-04 00:00:00+08:00 4.83 4.494285 0.000000
Adjustment too small
--------------------
3IN.L
.. code-block:: text
# ORIGINAL:
Close Adj Close Dividends
2024-06-13 00:00:00+01:00 3.185 3.185000 0.05950
2024-06-12 00:00:00+01:00 3.270 3.269405 0.00000
.. code-block:: text
# REPAIRED:
Close Adj Close Dividends
2024-06-13 00:00:00+01:00 3.185 3.185000 0.05950
2024-06-12 00:00:00+01:00 3.270 3.210500 0.00000
Duplicate (within 7 days)
-------------------------
ALC.SW
.. code-block:: text
# ORIGINAL:
Close Adj Close Dividends
2023-05-10 00:00:00+02:00 70.580002 70.352142 0.21
2023-05-09 00:00:00+02:00 65.739998 65.318443 0.21
2023-05-08 00:00:00+02:00 66.379997 65.745682 0.00
.. code-block:: text
# REPAIRED:
Close Adj Close Dividends
2023-05-10 00:00:00+02:00 70.580002 70.352142 0.00
2023-05-09 00:00:00+02:00 65.739998 65.527764 0.21
2023-05-08 00:00:00+02:00 66.379997 65.956371 0.00
Dividend too big
----------------
HLCL.L
.. code-block:: text
# ORIGINAL:
Close Adj Close Dividends
2024-06-27 00:00:00+01:00 2.360 2.3600 1.78
2024-06-26 00:00:00+01:00 2.375 2.3572 0.00
# REPAIRED:
Close Adj Close Dividends
2024-06-27 00:00:00+01:00 2.360 2.3600 0.0178
2024-06-26 00:00:00+01:00 2.375 2.3572 0.0000
Dividend & adjust too big
-------------------------
LTI.L
.. code-block:: text
# ORIGINAL:
Close Adj Close Adj Dividends
2024-08-08 00:00:00+01:00 768.0 768.0 1.0000 5150.0
2024-08-07 00:00:00+01:00 819.0 -4331.0 -5.2882 0.0
Close Adj Close Adj Dividends
2024-08-08 00:00:00+01:00 768.0 768.0 1.0000 51.5
2024-08-07 00:00:00+01:00 819.0 767.5 0.9371 0.0
Dividend too small
------------------
BVT.L
.. code-block:: text
# ORIGINAL:
Close Adj Close Adj Dividends
2022-02-03 00:00:00+00:00 0.7534 0.675197 0.8962 0.00001
2022-02-01 00:00:00+00:00 0.7844 0.702970 0.8962 0.00000
.. code-block:: text
# REPAIRED:
Close Adj Close Adj Dividends
2022-02-03 00:00:00+00:00 0.7534 0.675197 0.8962 0.001
2022-02-01 00:00:00+00:00 0.7844 0.702075 0.8950 0.000
Adjusted 2x on day before
-------------------------
clue: Close < Low
2020.OL
.. code-block:: text
# ORIGINAL:
Low Close Adj Close Dividends
2023-12-21 00:00:00+01:00 120.199997 121.099998 118.868782 0.18
2023-12-20 00:00:00+01:00 122.000000 121.900002 119.477371 0.00
.. code-block:: text
# REPAIRED:
Low Close Adj Close Dividends
2023-12-21 00:00:00+01:00 120.199997 121.099998 118.868782 0.18
2023-12-20 00:00:00+01:00 122.000000 122.080002 119.654045 0.00
ex-div date wrong
-----------------
TETY.ST
.. code-block:: text
# ORIGINAL:
Close Adj Close Dividends
2022-06-22 00:00:00+02:00 66.699997 60.085415 0.0
2022-06-21 00:00:00+02:00 71.599998 64.499489 0.0
2022-06-20 00:00:00+02:00 71.800003 64.679657 5.0
2022-06-17 00:00:00+02:00 71.000000 59.454838 0.0
.. code-block:: text
# REPAIRED:
Close Adj Close Dividends
2022-06-22 00:00:00+02:00 66.699997 60.085415 5.0
2022-06-21 00:00:00+02:00 71.599998 60.007881 0.0
2022-06-20 00:00:00+02:00 71.800003 60.175503 0.0
2022-06-17 00:00:00+02:00 71.000000 59.505021 0.0

View File

@@ -1,40 +0,0 @@
Branches
---------
To support rapid development without breaking stable versions, this project uses a two-layer branch model:
.. image:: assets/branches.png
:alt: Branching Model
`Inspiration <https://miro.medium.com/max/700/1*2YagIpX6LuauC3ASpwHekg.png>`_
- **dev**: New features and some bug fixes are merged here. This branch allows collective testing, conflict resolution, and further stabilization before merging into the stable branch.
- **main**: Stable branch where PIP releases are created.
By default, branches target **main**, but most contributions should target **dev**.
**Exceptions**:
Direct merges to **main** are allowed if:
- `yfinance` is massively broken
- Part of `yfinance` is broken, and the fix is simple and isolated
- Not updating the code (e.g. docs)
Rebasing
--------
If asked to move your branch from **main** to **dev**:
1. Ensure all relevant branches are pulled.
2. Run:
.. code-block:: bash
git checkout {branch}
git rebase --onto dev main {brach}
git push --force-with-lease origin {branch}
Running a branch
----------------
Please see `this page </development/running>`_.

View File

@@ -0,0 +1,90 @@
****
Code
****
To support rapid development without breaking stable versions, this project uses a two-layer branch model:
.. image:: assets/branches.png
:alt: Branching Model
`Inspiration <https://miro.medium.com/max/700/1*2YagIpX6LuauC3ASpwHekg.png>`_
- **dev**: New features and some bug fixes are merged here. This branch allows collective testing, conflict resolution, and further stabilization before merging into the stable branch.
- **main**: Stable branch where PIP releases are created.
By default, branches target **main**, but most contributions should target **dev**.
**Exceptions**:
Direct merges to **main** are allowed if:
- `yfinance` is massively broken
- Part of `yfinance` is broken, and the fix is simple and isolated
- Not updating the code (e.g. docs)
Creating your branch
--------------------
1. Fork the repository on GitHub. If already forked, remember to ``Sync fork``
2. Clone your forked repository:
.. code-block:: bash
git clone https://github.com/{user}/{repo}.git
3. Create a new branch for your feature or bug fix, from appropriate base branch:
.. code-block:: bash
git checkout {base e.g. dev}
git pull
git checkout -b {your branch}
4. Make your changes, commit them, and push your branch to GitHub. To keep the commit history and `network graph <https://github.com/ranaroussi/yfinance/network>`_ compact, give your commits a very short summary then description:
.. code-block:: bash
git commit -m "short sentence summary" -m "full commit message"
# Long message can be multiple lines (tip: copy-paste)
6. `Open a pull request on Github <https://github.com/ranaroussi/yfinance/pulls>`_.
Running a branch
----------------
Please see `this page </development/running>`_.
Git stuff
---------
- You might be asked to move your branch from ``main`` to ``dev``. This is a ``git rebase``. Remember to update **all** branches involved.
.. code-block:: bash
# update all branches:
git checkout main
git pull
git checkout dev
git pull
# rebase from main to dev:
git checkout {your branch}
git pull
git rebase --onto dev main {your branch}
git push --force-with-lease origin {your branch}
- ``git rebase`` can also be used to update your branch with new commits from base, but without adding a commit to your branch history like git merge does. This keeps history clean and avoids future merge problems.
.. code-block:: bash
git checkout {base branch e.g. dev}
git pull
git checkout {your branch}
git rebase {base}
git push --force-with-lease origin {your branch}
- ``git squash`` tiny or negligible commits with meaningful ones, or to combine successive related commits. `git squash guide <https://docs.gitlab.com/ee/topics/git/git_rebase.html#interactive-rebase>`_
.. code-block:: bash
git rebase -i HEAD~2
git push --force-with-lease origin {your branch}

View File

@@ -1,61 +0,0 @@
********************************
Contributing to yfinance
********************************
`yfinance` relies on the community to investigate bugs and contribute code. Here&apos;s how you can help:
Contributing
------------
1. Fork the repository on GitHub. If already forked, remember to `Sync fork`
2. Clone your forked repository:
.. code-block:: bash
git clone https://github.com/{user}/{repo}.git
3. Create a new branch for your feature or bug fix:
.. code-block:: bash
git checkout -b {branch}
4. Make your changes, commit them, and push your branch to GitHub. To keep the commit history and `network graph <https://github.com/ranaroussi/yfinance/network>`_ compact:
Use short summaries for commits
.. code-block:: bash
git commit -m "short summary" -m "full commit message"
**Squash** tiny or negligible commits with meaningful ones.
.. code-block:: bash
git rebase -i HEAD~2
git push --force-with-lease origin {branch}
5. Open a pull request on the `yfinance` `Github <https://github.com/ranaroussi/yfinance/pulls>`_ page.
Git stuff
---------
To keep the Git commit history and [network graph](https://github.com/ranaroussi/yfinance/network) compact please follow these two rules:
- For long commit messages use this: `git commit -m "short sentence summary" -m "full commit message"`
- `squash` tiny/negligible commits back with meaningful commits, or to combine successive related commits. [Guide](https://docs.gitlab.com/ee/topics/git/git_rebase.html#interactive-rebase) but basically it's:
.. code-block:: bash
git rebase -i HEAD~2
git push --force-with-lease origin {branch}
### rebase
You might be asked to move your branch from `main` to `dev`. Make sure you have pulled **all** relevant branches then run:
.. code-block:: bash
git checkout {branch}
git rebase --onto dev main {brach}
git push --force-with-lease origin {branch}

View File

@@ -1,6 +1,6 @@
*************************************
Contribution to the documentation
*************************************
*************
Documentation
*************
.. contents:: Documentation:
:local:
@@ -36,15 +36,18 @@ To build the documentation locally, follow these steps:
3. **View Documentation Locally**:
..code-block:: bash
.. code-block:: bash
python -m http.server -d ./doc/_build/html
Then open "localhost:8000" in browser
Building documentation on main
------------------------------
The documentation updates are built on merge to ``main`` branch. This is done via GitHub Actions workflow based on ``/yfinance/.github/workflows/deploy_doc.yml``.
Publishing documentation
------------------------
Merge into ``main`` branch triggers auto-generating documentation by action ``.github/workflows/deploy_doc.yml``.
This publishes the generated HTML into branch ``documentation``.
1. Review the changes locally and push to ``dev``.

View File

@@ -2,12 +2,12 @@
Development
===========
yfinance relies on the community to investigate bugs and contribute code. Here's how you can help:
.. toctree::
:maxdepth: 1
contributing
code
running
documentation
reporting_bug
branches
testing
running
testing

View File

@@ -1,5 +0,0 @@
********************************
Reporting a Bug
********************************
Open a new issue on our `GitHub <https://github.com/ranaroussi/yfinance/issues>`_.

View File

@@ -5,12 +5,14 @@ With PIP
--------
.. code-block:: bash
pip install git+https://github.com/{user}/{repo}.git@{branch}
pip install git+https://github.com/{user}/{repo}.git@{branch}
E.g.:
.. code-block:: bash
pip install git+https://github.com/ranaroussi/yfinance.git@feature/name
pip install git+https://github.com/ranaroussi/yfinance.git@feature/name
With Git
--------

View File

@@ -0,0 +1,38 @@
import yfinance as yf
from datetime import datetime, timedelta
# Default init (today + 7 days)
calendar = yf.Calendars()
# Today's events: calendar of 1 day
tomorrow = datetime.now() + timedelta(days=1)
calendar = yf.Calendars(end=tomorrow)
# Default calendar queries - accessing the properties will fetch the data from YF
calendar.earnings_calendar
calendar.ipo_info_calendar
calendar.splits_calendar
calendar.economic_events_calendar
# Manual queries
calendar.get_earnings_calendar()
calendar.get_ipo_info_calendar()
calendar.get_splits_calendar()
calendar.get_economic_events_calendar()
# Earnings calendar custom filters
calendar.get_earnings_calendar(
market_cap=100_000_000, # filter out small-cap
filter_most_active=True, # show only actively traded. Uses: `screen(query="MOST_ACTIVES")`
)
# Example of real use case:
# Get inminent unreported earnings events
today = datetime.now()
is_friday = today.weekday() == 4
day_after_tomorrow = today + timedelta(days=4 if is_friday else 2)
calendar = yf.Calendars(today, day_after_tomorrow)
df = calendar.get_earnings_calendar(limit=100)
unreported_df = df[df["Reported EPS"].isnull()]

View File

@@ -16,6 +16,7 @@ The following are the publicly available classes, and functions exposed by the `
- :attr:`Ticker <yfinance.Ticker>`: Class for accessing single ticker data.
- :attr:`Tickers <yfinance.Tickers>`: Class for handling multiple tickers.
- :attr:`Market <yfinance.Market>`: Class for accessing market summary.
- :attr:`Calendars <yfinance.Calendars>`: Class for accessing calendar events data.
- :attr:`download <yfinance.download>`: Function to download market data for multiple tickers.
- :attr:`Search <yfinance.Search>`: Class for accessing search results.
- :attr:`Lookup <yfinance.Lookup>`: Class for looking up tickers.
@@ -23,7 +24,6 @@ The following are the publicly available classes, and functions exposed by the `
- :class:`AsyncWebSocket <yfinance.AsyncWebSocket>`: Class for asynchronously streaming live market data.
- :attr:`Sector <yfinance.Sector>`: Domain class for accessing sector information.
- :attr:`Industry <yfinance.Industry>`: Domain class for accessing industry information.
- :attr:`Market <yfinance.Market>`: Class for accessing market status & summary.
- :attr:`EquityQuery <yfinance.EquityQuery>`: Class to build equity query filters.
- :attr:`FundQuery <yfinance.FundQuery>`: Class to build fund query filters.
- :attr:`screen <yfinance.screen>`: Run equity/fund queries.
@@ -38,9 +38,9 @@ The following are the publicly available classes, and functions exposed by the `
yfinance.ticker_tickers
yfinance.stock
yfinance.market
yfinance.calendars
yfinance.financials
yfinance.analysis
yfinance.market
yfinance.search
yfinance.lookup
yfinance.websocket

View File

@@ -0,0 +1,21 @@
=====================
Calendars
=====================
.. currentmodule:: yfinance
Class
------------
The `Calendars` class allows you to get information about upcoming events, for example, earning events.
.. autosummary::
:toctree: api/
Calendars
Sample Code
------------------
.. literalinclude:: examples/calendars.py
:language: python

View File

@@ -20,6 +20,9 @@ Ticker stock methods
:meth:`yfinance.scrapers.history.PriceHistory.history`
Documentation for history
:doc:`../advanced/price_repair`
Documentation for price repair
.. autosummary::
:toctree: api/
:recursive:

View File

@@ -1,5 +1,5 @@
{% set name = "yfinance" %}
{% set version = "0.2.59" %}
{% set version = "1.0" %}
package:
name: "{{ name|lower }}"
@@ -15,6 +15,7 @@ build:
script: "{{ PYTHON }} -m pip install . --no-deps --ignore-installed -vv "
requirements:
# curl_cffi 0.14 has major problems, see their Github
host:
- pandas >=1.3.0
- numpy >=1.16.5
@@ -26,7 +27,7 @@ requirements:
- frozendict >=2.3.4
- beautifulsoup4 >=4.11.1
- html5lib >=1.1
- curl_cffi >=0.7
- curl_cffi >=0.7,<0.14
- peewee >=3.16.2
- pip
- python
@@ -42,7 +43,7 @@ requirements:
- frozendict >=2.3.4
- beautifulsoup4 >=4.11.1
- html5lib >=1.1
- curl_cffi >=0.7
- curl_cffi >=0.7,<0.14
- peewee >=3.16.2
- python

View File

@@ -10,6 +10,7 @@ peewee>=3.16.2
requests_cache>=1.0
requests_ratelimiter>=0.3.1
scipy>=1.6.3
curl_cffi>=0.7
protobuf>=5.29.0,<6
websockets>=11.0
# curl_cffi 0.14 has major problems, see their Github
curl_cffi>=0.7,<0.14
protobuf>=3.19.0
websockets>=13.0

View File

@@ -59,12 +59,13 @@ setup(
platforms=['any'],
keywords='pandas, yahoo finance, pandas datareader',
packages=find_packages(exclude=['contrib', 'docs', 'tests', 'examples']),
# curl_cffi 0.14 has major problems, see their Github
install_requires=['pandas>=1.3.0', 'numpy>=1.16.5',
'requests>=2.31', 'multitasking>=0.0.7',
'platformdirs>=2.0.0', 'pytz>=2022.5',
'frozendict>=2.3.4', 'peewee>=3.16.2',
'beautifulsoup4>=4.11.1', 'curl_cffi>=0.7',
'protobuf>=5.29.0,<6', 'websockets>=11.0'],
'beautifulsoup4>=4.11.1', 'curl_cffi>=0.7,<0.14',
'protobuf>=3.19.0', 'websockets>=13.0'],
extras_require={
'nospam': ['requests_cache>=1.0', 'requests_ratelimiter>=0.3.1'],
'repair': ['scipy>=1.6.3'],

View File

@@ -5,8 +5,8 @@ import datetime as _dt
import sys
import os
import yfinance
from requests_ratelimiter import LimiterSession
from pyrate_limiter import Duration, RequestRate, Limiter
# from requests_ratelimiter import LimiterSession
# from pyrate_limiter import Duration, RequestRate, Limiter
_parent_dp = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
_src_dp = _parent_dp
@@ -25,12 +25,15 @@ if os.path.isdir(testing_cache_dirpath):
import shutil
shutil.rmtree(testing_cache_dirpath)
# Setup a session to only rate-limit
history_rate = RequestRate(1, Duration.SECOND)
limiter = Limiter(history_rate)
session_gbl = LimiterSession(limiter=limiter)
# Since switching to curl_cffi, the requests_ratelimiter|cache won't work.
session_gbl = None
# Use this instead if you also want caching:
# # Setup a session to only rate-limit
# history_rate = RequestRate(1, Duration.SECOND)
# limiter = Limiter(history_rate)
# session_gbl = LimiterSession(limiter=limiter)
# # Use this instead if you also want caching:
# from requests_cache import CacheMixin, SQLiteCache
# from requests_ratelimiter import LimiterMixin
# from requests import Session

55
tests/test_calendars.py Normal file
View File

@@ -0,0 +1,55 @@
from datetime import datetime, timedelta, timezone
import unittest
import pandas as pd
from tests.context import yfinance as yf, session_gbl
class TestCalendars(unittest.TestCase):
def setUp(self):
self.calendars = yf.Calendars(session=session_gbl)
def test_get_earnings_calendar(self):
result = self.calendars.get_earnings_calendar(limit=1)
tickers = self.calendars.earnings_calendar.index.tolist()
self.assertIsInstance(result, pd.DataFrame)
self.assertEqual(len(result), 1)
self.assertIsInstance(tickers, list)
self.assertEqual(len(tickers), len(result))
self.assertEqual(tickers, result.index.tolist())
first_ticker = result.index.tolist()[0]
result_first_ticker = self.calendars.earnings_calendar.loc[first_ticker].name
self.assertEqual(first_ticker, result_first_ticker)
def test_get_earnings_calendar_init_params(self):
result = self.calendars.get_earnings_calendar(limit=5)
self.assertGreaterEqual(result['Event Start Date'].iloc[0], pd.to_datetime(datetime.now(tz=timezone.utc)))
start = datetime.now(tz=timezone.utc) - timedelta(days=7)
result = yf.Calendars(start=start).get_earnings_calendar(limit=5)
self.assertGreaterEqual(result['Event Start Date'].iloc[0], pd.to_datetime(start))
def test_get_ipo_info_calendar(self):
result = self.calendars.get_ipo_info_calendar(limit=5)
self.assertIsInstance(result, pd.DataFrame)
self.assertEqual(len(result), 5)
def test_get_economic_events_calendar(self):
result = self.calendars.get_economic_events_calendar(limit=5)
self.assertIsInstance(result, pd.DataFrame)
self.assertEqual(len(result), 5)
def test_get_splits_calendar(self):
result = self.calendars.get_splits_calendar(limit=5)
self.assertIsInstance(result, pd.DataFrame)
self.assertEqual(len(result), 5)
if __name__ == "__main__":
unittest.main()

View File

@@ -61,7 +61,7 @@ class TestPriceRepairAssumptions(unittest.TestCase):
vol_diff_pct0 = (dfr['Volume'].iloc[0] - df_truth['Volume'].iloc[0])/df_truth['Volume'].iloc[0]
vol_diff_pct1 = (dfr['Volume'].iloc[-1] - df_truth['Volume'].iloc[-1])/df_truth['Volume'].iloc[-1]
vol_diff_pct = _np.array([vol_diff_pct0, vol_diff_pct1])
vol_match = vol_diff_pct > -0.23
vol_match = vol_diff_pct > -0.32
vol_match_nmatch = _np.sum(vol_match)
vol_match_ndiff = len(vol_match) - vol_match_nmatch
if vol_match.all():
@@ -78,6 +78,8 @@ class TestPriceRepairAssumptions(unittest.TestCase):
if debug:
print("- investigate:")
print(f" - interval = {interval}")
print(f" - period = {period}")
print("- df_truth:")
print(df_truth)#[['Open', 'Close', 'Volume']])
df_1d = dat.history(interval='1d', period=period)
@@ -361,27 +363,23 @@ class TestPriceRepair(unittest.TestCase):
hist = dat._lazy_load_price_history()
tz_exchange = dat.fast_info["timezone"]
df_bad = _pd.DataFrame(data={"Open": [0, 114.37, 114.20],
"High": [0, 114.40, 114.40],
"Low": [0, 114.36, 114.20],
"Close": [114.39, 114.38, 114.45],
"Adj Close": [114.39, 114.38, 114.45],
"Volume": [9, 15666, 1094]},
index=_pd.to_datetime([_dt.datetime(2025, 3, 17),
_dt.datetime(2025, 3, 14),
_dt.datetime(2025, 3, 13)]))
df_bad = df_bad.sort_index()
df_bad.index.name = "Date"
df_bad.index = df_bad.index.tz_localize(tz_exchange)
correct_df = dat.history(period='1mo', auto_adjust=False)
dt_bad = correct_df.index[len(correct_df)//2]
df_bad = correct_df.copy()
for c in df_bad.columns:
df_bad.loc[dt_bad, c] = _np.nan
repaired_df = hist._fix_zeroes(df_bad, "1d", tz_exchange, prepost=False)
correct_df = df_bad.copy()
correct_df.loc["2025-03-17", "Open"] = 114.62
correct_df.loc["2025-03-17", "High"] = 114.62
correct_df.loc["2025-03-17", "Low"] = 114.41
for c in ["Open", "Low", "High", "Close"]:
self.assertTrue(_np.isclose(repaired_df[c], correct_df[c], rtol=1e-7).all())
try:
self.assertTrue(_np.isclose(repaired_df[c], correct_df[c], rtol=1e-7).all())
except Exception:
print(f"# column = {c}")
print("# correct:") ; print(correct_df[c])
print("# repaired:") ; print(repaired_df[c])
raise
self.assertTrue("Repaired?" in repaired_df.columns)
self.assertFalse(repaired_df["Repaired?"].isna().any())
@@ -421,7 +419,13 @@ class TestPriceRepair(unittest.TestCase):
df_slice_bad_repaired = hist._fix_zeroes(df_slice_bad, "1d", tz_exchange, prepost=False)
for c in ["Close", "Adj Close"]:
self.assertTrue(_np.isclose(df_slice_bad_repaired[c], df_slice[c], rtol=rtol).all())
try:
self.assertTrue(_np.isclose(df_slice_bad_repaired[c], df_slice[c], rtol=rtol).all())
except Exception:
print(f"# column = {c}")
print("# correct:") ; print(df_slice[c])
print("# repaired:") ; print(df_slice_bad_repaired[c])
raise
self.assertTrue("Repaired?" in df_slice_bad_repaired.columns)
self.assertFalse(df_slice_bad_repaired["Repaired?"].isna().any())
@@ -464,7 +468,7 @@ class TestPriceRepair(unittest.TestCase):
# Stocks that split in 2022 but no problems in Yahoo data,
# so repair should change nothing
good_tkrs = ['AMZN', 'DXCM', 'FTNT', 'GOOG', 'GME', 'PANW', 'SHOP', 'TSLA']
good_tkrs += ['AEI', 'GHI', 'IRON', 'LXU', 'RSLS', 'TISI']
good_tkrs += ['AEI', 'GHI', 'IRON', 'LXU', 'TISI']
good_tkrs += ['BOL.ST', 'TUI1.DE']
intervals = ['1d', '1wk', '1mo', '3mo']
for tkr in good_tkrs:
@@ -589,7 +593,6 @@ class TestPriceRepair(unittest.TestCase):
# Div 0.01x
bad_tkrs += ['NVT.L']
bad_tkrs += ['TENT.L']
# Missing div adjusts:
bad_tkrs += ['1398.HK']

View File

@@ -2,6 +2,7 @@ from tests.context import yfinance as yf
from tests.context import session_gbl
import unittest
import socket
import datetime as _dt
import pytz as _tz
@@ -224,7 +225,7 @@ class TestPriceHistory(unittest.TestCase):
start_d = _dt.date(2022, 1, 1)
end_d = _dt.date(2023, 1, 1)
tkr_div_dates = {'BHP.AX': [_dt.date(2022, 9, 1), _dt.date(2022, 2, 24)], # Yahoo claims 23-Feb but wrong because DST
tkr_div_dates = {'BHP.AX': [_dt.date(2022, 9, 1), _dt.date(2022, 2, 24)],
'IMP.JO': [_dt.date(2022, 9, 21), _dt.date(2022, 3, 16)],
'BP.L': [_dt.date(2022, 11, 10), _dt.date(2022, 8, 11), _dt.date(2022, 5, 12),
_dt.date(2022, 2, 17)],
@@ -456,6 +457,21 @@ class TestPriceHistory(unittest.TestCase):
dat.history(start=start, end=end, interval=interval)
def test_transient_error_detection(self):
"""Test that _is_transient_error correctly identifies transient vs permanent errors"""
from yfinance.data import _is_transient_error
from yfinance.exceptions import YFPricesMissingError
# Transient errors (should retry)
self.assertTrue(_is_transient_error(socket.error("Network error")))
self.assertTrue(_is_transient_error(TimeoutError("Timeout")))
self.assertTrue(_is_transient_error(OSError("OS error")))
# Permanent errors (should NOT retry)
self.assertFalse(_is_transient_error(ValueError("Invalid")))
self.assertFalse(_is_transient_error(YFPricesMissingError('INVALID', '')))
self.assertFalse(_is_transient_error(KeyError("key")))
if __name__ == '__main__':
unittest.main()

View File

@@ -15,12 +15,12 @@ import pandas as pd
from tests.context import yfinance as yf
from tests.context import session_gbl
from yfinance.exceptions import YFPricesMissingError, YFInvalidPeriodError, YFNotImplementedError, YFTickerMissingError, YFTzMissingError, YFDataException
from yfinance.config import YfConfig
import unittest
import requests_cache
# import requests_cache
from typing import Union, Any, get_args, _GenericAlias
from urllib.parse import urlparse, parse_qs, urlencode, urlunparse
# from urllib.parse import urlparse, parse_qs, urlencode, urlunparse
ticker_attributes = (
("major_holders", pd.DataFrame),
@@ -132,10 +132,11 @@ class TestTicker(unittest.TestCase):
def test_invalid_period(self):
tkr = 'VALE'
dat = yf.Ticker(tkr, session=self.session)
YfConfig.debug.hide_exceptions = False
with self.assertRaises(YFInvalidPeriodError):
dat.history(period="2wks", interval="1d", raise_errors=True)
dat.history(period="2wks", interval="1d")
with self.assertRaises(YFInvalidPeriodError):
dat.history(period="2mos", interval="1d", raise_errors=True)
dat.history(period="2mos", interval="1d")
def test_valid_custom_periods(self):
valid_periods = [
@@ -151,9 +152,11 @@ class TestTicker(unittest.TestCase):
tkr = "AAPL"
dat = yf.Ticker(tkr, session=self.session)
YfConfig.debug.hide_exceptions = False
for period, interval in valid_periods:
with self.subTest(period=period, interval=interval):
df = dat.history(period=period, interval=interval, raise_errors=True)
df = dat.history(period=period, interval=interval)
self.assertIsInstance(df, pd.DataFrame)
self.assertFalse(df.empty, f"No data returned for period={period}, interval={interval}")
self.assertIn("Close", df.columns, f"'Close' column missing for period={period}, interval={interval}")
@@ -185,21 +188,25 @@ class TestTicker(unittest.TestCase):
self.assertLessEqual(df.index[-1].to_pydatetime().replace(tzinfo=None), now,
f"End date {df.index[-1]} out of range for period={period}")
def test_prices_missing(self):
# this test will need to be updated every time someone wants to run a test
# hard to find a ticker that matches this error other than options
# META call option, 2024 April 26th @ strike of 180000
tkr = 'META240426C00180000'
dat = yf.Ticker(tkr, session=self.session)
with self.assertRaises(YFPricesMissingError):
dat.history(period="5d", interval="1m", raise_errors=True)
# # 2025-12-11: test failing and no time to find new tkr
# def test_prices_missing(self):
# # this test will need to be updated every time someone wants to run a test
# # hard to find a ticker that matches this error other than options
# # META call option, 2024 April 26th @ strike of 180000
# tkr = 'META240426C00180000'
# dat = yf.Ticker(tkr, session=self.session)
# YfConfig.debug.hide_exceptions = False
# with self.assertRaises(YFPricesMissingError):
# dat.history(period="5d", interval="1m")
def test_ticker_missing(self):
tkr = 'ATVI'
dat = yf.Ticker(tkr, session=self.session)
# A missing ticker can trigger either a niche error or the generalized error
with self.assertRaises((YFTickerMissingError, YFTzMissingError, YFPricesMissingError)):
dat.history(period="3mo", interval="1d", raise_errors=True)
YfConfig.debug.hide_exceptions = False
dat.history(period="3mo", interval="1d")
def test_goodTicker(self):
# that yfinance works when full api is called on same instance of ticker
@@ -234,6 +241,23 @@ class TestTicker(unittest.TestCase):
for attribute_name, attribute_type in ticker_attributes:
assert_attribute_type(self, dat, attribute_name, attribute_type)
def test_ticker_with_symbol_mic(self):
equities = [
("OR", "XPAR"), # L'Oréal on Euronext Paris
("AAPL", "XNYS"), # Apple on NYSE
("GOOGL", "XNAS"), # Alphabet on NASDAQ
("BMW", "XETR"), # BMW on XETRA
]
for eq in equities:
# No exception = pass
yf.Ticker(eq)
yf.Ticker((eq[0], eq[1].lower()))
def test_ticker_with_symbol_mic_invalid(self):
with self.assertRaises(ValueError) as cm:
yf.Ticker(('ABC', 'XXXX'))
self.assertIn("Unknown MIC code: 'XXXX'", str(cm.exception))
class TestTickerHistory(unittest.TestCase):
session = None
@@ -284,36 +308,37 @@ class TestTickerHistory(unittest.TestCase):
else:
self.assertIsInstance(data.columns, pd.MultiIndex)
def test_no_expensive_calls_introduced(self):
"""
Make sure calling history to get price data has not introduced more calls to yahoo than absolutely necessary.
As doing other type of scraping calls than "query2.finance.yahoo.com/v8/finance/chart" to yahoo website
will quickly trigger spam-block when doing bulk download of history data.
"""
symbol = "GOOGL"
period = "1y"
with requests_cache.CachedSession(backend="memory") as session:
ticker = yf.Ticker(symbol, session=session)
ticker.history(period=period)
actual_urls_called = [r.url for r in session.cache.filter()]
# Hopefully one day we find an equivalent "requests_cache" that works with "curl_cffi"
# def test_no_expensive_calls_introduced(self):
# """
# Make sure calling history to get price data has not introduced more calls to yahoo than absolutely necessary.
# As doing other type of scraping calls than "query2.finance.yahoo.com/v8/finance/chart" to yahoo website
# will quickly trigger spam-block when doing bulk download of history data.
# """
# symbol = "GOOGL"
# period = "1y"
# with requests_cache.CachedSession(backend="memory") as session:
# ticker = yf.Ticker(symbol, session=session)
# ticker.history(period=period)
# actual_urls_called = [r.url for r in session.cache.filter()]
# Remove 'crumb' argument
for i in range(len(actual_urls_called)):
u = actual_urls_called[i]
parsed_url = urlparse(u)
query_params = parse_qs(parsed_url.query)
query_params.pop('crumb', None)
query_params.pop('cookie', None)
u = urlunparse(parsed_url._replace(query=urlencode(query_params, doseq=True)))
actual_urls_called[i] = u
actual_urls_called = tuple(actual_urls_called)
# # Remove 'crumb' argument
# for i in range(len(actual_urls_called)):
# u = actual_urls_called[i]
# parsed_url = urlparse(u)
# query_params = parse_qs(parsed_url.query)
# query_params.pop('crumb', None)
# query_params.pop('cookie', None)
# u = urlunparse(parsed_url._replace(query=urlencode(query_params, doseq=True)))
# actual_urls_called[i] = u
# actual_urls_called = tuple(actual_urls_called)
expected_urls = [
f"https://query2.finance.yahoo.com/v8/finance/chart/{symbol}?interval=1d&range=1d", # ticker's tz
f"https://query2.finance.yahoo.com/v8/finance/chart/{symbol}?events=div%2Csplits%2CcapitalGains&includePrePost=False&interval=1d&range={period}"
]
for url in actual_urls_called:
self.assertTrue(url in expected_urls, f"Unexpected URL called: {url}")
# expected_urls = [
# f"https://query2.finance.yahoo.com/v8/finance/chart/{symbol}?interval=1d&range=1d", # ticker's tz
# f"https://query2.finance.yahoo.com/v8/finance/chart/{symbol}?events=div%2Csplits%2CcapitalGains&includePrePost=False&interval=1d&range={period}"
# ]
# for url in actual_urls_called:
# self.assertTrue(url in expected_urls, f"Unexpected URL called: {url}")
def test_dividends(self):
data = self.ticker.dividends
@@ -487,7 +512,7 @@ class TestTickerMiscFinancials(unittest.TestCase):
def test_isin(self):
data = self.ticker.isin
self.assertIsInstance(data, str, "data has wrong type")
self.assertEqual("ARDEUT116159", data, "data is empty")
self.assertEqual("CA02080M1005", data, "data is empty")
data_cached = self.ticker.isin
self.assertIs(data, data_cached, "data not cached")
@@ -832,14 +857,14 @@ class TestTickerMiscFinancials(unittest.TestCase):
data_cached = self.ticker.calendar
self.assertIs(data, data_cached, "data not cached")
# # sustainability stopped working
# def test_sustainability(self):
# data = self.ticker.sustainability
# self.assertIsInstance(data, pd.DataFrame, "data has wrong type")
# self.assertFalse(data.empty, "data is empty")
def test_sustainability(self):
data = self.ticker.sustainability
self.assertIsInstance(data, pd.DataFrame, "data has wrong type")
self.assertFalse(data.empty, "data is empty")
data_cached = self.ticker.sustainability
self.assertIs(data, data_cached, "data not cached")
# data_cached = self.ticker.sustainability
# self.assertIs(data, data_cached, "data not cached")
# def test_shares(self):
# data = self.ticker.shares
@@ -889,8 +914,6 @@ class TestTickerAnalysts(unittest.TestCase):
data = self.ticker.upgrades_downgrades
self.assertIsInstance(data, pd.DataFrame, "data has wrong type")
self.assertFalse(data.empty, "data is empty")
self.assertTrue(len(data.columns) == 4, "data has wrong number of columns")
self.assertCountEqual(data.columns.values.tolist(), ['Firm', 'ToGrade', 'FromGrade', 'Action'], "data has wrong column names")
self.assertIsInstance(data.index, pd.DatetimeIndex, "data has wrong index type")
data_cached = self.ticker.upgrades_downgrades
@@ -984,7 +1007,7 @@ class TestTickerInfo(unittest.TestCase):
self.symbols.append("QCSTIX") # good for testing, doesn't trade
self.symbols += ["BTC-USD", "IWO", "VFINX", "^GSPC"]
self.symbols += ["SOKE.IS", "ADS.DE"] # detected bugs
self.symbols += ["EXTO", "NEPT" ] # Issues 2343 and 2363
self.symbols += ["EXTO" ] # Issues 2343
self.tickers = [yf.Ticker(s, session=self.session) for s in self.symbols]
def tearDown(self):
@@ -1000,7 +1023,6 @@ class TestTickerInfo(unittest.TestCase):
self.assertIsInstance(data, dict, "data has wrong type")
expected_keys = ['industry', 'currentPrice', 'exchange', 'floatShares', 'companyOfficers', 'bid']
for k in expected_keys:
print(k)
self.assertIn("symbol", data.keys(), f"Did not find expected key '{k}' in info dict")
self.assertEqual(self.symbols[0], data["symbol"], "Wrong symbol value in info dict")
@@ -1037,15 +1059,6 @@ class TestTickerInfo(unittest.TestCase):
self.assertCountEqual(['quoteType', 'symbol', 'underlyingSymbol', 'uuid', 'maxAge', 'trailingPegRatio'], data.keys())
self.assertIn("trailingPegRatio", data.keys(), "Did not find expected key 'trailingPegRatio' in info dict")
# Test issue 2363 (Empty QuoteResponse)
data = self.tickers[11].info
expected_keys = ['maxAge', 'priceHint', 'previousClose', 'open', 'dayLow', 'dayHigh', 'regularMarketPreviousClose',
'regularMarketOpen', 'regularMarketDayLow', 'regularMarketDayHigh', 'volume', 'regularMarketVolume',
'bid', 'ask', 'bidSize', 'askSize', 'fiftyTwoWeekLow', 'fiftyTwoWeekHigh', 'currency', 'tradeable',
'exchange', 'quoteType', 'symbol', 'underlyingSymbol', 'shortName', 'timeZoneFullName', 'timeZoneShortName',
'uuid', 'gmtOffSetMilliseconds', 'trailingPegRatio']
self.assertCountEqual(expected_keys, data.keys())
# def test_fast_info_matches_info(self):
# fast_info_keys = set()
# for ticker in self.tickers:

View File

@@ -15,7 +15,7 @@ import pandas as pd
import unittest
from yfinance.utils import is_valid_period_format, _dts_in_same_interval
from yfinance.utils import is_valid_period_format, _dts_in_same_interval, _parse_user_dt
class TestPandas(unittest.TestCase):
@@ -169,6 +169,19 @@ class TestDateIntervalCheck(unittest.TestCase):
dt3 = pd.Timestamp("2024-10-15 10:31:00")
self.assertFalse(_dts_in_same_interval(dt1, dt3, "1min"))
def test_parse_user_dt(self):
exchange_tz = "US/Eastern"
dtstr = "2024-01-04"
epoch = 1704344400 # output of `pd.Timestamp("2024-01-04", tz="US/Eastern").timestamp()`
expected = pd.Timestamp(dtstr, tz=exchange_tz)
self.assertEqual(_parse_user_dt(epoch, exchange_tz), expected)
self.assertEqual(_parse_user_dt(dtstr, exchange_tz), expected)
self.assertEqual(_parse_user_dt(datetime(year=2024, month=1, day=4).date(), exchange_tz), expected)
self.assertEqual(_parse_user_dt(datetime(year=2024, month=1, day=4), exchange_tz), expected)
with self.assertRaises(ValueError):
self.assertEqual(_parse_user_dt(float(epoch), exchange_tz), expected)
if __name__ == "__main__":
unittest.main()

View File

@@ -23,6 +23,7 @@ from . import version
from .search import Search
from .lookup import Lookup
from .ticker import Ticker
from .calendars import Calendars
from .tickers import Tickers
from .multi import download
from .live import WebSocket, AsyncWebSocket
@@ -31,7 +32,7 @@ from .cache import set_tz_cache_location
from .domain.sector import Sector
from .domain.industry import Industry
from .domain.market import Market
from .data import YfData
from .config import YfConfig as config
from .screener.query import EquityQuery, FundQuery
from .screener.screener import screen, PREDEFINED_SCREENER_QUERIES
@@ -42,13 +43,17 @@ __author__ = "Ran Aroussi"
import warnings
warnings.filterwarnings('default', category=DeprecationWarning, module='^yfinance')
__all__ = ['download', 'Market', 'Search', 'Lookup', 'Ticker', 'Tickers', 'enable_debug_mode', 'set_tz_cache_location', 'Sector', 'Industry', 'WebSocket', 'AsyncWebSocket']
__all__ = ['download', 'Market', 'Search', 'Lookup', 'Ticker', 'Tickers', 'enable_debug_mode', 'set_tz_cache_location', 'Sector', 'Industry', 'WebSocket', 'AsyncWebSocket', 'Calendars']
# screener stuff:
__all__ += ['EquityQuery', 'FundQuery', 'screen', 'PREDEFINED_SCREENER_QUERIES']
# Config stuff:
_NOTSET=object()
def set_config(proxy=_NOTSET):
def set_config(proxy=_NOTSET, retries=_NOTSET):
if proxy is not _NOTSET:
YfData(proxy=proxy)
warnings.warn("Set proxy via new config control: yf.config.network.proxy = proxy", DeprecationWarning)
config.network.proxy = proxy
if retries is not _NOTSET:
warnings.warn("Set retries via new config control: yf.config.network.retries = retries", DeprecationWarning)
config.network.retries = retries
__all__ += ["set_config"]

View File

@@ -22,7 +22,6 @@
from __future__ import print_function
import json as _json
import warnings
from typing import Optional, Union
from urllib.parse import quote as urlencode
@@ -32,8 +31,10 @@ from curl_cffi import requests
from . import utils, cache
from .const import _MIC_TO_YAHOO_SUFFIX
from .data import YfData
from .exceptions import YFEarningsDateMissing, YFRateLimitError
from .config import YfConfig
from .exceptions import YFDataException, YFEarningsDateMissing, YFRateLimitError
from .live import WebSocket
from .scrapers.analysis import Analysis
from .scrapers.fundamentals import Fundamentals
@@ -42,13 +43,43 @@ from .scrapers.quote import Quote, FastInfo
from .scrapers.history import PriceHistory
from .scrapers.funds import FundsData
from .const import _BASE_URL_, _ROOT_URL_, _QUERY1_URL_, _SENTINEL_
from .const import _BASE_URL_, _ROOT_URL_, _QUERY1_URL_
from io import StringIO
from bs4 import BeautifulSoup
_tz_info_fetch_ctr = 0
class TickerBase:
def __init__(self, ticker, session=None, proxy=_SENTINEL_):
def __init__(self, ticker, session=None):
"""
Initialize a Yahoo Finance Ticker object.
Args:
ticker (str | tuple[str, str]):
Yahoo Finance symbol (e.g. "AAPL")
or a tuple of (symbol, MIC) e.g. ('OR','XPAR')
(MIC = market identifier code)
session (requests.Session, optional):
Custom requests session.
"""
if isinstance(ticker, tuple):
if len(ticker) != 2:
raise ValueError("Ticker tuple must be (symbol, mic_code)")
base_symbol, mic_code = ticker
# ticker = yahoo_ticker(base_symbol, mic_code)
if mic_code.startswith('.'):
mic_code = mic_code[1:]
if mic_code.upper() not in _MIC_TO_YAHOO_SUFFIX:
raise ValueError(f"Unknown MIC code: '{mic_code}'")
sfx = _MIC_TO_YAHOO_SUFFIX[mic_code.upper()]
if sfx != '':
ticker = f'{base_symbol}.{sfx}'
else:
ticker = base_symbol
self.ticker = ticker.upper()
self.session = session or requests.Session(impersonate="chrome")
self._tz = None
@@ -66,17 +97,19 @@ class TickerBase:
if self.ticker == "":
raise ValueError("Empty ticker name")
self._data: YfData = YfData(session=session)
# accept isin as ticker
if utils.is_isin(self.ticker):
isin = self.ticker
self.ticker = utils.get_ticker_by_isin(self.ticker, None, session)
c = cache.get_isin_cache()
self.ticker = c.lookup(isin)
if not self.ticker:
self.ticker = utils.get_ticker_by_isin(isin)
if self.ticker == "":
raise ValueError(f"Invalid ISIN number: {isin}")
self._data: YfData = YfData(session=session)
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
if self.ticker:
c.store(isin, self.ticker)
# self._price_history = PriceHistory(self._data, self.ticker)
self._price_history = None # lazy-load
@@ -152,6 +185,8 @@ class TickerBase:
# Must propagate this
raise
except Exception as e:
if not YfConfig.debug.hide_exceptions:
raise
logger.error(f"Failed to get ticker '{self.ticker}' reason: {e}")
return None
else:
@@ -163,6 +198,8 @@ class TickerBase:
try:
return data["chart"]["result"][0]["meta"]["exchangeTimezoneName"]
except Exception as err:
if not YfConfig.debug.hide_exceptions:
raise
logger.error(f"Could not get exchangeTimezoneName for ticker '{self.ticker}' reason: {err}")
logger.debug("Got response: ")
logger.debug("-------------")
@@ -170,169 +207,100 @@ class TickerBase:
logger.debug("-------------")
return None
def get_recommendations(self, proxy=_SENTINEL_, as_dict=False):
def get_recommendations(self, as_dict=False):
"""
Returns a DataFrame with the recommendations
Columns: period strongBuy buy hold sell strongSell
"""
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
data = self._quote.recommendations
if as_dict:
return data.to_dict()
return data
def get_recommendations_summary(self, proxy=_SENTINEL_, as_dict=False):
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
def get_recommendations_summary(self, as_dict=False):
return self.get_recommendations(as_dict=as_dict)
def get_upgrades_downgrades(self, proxy=_SENTINEL_, as_dict=False):
def get_upgrades_downgrades(self, as_dict=False):
"""
Returns a DataFrame with the recommendations changes (upgrades/downgrades)
Index: date of grade
Columns: firm toGrade fromGrade action
"""
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
data = self._quote.upgrades_downgrades
if as_dict:
return data.to_dict()
return data
def get_calendar(self, proxy=_SENTINEL_) -> dict:
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
def get_calendar(self) -> dict:
return self._quote.calendar
def get_sec_filings(self, proxy=_SENTINEL_) -> dict:
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
def get_sec_filings(self) -> dict:
return self._quote.sec_filings
def get_major_holders(self, proxy=_SENTINEL_, as_dict=False):
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
def get_major_holders(self, as_dict=False):
data = self._holders.major
if as_dict:
return data.to_dict()
return data
def get_institutional_holders(self, proxy=_SENTINEL_, as_dict=False):
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
def get_institutional_holders(self, as_dict=False):
data = self._holders.institutional
if data is not None:
if as_dict:
return data.to_dict()
return data
def get_mutualfund_holders(self, proxy=_SENTINEL_, as_dict=False):
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
def get_mutualfund_holders(self, as_dict=False):
data = self._holders.mutualfund
if data is not None:
if as_dict:
return data.to_dict()
return data
def get_insider_purchases(self, proxy=_SENTINEL_, as_dict=False):
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
def get_insider_purchases(self, as_dict=False):
data = self._holders.insider_purchases
if data is not None:
if as_dict:
return data.to_dict()
return data
def get_insider_transactions(self, proxy=_SENTINEL_, as_dict=False):
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
def get_insider_transactions(self, as_dict=False):
data = self._holders.insider_transactions
if data is not None:
if as_dict:
return data.to_dict()
return data
def get_insider_roster_holders(self, proxy=_SENTINEL_, as_dict=False):
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
def get_insider_roster_holders(self, as_dict=False):
data = self._holders.insider_roster
if data is not None:
if as_dict:
return data.to_dict()
return data
def get_info(self, proxy=_SENTINEL_) -> dict:
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
def get_info(self) -> dict:
data = self._quote.info
return data
def get_fast_info(self, proxy=_SENTINEL_):
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
def get_fast_info(self):
if self._fast_info is None:
self._fast_info = FastInfo(self)
return self._fast_info
@property
def basic_info(self):
warnings.warn("'Ticker.basic_info' is deprecated and will be removed in future, Switch to 'Ticker.fast_info'", DeprecationWarning)
return self.fast_info
def get_sustainability(self, proxy=_SENTINEL_, as_dict=False):
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
def get_sustainability(self, as_dict=False):
data = self._quote.sustainability
if as_dict:
return data.to_dict()
return data
def get_analyst_price_targets(self, proxy=_SENTINEL_) -> dict:
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
def get_analyst_price_targets(self) -> dict:
"""
Keys: current low high mean median
"""
data = self._analysis.analyst_price_targets
return data
def get_earnings_estimate(self, proxy=_SENTINEL_, as_dict=False):
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
def get_earnings_estimate(self, as_dict=False):
"""
Index: 0q +1q 0y +1y
Columns: numberOfAnalysts avg low high yearAgoEps growth
@@ -340,11 +308,7 @@ class TickerBase:
data = self._analysis.earnings_estimate
return data.to_dict() if as_dict else data
def get_revenue_estimate(self, proxy=_SENTINEL_, as_dict=False):
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
def get_revenue_estimate(self, as_dict=False):
"""
Index: 0q +1q 0y +1y
Columns: numberOfAnalysts avg low high yearAgoRevenue growth
@@ -352,11 +316,7 @@ class TickerBase:
data = self._analysis.revenue_estimate
return data.to_dict() if as_dict else data
def get_earnings_history(self, proxy=_SENTINEL_, as_dict=False):
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
def get_earnings_history(self, as_dict=False):
"""
Index: pd.DatetimeIndex
Columns: epsEstimate epsActual epsDifference surprisePercent
@@ -364,43 +324,34 @@ class TickerBase:
data = self._analysis.earnings_history
return data.to_dict() if as_dict else data
def get_eps_trend(self, proxy=_SENTINEL_, as_dict=False):
def get_eps_trend(self, as_dict=False):
"""
Index: 0q +1q 0y +1y
Columns: current 7daysAgo 30daysAgo 60daysAgo 90daysAgo
"""
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
data = self._analysis.eps_trend
return data.to_dict() if as_dict else data
def get_eps_revisions(self, proxy=_SENTINEL_, as_dict=False):
def get_eps_revisions(self, as_dict=False):
"""
Index: 0q +1q 0y +1y
Columns: upLast7days upLast30days downLast7days downLast30days
"""
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
data = self._analysis.eps_revisions
return data.to_dict() if as_dict else data
def get_growth_estimates(self, proxy=_SENTINEL_, as_dict=False):
def get_growth_estimates(self, as_dict=False):
"""
Index: 0q +1q 0y +1y +5y -5y
Columns: stock industry sector index
"""
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
data = self._analysis.growth_estimates
return data.to_dict() if as_dict else data
def get_earnings(self, proxy=_SENTINEL_, as_dict=False, freq="yearly"):
def get_earnings(self, as_dict=False, freq="yearly"):
"""
:Parameters:
as_dict: bool
@@ -410,9 +361,6 @@ class TickerBase:
"yearly" or "quarterly" or "trailing"
Default is "yearly"
"""
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
if self._fundamentals.earnings is None:
return None
@@ -424,7 +372,7 @@ class TickerBase:
return dict_data
return data
def get_income_stmt(self, proxy=_SENTINEL_, as_dict=False, pretty=False, freq="yearly"):
def get_income_stmt(self, as_dict=False, pretty=False, freq="yearly"):
"""
:Parameters:
as_dict: bool
@@ -437,9 +385,6 @@ class TickerBase:
"yearly" or "quarterly" or "trailing"
Default is "yearly"
"""
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
data = self._fundamentals.financials.get_income_time_series(freq=freq)
@@ -450,21 +395,13 @@ class TickerBase:
return data.to_dict()
return data
def get_incomestmt(self, proxy=_SENTINEL_, as_dict=False, pretty=False, freq="yearly"):
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
def get_incomestmt(self, as_dict=False, pretty=False, freq="yearly"):
return self.get_income_stmt(as_dict, pretty, freq)
return self.get_income_stmt(proxy, as_dict, pretty, freq)
def get_financials(self, as_dict=False, pretty=False, freq="yearly"):
return self.get_income_stmt(as_dict, pretty, freq)
def get_financials(self, proxy=_SENTINEL_, as_dict=False, pretty=False, freq="yearly"):
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
return self.get_income_stmt(proxy, as_dict, pretty, freq)
def get_balance_sheet(self, proxy=_SENTINEL_, as_dict=False, pretty=False, freq="yearly"):
def get_balance_sheet(self, as_dict=False, pretty=False, freq="yearly"):
"""
:Parameters:
as_dict: bool
@@ -477,9 +414,6 @@ class TickerBase:
"yearly" or "quarterly"
Default is "yearly"
"""
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
data = self._fundamentals.financials.get_balance_sheet_time_series(freq=freq)
@@ -491,14 +425,10 @@ class TickerBase:
return data.to_dict()
return data
def get_balancesheet(self, proxy=_SENTINEL_, as_dict=False, pretty=False, freq="yearly"):
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
def get_balancesheet(self, as_dict=False, pretty=False, freq="yearly"):
return self.get_balance_sheet(as_dict, pretty, freq)
return self.get_balance_sheet(proxy, as_dict, pretty, freq)
def get_cash_flow(self, proxy=_SENTINEL_, as_dict=False, pretty=False, freq="yearly") -> Union[pd.DataFrame, dict]:
def get_cash_flow(self, as_dict=False, pretty=False, freq="yearly") -> Union[pd.DataFrame, dict]:
"""
:Parameters:
as_dict: bool
@@ -511,9 +441,6 @@ class TickerBase:
"yearly" or "quarterly"
Default is "yearly"
"""
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
data = self._fundamentals.financials.get_cash_flow_time_series(freq=freq)
@@ -525,63 +452,39 @@ class TickerBase:
return data.to_dict()
return data
def get_cashflow(self, proxy=_SENTINEL_, as_dict=False, pretty=False, freq="yearly"):
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
return self.get_cash_flow(proxy, as_dict, pretty, freq)
def get_cashflow(self, as_dict=False, pretty=False, freq="yearly"):
return self.get_cash_flow(as_dict, pretty, freq)
def get_dividends(self, proxy=_SENTINEL_, period="max") -> pd.Series:
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
def get_dividends(self, period="max") -> pd.Series:
return self._lazy_load_price_history().get_dividends(period=period)
def get_capital_gains(self, proxy=_SENTINEL_, period="max") -> pd.Series:
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
def get_capital_gains(self, period="max") -> pd.Series:
return self._lazy_load_price_history().get_capital_gains(period=period)
def get_splits(self, proxy=_SENTINEL_, period="max") -> pd.Series:
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
def get_splits(self, period="max") -> pd.Series:
return self._lazy_load_price_history().get_splits(period=period)
def get_actions(self, proxy=_SENTINEL_, period="max") -> pd.Series:
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
def get_actions(self, period="max") -> pd.Series:
return self._lazy_load_price_history().get_actions(period=period)
def get_shares(self, proxy=_SENTINEL_, as_dict=False) -> Union[pd.DataFrame, dict]:
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
def get_shares(self, as_dict=False) -> Union[pd.DataFrame, dict]:
data = self._fundamentals.shares
if as_dict:
return data.to_dict()
return data
@utils.log_indent_decorator
def get_shares_full(self, start=None, end=None, proxy=_SENTINEL_):
def get_shares_full(self, start=None, end=None):
logger = utils.get_yf_logger()
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
# Process dates
tz = self._get_ticker_tz(timeout=10)
dt_now = pd.Timestamp.utcnow().tz_convert(tz)
if start is not None:
start_ts = utils._parse_user_dt(start, tz)
start = pd.Timestamp.fromtimestamp(start_ts).tz_localize("UTC").tz_convert(tz)
start = utils._parse_user_dt(start, tz)
if end is not None:
end_ts = utils._parse_user_dt(end, tz)
end = pd.Timestamp.fromtimestamp(end_ts).tz_localize("UTC").tz_convert(tz)
end = utils._parse_user_dt(end, tz)
if end is None:
end = dt_now
if start is None:
@@ -599,6 +502,8 @@ class TickerBase:
json_data = self._data.cache_get(url=shares_url)
json_data = json_data.json()
except (_json.JSONDecodeError, requests.exceptions.RequestException):
if not YfConfig.debug.hide_exceptions:
raise
logger.error(f"{self.ticker}: Yahoo web request for share count failed")
return None
try:
@@ -606,6 +511,8 @@ class TickerBase:
except KeyError:
fail = False
if fail:
if not YfConfig.debug.hide_exceptions:
raise requests.exceptions.HTTPError("Yahoo web request for share count returned 'Bad Request'")
logger.error(f"{self.ticker}: Yahoo web request for share count failed")
return None
@@ -615,6 +522,8 @@ class TickerBase:
try:
df = pd.Series(shares_data[0]["shares_out"], index=pd.to_datetime(shares_data[0]["timestamp"], unit="s"))
except Exception as e:
if not YfConfig.debug.hide_exceptions:
raise
logger.error(f"{self.ticker}: Failed to parse shares count data: {e}")
return None
@@ -622,11 +531,7 @@ class TickerBase:
df = df.sort_index()
return df
def get_isin(self, proxy=_SENTINEL_) -> Optional[str]:
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
def get_isin(self) -> Optional[str]:
# *** experimental ***
if self._isin is not None:
return self._isin
@@ -662,16 +567,13 @@ class TickerBase:
self._isin = data.split(search_str)[1].split('"')[0].split('|')[0]
return self._isin
def get_news(self, count=10, tab="news", proxy=_SENTINEL_) -> list:
def get_news(self, count=10, tab="news") -> list:
"""Allowed options for tab: "news", "all", "press releases"""
if self._news:
return self._news
logger = utils.get_yf_logger()
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
tab_queryrefs = {
"all": "newsAll",
@@ -693,12 +595,12 @@ class TickerBase:
data = self._data.post(url, body=payload)
if data is None or "Will be right back" in data.text:
raise RuntimeError("*** YAHOO! FINANCE IS CURRENTLY DOWN! ***\n"
"Our engineers are working quickly to resolve "
"the issue. Thank you for your patience.")
raise YFDataException("*** YAHOO! FINANCE IS CURRENTLY DOWN! ***")
try:
data = data.json()
except _json.JSONDecodeError:
if not YfConfig.debug.hide_exceptions:
raise
logger.error(f"{self.ticker}: Failed to retrieve the news and received faulty response instead.")
data = {}
@@ -707,10 +609,119 @@ class TickerBase:
self._news = [article for article in news if not article.get('ad', [])]
return self._news
def get_earnings_dates(self, limit = 12, offset = 0) -> Optional[pd.DataFrame]:
if limit > 100:
raise ValueError("Yahoo caps limit at 100")
if self._earnings_dates and limit in self._earnings_dates:
return self._earnings_dates[limit]
df = self._get_earnings_dates_using_scrape(limit, offset)
self._earnings_dates[limit] = df
return df
@utils.log_indent_decorator
def get_earnings_dates(self, limit=12, proxy=_SENTINEL_) -> Optional[pd.DataFrame]:
def _get_earnings_dates_using_scrape(self, limit = 12, offset = 0) -> Optional[pd.DataFrame]:
"""
Uses YfData.cache_get() to scrape earnings data from YahooFinance.
(https://finance.yahoo.com/calendar/earnings?symbol=INTC)
Args:
limit (int): Number of rows to extract (max=100)
offset (int): if 0, search from future EPS estimates.
if 1, search from the most recent EPS.
if x, search from x'th recent EPS.
Returns:
pd.DataFrame in the following format.
EPS Estimate Reported EPS Surprise(%)
Date
2025-10-30 2.97 - -
2025-07-22 1.73 1.54 -10.88
2025-05-06 2.63 2.7 2.57
2025-02-06 2.09 2.42 16.06
2024-10-31 1.92 1.55 -19.36
... ... ... ...
2014-07-31 0.61 0.65 7.38
2014-05-01 0.55 0.68 22.92
2014-02-13 0.55 0.58 6.36
2013-10-31 0.51 0.54 6.86
2013-08-01 0.46 0.5 7.86
"""
#####################################################
# Define Constants
#####################################################
if limit > 0 and limit <= 25:
size = 25
elif limit > 25 and limit <= 50:
size = 50
elif limit > 50 and limit <= 100:
size = 100
else:
raise ValueError("Please use limit <= 100")
# Define the URL
url = "https://finance.yahoo.com/calendar/earnings?symbol={}&offset={}&size={}".format(
self.ticker, offset, size
)
#####################################################
# Get data
#####################################################
response = self._data.cache_get(url)
#####################################################
# Response -> pd.DataFrame
#####################################################
# Parse the HTML content using BeautifulSoup
soup = BeautifulSoup(response.text, "html.parser")
# This page should have only one <table>
table = soup.find("table")
# If the table is found
if table:
# Get the HTML string of the table
table_html = str(table)
# Wrap the HTML string in a StringIO object
html_stringio = StringIO(table_html)
# Pass the StringIO object to pd.read_html()
df = pd.read_html(html_stringio, na_values=['-'])[0]
# Drop redundant columns
df = df.drop(["Symbol", "Company"], axis=1)
# Backwards compatibility
df.rename(columns={'Surprise (%)': 'Surprise(%)'}, inplace=True)
df = df.dropna(subset="Earnings Date")
# Parse earnings date
# - Pandas doesn't like EDT, EST
df['Earnings Date'] = df['Earnings Date'].str.replace('EDT', 'America/New_York')
df['Earnings Date'] = df['Earnings Date'].str.replace('EST', 'America/New_York')
# - separate timezone string (last word)
dt_parts = df['Earnings Date'].str.rsplit(' ', n=1, expand=True)
dts = dt_parts[0]
tzs = dt_parts[1]
df['Earnings Date'] = pd.to_datetime(dts, format='%B %d, %Y at %I %p')
df['Earnings Date'] = pd.Series([dt.tz_localize(tz) for dt, tz in zip(df['Earnings Date'], tzs)])
df = df.set_index("Earnings Date")
else:
err_msg = "No earnings dates found, symbol may be delisted"
logger = utils.get_yf_logger()
logger.error(f'{self.ticker}: {err_msg}')
return None
return df
@utils.log_indent_decorator
def _get_earnings_dates_using_screener(self, limit=12) -> Optional[pd.DataFrame]:
"""
Get earning dates (future and historic)
In Summer 2025, Yahoo stopped updating the data at this endpoint.
So reverting to scraping HTML.
Args:
limit (int): max amount of upcoming and recent earnings dates to return.
@@ -721,31 +732,16 @@ class TickerBase:
"""
logger = utils.get_yf_logger()
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
clamped_limit = min(limit, 100) # YF caps at 100, don't go higher
if self._earnings_dates and clamped_limit in self._earnings_dates:
return self._earnings_dates[clamped_limit]
# Fetch data
url = f"{_QUERY1_URL_}/v1/finance/visualization"
params = {"lang": "en-US", "region": "US"}
body = {
"size": clamped_limit,
"query": {
"operator": "and",
"operands": [
{"operator": "eq", "operands": ["ticker", self.ticker]},
{"operator": "eq", "operands": ["eventtype", "2"]}
]
},
"size": limit,
"query": { "operator": "eq", "operands": ["ticker", self.ticker] },
"sortField": "startdatetime",
"sortType": "DESC",
"entityIdType": "earnings",
"includeFields": ["startdatetime", "timeZoneShortName", "epsestimate", "epsactual", "epssurprisepct"]
"includeFields": ["startdatetime", "timeZoneShortName", "epsestimate", "epsactual", "epssurprisepct", "eventtype"]
}
response = self._data.post(url, params=params, body=body)
json_data = response.json()
@@ -761,6 +757,14 @@ class TickerBase:
logger.error(f'{self.ticker}: {err_msg}')
return None
# Convert eventtype
# - 1 = earnings call (manually confirmed)
# - 2 = earnings report
# - 11 = stockholders meeting (manually confirmed)
df['Event Type'] = df['Event Type'].replace('^1$', 'Call', regex=True)
df['Event Type'] = df['Event Type'].replace('^2$', 'Earnings', regex=True)
df['Event Type'] = df['Event Type'].replace('^11$', 'Meeting', regex=True)
# Calculate earnings date
df['Earnings Date'] = pd.to_datetime(df['Event Start Date'])
tz = self._get_ticker_tz(timeout=30)
@@ -778,21 +782,13 @@ class TickerBase:
df.set_index('Earnings Date', inplace=True)
df.rename(columns={'Surprise (%)': 'Surprise(%)'}, inplace=True) # Compatibility
self._earnings_dates[clamped_limit] = df
self._earnings_dates[limit] = df
return df
def get_history_metadata(self, proxy=_SENTINEL_) -> dict:
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
return self._lazy_load_price_history().get_history_metadata(proxy)
def get_funds_data(self, proxy=_SENTINEL_) -> Optional[FundsData]:
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
def get_history_metadata(self) -> dict:
return self._lazy_load_price_history().get_history_metadata()
def get_funds_data(self) -> Optional[FundsData]:
if not self._funds_data:
self._funds_data = FundsData(self._data, self.ticker)

View File

@@ -3,13 +3,15 @@ from threading import Lock
import os as _os
import platformdirs as _ad
import atexit as _atexit
import datetime as _datetime
import datetime as _dt
import pickle as _pkl
from .utils import get_yf_logger
_cache_init_lock = Lock()
# --------------
# TimeZone cache
# --------------
@@ -105,7 +107,7 @@ _atexit.register(_TzDBManager.close_db)
tz_db_proxy = _peewee.Proxy()
class _KV(_peewee.Model):
class _TZ_KV(_peewee.Model):
key = _peewee.CharField(primary_key=True)
value = _peewee.CharField(null=True)
@@ -146,11 +148,11 @@ class _TzCache:
db.connect()
tz_db_proxy.initialize(db)
try:
db.create_tables([_KV])
db.create_tables([_TZ_KV])
except _peewee.OperationalError as e:
if 'WITHOUT' in str(e):
_KV._meta.without_rowid = False
db.create_tables([_KV])
_TZ_KV._meta.without_rowid = False
db.create_tables([_TZ_KV])
else:
raise
self.initialised = 1 # success
@@ -166,8 +168,8 @@ class _TzCache:
return None
try:
return _KV.get(_KV.key == key).value
except _KV.DoesNotExist:
return _TZ_KV.get(_TZ_KV.key == key).value
except _TZ_KV.DoesNotExist:
return None
def store(self, key, value):
@@ -185,18 +187,18 @@ class _TzCache:
return
try:
if value is None:
q = _KV.delete().where(_KV.key == key)
q = _TZ_KV.delete().where(_TZ_KV.key == key)
q.execute()
return
with db.atomic():
_KV.insert(key=key, value=value).execute()
_TZ_KV.insert(key=key, value=value).execute()
except _peewee.IntegrityError:
# Integrity error means the key already exists. Try updating the key.
old_value = self.lookup(key)
if old_value != value:
get_yf_logger().debug(f"Value for key {key} changed from {old_value} to {value}.")
with db.atomic():
q = _KV.update(value=value).where(_KV.key == key)
q = _TZ_KV.update(value=value).where(_TZ_KV.key == key)
q.execute()
@@ -301,16 +303,16 @@ class ISODateTimeField(_peewee.DateTimeField):
# because user discovered peewee allowed an invalid datetime
# to get written.
def db_value(self, value):
if value and isinstance(value, _datetime.datetime):
if value and isinstance(value, _dt.datetime):
return value.isoformat()
return super().db_value(value)
def python_value(self, value):
if value and isinstance(value, str) and 'T' in value:
return _datetime.datetime.fromisoformat(value)
return _dt.datetime.fromisoformat(value)
return super().python_value(value)
class _CookieSchema(_peewee.Model):
strategy = _peewee.CharField(primary_key=True)
fetch_date = ISODateTimeField(default=_datetime.datetime.now)
fetch_date = ISODateTimeField(default=_dt.datetime.now)
# Which cookie type depends on strategy
cookie_bytes = _peewee.BlobField()
@@ -374,7 +376,7 @@ class _CookieCache:
try:
data = _CookieSchema.get(_CookieSchema.strategy == strategy)
cookie = _pkl.loads(data.cookie_bytes)
return {'cookie':cookie, 'age':_datetime.datetime.now()-data.fetch_date}
return {'cookie':cookie, 'age':_dt.datetime.now()-data.fetch_date}
except _CookieSchema.DoesNotExist:
return None
@@ -415,6 +417,211 @@ def get_cookie_cache():
# --------------
# ISIN cache
# --------------
class _ISINCacheException(Exception):
pass
class _ISINCacheDummy:
"""Dummy cache to use if isin cache is disabled"""
def lookup(self, isin):
return None
def store(self, isin, tkr):
pass
@property
def tz_db(self):
return None
class _ISINCacheManager:
_isin_cache = None
@classmethod
def get_isin_cache(cls):
if cls._isin_cache is None:
with _cache_init_lock:
cls._initialise()
return cls._isin_cache
@classmethod
def _initialise(cls, cache_dir=None):
cls._isin_cache = _ISINCache()
class _ISINDBManager:
_db = None
_cache_dir = _os.path.join(_ad.user_cache_dir(), "py-yfinance")
@classmethod
def get_database(cls):
if cls._db is None:
cls._initialise()
return cls._db
@classmethod
def close_db(cls):
if cls._db is not None:
try:
cls._db.close()
except Exception:
# Must discard exceptions because Python trying to quit.
pass
@classmethod
def _initialise(cls, cache_dir=None):
if cache_dir is not None:
cls._cache_dir = cache_dir
if not _os.path.isdir(cls._cache_dir):
try:
_os.makedirs(cls._cache_dir)
except OSError as err:
raise _ISINCacheException(f"Error creating ISINCache folder: '{cls._cache_dir}' reason: {err}")
elif not (_os.access(cls._cache_dir, _os.R_OK) and _os.access(cls._cache_dir, _os.W_OK)):
raise _ISINCacheException(f"Cannot read and write in ISINCache folder: '{cls._cache_dir}'")
cls._db = _peewee.SqliteDatabase(
_os.path.join(cls._cache_dir, 'isin-tkr.db'),
pragmas={'journal_mode': 'wal', 'cache_size': -64}
)
@classmethod
def set_location(cls, new_cache_dir):
if cls._db is not None:
cls._db.close()
cls._db = None
cls._cache_dir = new_cache_dir
@classmethod
def get_location(cls):
return cls._cache_dir
# close DB when Python exists
_atexit.register(_ISINDBManager.close_db)
isin_db_proxy = _peewee.Proxy()
class _ISIN_KV(_peewee.Model):
key = _peewee.CharField(primary_key=True)
value = _peewee.CharField(null=True)
created_at = _peewee.DateTimeField(default=_dt.datetime.now)
class Meta:
database = isin_db_proxy
without_rowid = True
class _ISINCache:
def __init__(self):
self.initialised = -1
self.db = None
self.dummy = False
def get_db(self):
if self.db is not None:
return self.db
try:
self.db = _ISINDBManager.get_database()
except _ISINCacheException as err:
get_yf_logger().info(f"Failed to create ISINCache, reason: {err}. "
"ISINCache will not be used. "
"Tip: You can direct cache to use a different location with 'set_isin_cache_location(mylocation)'")
self.dummy = True
return None
return self.db
def initialise(self):
if self.initialised != -1:
return
db = self.get_db()
if db is None:
self.initialised = 0 # failure
return
db.connect()
isin_db_proxy.initialize(db)
try:
db.create_tables([_ISIN_KV])
except _peewee.OperationalError as e:
if 'WITHOUT' in str(e):
_ISIN_KV._meta.without_rowid = False
db.create_tables([_ISIN_KV])
else:
raise
self.initialised = 1 # success
def lookup(self, key):
if self.dummy:
return None
if self.initialised == -1:
self.initialise()
if self.initialised == 0: # failure
return None
try:
return _ISIN_KV.get(_ISIN_KV.key == key).value
except _ISIN_KV.DoesNotExist:
return None
def store(self, key, value):
if self.dummy:
return
if self.initialised == -1:
self.initialise()
if self.initialised == 0: # failure
return
db = self.get_db()
if db is None:
return
try:
if value is None:
q = _ISIN_KV.delete().where(_ISIN_KV.key == key)
q.execute()
return
# Remove existing rows with same value that are older than 1 week
one_week_ago = _dt.datetime.now() - _dt.timedelta(weeks=1)
old_rows_query = _ISIN_KV.delete().where(
(_ISIN_KV.value == value) &
(_ISIN_KV.created_at < one_week_ago)
)
old_rows_query.execute()
with db.atomic():
_ISIN_KV.insert(key=key, value=value).execute()
except _peewee.IntegrityError:
# Integrity error means the key already exists. Try updating the key.
old_value = self.lookup(key)
if old_value != value:
get_yf_logger().debug(f"Value for key {key} changed from {old_value} to {value}.")
with db.atomic():
q = _ISIN_KV.update(value=value, created_at=_dt.datetime.now()).where(_ISIN_KV.key == key)
q.execute()
def get_isin_cache():
return _ISINCacheManager.get_isin_cache()
# --------------
# Utils
# --------------
def set_cache_location(cache_dir: str):
"""
Sets the path to create the "py-yfinance" cache folder in.
@@ -425,6 +632,7 @@ def set_cache_location(cache_dir: str):
"""
_TzDBManager.set_location(cache_dir)
_CookieDBManager.set_location(cache_dir)
_ISINDBManager.set_location(cache_dir)
def set_tz_cache_location(cache_dir: str):
set_cache_location(cache_dir)

546
yfinance/calendars.py Normal file
View File

@@ -0,0 +1,546 @@
import json
from typing import Any, Optional
import warnings
import numpy as np
from requests import Session, Response, exceptions
import pandas as pd
from datetime import datetime, date, timedelta
from .const import _QUERY1_URL_
from .utils import log_indent_decorator, get_yf_logger, _parse_user_dt
from .screener import screen
from .data import YfData
from .exceptions import YFException
class CalendarQuery:
"""
Simple CalendarQuery class for calendar queries, similar to yf.screener.query.QueryBase.
Simple operand accepted by YF is of the form:
`{ "operator": operator, "operands": [field, ...values] }`
Nested operand accepted by YF:
`{ "operator": operator, "operands": [ ...CalendarQuery ] }`
### Simple example:
```python
op = CalendarQuery('eq', ['ticker', 'AAPL'])
print(op.to_dict())
```
"""
def __init__(self, operator: str, operand: list[Any] | list["CalendarQuery"]):
"""
:param operator: Operator string, e.g., 'eq', 'gte', 'and', 'or'.
:param operand: List of operands: can be values (str, int), or other Operands instances (nested).
"""
operator = operator.upper()
self.operator = operator
self.operands = operand
def append(self, operand: Any) -> None:
"""
Append an operand to the operands list.
:param operand: CalendarQuery to append (can be value or CalendarQuery instance).
"""
self.operands.append(operand)
@property
def is_empty(self) -> bool:
"""
Check if the operands list is empty.
:return: True if operands list is empty, False otherwise.
"""
return len(self.operands) == 0
def to_dict(self) -> dict:
"""
Query-ready dict for YF.
Simple operand accepted by YF is of the form:
`{ "operator": operator, "operands": [field, ...values] }`
Nested operand accepted by YF:
`{ "operator": operator, "operands": [ ...CalendarQuery ] }`
"""
op = self.operator
ops = self.operands
return {
"operator": op,
"operands": [o.to_dict() if isinstance(o, CalendarQuery) else o for o in ops],
}
_CALENDAR_URL_ = f"{_QUERY1_URL_}/v1/finance/visualization"
DATE_STR_FORMAT = "%Y-%m-%d"
PREDEFINED_CALENDARS = {
"sp_earnings": {
"sortField": "intradaymarketcap",
"includeFields": [
"ticker",
"companyshortname",
"intradaymarketcap",
"eventname",
"startdatetime",
"startdatetimetype",
"epsestimate",
"epsactual",
"epssurprisepct",
],
"nan_cols": ["Surprise (%)", "EPS Estimate", "Reported EPS"],
"datetime_cols": ["Event Start Date"],
"df_index": "Symbol",
"renames": {
"Surprise (%)": "Surprise(%)",
"Company Name": "Company",
"Market Cap (Intraday)": "Marketcap",
},
},
"ipo_info": {
"sortField": "startdatetime",
"includeFields": [
"ticker",
"companyshortname",
"exchange_short_name",
"filingdate",
"startdatetime",
"amendeddate",
"pricefrom",
"priceto",
"offerprice",
"currencyname",
"shares",
"dealtype",
],
"nan_cols": ["Price From", "Price To", "Price", "Shares"],
"datetime_cols": ["Filing Date", "Date", "Amended Date"],
"df_index": "Symbol",
"renames": {
"Exchange Short Name": "Exchange",
},
},
"economic_event": {
"sortField": "startdatetime",
"includeFields": [
"econ_release",
"country_code",
"startdatetime",
"period",
"after_release_actual",
"consensus_estimate",
"prior_release_actual",
"originally_reported_actual",
],
"nan_cols": ["Actual", "Market Expectation", "Prior to This", "Revised from"],
"datetime_cols": ["Event Time"],
"df_index": "Event",
"renames": {
"Country Code": "Region",
"Market Expectation": "Expected",
"Prior to This": "Last",
"Revised from": "Revised",
},
},
"splits": {
"sortField": "startdatetime",
"includeFields": [
"ticker",
"companyshortname",
"startdatetime",
"optionable",
"old_share_worth",
"share_worth",
],
"nan_cols": [],
"datetime_cols": ["Payable On"],
"df_index": "Symbol",
"renames": {
"Optionable?": "Optionable",
},
},
}
class Calendars:
"""
Get economic calendars, for example, Earnings, IPO, Economic Events, Splits
### Simple example default params:
```python
import yfinance as yf
calendars = yf.Calendars()
earnings_calendar = calendars.get_earnings_calendar(limit=50)
print(earnings_calendar)
```"""
def __init__(
self,
start: Optional[str | datetime | date] = None,
end: Optional[str | datetime | date] = None,
session: Optional[Session] = None,
):
"""
:param str | datetime | date start: start date (default today) \
eg. start="2025-11-08"
:param str | datetime | date end: end date (default `start + 7 days`) \
eg. end="2025-11-08"
:param session: requests.Session object, optional
"""
self._logger = get_yf_logger()
self.session = session or Session()
self._data: YfData = YfData(session=session)
_start = self._parse_date_param(start)
_end = self._parse_date_param(end)
self._start = _start or datetime.now().strftime(DATE_STR_FORMAT)
self._end = _end or (datetime.strptime(self._start, DATE_STR_FORMAT) + timedelta(days=7)).strftime(DATE_STR_FORMAT)
if not start and end:
self._logger.debug(f"Incomplete boundary: did not provide `start`, using today {self._start=} to {self._end=}")
elif start and not end:
self._logger.debug(f"Incomplete boundary: did not provide `end`, using {self._start=} to {self._end=}: +7 days from self._start")
self._most_active_qy: CalendarQuery = CalendarQuery("or", [])
self._cache_request_body = {}
self.calendars: dict[str, pd.DataFrame] = {}
def _parse_date_param(self, _date: Optional[str | datetime | date | int]) -> str:
if not _date:
return ""
else:
return _parse_user_dt(_date).strftime(DATE_STR_FORMAT)
def _get_data(
self, calendar_type: str, query: CalendarQuery, limit=12, offset=0, force=False
) -> pd.DataFrame:
if calendar_type not in PREDEFINED_CALENDARS:
raise YFException(f"Unknown calendar type: {calendar_type}")
params = {"lang": "en-US", "region": "US"}
body = {
"sortType": "DESC",
"entityIdType": calendar_type,
"sortField": PREDEFINED_CALENDARS[calendar_type]["sortField"],
"includeFields": PREDEFINED_CALENDARS[calendar_type]["includeFields"],
"size": min(limit, 100), # YF caps at 100, don't go higher
"offset": offset,
"query": query.to_dict(),
}
if self._cache_request_body.get(calendar_type, None) and not force:
cache_body = self._cache_request_body[calendar_type]
if cache_body == body and calendar_type in self.calendars:
# Uses cache if force=False and new request has same body as previous
self._logger.debug(f"Getting {calendar_type=} from local cache")
return self.calendars[calendar_type]
self._cache_request_body[calendar_type] = body
self._logger.debug(f"Fetching {calendar_type=} with {limit=}")
response: Response = self._data.post(_CALENDAR_URL_, params=params, body=body)
try:
json_data = response.json()
except json.JSONDecodeError:
self._logger.error(f"{calendar_type}: Failed to retrieve calendar.")
json_data = {}
# Error returned
if json_data.get("finance", {}).get("error", {}):
raise YFException(json_data.get("finance", {}).get("error", {}))
self.calendars[calendar_type] = self._create_df(json_data)
return self._cleanup_df(calendar_type)
def _create_df(self, json_data: dict) -> pd.DataFrame:
columns = []
for col in json_data["finance"]["result"][0]["documents"][0]["columns"]:
columns.append(col["label"])
if col["label"] == "Event Start Date" and col["type"] == "STRING":
# Rename duplicate columns Event Start Date
columns[-1] = "Timing"
rows = json_data["finance"]["result"][0]["documents"][0]["rows"]
return pd.DataFrame(rows, columns=columns)
def _cleanup_df(self, calendar_type: str) -> pd.DataFrame:
predef_cal: dict = PREDEFINED_CALENDARS[calendar_type]
df: pd.DataFrame = self.calendars[calendar_type]
if df.empty:
return df
# Convert types
nan_cols: list = predef_cal["nan_cols"]
if nan_cols:
df[nan_cols] = df[nan_cols].astype("float64").replace(0.0, np.nan)
# Format the dataframe
df.set_index(predef_cal["df_index"], inplace=True)
for rename_from, rename_to in predef_cal["renames"].items():
df.rename(columns={rename_from: rename_to}, inplace=True)
for datetime_col in predef_cal["datetime_cols"]:
df[datetime_col] = pd.to_datetime(df[datetime_col])
return df
@log_indent_decorator
def _get_most_active_operands(
self, _market_cap: Optional[float], force=False
) -> CalendarQuery:
"""
Retrieve tickers from YF, converts them into operands accepted by YF.
Saves the operands in self._most_active_qy.
Will not re-query if already populated.
Used for earnings calendar optional filter.
:param force: if True, will re-query even if operands already exist
:return: list of operands for active traded stocks
"""
if not self._most_active_qy.is_empty and not force:
return self._most_active_qy
self._logger.debug("Fetching 200 most_active for earnings calendar")
try:
json_raw: dict = screen(query="MOST_ACTIVES", count=200)
except exceptions.HTTPError:
self._logger.error("Failed to retrieve most active stocks.")
return self._most_active_qy
raw = json_raw.get("quotes", [{}])
self._most_active_qy = CalendarQuery("or", [])
for stock in raw:
if type(stock) is not dict:
continue
ticker = stock.get("symbol", "")
t_market_cap = stock.get("marketCap", 0)
# We filter market_cap here because we want to keep self._most_active_qy consistent
if ticker and (_market_cap is None or t_market_cap >= _market_cap):
self._most_active_qy.append(CalendarQuery("eq", ["ticker", ticker]))
return self._most_active_qy
def _get_startdatetime_operators(self, start=None, end=None) -> CalendarQuery:
"""
Get startdatetime operands for start/end dates.
If no dates passed, defaults to internal date set on initialization.
"""
_start = self._parse_date_param(start)
_end = self._parse_date_param(end)
if (start and not end) or (end and not start):
warnings.warn(
"When prividing custom `start` and `end` parameters, you may want to specify both, to avoid unexpected behaviour.",
UserWarning,
stacklevel=2,
)
return CalendarQuery(
"and",
[
CalendarQuery("gte", ["startdatetime", _start or self._start]),
CalendarQuery("lte", ["startdatetime", _end or self._end]),
],
)
### Manual getter functions:
@log_indent_decorator
def get_earnings_calendar(
self,
market_cap: Optional[float] = None,
filter_most_active: bool = True,
start=None,
end=None,
limit=12,
offset=0,
force=False,
) -> pd.DataFrame:
"""
Retrieve earnings calendar from YF as a DataFrame.
Will re-query every time it is called, overwriting previous data.
:param market_cap: market cap cutoff in USD, default None
:param filter_most_active: will filter for actively traded stocks (default True)
:param str | datetime | date start: overwrite start date (default set by __init__) \
eg. start="2025-11-08"
:param str | datetime | date end: overwrite end date (default set by __init__) \
eg. end="2025-11-08"
:param limit: maximum number of results to return (YF caps at 100)
:param offset: offsets the results for pagination. YF default 0
:param force: if True, will re-query even if cache already exists
:return: DataFrame with earnings calendar
"""
_start = self._parse_date_param(start)
_end = self._parse_date_param(end)
if (start and not end) or (end and not start):
warnings.warn(
"When prividing custom `start` and `end` parameters, you may want to specify both, to avoid unexpected behaviour.",
UserWarning,
stacklevel=2,
)
query = CalendarQuery(
"and",
[
CalendarQuery("eq", ["region", "us"]),
CalendarQuery(
"or",
[
CalendarQuery("eq", ["eventtype", "EAD"]),
CalendarQuery("eq", ["eventtype", "ERA"]),
],
),
CalendarQuery("gte", ["startdatetime", _start or self._start]),
CalendarQuery("lte", ["startdatetime", _end or self._end]),
],
)
if market_cap is not None:
if market_cap < 10_000_000:
warnings.warn(
f"market_cap {market_cap} is very low, did you mean to set it higher?",
UserWarning,
stacklevel=2,
)
query.append(CalendarQuery("gte", ["intradaymarketcap", market_cap]))
if filter_most_active and not offset:
# YF does not like filter most active while offsetting
query.append(self._get_most_active_operands(market_cap))
return self._get_data(
calendar_type="sp_earnings",
query=query,
limit=limit,
offset=offset,
force=force,
)
@log_indent_decorator
def get_ipo_info_calendar(
self, start=None, end=None, limit=12, offset=0, force=False
) -> pd.DataFrame:
"""
Retrieve IPOs calendar from YF as a Dataframe.
:param str | datetime | date start: overwrite start date (default set by __init__) \
eg. start="2025-11-08"
:param str | datetime | date end: overwrite end date (default set by __init__) \
eg. end="2025-11-08"
:param limit: maximum number of results to return (YF caps at 100)
:param offset: offsets the results for pagination. YF default 0
:param force: if True, will re-query even if cache already exists
:return: DataFrame with IPOs calendar
"""
_start = self._parse_date_param(start)
_end = self._parse_date_param(end)
if (start and not end) or (end and not start):
warnings.warn(
"When prividing custom `start` and `end` parameters, you may want to specify both, to avoid unexpected behaviour.",
UserWarning,
stacklevel=2,
)
query = CalendarQuery(
"or",
[
CalendarQuery("gtelt", ["startdatetime", _start or self._start, _end or self._end]),
CalendarQuery("gtelt", ["filingdate", _start or self._start, _end or self._end]),
CalendarQuery("gtelt", ["amendeddate", _start or self._start, _end or self._end]),
],
)
return self._get_data(
calendar_type="ipo_info",
query=query,
limit=limit,
offset=offset,
force=force,
)
@log_indent_decorator
def get_economic_events_calendar(
self, start=None, end=None, limit=12, offset=0, force=False
) -> pd.DataFrame:
"""
Retrieve Economic Events calendar from YF as a DataFrame.
:param str | datetime | date start: overwrite start date (default set by __init__) \
eg. start="2025-11-08"
:param str | datetime | date end: overwrite end date (default set by __init__) \
eg. end="2025-11-08"
:param limit: maximum number of results to return (YF caps at 100)
:param offset: offsets the results for pagination. YF default 0
:param force: if True, will re-query even if cache already exists
:return: DataFrame with Economic Events calendar
"""
return self._get_data(
calendar_type="economic_event",
query=self._get_startdatetime_operators(start, end),
limit=limit,
offset=offset,
force=force,
)
@log_indent_decorator
def get_splits_calendar(
self, start=None, end=None, limit=12, offset=0, force=False
) -> pd.DataFrame:
"""
Retrieve Splits calendar from YF as a DataFrame.
:param str | datetime | date start: overwrite start date (default set by __init__) \
eg. start="2025-11-08"
:param str | datetime | date end: overwrite end date (default set by __init__) \
eg. end="2025-11-08"
:param limit: maximum number of results to return (YF caps at 100)
:param offset: offsets the results for pagination. YF default 0
:param force: if True, will re-query even if cache already exists
:return: DataFrame with Splits calendar
"""
return self._get_data(
calendar_type="splits",
query=self._get_startdatetime_operators(start, end),
limit=limit,
offset=offset,
force=force,
)
### Easy / Default getter functions:
@property
def earnings_calendar(self) -> pd.DataFrame:
"""Earnings calendar with default settings."""
if "sp_earnings" in self.calendars:
return self.calendars["sp_earnings"]
return self.get_earnings_calendar()
@property
def ipo_info_calendar(self) -> pd.DataFrame:
"""IPOs calendar with default settings."""
if "ipo_info" in self.calendars:
return self.calendars["ipo_info"]
return self.get_ipo_info_calendar()
@property
def economic_events_calendar(self) -> pd.DataFrame:
"""Economic events calendar with default settings."""
if "economic_event" in self.calendars:
return self.calendars["economic_event"]
return self.get_economic_events_calendar()
@property
def splits_calendar(self) -> pd.DataFrame:
"""Splits calendar with default settings."""
if "splits" in self.calendars:
return self.calendars["splits"]
return self.get_splits_calendar()

58
yfinance/config.py Normal file
View File

@@ -0,0 +1,58 @@
import json
class NestedConfig:
def __init__(self, name, data):
self.__dict__['name'] = name
self.__dict__['data'] = data
def __getattr__(self, key):
return self.data.get(key)
def __setattr__(self, key, value):
self.data[key] = value
def __len__(self):
return len(self.__dict__['data'])
def __repr__(self):
return json.dumps(self.data, indent=4)
class ConfigMgr:
def __init__(self):
self._initialised = False
def _load_option(self):
self._initialised = True # prevent infinite loop
self.options = {}
# Initialise defaults
n = self.__getattr__('network')
n.proxy = None
n.retries = 0
d = self.__getattr__('debug')
d.hide_exceptions = True
d.logging = False
def __getattr__(self, key):
if not self._initialised:
self._load_option()
if key not in self.options:
self.options[key] = {}
return NestedConfig(key, self.options[key])
def __contains__(self, key):
if not self._initialised:
self._load_option()
return key in self.options
def __repr__(self):
if not self._initialised:
self._load_option()
all_options = self.options.copy()
return json.dumps(all_options, indent=4)
YfConfig = ConfigMgr()

View File

@@ -159,153 +159,223 @@ quote_summary_valid_modules = (
"futuresChain",
)
# map last updated as of 2024.09.18
# map last updated as of 2025.12.19
SECTOR_INDUSTY_MAPPING = {
'basic-materials': {'specialty-chemicals',
'gold',
'building-materials',
'copper',
'steel',
'agricultural-inputs',
'chemicals',
'other-industrial-metals-mining',
'lumber-wood-production',
'aluminum',
'other-precious-metals-mining',
'coking-coal',
'paper-paper-products',
'silver'},
'communication-services': {'internet-content-information',
'telecom-services',
'entertainment',
'electronic-gaming-multimedia',
'advertising-agencies',
'broadcasting',
'publishing'},
'consumer-cyclical': {'internet-retail',
'auto-manufacturers',
'restaurants',
'home-improvement-retail',
'travel-services',
'specialty-retail',
'apparel-retail',
'residential-construction',
'footwear-accessories',
'packaging-containers',
'lodging',
'auto-parts',
'auto-truck-dealerships',
'gambling',
'resorts-casinos',
'leisure',
'apparel-manufacturing',
'personal-services',
'furnishings-fixtures-appliances',
'recreational-vehicles',
'luxury-goods',
'department-stores',
'textile-manufacturing'},
'consumer-defensive': {'discount-stores',
'beverages-non-alcoholic',
'household-personal-products',
'packaged-foods',
'tobacco',
'confectioners',
'farm-products',
'food-distribution',
'grocery-stores',
'beverages-brewers',
'education-training-services',
'beverages-wineries-distilleries'},
'energy': {'oil-gas-integrated',
'oil-gas-midstream',
'oil-gas-e-p',
'oil-gas-equipment-services',
'oil-gas-refining-marketing',
'uranium',
'oil-gas-drilling',
'thermal-coal'},
'financial-services': {'banks-diversified',
'credit-services',
'asset-management',
'insurance-diversified',
'banks-regional',
'capital-markets',
'financial-data-stock-exchanges',
'insurance-property-casualty',
'insurance-brokers',
'insurance-life',
'insurance-specialty',
'mortgage-finance',
'insurance-reinsurance',
'shell-companies',
'financial-conglomerates'},
'healthcare': {'drug-manufacturers-general',
'healthcare-plans',
'biotechnology',
'medical-devices',
'diagnostics-research',
'medical-instruments-supplies',
'medical-care-facilities',
'drug-manufacturers-specialty-generic',
'health-information-services',
'medical-distribution',
'pharmaceutical-retailers'},
'industrials': {'aerospace-defense',
'specialty-industrial-machinery',
'railroads',
'building-products-equipment',
'farm-heavy-construction-machinery',
'specialty-business-services',
'integrated-freight-logistics',
'waste-management',
'conglomerates',
'industrial-distribution',
'engineering-construction',
'rental-leasing-services',
'consulting-services',
'trucking',
'electrical-equipment-parts',
'airlines',
'tools-accessories',
'pollution-treatment-controls',
'security-protection-services',
'marine-shipping',
'metal-fabrication',
'infrastructure-operations',
'staffing-employment-services',
'airports-air-services',
'business-equipment-supplies'},
'real-estate': {'reit-specialty',
'reit-industrial',
'reit-retail',
'reit-residential',
'reit-healthcare-facilities',
'real-estate-services',
'reit-office',
'reit-diversified',
'reit-mortgage',
'reit-hotel-motel',
'real-estate-development',
'real-estate-diversified'},
'technology': {'software-infrastructure',
'semiconductors',
'consumer-electronics',
'software-application',
'information-technology-services',
'semiconductor-equipment-materials',
'communication-equipment',
'computer-hardware',
'electronic-components',
'scientific-technical-instruments',
'solar',
'electronics-computer-distribution'},
'utilities': {'utilities-regulated-electric',
'utilities-renewable',
'utilities-diversified',
'utilities-regulated-gas',
'utilities-independent-power-producers',
'utilities-regulated-water'}
'Basic Materials': {'Specialty Chemicals',
'Gold',
'Building Materials',
'Copper',
'Steel',
'Agricultural Inputs',
'Chemicals',
'Other Industrial Metals & Mining',
'Lumber & Wood Production',
'Aluminum',
'Other Precious Metals & Mining',
'Coking Coal',
'Paper & Paper Products',
'Silver'},
'Communication Services': {'Advertising Agencies',
'Broadcasting',
'Electronic Gaming & Multimedia',
'Entertainment',
'Internet Content & Information',
'Publishing',
'Telecom Services'},
'Consumer Cyclical': {'Apparel Manufacturing',
'Apparel Retail',
'Auto & Truck Dealerships',
'Auto Manufacturers',
'Auto Parts',
'Department Stores',
'Footwear & Accessories',
'Furnishings, Fixtures & Appliances',
'Gambling',
'Home Improvement Retail',
'Internet Retail',
'Leisure',
'Lodging',
'Luxury Goods',
'Packaging & Containers',
'Personal Services',
'Recreational Vehicles',
'Residential Construction',
'Resorts & Casinos',
'Restaurants',
'Specialty Retail',
'Textile Manufacturing',
'Travel Services'},
'Consumer Defensive': {'Beverages—Brewers',
'Beverages—Non-Alcoholic',
'Beverages—Wineries & Distilleries',
'Confectioners',
'Discount Stores',
'Education & Training Services',
'Farm Products',
'Food Distribution',
'Grocery Stores',
'Household & Personal Products',
'Packaged Foods',
'Tobacco'},
'Energy': {'Oil & Gas Drilling',
'Oil & Gas E&P',
'Oil & Gas Equipment & Services',
'Oil & Gas Integrated',
'Oil & Gas Midstream',
'Oil & Gas Refining & Marketing',
'Thermal Coal',
'Uranium'},
'Financial Services': {'Asset Management',
'Banks—Diversified',
'Banks—Regional',
'Capital Markets',
'Credit Services',
'Financial Conglomerates',
'Financial Data & Stock Exchanges',
'Insurance Brokers',
'Insurance—Diversified',
'Insurance—Life',
'Insurance—Property & Casualty',
'Insurance—Reinsurance',
'Insurance—Specialty',
'Mortgage Finance',
'Shell Companies'},
'Healthcare': {'Biotechnology',
'Diagnostics & Research',
'Drug Manufacturers—General',
'Drug Manufacturers—Specialty & Generic',
'Health Information Services',
'Healthcare Plans',
'Medical Care Facilities',
'Medical Devices',
'Medical Instruments & Supplies',
'Medical Distribution',
'Pharmaceutical Retailers'},
'Industrials': {'Aerospace & Defense',
'Airlines',
'Airports & Air Services',
'Building Products & Equipment',
'Business Equipment & Supplies',
'Conglomerates',
'Consulting Services',
'Electrical Equipment & Parts',
'Engineering & Construction',
'Farm & Heavy Construction Machinery',
'Industrial Distribution',
'Infrastructure Operations',
'Integrated Freight & Logistics',
'Marine Shipping',
'Metal Fabrication',
'Pollution & Treatment Controls',
'Railroads',
'Rental & Leasing Services',
'Security & Protection Services',
'Specialty Business Services',
'Specialty Industrial Machinery',
'Staffing & Employment Services',
'Tools & Accessories',
'Trucking',
'Waste Management'},
'Real Estate': {'Real Estate—Development',
'Real Estate Services',
'Real Estate—Diversified',
'REIT—Healthcare Facilities',
'REIT—Hotel & Motel',
'REIT—Industrial',
'REIT—Office',
'REIT—Residential',
'REIT—Retail',
'REIT—Mortgage',
'REIT—Specialty',
'REIT—Diversified'},
'Technology': {'Communication Equipment',
'Computer Hardware',
'Consumer Electronics',
'Electronic Components',
'Electronics & Computer Distribution',
'Information Technology Services',
'Scientific & Technical Instruments',
'Semiconductor Equipment & Materials',
'Semiconductors',
'Software—Application',
'Software—Infrastructure',
'Solar'},
'Utilities': {'Utilities—Diversified',
'Utilities—Independent Power Producers',
'Utilities—Regulated Electric',
'Utilities—Regulated Gas',
'Utilities—Regulated Water',
'Utilities—Renewable'},
}
SECTOR_INDUSTY_MAPPING_LC = {}
for k in SECTOR_INDUSTY_MAPPING.keys():
k2 = k.lower().replace('& ', '').replace('- ', '').replace(', ', ' ').replace(' ', '-')
SECTOR_INDUSTY_MAPPING_LC[k2] = []
for v in SECTOR_INDUSTY_MAPPING[k]:
v2 = v.lower().replace('& ', '').replace('- ', '').replace(', ', ' ').replace(' ', '-')
SECTOR_INDUSTY_MAPPING_LC[k2].append(v2)
# _MIC_TO_YAHOO_SUFFIX maps Market Identifier Codes (MIC) to Yahoo Finance market suffixes.
# c.f. :
# https://help.yahoo.com/kb/finance-for-web/SLN2310.html;_ylt=AwrJKiCZFo9g3Y8AsDWPAwx.;_ylu=Y29sbwMEcG9zAzEEdnRpZAMEc2VjA3Ny?locale=en_US
# https://www.iso20022.org/market-identifier-codes
_MIC_TO_YAHOO_SUFFIX = {
'XCBT': 'CBT', 'XCME': 'CME', 'IFUS': 'NYB', 'CECS': 'CMX', 'XNYM': 'NYM', 'XNYS': '', 'XNAS': '', # United States
'XBUE': 'BA', # Argentina
'XVIE': 'VI', # Austria
'XASX': 'AX', 'XAUS': 'XA', # Australia
'XBRU': 'BR', # Belgium
'BVMF': 'SA', # Brazil
'CNSX': 'CN', 'NEOE': 'NE', 'XTSE': 'TO', 'XTSX': 'V', # Canada
'XSGO': 'SN', # Chile
'XSHG': 'SS', 'XSHE': 'SZ', # China
'XBOG': 'CL', # Colombia
'XPRA': 'PR', # Czech Republic
'XCSE': 'CO', # Denmark
'XCAI': 'CA', # Egypt
'XTAL': 'TL', # Estonia
'CEUX': 'XD', 'XEUR': 'NX', # Europe (Cboe Europe, Euronext)
'XHEL': 'HE', # Finland
'XPAR': 'PA', # France
'XBER': 'BE', 'XBMS': 'BM', 'XDUS': 'DU', 'XFRA': 'F', 'XHAM': 'HM', 'XHAN': 'HA', 'XMUN': 'MU', 'XSTU': 'SG', 'XETR': 'DE', # Germany
'XATH': 'AT', # Greece
'XHKG': 'HK', # Hong Kong
'XBUD': 'BD', # Hungary
'XICE': 'IC', # Iceland
'XBOM': 'BO', 'XNSE': 'NS', # India
'XIDX': 'JK', # Indonesia
'XDUB': 'IR', # Ireland
'XTAE': 'TA', # Israel
'MTAA': 'MI', 'EUTL': 'TI', # Italy
'XTKS': 'T', # Japan
'XKFE': 'KW', # Kuwait
'XRIS': 'RG', # Latvia
'XVIL': 'VS', # Lithuania
'XKLS': 'KL', # Malaysia
'XMEX': 'MX', # Mexico
'XAMS': 'AS', # Netherlands
'XNZE': 'NZ', # New Zealand
'XOSL': 'OL', # Norway
'XPHS': 'PS', # Philippines
'XWAR': 'WA', # Poland
'XLIS': 'LS', # Portugal
'XQAT': 'QA', # Qatar
'XBSE': 'RO', # Romania
'XSES': 'SI', # Singapore
'XJSE': 'JO', # South Africa
'XKRX': 'KS', 'KQKS': 'KQ', # South Korea
'BMEX': 'MC', # Spain
'XTAD': 'SAU', # Saudi Arabia
'XSTO': 'ST', # Sweden
'XSWX': 'SW', # Switzerland
'ROCO': 'TWO', 'XTAI': 'TW', # Taiwan
'XBKK': 'BK', # Thailand
'XIST': 'IS', # Turkey
'XDFM': 'AE', # UAE
'AQXE': 'AQ', 'XCHI': 'XC', 'XLON': 'L', 'ILSE': 'IL', # United Kingdom
'XCAR': 'CR', # Venezuela
'XSTC': 'VN' # Vietnam
}
def merge_two_level_dicts(dict1, dict2):
@@ -380,6 +450,7 @@ EQUITY_SCREENER_EQ_MAP = {
'se': {'STO'},
'sg': {'SES'},
'sr': {},
'sw': {'EBS'},
'th': {'SET'},
'tr': {'IST'},
'tw': {'TAI', 'TWO'},
@@ -393,6 +464,7 @@ EQUITY_SCREENER_EQ_MAP = {
"Real Estate", "Technology", "Energy", "Utilities", "Financial Services",
"Consumer Defensive", "Consumer Cyclical"
},
"industry": SECTOR_INDUSTY_MAPPING,
"peer_group": {
"US Fund Equity Energy",
"US CE Convertibles",
@@ -529,7 +601,8 @@ EQUITY_SCREENER_FIELDS = {
"eq_fields": {
"region",
"sector",
"peer_group"},
"peer_group",
"industry"},
"price":{
"lastclosemarketcap.lasttwelvemonths",
"percentchange",

View File

@@ -1,16 +1,32 @@
import functools
from functools import lru_cache
import socket
import time as _time
from curl_cffi import requests
from urllib.parse import urlsplit, urljoin
from bs4 import BeautifulSoup
import datetime
from frozendict import frozendict
from . import utils, cache
from .config import YfConfig
import threading
from .exceptions import YFRateLimitError
from .exceptions import YFException, YFDataException, YFRateLimitError
def _is_transient_error(exception):
"""Check if error is transient (network/timeout) and should be retried."""
if isinstance(exception, (TimeoutError, socket.error, OSError)):
return True
error_type_name = type(exception).__name__
transient_error_types = {
'Timeout', 'TimeoutError', 'ConnectionError', 'ConnectTimeout',
'ReadTimeout', 'ChunkedEncodingError', 'RemoteDisconnected',
}
return error_type_name in transient_error_types
cache_maxsize = 64
@@ -53,9 +69,6 @@ class SingletonMeta(type):
if 'session' in kwargs or (args and len(args) > 0):
session = kwargs.get('session') if 'session' in kwargs else args[0]
cls._instances[cls]._set_session(session)
if 'proxy' in kwargs or (args and len(args) > 1):
proxy = kwargs.get('proxy') if 'proxy' in kwargs else args[1]
cls._instances[cls]._set_proxy(proxy)
return cls._instances[cls]
@@ -65,7 +78,7 @@ class YfData(metaclass=SingletonMeta):
Singleton means one session one cookie shared by all threads.
"""
def __init__(self, session=None, proxy=None):
def __init__(self, session=None):
self._crumb = None
self._cookie = None
@@ -76,20 +89,15 @@ class YfData(metaclass=SingletonMeta):
self._cookie_lock = threading.Lock()
self._session, self._proxy = None, None
self._session = None
self._set_session(session or requests.Session(impersonate="chrome"))
self._set_proxy(proxy)
def _set_session(self, session):
if session is None:
return
with self._cookie_lock:
self._session = session
if self._proxy is not None:
self._session.proxies = self._proxy
try:
self._session.cache
session.cache
except AttributeError:
# Not caching
self._session_is_caching = False
@@ -98,17 +106,16 @@ class YfData(metaclass=SingletonMeta):
# Can't simply use a non-caching session to fetch cookie & crumb,
# because then the caching-session won't have cookie.
self._session_is_caching = True
from requests_cache import DO_NOT_CACHE
self._expire_after = DO_NOT_CACHE
# But since switch to curl_cffi, can't use requests_cache with it.
raise YFDataException("request_cache sessions don't work with curl_cffi, which is necessary now for Yahoo API. Solution: stop setting session, let YF handle.")
if not isinstance(session, requests.session.Session):
raise YFDataException(f"Yahoo API requires curl_cffi session not {type(session)}. Solution: stop setting session, let YF handle.")
def _set_proxy(self, proxy=None):
with self._cookie_lock:
if proxy is not None:
proxy = {'http': proxy, 'https': proxy} if isinstance(proxy, str) else proxy
else:
proxy = {}
self._proxy = proxy
self._session.proxies = proxy
self._session = session
if YfConfig.network.proxy is not None:
self._session.proxies = YfConfig.network.proxy
def _set_cookie_strategy(self, strategy, have_lock=False):
if strategy == self._cookie_strategy:
@@ -133,53 +140,82 @@ class YfData(metaclass=SingletonMeta):
if not have_lock:
self._cookie_lock.release()
def _save_session_cookies(self):
try:
cache.get_cookie_cache().store('csrf', self._session.cookies)
except Exception:
@utils.log_indent_decorator
def _save_cookie_curlCffi(self):
if self._session is None:
return False
cookies = self._session.cookies.jar._cookies
if len(cookies) == 0:
return False
yh_domains = [k for k in cookies.keys() if 'yahoo' in k]
if len(yh_domains) > 1:
# Possible when cookie fetched with CSRF method. Discard consent cookie.
yh_domains = [k for k in yh_domains if 'consent' not in k]
if len(yh_domains) > 1:
utils.get_yf_logger().debug(f'Multiple Yahoo cookies, not sure which to cache: {yh_domains}')
return False
if len(yh_domains) == 0:
return False
yh_domain = yh_domains[0]
yh_cookie = {yh_domain: cookies[yh_domain]}
cache.get_cookie_cache().store('curlCffi', yh_cookie)
return True
def _load_session_cookies(self):
cookie_dict = cache.get_cookie_cache().lookup('csrf')
if cookie_dict is None:
@utils.log_indent_decorator
def _load_cookie_curlCffi(self):
if self._session is None:
return False
# Periodically refresh, 24 hours seems fair.
if cookie_dict['age'] > datetime.timedelta(days=1):
cookie_dict = cache.get_cookie_cache().lookup('curlCffi')
if cookie_dict is None or len(cookie_dict) == 0:
return False
self._session.cookies.update(cookie_dict['cookie'])
utils.get_yf_logger().debug('loaded persistent cookie')
def _save_cookie_basic(self, cookie):
try:
cache.get_cookie_cache().store('basic', cookie)
except Exception:
cookies = cookie_dict['cookie']
domain = list(cookies.keys())[0]
cookie = cookies[domain]['/']['A3']
expiry_ts = cookie.expires
if expiry_ts > 2e9:
# convert ms to s
expiry_ts //= 1e3
expiry_dt = datetime.datetime.fromtimestamp(expiry_ts, tz=datetime.timezone.utc)
expired = expiry_dt < datetime.datetime.now(datetime.timezone.utc)
if expired:
utils.get_yf_logger().debug('cached cookie expired')
return False
self._session.cookies.jar._cookies.update(cookies)
self._cookie = cookie
return True
def _load_cookie_basic(self):
cookie_dict = cache.get_cookie_cache().lookup('basic')
if cookie_dict is None:
return None
# Periodically refresh, 24 hours seems fair.
if cookie_dict['age'] > datetime.timedelta(days=1):
return None
utils.get_yf_logger().debug('loaded persistent cookie')
return cookie_dict['cookie']
@utils.log_indent_decorator
def _get_cookie_basic(self, timeout=30):
if self._cookie is not None:
utils.get_yf_logger().debug('reusing cookie')
return True
elif self._load_cookie_curlCffi():
utils.get_yf_logger().debug('reusing persistent cookie')
return True
# To avoid infinite recursion, do NOT use self.get()
# - 'allow_redirects' copied from @psychoz971 solution - does it help USA?
self._session.get(
url='https://fc.yahoo.com',
timeout=timeout,
allow_redirects=True)
try:
self._session.get(
url='https://fc.yahoo.com',
timeout=timeout,
allow_redirects=True)
except requests.exceptions.DNSError as e:
# Possible because url on some privacy/ad blocklists.
# Can ignore because have second strategy.
utils.get_yf_logger().debug("Handling DNS error on cookie fetch: " + str(e))
return False
self._save_cookie_curlCffi()
return True
@utils.log_indent_decorator
def _get_crumb_basic(self, timeout=30):
if self._crumb is not None:
utils.get_yf_logger().debug('reusing crumb')
return self._crumb
self._get_cookie_basic()
if not self._get_cookie_basic():
return None
# - 'allow_redirects' copied from @psychoz971 solution - does it help USA?
get_args = {
'url': "https://query1.finance.yahoo.com/v1/test/getcrumb",
@@ -192,6 +228,10 @@ class YfData(metaclass=SingletonMeta):
else:
crumb_response = self._session.get(**get_args)
self._crumb = crumb_response.text
if crumb_response.status_code == 429 or "Too Many Requests" in self._crumb:
utils.get_yf_logger().debug(f"Didn't receive crumb {self._crumb}")
raise YFRateLimitError()
if self._crumb is None or '<html>' in self._crumb:
utils.get_yf_logger().debug("Didn't receive crumb")
return None
@@ -201,16 +241,17 @@ class YfData(metaclass=SingletonMeta):
@utils.log_indent_decorator
def _get_cookie_and_crumb_basic(self, timeout):
self._get_cookie_basic(timeout)
crumb = self._get_crumb_basic(timeout)
return crumb
if not self._get_cookie_basic(timeout):
return None
return self._get_crumb_basic(timeout)
@utils.log_indent_decorator
def _get_cookie_csrf(self, timeout):
if self._cookie is not None:
utils.get_yf_logger().debug('reusing cookie')
return True
elif self._load_session_cookies():
elif self._load_cookie_curlCffi():
utils.get_yf_logger().debug('reusing persistent cookie')
self._cookie = True
return True
@@ -270,7 +311,7 @@ class YfData(metaclass=SingletonMeta):
# No idea why happens, but handle nicely so can switch to other cookie method.
utils.get_yf_logger().debug('_get_cookie_csrf() encountering requests.exceptions.ChunkedEncodingError, aborting')
self._cookie = True
self._save_session_cookies()
self._save_cookie_curlCffi()
return True
@utils.log_indent_decorator
@@ -295,6 +336,10 @@ class YfData(metaclass=SingletonMeta):
r = self._session.get(**get_args)
self._crumb = r.text
if r.status_code == 429 or "Too Many Requests" in self._crumb:
utils.get_yf_logger().debug(f"Didn't receive crumb {self._crumb}")
raise YFRateLimitError()
if self._crumb is None or '<html>' in self._crumb or self._crumb == '':
utils.get_yf_logger().debug("Didn't receive crumb")
return None
@@ -327,14 +372,24 @@ class YfData(metaclass=SingletonMeta):
@utils.log_indent_decorator
def get(self, url, params=None, timeout=30):
return self._make_request(url, request_method = self._session.get, params=params, timeout=timeout)
response = self._make_request(url, request_method = self._session.get, params=params, timeout=timeout)
# Accept cookie-consent if redirected to consent page
if not self._is_this_consent_url(response.url):
# "Consent Page not detected"
pass
else:
# "Consent Page detected"
response = self._accept_consent_form(response, timeout)
return response
@utils.log_indent_decorator
def post(self, url, body, params=None, timeout=30):
return self._make_request(url, request_method = self._session.post, body=body, params=params, timeout=timeout)
def post(self, url, body=None, params=None, timeout=30, data=None):
return self._make_request(url, request_method = self._session.post, body=body, params=params, timeout=timeout, data=data)
@utils.log_indent_decorator
def _make_request(self, url, request_method, body=None, params=None, timeout=30):
def _make_request(self, url, request_method, body=None, params=None, timeout=30, data=None):
# Important: treat input arguments as immutable.
if len(url) > 200:
@@ -343,10 +398,13 @@ class YfData(metaclass=SingletonMeta):
utils.get_yf_logger().debug(f'url={url}')
utils.get_yf_logger().debug(f'params={params}')
# sync with config
self._session.proxies = YfConfig.network.proxy
if params is None:
params = {}
if 'crumb' in params:
raise Exception("Don't manually add 'crumb' to params dict, let data.py handle it")
raise YFException("Don't manually add 'crumb' to params dict, let data.py handle it")
crumb, strategy = self._get_cookie_and_crumb()
if crumb is not None:
@@ -362,8 +420,20 @@ class YfData(metaclass=SingletonMeta):
if body:
request_args['json'] = body
response = request_method(**request_args)
if data:
request_args['data'] = data
request_args['headers'] = {"Content-Type": "application/json"}
for attempt in range(YfConfig.network.retries + 1):
try:
response = request_method(**request_args)
break
except Exception as e:
if _is_transient_error(e) and attempt < YfConfig.network.retries:
_time.sleep(2 ** attempt)
else:
raise
utils.get_yf_logger().debug(f'response code={response.status_code}')
if response.status_code >= 400:
# Retry with other cookie strategy
@@ -391,4 +461,84 @@ class YfData(metaclass=SingletonMeta):
utils.get_yf_logger().debug(f'get_raw_json(): {url}')
response = self.get(url, params=params, timeout=timeout)
response.raise_for_status()
return response.json()
return response.json()
def _is_this_consent_url(self, response_url: str) -> bool:
"""
Check if given response_url is consent page
Args:
response_url (str) : response.url
Returns:
True : This is cookie-consent page
False : This is not cookie-consent page
"""
try:
return urlsplit(response_url).hostname and urlsplit(
response_url
).hostname.endswith("consent.yahoo.com")
except Exception:
return False
def _accept_consent_form(
self, consent_resp: requests.Response, timeout: int
) -> requests.Response:
"""
Click 'Accept all' to cookie-consent form and return response object.
Args:
consent_resp (requests.Response) : Response instance of cookie-consent page
timeout (int) : Raise TimeoutError if post doesn't respond
Returns:
response (requests.Response) : Reponse instance received from the server after accepting cookie-consent post.
"""
soup = BeautifulSoup(consent_resp.text, "html.parser")
# Heuristic: pick the first form; Yahoo's CMP tends to have a single form for consent
form = soup.find("form")
if not form:
return consent_resp
# action : URL to send "Accept Cookies"
action = form.get("action") or consent_resp.url
action = urljoin(consent_resp.url, action)
# Collect inputs (hidden tokens, etc.)
"""
<input name="csrfToken" type="hidden" value="..."/>
<input name="sessionId" type="hidden" value="..."/>
<input name="originalDoneUrl" type="hidden" value="..."/>
<input name="namespace" type="hidden" value="yahoo"/>
"""
data = {}
for inp in form.find_all("input"):
name = inp.get("name")
if not name:
continue
typ = (inp.get("type") or "text").lower()
val = inp.get("value") or ""
if typ in ("checkbox", "radio"):
# If it's clearly an "agree"/"accept" field or already checked, include it
if (
"agree" in name.lower()
or "accept" in name.lower()
or inp.has_attr("checked")
):
data[name] = val if val != "" else "1"
else:
data[name] = val
# If no explicit agree/accept in inputs, add a best-effort flag
lowered = {k.lower() for k in data.keys()}
if not any(("agree" in k or "accept" in k) for k in lowered):
data["agree"] = "1"
# Submit the form with "Referer". Some servers check this header as a simple CSRF protection measure.
headers = {"Referer": consent_resp.url}
response = self._session.post(
action, data=data, headers=headers, timeout=timeout, allow_redirects=True
)
return response

View File

@@ -1,10 +1,10 @@
from abc import ABC, abstractmethod
from ..ticker import Ticker
from ..const import _QUERY1_URL_, _SENTINEL_
from ..data import YfData
from ..utils import print_once
from typing import Dict, List, Optional
import pandas as _pd
from typing import Dict, List, Optional
from ..const import _QUERY1_URL_
from ..data import YfData
from ..ticker import Ticker
_QUERY_URL_ = f'{_QUERY1_URL_}/v1/finance'
@@ -14,9 +14,9 @@ class Domain(ABC):
and methods for fetching and parsing data. Derived classes must implement the `_fetch_and_parse()` method.
"""
def __init__(self, key: str, session=None, proxy=_SENTINEL_):
def __init__(self, key: str, session=None):
"""
Initializes the Domain object with a key, session, and proxy.
Initializes the Domain object with a key, session.
Args:
key (str): Unique key identifying the domain entity.
@@ -25,9 +25,6 @@ class Domain(ABC):
self._key: str = key
self.session = session
self._data: YfData = YfData(session=session)
if proxy is not _SENTINEL_:
print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
self._name: Optional[str] = None
self._symbol: Optional[str] = None

View File

@@ -1,27 +1,26 @@
from __future__ import print_function
from typing import Dict, Optional
import pandas as _pd
from typing import Dict, Optional
from .. import utils
from ..config import YfConfig
from ..data import YfData
from .domain import Domain, _QUERY_URL_
from .. import utils
from ..data import YfData
from ..const import _SENTINEL_
class Industry(Domain):
"""
Represents an industry within a sector.
"""
def __init__(self, key, session=None, proxy=_SENTINEL_):
def __init__(self, key, session=None):
"""
Args:
key (str): The key identifier for the industry.
session (optional): The session to use for requests.
"""
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
YfData(session=session, proxy=proxy)
YfData(session=session)
super(Industry, self).__init__(key, session)
self._query_url = f'{_QUERY_URL_}/industries/{self._key}'
@@ -93,7 +92,7 @@ class Industry(Domain):
Returns:
Optional[pd.DataFrame]: DataFrame containing parsed top performing companies data.
"""
compnaies_column = ['symbol','name','ytd return',' last price','target price']
compnaies_column = ['symbol','name','ytd return','last price','target price']
compnaies_values = [(c.get('symbol', None),
c.get('name', None),
c.get('ytdReturn',{}).get('raw', None),
@@ -115,7 +114,7 @@ class Industry(Domain):
Returns:
Optional[pd.DataFrame]: DataFrame containing parsed top growth companies data.
"""
compnaies_column = ['symbol','name','ytd return',' growth estimate']
compnaies_column = ['symbol','name','ytd return','growth estimate']
compnaies_values = [(c.get('symbol', None),
c.get('name', None),
c.get('ytdReturn',{}).get('raw', None),
@@ -144,6 +143,8 @@ class Industry(Domain):
return result
except Exception as e:
if not YfConfig.debug.hide_exceptions:
raise
logger = utils.get_yf_logger()
logger.error(f"Failed to get industry data for '{self._key}' reason: {e}")
logger.debug("Got response: ")

View File

@@ -1,20 +1,18 @@
import datetime as dt
from ..data import YfData
from ..data import utils
from ..const import _QUERY1_URL_, _SENTINEL_
import json as _json
from ..config import YfConfig
from ..const import _QUERY1_URL_
from ..data import utils, YfData
from ..exceptions import YFDataException
class Market:
def __init__(self, market:'str', session=None, proxy=_SENTINEL_, timeout=30):
def __init__(self, market:'str', session=None, timeout=30):
self.market = market
self.session = session
self.timeout = timeout
self._data = YfData(session=self.session)
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
self._logger = utils.get_yf_logger()
@@ -24,12 +22,12 @@ class Market:
def _fetch_json(self, url, params):
data = self._data.cache_get(url=url, params=params, timeout=self.timeout)
if data is None or "Will be right back" in data.text:
raise RuntimeError("*** YAHOO! FINANCE IS CURRENTLY DOWN! ***\n"
"Our engineers are working quickly to resolve "
"the issue. Thank you for your patience.")
raise YFDataException("*** YAHOO! FINANCE IS CURRENTLY DOWN! ***")
try:
return data.json()
except _json.JSONDecodeError:
if not YfConfig.debug.hide_exceptions:
raise
self._logger.error(f"{self.market}: Failed to retrieve market data and recieved faulty data.")
return {}
@@ -66,6 +64,8 @@ class Market:
self._summary = self._summary['marketSummaryResponse']['result']
self._summary = {x['exchange']:x for x in self._summary}
except Exception as e:
if not YfConfig.debug.hide_exceptions:
raise
self._logger.error(f"{self.market}: Failed to parse market summary")
self._logger.debug(f"{type(e)}: {e}")
@@ -75,18 +75,22 @@ class Market:
self._status = self._status['finance']['marketTimes'][0]['marketTime'][0]
self._status['timezone'] = self._status['timezone'][0]
del self._status['time'] # redundant
try:
self._status.update({
"open": dt.datetime.fromisoformat(self._status["open"]),
"close": dt.datetime.fromisoformat(self._status["close"]),
"tz": dt.timezone(dt.timedelta(hours=int(self._status["timezone"]["gmtoffset"]))/1000, self._status["timezone"]["short"])
})
except Exception as e:
self._logger.error(f"{self.market}: Failed to update market status")
self._logger.debug(f"{type(e)}: {e}")
except Exception as e:
if not YfConfig.debug.hide_exceptions:
raise
self._logger.error(f"{self.market}: Failed to parse market status")
self._logger.debug(f"{type(e)}: {e}")
try:
self._status.update({
"open": dt.datetime.fromisoformat(self._status["open"]),
"close": dt.datetime.fromisoformat(self._status["close"]),
"tz": dt.timezone(dt.timedelta(hours=int(self._status["timezone"]["gmtoffset"]))/1000, self._status["timezone"]["short"])
})
except Exception as e:
if not YfConfig.debug.hide_exceptions:
raise
self._logger.error(f"{self.market}: Failed to update market status")
self._logger.debug(f"{type(e)}: {e}")

View File

@@ -1,13 +1,13 @@
from __future__ import print_function
from typing import Dict, Optional
from ..utils import dynamic_docstring, generate_list_table_from_dict
from ..const import SECTOR_INDUSTY_MAPPING, _SENTINEL_
import pandas as _pd
from typing import Dict, Optional
from ..config import YfConfig
from ..const import SECTOR_INDUSTY_MAPPING_LC
from ..utils import dynamic_docstring, generate_list_table_from_dict, get_yf_logger
from .domain import Domain, _QUERY_URL_
from .. import utils
from ..data import YfData
class Sector(Domain):
"""
@@ -15,22 +15,17 @@ class Sector(Domain):
such as top ETFs, top mutual funds, and industry data.
"""
def __init__(self, key, session=None, proxy=_SENTINEL_):
def __init__(self, key, session=None):
"""
Args:
key (str): The key representing the sector.
session (requests.Session, optional): A session for making requests. Defaults to None.
proxy (dict, optional): A dictionary containing proxy settings for the request. Defaults to None.
.. seealso::
:attr:`Sector.industries <yfinance.Sector.industries>`
Map of sector and industry
"""
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
YfData(session=session, proxy=proxy)
super(Sector, self).__init__(key, session)
self._query_url: str = f'{_QUERY_URL_}/sectors/{self._key}'
self._top_etfs: Optional[Dict] = None
@@ -68,7 +63,7 @@ class Sector(Domain):
self._ensure_fetched(self._top_mutual_funds)
return self._top_mutual_funds
@dynamic_docstring({"sector_industry": generate_list_table_from_dict(SECTOR_INDUSTY_MAPPING,bullets=True)})
@dynamic_docstring({"sector_industry": generate_list_table_from_dict(SECTOR_INDUSTY_MAPPING_LC,bullets=True)})
@property
def industries(self) -> _pd.DataFrame:
"""
@@ -147,7 +142,9 @@ class Sector(Domain):
self._industries = self._parse_industries(data.get('industries', {}))
except Exception as e:
logger = utils.get_yf_logger()
if not YfConfig.debug.hide_exceptions:
raise
logger = get_yf_logger()
logger.error(f"Failed to get sector data for '{self._key}' reason: {e}")
logger.debug("Got response: ")
logger.debug("-------------")

View File

@@ -45,7 +45,7 @@ class YFInvalidPeriodError(YFException):
self.invalid_period = invalid_period
self.valid_ranges = valid_ranges
super().__init__(f"{self.ticker}: Period '{invalid_period}' is invalid, "
f"must be of the format {valid_ranges}, etc.")
f"must be one of: {valid_ranges}")
class YFRateLimitError(YFException):

View File

@@ -1,12 +1,13 @@
import asyncio
import base64
import json
from typing import List, Optional, Callable
from typing import List, Optional, Callable, Union
from websockets.sync.client import connect as sync_connect
from websockets.asyncio.client import connect as async_connect
from yfinance import utils
from yfinance.config import YfConfig
from yfinance.pricing_pb2 import PricingData
from google.protobuf.json_format import MessageToDict
@@ -27,6 +28,8 @@ class BaseWebSocket:
pricing_data.ParseFromString(decoded_bytes)
return MessageToDict(pricing_data, preserving_proto_field_name=True)
except Exception as e:
if not YfConfig.debug.hide_exceptions:
raise
self.logger.error("Failed to decode message: %s", e, exc_info=True)
if self.verbose:
print("Failed to decode message: %s", e)
@@ -61,6 +64,8 @@ class AsyncWebSocket(BaseWebSocket):
if self.verbose:
print("Connected to WebSocket.")
except Exception as e:
if not YfConfig.debug.hide_exceptions:
raise
self.logger.error("Failed to connect to WebSocket: %s", e, exc_info=True)
if self.verbose:
print(f"Failed to connect to WebSocket: {e}")
@@ -79,17 +84,19 @@ class AsyncWebSocket(BaseWebSocket):
if self.verbose:
print(f"Heartbeat subscription sent for symbols: {self._subscriptions}")
except Exception as e:
if not YfConfig.debug.hide_exceptions:
raise
self.logger.error("Error in heartbeat subscription: %s", e, exc_info=True)
if self.verbose:
print(f"Error in heartbeat subscription: {e}")
break
async def subscribe(self, symbols: str | List[str]):
async def subscribe(self, symbols: Union[str, List[str]]):
"""
Subscribe to a stock symbol or a list of stock symbols.
Args:
symbols (str | List[str]): Stock symbol(s) to subscribe to.
symbols (Union[str, List[str]]): Stock symbol(s) to subscribe to.
"""
await self._connect()
@@ -109,12 +116,12 @@ class AsyncWebSocket(BaseWebSocket):
if self.verbose:
print(f"Subscribed to symbols: {symbols}")
async def unsubscribe(self, symbols: str | List[str]):
async def unsubscribe(self, symbols: Union[str, List[str]]):
"""
Unsubscribe from a stock symbol or a list of stock symbols.
Args:
symbols (str | List[str]): Stock symbol(s) to unsubscribe from.
symbols (Union[str, List[str]]): Stock symbol(s) to unsubscribe from.
"""
await self._connect()
@@ -162,6 +169,8 @@ class AsyncWebSocket(BaseWebSocket):
else:
self._message_handler(decoded_message)
except Exception as handler_exception:
if not YfConfig.debug.hide_exceptions:
raise
self.logger.error("Error in message handler: %s", handler_exception, exc_info=True)
if self.verbose:
print("Error in message handler:", handler_exception)
@@ -176,6 +185,8 @@ class AsyncWebSocket(BaseWebSocket):
break
except Exception as e:
if not YfConfig.debug.hide_exceptions:
raise
self.logger.error("Error while listening to messages: %s", e, exc_info=True)
if self.verbose:
print("Error while listening to messages: %s", e)
@@ -235,12 +246,12 @@ class WebSocket(BaseWebSocket):
self._ws = None
raise
def subscribe(self, symbols: str | List[str]):
def subscribe(self, symbols: Union[str, List[str]]):
"""
Subscribe to a stock symbol or a list of stock symbols.
Args:
symbols (str | List[str]): Stock symbol(s) to subscribe to.
symbols (Union[str, List[str]]): Stock symbol(s) to subscribe to.
"""
self._connect()
@@ -256,12 +267,12 @@ class WebSocket(BaseWebSocket):
if self.verbose:
print(f"Subscribed to symbols: {symbols}")
def unsubscribe(self, symbols: str | List[str]):
def unsubscribe(self, symbols: Union[str, List[str]]):
"""
Unsubscribe from a stock symbol or a list of stock symbols.
Args:
symbols (str | List[str]): Stock symbol(s) to unsubscribe from.
symbols (Union[str, List[str]]): Stock symbol(s) to unsubscribe from.
"""
self._connect()
@@ -301,6 +312,8 @@ class WebSocket(BaseWebSocket):
try:
message_handler(decoded_message)
except Exception as handler_exception:
if not YfConfig.debug.hide_exceptions:
raise
self.logger.error("Error in message handler: %s", handler_exception, exc_info=True)
if self.verbose:
print("Error in message handler:", handler_exception)
@@ -314,6 +327,8 @@ class WebSocket(BaseWebSocket):
break
except Exception as e:
if not YfConfig.debug.hide_exceptions:
raise
self.logger.error("Error while listening to messages: %s", e, exc_info=True)
if self.verbose:
print("Error while listening to messages: %s", e)

View File

@@ -20,13 +20,13 @@
#
import json as _json
import pandas as pd
from . import utils
from .const import _QUERY1_URL_, _SENTINEL_
from .config import YfConfig
from .const import _QUERY1_URL_
from .data import YfData
from .exceptions import YFException
from .exceptions import YFDataException
LOOKUP_TYPES = ["all", "equity", "mutualfund", "etf", "index", "future", "currency", "cryptocurrency"]
@@ -38,19 +38,14 @@ class Lookup:
:param query: The search query for financial data lookup.
:type query: str
:param session: Custom HTTP session for requests (default None).
:param proxy: Proxy settings for requests (default None).
:param timeout: Request timeout in seconds (default 30).
:param raise_errors: Raise exceptions on error (default True).
"""
def __init__(self, query: str, session=None, proxy=None, timeout=30, raise_errors=True):
def __init__(self, query: str, session=None, timeout=30, raise_errors=True):
self.session = session
self._data = YfData(session=self.session)
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
self.query = query
self.timeout = timeout
@@ -81,18 +76,19 @@ class Lookup:
data = self._data.get(url=url, params=params, timeout=self.timeout)
if data is None or "Will be right back" in data.text:
raise RuntimeError("*** YAHOO! FINANCE IS CURRENTLY DOWN! ***\n"
"Our engineers are working quickly to resolve "
"the issue. Thank you for your patience.")
raise YFDataException("*** YAHOO! FINANCE IS CURRENTLY DOWN! ***")
try:
data = data.json()
except _json.JSONDecodeError:
self._logger.error(f"{self.query}: Failed to retrieve lookup results and received faulty response instead.")
if not YfConfig.debug.hide_exceptions:
raise
self._logger.error(f"{self.ticker}: 'lookup' fetch received faulty data")
data = {}
# Error returned
if data.get("finance", {}).get("error", {}):
raise YFException(data.get("finance", {}).get("error", {}))
error = data.get("finance", {}).get("error", {})
raise YFDataException(f"{self.ticker}: 'lookup' fetch returned error: {error}")
self._cache[cache_key] = data
return data

View File

@@ -33,13 +33,13 @@ from curl_cffi import requests
from . import Ticker, utils
from .data import YfData
from . import shared
from .const import _SENTINEL_
from .config import YfConfig
@utils.log_indent_decorator
def download(tickers, start=None, end=None, actions=False, threads=True,
ignore_tz=None, group_by='column', auto_adjust=None, back_adjust=False,
repair=False, keepna=False, progress=True, period="max", interval="1d",
prepost=False, proxy=_SENTINEL_, rounding=False, timeout=10, session=None,
ignore_tz=None, group_by='column', auto_adjust=True, back_adjust=False,
repair=False, keepna=False, progress=True, period=None, interval="1d",
prepost=False, rounding=False, timeout=10, session=None,
multi_level_index=True) -> Union[_pd.DataFrame, None]:
"""
Download yahoo tickers
@@ -48,6 +48,7 @@ def download(tickers, start=None, end=None, actions=False, threads=True,
List of tickers to download
period : str
Valid periods: 1d,5d,1mo,3mo,6mo,1y,2y,5y,10y,ytd,max
Default: 1mo
Either Use period parameter or use start and end
interval : str
Valid intervals: 1m,2m,5m,15m,30m,60m,90m,1h,1d,5d,1wk,1mo,3mo
@@ -93,10 +94,8 @@ def download(tickers, start=None, end=None, actions=False, threads=True,
logger = utils.get_yf_logger()
session = session or requests.Session(impersonate="chrome")
if auto_adjust is None:
# Warn users that default has changed to True
utils.print_once("YF.download() has changed argument auto_adjust default to True")
auto_adjust = True
# Ensure data initialised with session.
YfData(session=session)
if logger.isEnabledFor(logging.DEBUG):
if threads:
@@ -127,7 +126,7 @@ def download(tickers, start=None, end=None, actions=False, threads=True,
for ticker in tickers:
if utils.is_isin(ticker):
isin = ticker
ticker = utils.get_ticker_by_isin(ticker, session=session)
ticker = utils.get_ticker_by_isin(ticker)
shared._ISINS[ticker] = isin
_tickers_.append(ticker)
@@ -143,13 +142,6 @@ def download(tickers, start=None, end=None, actions=False, threads=True,
shared._ERRORS = {}
shared._TRACEBACKS = {}
# Ensure data initialised with session.
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
YfData(session=session, proxy=proxy)
else:
YfData(session=session)
# download using threads
if threads:
if threads is True:
@@ -260,7 +252,7 @@ def _realign_dfs():
@_multitasking.task
def _download_one_threaded(ticker, start=None, end=None,
auto_adjust=False, back_adjust=False, repair=False,
actions=False, progress=True, period="max",
actions=False, progress=True, period=None,
interval="1d", prepost=False,
keepna=False, rounding=False, timeout=10):
_download_one(ticker, start, end, auto_adjust, back_adjust, repair,
@@ -272,25 +264,27 @@ def _download_one_threaded(ticker, start=None, end=None,
def _download_one(ticker, start=None, end=None,
auto_adjust=False, back_adjust=False, repair=False,
actions=False, period="max", interval="1d",
actions=False, period=None, interval="1d",
prepost=False, rounding=False,
keepna=False, timeout=10):
data = None
backup = YfConfig.network.hide_exceptions
YfConfig.network.hide_exceptions = False
try:
data = Ticker(ticker).history(
period=period, interval=interval,
start=start, end=end, prepost=prepost,
actions=actions, auto_adjust=auto_adjust,
back_adjust=back_adjust, repair=repair,
rounding=rounding, keepna=keepna, timeout=timeout,
raise_errors=True
rounding=rounding, keepna=keepna, timeout=timeout
)
shared._DFS[ticker.upper()] = data
except Exception as e:
# glob try/except needed as current thead implementation breaks if exception is raised.
shared._DFS[ticker.upper()] = utils.empty_df()
shared._ERRORS[ticker.upper()] = repr(e)
shared._TRACEBACKS[ticker.upper()] = traceback.format_exc()
else:
shared._DFS[ticker.upper()] = data
YfConfig.network.hide_exceptions = backup
return data

View File

@@ -1,22 +1,12 @@
# -*- coding: utf-8 -*-
# Generated by the protocol buffer compiler. DO NOT EDIT!
# NO CHECKED-IN PROTOBUF GENCODE
# source: yfinance/pricing.proto
# Protobuf Python Version: 5.29.0
# source: pricing.proto
"""Generated protocol buffer code."""
from google.protobuf import descriptor as _descriptor
from google.protobuf import descriptor_pool as _descriptor_pool
from google.protobuf import runtime_version as _runtime_version
from google.protobuf import message as _message
from google.protobuf import reflection as _reflection
from google.protobuf import symbol_database as _symbol_database
from google.protobuf.internal import builder as _builder
_runtime_version.ValidateProtobufRuntimeVersion(
_runtime_version.Domain.PUBLIC,
5,
29,
0,
'',
'yfinance/pricing.proto'
)
# @@protoc_insertion_point(imports)
_sym_db = _symbol_database.Default()
@@ -24,13 +14,20 @@ _sym_db = _symbol_database.Default()
DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x16yfinance/pricing.proto\"\x9a\x05\n\x0bPricingData\x12\n\n\x02id\x18\x01 \x01(\t\x12\r\n\x05price\x18\x02 \x01(\x02\x12\x0c\n\x04time\x18\x03 \x01(\x12\x12\x10\n\x08\x63urrency\x18\x04 \x01(\t\x12\x10\n\x08\x65xchange\x18\x05 \x01(\t\x12\x12\n\nquote_type\x18\x06 \x01(\x05\x12\x14\n\x0cmarket_hours\x18\x07 \x01(\x05\x12\x16\n\x0e\x63hange_percent\x18\x08 \x01(\x02\x12\x12\n\nday_volume\x18\t \x01(\x12\x12\x10\n\x08\x64\x61y_high\x18\n \x01(\x02\x12\x0f\n\x07\x64\x61y_low\x18\x0b \x01(\x02\x12\x0e\n\x06\x63hange\x18\x0c \x01(\x02\x12\x12\n\nshort_name\x18\r \x01(\t\x12\x13\n\x0b\x65xpire_date\x18\x0e \x01(\x12\x12\x12\n\nopen_price\x18\x0f \x01(\x02\x12\x16\n\x0eprevious_close\x18\x10 \x01(\x02\x12\x14\n\x0cstrike_price\x18\x11 \x01(\x02\x12\x19\n\x11underlying_symbol\x18\x12 \x01(\t\x12\x15\n\ropen_interest\x18\x13 \x01(\x12\x12\x14\n\x0coptions_type\x18\x14 \x01(\x12\x12\x13\n\x0bmini_option\x18\x15 \x01(\x12\x12\x11\n\tlast_size\x18\x16 \x01(\x12\x12\x0b\n\x03\x62id\x18\x17 \x01(\x02\x12\x10\n\x08\x62id_size\x18\x18 \x01(\x12\x12\x0b\n\x03\x61sk\x18\x19 \x01(\x02\x12\x10\n\x08\x61sk_size\x18\x1a \x01(\x12\x12\x12\n\nprice_hint\x18\x1b \x01(\x12\x12\x10\n\x08vol_24hr\x18\x1c \x01(\x12\x12\x1a\n\x12vol_all_currencies\x18\x1d \x01(\x12\x12\x15\n\rfrom_currency\x18\x1e \x01(\t\x12\x13\n\x0blast_market\x18\x1f \x01(\t\x12\x1a\n\x12\x63irculating_supply\x18 \x01(\x01\x12\x12\n\nmarket_cap\x18! \x01(\x01\x62\x06proto3')
DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\rpricing.proto\"\x9a\x05\n\x0bPricingData\x12\n\n\x02id\x18\x01 \x01(\t\x12\r\n\x05price\x18\x02 \x01(\x02\x12\x0c\n\x04time\x18\x03 \x01(\x12\x12\x10\n\x08\x63urrency\x18\x04 \x01(\t\x12\x10\n\x08\x65xchange\x18\x05 \x01(\t\x12\x12\n\nquote_type\x18\x06 \x01(\x05\x12\x14\n\x0cmarket_hours\x18\x07 \x01(\x05\x12\x16\n\x0e\x63hange_percent\x18\x08 \x01(\x02\x12\x12\n\nday_volume\x18\t \x01(\x12\x12\x10\n\x08\x64\x61y_high\x18\n \x01(\x02\x12\x0f\n\x07\x64\x61y_low\x18\x0b \x01(\x02\x12\x0e\n\x06\x63hange\x18\x0c \x01(\x02\x12\x12\n\nshort_name\x18\r \x01(\t\x12\x13\n\x0b\x65xpire_date\x18\x0e \x01(\x12\x12\x12\n\nopen_price\x18\x0f \x01(\x02\x12\x16\n\x0eprevious_close\x18\x10 \x01(\x02\x12\x14\n\x0cstrike_price\x18\x11 \x01(\x02\x12\x19\n\x11underlying_symbol\x18\x12 \x01(\t\x12\x15\n\ropen_interest\x18\x13 \x01(\x12\x12\x14\n\x0coptions_type\x18\x14 \x01(\x12\x12\x13\n\x0bmini_option\x18\x15 \x01(\x12\x12\x11\n\tlast_size\x18\x16 \x01(\x12\x12\x0b\n\x03\x62id\x18\x17 \x01(\x02\x12\x10\n\x08\x62id_size\x18\x18 \x01(\x12\x12\x0b\n\x03\x61sk\x18\x19 \x01(\x02\x12\x10\n\x08\x61sk_size\x18\x1a \x01(\x12\x12\x12\n\nprice_hint\x18\x1b \x01(\x12\x12\x10\n\x08vol_24hr\x18\x1c \x01(\x12\x12\x1a\n\x12vol_all_currencies\x18\x1d \x01(\x12\x12\x15\n\rfrom_currency\x18\x1e \x01(\t\x12\x13\n\x0blast_market\x18\x1f \x01(\t\x12\x1a\n\x12\x63irculating_supply\x18 \x01(\x01\x12\x12\n\nmarket_cap\x18! \x01(\x01\x62\x06proto3')
_PRICINGDATA = DESCRIPTOR.message_types_by_name['PricingData']
PricingData = _reflection.GeneratedProtocolMessageType('PricingData', (_message.Message,), {
'DESCRIPTOR' : _PRICINGDATA,
'__module__' : 'pricing_pb2'
# @@protoc_insertion_point(class_scope:PricingData)
})
_sym_db.RegisterMessage(PricingData)
_globals = globals()
_builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals)
_builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'yfinance.pricing_pb2', _globals)
if not _descriptor._USE_C_DESCRIPTORS:
DESCRIPTOR._loaded_options = None
_globals['_PRICINGDATA']._serialized_start=27
_globals['_PRICINGDATA']._serialized_end=693
DESCRIPTOR._options = None
_PRICINGDATA._serialized_start=18
_PRICINGDATA._serialized_end=684
# @@protoc_insertion_point(module_scope)

View File

@@ -1,19 +1,16 @@
import curl_cffi
import pandas as pd
import requests
from yfinance import utils
from yfinance.config import YfConfig
from yfinance.const import quote_summary_valid_modules
from yfinance.data import YfData
from yfinance.const import quote_summary_valid_modules, _SENTINEL_
from yfinance.scrapers.quote import _QUOTE_SUMMARY_URL_
from yfinance.exceptions import YFException
from yfinance.scrapers.quote import _QUOTE_SUMMARY_URL_
class Analysis:
def __init__(self, data: YfData, symbol: str, proxy=_SENTINEL_):
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
data._set_proxy(proxy)
def __init__(self, data: YfData, symbol: str):
self._data = data
self._symbol = symbol
@@ -83,6 +80,8 @@ class Analysis:
data = self._fetch(['financialData'])
data = data['quoteSummary']['result'][0]['financialData']
except (TypeError, KeyError):
if not YfConfig.debug.hide_exceptions:
raise
self._analyst_price_targets = {}
return self._analyst_price_targets
@@ -106,6 +105,8 @@ class Analysis:
data = self._fetch(['earningsHistory'])
data = data['quoteSummary']['result'][0]['earningsHistory']['history']
except (TypeError, KeyError):
if not YfConfig.debug.hide_exceptions:
raise
self._earnings_history = pd.DataFrame()
return self._earnings_history
@@ -142,6 +143,8 @@ class Analysis:
trends = self._fetch(['industryTrend', 'sectorTrend', 'indexTrend'])
trends = trends['quoteSummary']['result'][0]
except (TypeError, KeyError):
if not YfConfig.debug.hide_exceptions:
raise
self._growth_estimates = pd.DataFrame()
return self._growth_estimates
@@ -178,8 +181,10 @@ class Analysis:
params_dict = {"modules": modules, "corsDomain": "finance.yahoo.com", "formatted": "false", "symbol": self._symbol}
try:
result = self._data.get_raw_json(_QUOTE_SUMMARY_URL_ + f"/{self._symbol}", params=params_dict)
except requests.exceptions.HTTPError as e:
utils.get_yf_logger().error(str(e))
except curl_cffi.requests.exceptions.HTTPError as e:
if not YfConfig.debug.hide_exceptions:
raise
utils.get_yf_logger().error(str(e) + e.response.text)
return None
return result
@@ -188,4 +193,6 @@ class Analysis:
data = self._fetch(['earningsTrend'])
self._earnings_trend = data['quoteSummary']['result'][0]['earningsTrend']['trend']
except (TypeError, KeyError):
if not YfConfig.debug.hide_exceptions:
raise
self._earnings_trend = []

View File

@@ -5,16 +5,13 @@ import warnings
import pandas as pd
from yfinance import utils, const
from yfinance.config import YfConfig
from yfinance.data import YfData
from yfinance.exceptions import YFException, YFNotImplementedError
class Fundamentals:
def __init__(self, data: YfData, symbol: str, proxy=const._SENTINEL_):
if proxy is not const._SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
data._set_proxy(proxy)
def __init__(self, data: YfData, symbol: str):
self._data = data
self._symbol = symbol
@@ -51,31 +48,19 @@ class Financials:
self._balance_sheet_time_series = {}
self._cash_flow_time_series = {}
def get_income_time_series(self, freq="yearly", proxy=const._SENTINEL_) -> pd.DataFrame:
if proxy is not const._SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
def get_income_time_series(self, freq="yearly") -> pd.DataFrame:
res = self._income_time_series
if freq not in res:
res[freq] = self._fetch_time_series("income", freq)
return res[freq]
def get_balance_sheet_time_series(self, freq="yearly", proxy=const._SENTINEL_) -> pd.DataFrame:
if proxy is not const._SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
def get_balance_sheet_time_series(self, freq="yearly") -> pd.DataFrame:
res = self._balance_sheet_time_series
if freq not in res:
res[freq] = self._fetch_time_series("balance-sheet", freq)
return res[freq]
def get_cash_flow_time_series(self, freq="yearly", proxy=const._SENTINEL_) -> pd.DataFrame:
if proxy is not const._SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
def get_cash_flow_time_series(self, freq="yearly") -> pd.DataFrame:
res = self._cash_flow_time_series
if freq not in res:
res[freq] = self._fetch_time_series("cash-flow", freq)
@@ -104,6 +89,8 @@ class Financials:
if statement is not None:
return statement
except YFException as e:
if not YfConfig.debug.hide_exceptions:
raise
utils.get_yf_logger().error(f"{self._symbol}: Failed to create {name} financials table for reason: {e}")
return pd.DataFrame()
@@ -117,6 +104,8 @@ class Financials:
try:
return self._get_financials_time_series(timescale, keys)
except Exception:
if not YfConfig.debug.hide_exceptions:
raise
pass
def _get_financials_time_series(self, timescale, keys: list) -> pd.DataFrame:
@@ -159,6 +148,10 @@ class Financials:
df.index = df.index.str.replace("^" + timescale, "", regex=True)
# Ensure float type, not object
for d in df.columns:
df[d] = df[d].astype('float')
# Reorder table to match order on Yahoo website
df = df.reindex([k for k in keys if k in df.index])
df = df[sorted(df.columns, reverse=True)]

View File

@@ -1,12 +1,12 @@
import pandas as pd
from yfinance.data import YfData
from yfinance.const import _BASE_URL_, _SENTINEL_
from yfinance.exceptions import YFDataException
from yfinance import utils
from typing import Dict, Optional
from yfinance import utils
from yfinance.config import YfConfig
from yfinance.const import _BASE_URL_
from yfinance.data import YfData
from yfinance.exceptions import YFDataException
_QUOTE_SUMMARY_URL_ = f"{_BASE_URL_}/v10/finance/quoteSummary/"
class FundsData:
@@ -17,7 +17,7 @@ class FundsData:
Notes:
- fundPerformance module is not implemented as better data is queryable using history
"""
def __init__(self, data: YfData, symbol: str, proxy=_SENTINEL_):
def __init__(self, data: YfData, symbol: str):
"""
Args:
data (YfData): The YfData object for fetching data.
@@ -25,9 +25,6 @@ class FundsData:
"""
self._data = data
self._symbol = symbol
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
# quoteType
self._quote_type = None
@@ -193,8 +190,12 @@ class FundsData:
self._parse_top_holdings(data["topHoldings"])
self._parse_fund_profile(data["fundProfile"])
except KeyError:
raise YFDataException("No Fund data found.")
if not YfConfig.debug.hide_exceptions:
raise
raise YFDataException(f"{self._symbol}: No Fund data found.")
except Exception as e:
if not YfConfig.debug.hide_exceptions:
raise
logger = utils.get_yf_logger()
logger.error(f"Failed to get fund data for '{self._symbol}' reason: {e}")
logger.debug("Got response: ")

View File

@@ -1,25 +1,24 @@
from curl_cffi import requests
from math import isclose
import bisect
import datetime as _datetime
import dateutil as _dateutil
import logging
import numpy as np
import pandas as pd
from math import isclose
import time as _time
import bisect
from curl_cffi import requests
import warnings
from yfinance import shared, utils
from yfinance.const import _BASE_URL_, _PRICE_COLNAMES_, _SENTINEL_
from yfinance.exceptions import YFInvalidPeriodError, YFPricesMissingError, YFTzMissingError, YFRateLimitError
from yfinance.config import YfConfig
from yfinance.const import _BASE_URL_, _PRICE_COLNAMES_
from yfinance.exceptions import YFDataException, YFInvalidPeriodError, YFPricesMissingError, YFRateLimitError, YFTzMissingError
class PriceHistory:
def __init__(self, data, ticker, tz, session=None, proxy=_SENTINEL_):
def __init__(self, data, ticker, tz, session=None):
self._data = data
self.ticker = ticker.upper()
self.tz = tz
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
self.session = session or requests.Session(impersonate="chrome")
self._history_cache = {}
@@ -30,55 +29,56 @@ class PriceHistory:
self._reconstruct_start_interval = None
@utils.log_indent_decorator
def history(self, period="1mo", interval="1d",
def history(self, period=None, interval="1d",
start=None, end=None, prepost=False, actions=True,
auto_adjust=True, back_adjust=False, repair=False, keepna=False,
proxy=_SENTINEL_, rounding=False, timeout=10,
rounding=False, timeout=10,
raise_errors=False) -> pd.DataFrame:
"""
:Parameters:
period : str
Valid periods: 1d,5d,1mo,3mo,6mo,1y,2y,5y,10y,ytd,max
Either Use period parameter or use start and end
| Valid periods: 1d,5d,1mo,3mo,6mo,1y,2y,5y,10y,ytd,max
| Default: 1mo
| Can combine with start/end e.g. end = start + period
interval : str
Valid intervals: 1m,2m,5m,15m,30m,60m,90m,1h,1d,5d,1wk,1mo,3mo
Intraday data cannot extend last 60 days
start: str
Download start date string (YYYY-MM-DD) or _datetime, inclusive.
Default is 99 years ago
E.g. for start="2020-01-01", the first data point will be on "2020-01-01"
end: str
Download end date string (YYYY-MM-DD) or _datetime, exclusive.
Default is now
E.g. for end="2023-01-01", the last data point will be on "2022-12-31"
| Valid intervals: 1m,2m,5m,15m,30m,60m,90m,1h,1d,5d,1wk,1mo,3mo
| Intraday data cannot extend last 60 days
start : str
| Download start date string (YYYY-MM-DD) or _datetime, inclusive.
| Default: 99 years ago
| E.g. for start="2020-01-01", first data point = "2020-01-01"
end : str
| Download end date string (YYYY-MM-DD) or _datetime, exclusive.
| Default: now
| E.g. for end="2023-01-01", last data point = "2022-12-31"
prepost : bool
Include Pre and Post market data in results?
Default is False
auto_adjust: bool
Adjust all OHLC automatically? Default is True
back_adjust: bool
Back-adjusted data to mimic true historical prices
repair: bool
Detect currency unit 100x mixups and attempt repair.
Default is False
keepna: bool
Keep NaN rows returned by Yahoo?
Default is False
rounding: bool
Round values to 2 decimal places?
Optional. Default is False = precision suggested by Yahoo!
timeout: None or float
If not None stops waiting for a response after given number of
seconds. (Can also be a fraction of a second e.g. 0.01)
Default is 10 seconds.
raise_errors: bool
| Include Pre and Post market data in results?
| Default: False
auto_adjust : bool
| Adjust all OHLC automatically?
| Default: True
back_adjust : bool
| Back-adjusted data to mimic true historical prices
repair : bool
| Fixes price errors in Yahoo data: 100x, missing, bad dividend adjust.
| Default: False
| Full details at: :doc:`../advanced/price_repair`.
keepna : bool
| Keep NaN rows returned by Yahoo?
| Default: False
rounding : bool
| Optional: Round values to 2 decimal places?
| Default: False = use precision suggested by Yahoo!
timeout : None or float
| Optional: timeout fetches after N seconds
| Default: 10 seconds
raise_errors : bool
If True, then raise errors as Exceptions instead of logging.
"""
logger = utils.get_yf_logger()
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
if raise_errors:
warnings.warn("'raise_errors' deprecated, do: yf.config.debug.hide_exceptions = False", DeprecationWarning, stacklevel=5)
interval_user = interval
period_user = period
@@ -86,7 +86,7 @@ class PriceHistory:
# Yahoo's way of adjusting mutiday intervals is fundamentally broken.
# Have to fetch 1d, adjust, then resample.
if interval == '5d':
raise Exception("Yahoo's interval '5d' is nonsense, not supported with repair")
raise ValueError("Yahoo's interval '5d' is nonsense, not supported with repair")
if start is None and end is None and period is not None:
tz = self.tz
if tz is None:
@@ -95,7 +95,7 @@ class PriceHistory:
err_msg = str(_exception)
shared._DFS[self.ticker] = utils.empty_df()
shared._ERRORS[self.ticker] = err_msg.split(': ', 1)[1]
if raise_errors:
if raise_errors or (not YfConfig.debug.hide_exceptions):
raise _exception
else:
logger.error(err_msg)
@@ -112,7 +112,7 @@ class PriceHistory:
start_user = start
end_user = end
if start or period is None or period.lower() == "max":
if start or end or (period and period.lower() == "max"):
# Check can get TZ. Fail => probably delisted
tz = self.tz
if tz is None:
@@ -121,27 +121,55 @@ class PriceHistory:
err_msg = str(_exception)
shared._DFS[self.ticker] = utils.empty_df()
shared._ERRORS[self.ticker] = err_msg.split(': ', 1)[1]
if raise_errors:
if raise_errors or (not YfConfig.debug.hide_exceptions):
raise _exception
else:
logger.error(err_msg)
return utils.empty_df()
if end is None:
end = int(_time.time())
else:
end = utils._parse_user_dt(end, tz)
if start is None:
if interval == "1m":
start = end - 604800 # 7 days
elif interval in ("5m", "15m", "30m", "90m"):
start = end - 5184000 # 60 days
elif interval in ("1h", '60m'):
start = end - 63072000 # 730 days
else:
start = end - 3122064000 # 99 years
else:
start = utils._parse_user_dt(start, tz)
if start:
start_dt = utils._parse_user_dt(start, tz)
start = int(start_dt.timestamp())
if end:
end_dt = utils._parse_user_dt(end, tz)
end = int(end_dt.timestamp())
if period is None:
if not (start or end):
period = '1mo' # default
elif not start:
start_dt = end_dt - utils._interval_to_timedelta('1mo')
start = int(start_dt.timestamp())
elif not end:
end_dt = pd.Timestamp.utcnow().tz_convert(tz)
end = int(end_dt.timestamp())
else:
if period.lower() == "max":
if end is None:
end = int(_time.time())
if start is None:
if interval == "1m":
start = end - 691200 # 8 days
elif interval in ("2m", "5m", "15m", "30m", "90m"):
start = end - 5184000 # 60 days
elif interval in ("1h", "60m"):
start = end - 63072000 # 730 days
else:
start = end - 3122064000 # 99 years
start += 5 # allow for processing time
elif start and end:
raise ValueError("Setting period, start and end is nonsense. Set maximum 2 of them.")
elif start or end:
period_td = utils._interval_to_timedelta(period)
if end is None:
end_dt = start_dt + period_td
end = int(end_dt.timestamp())
if start is None:
start_dt = end_dt - period_td
start = int(start_dt.timestamp())
period = None
if start or end:
params = {"period1": start, "period2": end}
else:
period = period.lower()
@@ -182,23 +210,21 @@ class PriceHistory:
timeout=timeout
)
if "Will be right back" in data.text or data is None:
raise RuntimeError("*** YAHOO! FINANCE IS CURRENTLY DOWN! ***\n"
"Our engineers are working quickly to resolve "
"the issue. Thank you for your patience.")
raise YFDataException("*** YAHOO! FINANCE IS CURRENTLY DOWN! ***")
data = data.json()
# Special case for rate limits
except YFRateLimitError:
raise
except Exception:
if raise_errors:
if raise_errors or (not YfConfig.debug.hide_exceptions):
raise
# Store the meta data that gets retrieved simultaneously
try:
self._history_metadata = data["chart"]["result"][0]["meta"]
except Exception:
if data['chart']['result'] is None:
self._history_metadata = {}
else:
self._history_metadata = data["chart"]["result"][0]["meta"]
intraday = params["interval"][-1] in ("m", 'h')
_price_data_debug = ''
@@ -244,7 +270,7 @@ class PriceHistory:
err_msg = str(_exception)
shared._DFS[self.ticker] = utils.empty_df()
shared._ERRORS[self.ticker] = err_msg.split(': ', 1)[1]
if raise_errors:
if raise_errors or (not YfConfig.debug.hide_exceptions):
raise _exception
else:
logger.error(err_msg)
@@ -292,15 +318,10 @@ class PriceHistory:
'Adj Close': quotes2['Adj Close'].last(),
'Volume': quotes2['Volume'].sum()
})
try:
quotes['Dividends'] = quotes2['Dividends'].max()
quotes['Stock Splits'] = quotes2['Stock Splits'].max()
except Exception:
pass
# Note: ordering is important. If you change order, run the tests!
quotes = utils.set_df_tz(quotes, params["interval"], tz_exchange)
quotes = utils.fix_Yahoo_dst_issue(quotes, params["interval"])
quotes = utils.set_df_tz(quotes, interval, tz_exchange)
quotes = utils.fix_Yahoo_dst_issue(quotes, interval)
intraday = params["interval"][-1] in ("m", 'h')
if not prepost and intraday and "tradingPeriods" in self._history_metadata:
tps = self._history_metadata["tradingPeriods"]
@@ -308,7 +329,7 @@ class PriceHistory:
self._history_metadata = utils.format_history_metadata(self._history_metadata, tradingPeriodsOnly=True)
self._history_metadata_formatted = True
tps = self._history_metadata["tradingPeriods"]
quotes = utils.fix_Yahoo_returning_prepost_unrequested(quotes, params["interval"], tps)
quotes = utils.fix_Yahoo_returning_prepost_unrequested(quotes, interval, tps)
if quotes.empty:
msg = f'{self.ticker}: OHLC after cleaning: EMPTY'
elif len(quotes) == 1:
@@ -326,6 +347,24 @@ class PriceHistory:
splits = utils.set_df_tz(splits, interval, tz_exchange)
if dividends is not None:
dividends = utils.set_df_tz(dividends, interval, tz_exchange)
if 'currency' in dividends.columns:
# Rare, only seen with Vietnam market
price_currency = self._history_metadata['currency']
if price_currency is None:
price_currency = ''
f_currency_mismatch = dividends['currency'] != price_currency
if f_currency_mismatch.any():
if not repair or price_currency == '':
# Append currencies to values, let user decide action.
dividends['Dividends'] = dividends['Dividends'].astype(str) + ' ' + dividends['currency']
else:
# Attempt repair = currency conversion
dividends = self._dividends_convert_fx(dividends, price_currency, repair)
if (dividends['currency'] != price_currency).any():
# FX conversion failed
dividends['Dividends'] = dividends['Dividends'].astype(str) + ' ' + dividends['currency']
dividends = dividends.drop('currency', axis=1)
if capital_gains is not None:
capital_gains = utils.set_df_tz(capital_gains, interval, tz_exchange)
if start is not None:
@@ -403,6 +442,7 @@ class PriceHistory:
# First make currency consistent. On some exchanges, dividends often in different currency
# to prices, e.g. £ vs pence.
df, currency = self._standardise_currency(df, currency)
self._history_metadata['currency'] = currency
df = self._fix_bad_div_adjust(df, interval, currency)
@@ -426,16 +466,15 @@ class PriceHistory:
elif back_adjust:
df = utils.back_adjust(df)
except Exception as e:
if raise_errors or (not YfConfig.debug.hide_exceptions):
raise
if auto_adjust:
err_msg = "auto_adjust failed with %s" % e
else:
err_msg = "back_adjust failed with %s" % e
shared._DFS[self.ticker] = utils.empty_df()
shared._ERRORS[self.ticker] = err_msg
if raise_errors:
raise Exception('%s: %s' % (self.ticker, err_msg))
else:
logger.error('%s: %s' % (self.ticker, err_msg))
logger.error('%s: %s' % (self.ticker, err_msg))
if rounding:
df = np.round(df, data["chart"]["result"][0]["meta"]["priceHint"])
@@ -479,11 +518,7 @@ class PriceHistory:
self._history_cache[cache_key] = df
return df
def get_history_metadata(self, proxy=_SENTINEL_) -> dict:
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
def get_history_metadata(self) -> dict:
if self._history_metadata is None or 'tradingPeriods' not in self._history_metadata:
# Request intraday data, because then Yahoo returns exchange schedule (tradingPeriods).
self._get_history_cache(period="5d", interval="1h")
@@ -494,44 +529,28 @@ class PriceHistory:
return self._history_metadata
def get_dividends(self, period="max", proxy=_SENTINEL_) -> pd.Series:
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
def get_dividends(self, period="max") -> pd.Series:
df = self._get_history_cache(period=period)
if "Dividends" in df.columns:
dividends = df["Dividends"]
return dividends[dividends != 0]
return pd.Series()
def get_capital_gains(self, period="max", proxy=_SENTINEL_) -> pd.Series:
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
def get_capital_gains(self, period="max") -> pd.Series:
df = self._get_history_cache(period=period)
if "Capital Gains" in df.columns:
capital_gains = df["Capital Gains"]
return capital_gains[capital_gains != 0]
return pd.Series()
def get_splits(self, period="max", proxy=_SENTINEL_) -> pd.Series:
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
def get_splits(self, period="max") -> pd.Series:
df = self._get_history_cache(period=period)
if "Stock Splits" in df.columns:
splits = df["Stock Splits"]
return splits[splits != 0]
return pd.Series()
def get_actions(self, period="max", proxy=_SENTINEL_) -> pd.Series:
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
def get_actions(self, period="max") -> pd.Series:
df = self._get_history_cache(period=period)
action_columns = []
@@ -552,21 +571,31 @@ class PriceHistory:
if df_interval == target_interval:
return df
offset = None
origin = 'epoch' # default
if target_interval == '1wk':
resample_period = 'W-MON'
if period == 'ytd':
resample_period = '7D' # was 'W'
year_start = pd.Timestamp(f"{_datetime.datetime.now().year}-01-01")
origin = year_start.tz_localize(df.index.tz)
else:
resample_period = 'W-MON'
elif target_interval == '5d':
resample_period = '5D'
if period == 'ytd':
year_start = pd.Timestamp(f"{_datetime.datetime.now().year}-01-01")
origin = year_start.tz_localize(df.index.tz)
elif target_interval == '1mo':
resample_period = 'MS'
elif target_interval == '3mo':
resample_period = 'QS'
if period == 'ytd':
align_month = 'JAN'
else:
align_month = _datetime.datetime.now().strftime('%b').upper()
resample_period = f"QS-{align_month}"
else:
raise Exception(f"Not implemented resampling to interval '{target_interval}'")
raise ValueError(f"Not implemented resampling to interval '{target_interval}'")
resample_map = {
'Open': 'first', 'Low': 'min', 'High': 'max', 'Close': 'last',
'Volume': 'sum', 'Dividends': 'sum', 'Stock Splits': 'prod'
@@ -578,7 +607,10 @@ class PriceHistory:
if 'Capital Gains' in df.columns:
resample_map['Capital Gains'] = 'sum'
df.loc[df['Stock Splits']==0.0, 'Stock Splits'] = 1.0
df2 = df.resample(resample_period, label='left', closed='left', offset=offset).agg(resample_map)
if origin != 'epoch':
df2 = df.resample(resample_period, label='left', closed='left', origin=origin).agg(resample_map)
else:
df2 = df.resample(resample_period, label='left', closed='left', offset=offset).agg(resample_map)
df2.loc[df2['Stock Splits']==1.0, 'Stock Splits'] = 0.0
return df2
@@ -589,7 +621,7 @@ class PriceHistory:
log_extras = {'yf_cat': 'price-reconstruct', 'yf_interval': interval, 'yf_symbol': self.ticker}
if not isinstance(df, pd.DataFrame):
raise Exception("'df' must be a Pandas DataFrame not", type(df))
raise ValueError("'df' must be a Pandas DataFrame not", type(df))
if interval == "1m":
# Can't go smaller than 1m so can't reconstruct
return df
@@ -825,37 +857,39 @@ class PriceHistory:
# But in case are repairing a chunk of bad 1d data, back/forward-fill the
# good div-adjustments - not perfect, but a good backup.
div_adjusts[f_tag] = np.nan
div_adjusts = div_adjusts.ffill().bfill()
for idx in np.where(f_tag)[0]:
dt = df_new_calib.index[idx]
n = len(div_adjusts)
if df_new.loc[dt, "Dividends"] != 0:
if idx < n - 1:
# Easy, take div-adjustment from next-day
div_adjusts.iloc[idx] = div_adjusts.iloc[idx + 1]
if not div_adjusts.isna().all():
# Need some real values to calibrate
div_adjusts = div_adjusts.ffill().bfill()
for idx in np.where(f_tag)[0]:
dt = df_new_calib.index[idx]
n = len(div_adjusts)
if df_new.loc[dt, "Dividends"] != 0:
if idx < n - 1:
# Easy, take div-adjustment from next-day
div_adjusts.iloc[idx] = div_adjusts.iloc[idx + 1]
else:
# Take previous-day div-adjustment and reverse todays adjustment
div_adj = 1.0 - df_new_calib["Dividends"].iloc[idx] / df_new_calib['Close'].iloc[
idx - 1]
div_adjusts.iloc[idx] = div_adjusts.iloc[idx - 1] / div_adj
else:
# Take previous-day div-adjustment and reverse todays adjustment
div_adj = 1.0 - df_new_calib["Dividends"].iloc[idx] / df_new_calib['Close'].iloc[
idx - 1]
div_adjusts.iloc[idx] = div_adjusts.iloc[idx - 1] / div_adj
else:
if idx > 0:
# Easy, take div-adjustment from previous-day
div_adjusts.iloc[idx] = div_adjusts.iloc[idx - 1]
else:
# Must take next-day div-adjustment
div_adjusts.iloc[idx] = div_adjusts.iloc[idx + 1]
if df_new_calib["Dividends"].iloc[idx + 1] != 0:
div_adjusts.iloc[idx] *= 1.0 - df_new_calib["Dividends"].iloc[idx + 1] / \
df_new_calib['Close'].iloc[idx]
f_close_bad = df_block_calib['Close'] == tag
div_adjusts = div_adjusts.reindex(df_block.index, fill_value=np.nan).ffill().bfill()
df_new['Adj Close'] = df_block['Close'] * div_adjusts
if f_close_bad.any():
f_close_bad_new = f_close_bad.reindex(df_new.index, fill_value=False)
div_adjusts_new = div_adjusts.reindex(df_new.index, fill_value=np.nan).ffill().bfill()
div_adjusts_new_np = f_close_bad_new.to_numpy()
df_new.loc[div_adjusts_new_np, 'Adj Close'] = df_new['Close'][div_adjusts_new_np] * div_adjusts_new[div_adjusts_new_np]
if idx > 0:
# Easy, take div-adjustment from previous-day
div_adjusts.iloc[idx] = div_adjusts.iloc[idx - 1]
else:
# Must take next-day div-adjustment
div_adjusts.iloc[idx] = div_adjusts.iloc[idx + 1]
if df_new_calib["Dividends"].iloc[idx + 1] != 0:
div_adjusts.iloc[idx] *= 1.0 - df_new_calib["Dividends"].iloc[idx + 1] / \
df_new_calib['Close'].iloc[idx]
f_close_bad = df_block_calib['Close'] == tag
div_adjusts = div_adjusts.reindex(df_block.index, fill_value=np.nan).ffill().bfill()
df_new['Adj Close'] = df_block['Close'] * div_adjusts
if f_close_bad.any():
f_close_bad_new = f_close_bad.reindex(df_new.index, fill_value=False)
div_adjusts_new = div_adjusts.reindex(df_new.index, fill_value=np.nan).ffill().bfill()
div_adjusts_new_np = f_close_bad_new.to_numpy()
df_new.loc[div_adjusts_new_np, 'Adj Close'] = df_new['Close'][div_adjusts_new_np] * div_adjusts_new[div_adjusts_new_np]
# Check whether 'df_fine' has different split-adjustment.
# If different, then adjust to match 'df'
@@ -994,6 +1028,8 @@ class PriceHistory:
prices_in_subunits = False
except Exception:
# Should never happen but just-in-case
if not YfConfig.debug.hide_exceptions:
raise
pass
if prices_in_subunits:
for c in _PRICE_COLNAMES_:
@@ -1017,6 +1053,45 @@ class PriceHistory:
return df, currency2
def _dividends_convert_fx(self, dividends, fx, repair=False):
bad_div_currencies = [c for c in dividends['currency'].unique() if c != fx]
major_currencies = ['USD', 'JPY', 'EUR', 'CNY', 'GBP', 'CAD']
for c in bad_div_currencies:
fx2_tkr = None
if c == 'USD':
# Simple convert from USD to target FX
fx_tkr = f'{fx}=X'
reverse = False
elif fx == 'USD':
# Use same USD FX but reversed
fx_tkr = f'{fx}=X'
reverse = True
elif c in major_currencies and fx in major_currencies:
# Simple convert
fx_tkr = f'{c}{fx}=X'
reverse = False
else:
# No guarantee that Yahoo has direct FX conversion, so
# convert via USD
# - step 1: -> USD
fx_tkr = f'{c}=X'
reverse = True
# - step 2: USD -> FX
fx2_tkr = f'{fx}=X'
fx_dat = PriceHistory(self._data, fx_tkr, self.session)
fx_rate = fx_dat.history(period='1mo', repair=repair)['Close'].iloc[-1]
if reverse:
fx_rate = 1/fx_rate
dividends.loc[dividends['currency']==c, 'Dividends'] *= fx_rate
if fx2_tkr is not None:
fx2_dat = PriceHistory(self._data, fx2_tkr, self.session)
fx2_rate = fx2_dat.history(period='1mo', repair=repair)['Close'].iloc[-1]
dividends.loc[dividends['currency']==c, 'Dividends'] *= fx2_rate
dividends['currency'] = fx
return dividends
@utils.log_indent_decorator
def _fix_unit_mixups(self, df, interval, tz_exchange, prepost):
if df.empty:
@@ -1645,7 +1720,7 @@ class PriceHistory:
# elif k == 'div_true_date':
# div_status_df[k] = pd.Series(dtype='datetime64[ns, UTC]')
else:
raise Exception(k,v,type(v))
raise ValueError(k,v,type(v))
div_status_df.loc[dt, k] = v
checks += ['adj_missing', 'adj_exceeds_div', 'div_exceeds_adj']
@@ -1756,6 +1831,8 @@ class PriceHistory:
if not div_status_df[checks].any().any():
# Perfect
if df_modified:
if not df2_nan.empty:
df2 = pd.concat([df2, df2_nan]).sort_index()
return df2
else:
return df
@@ -1885,7 +1962,7 @@ class PriceHistory:
elif k == 'div_true_date':
div_status_df[k] = pd.Series(dtype='datetime64[ns, UTC]')
else:
raise Exception(k,v,type(v))
raise ValueError(k,v,type(v))
div_status_df.loc[dt, k] = v
if 'div_too_big' in div_status_df.columns and 'div_date_wrong' in div_status_df.columns:
# Where div_date_wrong = True, discard div_too_big. Helps with false-positive handling later.
@@ -2340,9 +2417,6 @@ class PriceHistory:
msg = f"Repaired {k}: {[str(dt.date()) for dt in sorted(div_repairs[k])]}"
logger.info(msg, extra=log_extras)
if 'Adj' in df2.columns:
raise Exception('"Adj" has snuck in df2')
if not df2_nan.empty:
df2 = pd.concat([df2, df2_nan]).sort_index()

View File

@@ -1,9 +1,10 @@
import curl_cffi
import pandas as pd
import requests
from yfinance import utils
from yfinance.config import YfConfig
from yfinance.const import _BASE_URL_
from yfinance.data import YfData
from yfinance.const import _BASE_URL_, _SENTINEL_
from yfinance.exceptions import YFDataException
_QUOTE_SUMMARY_URL_ = f"{_BASE_URL_}/v10/finance/quoteSummary"
@@ -11,12 +12,9 @@ _QUOTE_SUMMARY_URL_ = f"{_BASE_URL_}/v10/finance/quoteSummary"
class Holders:
_SCRAPE_URL_ = 'https://finance.yahoo.com/quote'
def __init__(self, data: YfData, symbol: str, proxy=_SENTINEL_):
def __init__(self, data: YfData, symbol: str):
self._data = data
self._symbol = symbol
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
data._set_proxy(proxy)
self._major = None
self._major_direct_holders = None
@@ -73,8 +71,10 @@ class Holders:
def _fetch_and_parse(self):
try:
result = self._fetch()
except requests.exceptions.HTTPError as e:
utils.get_yf_logger().error(str(e))
except curl_cffi.requests.exceptions.HTTPError as e:
if not YfConfig.debug.hide_exceptions:
raise
utils.get_yf_logger().error(str(e) + e.response.text)
self._major = pd.DataFrame()
self._major_direct_holders = pd.DataFrame()
@@ -97,6 +97,8 @@ class Holders:
self._parse_insider_holders(data.get("insiderHolders", {}))
self._parse_net_share_purchase_activity(data.get("netSharePurchaseActivity", {}))
except (KeyError, IndexError):
if not YfConfig.debug.hide_exceptions:
raise
raise YFDataException("Failed to parse holders json data.")
@staticmethod

View File

@@ -1,13 +1,13 @@
import curl_cffi
import datetime
import json
import numpy as _np
import pandas as pd
import requests
from yfinance import utils
from yfinance.config import YfConfig
from yfinance.const import quote_summary_valid_modules, _BASE_URL_, _QUERY1_URL_
from yfinance.data import YfData
from yfinance.const import quote_summary_valid_modules, _BASE_URL_, _QUERY1_URL_, _SENTINEL_
from yfinance.exceptions import YFDataException, YFException
info_retired_keys_price = {"currentPrice", "dayHigh", "dayLow", "open", "previousClose", "volume", "volume24Hr"}
@@ -26,11 +26,8 @@ _QUOTE_SUMMARY_URL_ = f"{_BASE_URL_}/v10/finance/quoteSummary"
class FastInfo:
# Contain small subset of info[] items that can be fetched faster elsewhere.
# Imitates a dict.
def __init__(self, tickerBaseObject, proxy=_SENTINEL_):
def __init__(self, tickerBaseObject):
self._tkr = tickerBaseObject
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._tkr._data._set_proxy(proxy)
self._prices_1y = None
self._prices_1wk_1h_prepost = None
@@ -106,7 +103,7 @@ class FastInfo:
def __getitem__(self, k):
if not isinstance(k, str):
raise KeyError("key must be a string")
raise KeyError(f"key must be a string not '{type(k)}'")
if k not in self._keys:
raise KeyError(f"'{k}' not valid key. Examine 'FastInfo.keys()'")
if k in self._cc_to_sc_key:
@@ -467,8 +464,6 @@ class FastInfo:
except Exception as e:
if "Cannot retrieve share count" in str(e):
shares = None
elif "failed to decrypt Yahoo" in str(e):
shares = None
else:
raise
@@ -486,12 +481,9 @@ class FastInfo:
class Quote:
def __init__(self, data: YfData, symbol: str, proxy=_SENTINEL_):
def __init__(self, data: YfData, symbol: str):
self._data = data
self._symbol = symbol
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
self._info = None
self._retired_info = None
@@ -523,6 +515,8 @@ class Quote:
try:
data = result["quoteSummary"]["result"][0]
except (KeyError, IndexError):
if not YfConfig.debug.hide_exceptions:
raise
raise YFDataException(f"Failed to parse json response from Yahoo Finance: {result}")
self._sustainability = pd.DataFrame(data)
return self._sustainability
@@ -537,6 +531,8 @@ class Quote:
try:
data = result["quoteSummary"]["result"][0]["recommendationTrend"]["trend"]
except (KeyError, IndexError):
if not YfConfig.debug.hide_exceptions:
raise
raise YFDataException(f"Failed to parse json response from Yahoo Finance: {result}")
self._recommendations = pd.DataFrame(data)
return self._recommendations
@@ -558,6 +554,8 @@ class Quote:
df.index = pd.to_datetime(df.index, unit='s')
self._upgrades_downgrades = df
except (KeyError, IndexError):
if not YfConfig.debug.hide_exceptions:
raise
raise YFDataException(f"Failed to parse json response from Yahoo Finance: {result}")
return self._upgrades_downgrades
@@ -588,8 +586,10 @@ class Quote:
params_dict = {"modules": modules, "corsDomain": "finance.yahoo.com", "formatted": "false", "symbol": self._symbol}
try:
result = self._data.get_raw_json(_QUOTE_SUMMARY_URL_ + f"/{self._symbol}", params=params_dict)
except requests.exceptions.HTTPError as e:
utils.get_yf_logger().error(str(e))
except curl_cffi.requests.exceptions.HTTPError as e:
if not YfConfig.debug.hide_exceptions:
raise
utils.get_yf_logger().error(str(e) + e.response.text)
return None
return result
@@ -597,8 +597,10 @@ class Quote:
params_dict = {"symbols": self._symbol, "formatted": "false"}
try:
result = self._data.get_raw_json(f"{_QUERY1_URL_}/v7/finance/quote?", params=params_dict)
except requests.exceptions.HTTPError as e:
utils.get_yf_logger().error(str(e))
except curl_cffi.requests.exceptions.HTTPError as e:
if not YfConfig.debug.hide_exceptions:
raise
utils.get_yf_logger().error(str(e) + e.response.text)
return None
return result
@@ -740,6 +742,8 @@ class Quote:
self._calendar['Revenue Low'] = earnings.get('revenueLow', None)
self._calendar['Revenue Average'] = earnings.get('revenueAverage', None)
except (KeyError, IndexError):
if not YfConfig.debug.hide_exceptions:
raise
raise YFDataException(f"Failed to parse json response from Yahoo Finance: {result}")

View File

@@ -165,7 +165,7 @@ class EquityQuery(QueryBase):
"""
return EQUITY_SCREENER_FIELDS
@dynamic_docstring({"valid_values_table": generate_list_table_from_dict_universal(EQUITY_SCREENER_EQ_MAP, concat_keys=['exchange'])})
@dynamic_docstring({"valid_values_table": generate_list_table_from_dict_universal(EQUITY_SCREENER_EQ_MAP, concat_keys=['exchange', 'industry'])})
@property
def valid_values(self) -> Dict:
"""

View File

@@ -1,15 +1,16 @@
import curl_cffi
from typing import Union
import warnings
from json import dumps
from yfinance.const import _QUERY1_URL_
from yfinance.data import YfData
from ..utils import dynamic_docstring, generate_list_table_from_dict_universal
from .query import EquityQuery as EqyQy
from .query import FundQuery as FndQy
from .query import QueryBase, EquityQuery, FundQuery
from yfinance.const import _QUERY1_URL_, _SENTINEL_
from yfinance.data import YfData
from ..utils import dynamic_docstring, generate_list_table_from_dict_universal, print_once
from typing import Union
import requests
_SCREENER_URL_ = f"{_QUERY1_URL_}/v1/finance/screener"
_PREDEFINED_URL_ = f"{_SCREENER_URL_}/predefined/saved"
@@ -59,7 +60,7 @@ def screen(query: Union[str, EquityQuery, FundQuery],
sortAsc: bool = None,
userId: str = None,
userIdType: str = None,
session = None, proxy = _SENTINEL_):
session = None):
"""
Run a screen: predefined query, or custom query.
@@ -111,11 +112,7 @@ def screen(query: Union[str, EquityQuery, FundQuery],
{predefined_screeners}
"""
if proxy is not _SENTINEL_:
print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
_data = YfData(session=session, proxy=proxy)
else:
_data = YfData(session=session)
_data = YfData(session=session)
# Only use defaults when user NOT give a predefined, because
# Yahoo's predefined endpoint auto-applies defaults. Also,
@@ -135,6 +132,18 @@ def screen(query: Union[str, EquityQuery, FundQuery],
if size is not None and size > 250:
raise ValueError("Yahoo limits query size to 250, reduce size.")
if offset is not None and isinstance(query, str):
# offset ignored by predefined API so switch to other API
post_query = PREDEFINED_SCREENER_QUERIES[query]
query = post_query['query']
# use predefined's attributes if user not specified
if sortField is None:
sortField = post_query['sortField']
if sortAsc is None:
sortAsc = post_query['sortType'].lower() == 'asc'
# and don't use defaults
defaults = {}
fields = {'offset': offset, 'count': count, "size": size, 'sortField': sortField, 'sortAsc': sortAsc, 'userId': userId, 'userIdType': userIdType}
params_dict = {"corsDomain": "finance.yahoo.com", "formatted": "false", "lang": "en-US", "region": "US"}
@@ -145,7 +154,7 @@ def screen(query: Union[str, EquityQuery, FundQuery],
# Switch to Yahoo's predefined endpoint
if size is not None:
print_once("YF deprecation warning: 'size' argument is deprecated for predefined screens, set 'count' instead.")
warnings.warn("Screen 'size' argument is deprecated for predefined screens, set 'count' instead.", DeprecationWarning, stacklevel=2)
count = size
size = None
fields['count'] = fields['size']
@@ -158,7 +167,7 @@ def screen(query: Union[str, EquityQuery, FundQuery],
resp = _data.get(url=_PREDEFINED_URL_, params=params_dict)
try:
resp.raise_for_status()
except requests.exceptions.HTTPError:
except curl_cffi.requests.exceptions.HTTPError:
if query not in PREDEFINED_SCREENER_QUERIES:
print(f"yfinance.screen: '{query}' is probably not a predefined query.")
raise
@@ -186,10 +195,11 @@ def screen(query: Union[str, EquityQuery, FundQuery],
elif isinstance(post_query['query'], FndQy):
post_query['quoteType'] = 'MUTUALFUND'
post_query['query'] = post_query['query'].to_dict()
data = dumps(post_query, separators=(",", ":"), ensure_ascii=False)
# Fetch
response = _data.post(_SCREENER_URL_,
body=post_query,
data=data,
params=params_dict)
response.raise_for_status()
return response.json()['finance']['result'][0]

View File

@@ -22,14 +22,16 @@
import json as _json
from . import utils
from .const import _BASE_URL_, _SENTINEL_
from .config import YfConfig
from .const import _BASE_URL_
from .data import YfData
from .exceptions import YFDataException
class Search:
def __init__(self, query, max_results=8, news_count=8, lists_count=8, include_cb=True, include_nav_links=False,
include_research=False, include_cultural_assets=False, enable_fuzzy_query=False, recommended=8,
session=None, proxy=_SENTINEL_, timeout=30, raise_errors=True):
session=None, timeout=30, raise_errors=True):
"""
Fetches and organizes search results from Yahoo Finance, including stock quotes and news articles.
@@ -51,10 +53,6 @@ class Search:
self.session = session
self._data = YfData(session=self.session)
if proxy is not _SENTINEL_:
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
self.query = query
self.max_results = max_results
self.enable_fuzzy_query = enable_fuzzy_query
@@ -103,13 +101,13 @@ class Search:
data = self._data.cache_get(url=url, params=params, timeout=self.timeout)
if data is None or "Will be right back" in data.text:
raise RuntimeError("*** YAHOO! FINANCE IS CURRENTLY DOWN! ***\n"
"Our engineers are working quickly to resolve "
"the issue. Thank you for your patience.")
raise YFDataException("*** YAHOO! FINANCE IS CURRENTLY DOWN! ***")
try:
data = data.json()
except _json.JSONDecodeError:
self._logger.error(f"{self.query}: Failed to retrieve search results and received faulty response instead.")
if not YfConfig.debug.hide_exceptions:
raise
self._logger.error(f"{self.query}: 'search' fetch received faulty data")
data = {}
self._response = data

View File

@@ -22,17 +22,17 @@
from __future__ import print_function
from collections import namedtuple as _namedtuple
from .scrapers.funds import FundsData
import pandas as _pd
from .base import TickerBase
from .const import _BASE_URL_, _SENTINEL_
from .const import _BASE_URL_
from .scrapers.funds import FundsData
class Ticker(TickerBase):
def __init__(self, ticker, session=None, proxy=_SENTINEL_):
super(Ticker, self).__init__(ticker, session=session, proxy=proxy)
def __init__(self, ticker, session=None):
super(Ticker, self).__init__(ticker, session=session)
self._expirations = {}
self._underlying = {}

View File

@@ -23,9 +23,7 @@ from __future__ import print_function
from . import Ticker, multi
from .live import WebSocket
from .utils import print_once
from .data import YfData
from .const import _SENTINEL_
class Tickers:
@@ -48,36 +46,25 @@ class Tickers:
# "Tickers", ticker_objects.keys(), rename=True
# )(*ticker_objects.values())
def history(self, period="1mo", interval="1d",
def history(self, period=None, interval="1d",
start=None, end=None, prepost=False,
actions=True, auto_adjust=True, repair=False,
proxy=_SENTINEL_,
threads=True, group_by='column', progress=True,
timeout=10, **kwargs):
if proxy is not _SENTINEL_:
print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
return self.download(
period, interval,
start, end, prepost,
actions, auto_adjust, repair,
proxy,
threads, group_by, progress,
timeout, **kwargs)
def download(self, period="1mo", interval="1d",
def download(self, period=None, interval="1d",
start=None, end=None, prepost=False,
actions=True, auto_adjust=True, repair=False,
proxy=_SENTINEL_,
threads=True, group_by='column', progress=True,
timeout=10, **kwargs):
if proxy is not _SENTINEL_:
print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
self._data._set_proxy(proxy)
data = multi.download(self.symbols,
start=start, end=end,
actions=actions,

View File

@@ -27,10 +27,11 @@ import re
import re as _re
import sys as _sys
import threading
from functools import lru_cache, wraps
from functools import wraps
from inspect import getmembers
from types import FunctionType
from typing import List, Optional
import warnings
import numpy as _np
import pandas as _pd
@@ -39,6 +40,8 @@ from dateutil.relativedelta import relativedelta
from pytz import UnknownTimeZoneError
from yfinance import const
from yfinance.exceptions import YFException
from yfinance.config import YfConfig
# From https://stackoverflow.com/a/59128615
def attributes(obj):
@@ -50,13 +53,6 @@ def attributes(obj):
if name[0] != '_' and name not in disallowed_names and hasattr(obj, name)}
@lru_cache(maxsize=20)
def print_once(msg):
# 'warnings' module suppression of repeat messages does not work.
# This function replicates correct behaviour
print(msg)
# Logging
# Note: most of this logic is adding indentation with function depth,
# so that DEBUG log is readable.
@@ -153,6 +149,12 @@ class YFLogFormatter(logging.Filter):
def get_yf_logger():
global yf_logger
global yf_log_indented
if yf_log_indented and not YfConfig.debug.logging:
_disable_debug_mode()
elif YfConfig.debug.logging and not yf_log_indented:
_enable_debug_mode()
if yf_log_indented:
yf_logger = get_indented_logger('yfinance')
elif yf_logger is None:
@@ -162,6 +164,10 @@ def get_yf_logger():
def enable_debug_mode():
warnings.warn("enable_debug_mode() is replaced by: yf.config.debug.logging = True (or False to disable)", DeprecationWarning)
_enable_debug_mode()
def _enable_debug_mode():
global yf_logger
global yf_log_indented
if not yf_log_indented:
@@ -177,22 +183,28 @@ def enable_debug_mode():
yf_log_indented = True
def _disable_debug_mode():
global yf_logger
global yf_log_indented
if yf_log_indented:
yf_logger = logging.getLogger('yfinance')
yf_logger.setLevel(logging.NOTSET)
yf_logger = None
yf_log_indented = False
def is_isin(string):
return bool(_re.match("^([A-Z]{2})([A-Z0-9]{9})([0-9])$", string))
def get_all_by_isin(isin, proxy=const._SENTINEL_, session=None):
def get_all_by_isin(isin):
if not (is_isin(isin)):
raise ValueError("Invalid ISIN number")
if proxy is not const._SENTINEL_:
print_once("YF deprecation warning: set proxy via new config function: yf.set_config(proxy=proxy)")
proxy = None
# Deferred this to prevent circular imports
from .search import Search
search = Search(query=isin, max_results=1, session=session, proxy=proxy)
search = Search(query=isin, max_results=1)
# Extract the first quote and news
ticker = search.quotes[0] if search.quotes else {}
@@ -210,18 +222,18 @@ def get_all_by_isin(isin, proxy=const._SENTINEL_, session=None):
}
def get_ticker_by_isin(isin, proxy=const._SENTINEL_, session=None):
data = get_all_by_isin(isin, proxy, session)
def get_ticker_by_isin(isin):
data = get_all_by_isin(isin)
return data.get('ticker', {}).get('symbol', '')
def get_info_by_isin(isin, proxy=const._SENTINEL_, session=None):
data = get_all_by_isin(isin, proxy, session)
def get_info_by_isin(isin):
data = get_all_by_isin(isin)
return data.get('ticker', {})
def get_news_by_isin(isin, proxy=const._SENTINEL_, session=None):
data = get_all_by_isin(isin, proxy, session)
def get_news_by_isin(isin):
data = get_all_by_isin(isin)
return data.get('news', {})
@@ -410,20 +422,23 @@ def snake_case_2_camelCase(s):
return sc
def _parse_user_dt(dt, exchange_tz):
def _parse_user_dt(dt, exchange_tz=_tz.utc):
if isinstance(dt, int):
# Should already be epoch, test with conversion:
_datetime.datetime.fromtimestamp(dt)
dt = _pd.Timestamp(dt, unit="s", tz=exchange_tz)
else:
# Convert str/date -> datetime, set tzinfo=exchange, get timestamp:
if isinstance(dt, str):
dt = _datetime.datetime.strptime(str(dt), '%Y-%m-%d')
if isinstance(dt, _datetime.date) and not isinstance(dt, _datetime.datetime):
dt = _datetime.datetime.combine(dt, _datetime.time(0))
if isinstance(dt, _datetime.datetime) and dt.tzinfo is None:
# Assume user is referring to exchange's timezone
dt = _tz.timezone(exchange_tz).localize(dt)
dt = int(dt.timestamp())
if isinstance(dt, _datetime.datetime):
if dt.tzinfo is None:
# Assume user is referring to exchange's timezone
dt = _pd.Timestamp(dt).tz_localize(exchange_tz)
else:
dt = _pd.Timestamp(dt).tz_convert(exchange_tz)
else: # if we reached here, then it hasn't been any known type
raise ValueError(f"Unable to parse input dt {dt} of type {type(dt)}")
return dt
@@ -530,7 +545,10 @@ def parse_actions(data):
dividends.set_index("date", inplace=True)
dividends.index = _pd.to_datetime(dividends.index, unit="s")
dividends.sort_index(inplace=True)
dividends.columns = ["Dividends"]
if 'currency' in dividends.columns and (dividends['currency'] == '').all():
# Currency column useless, drop it.
dividends = dividends.drop('currency', axis=1)
dividends = dividends.rename(columns={'amount': 'Dividends'})
if "capitalGains" in data["events"] and len(data["events"]['capitalGains']) > 0:
capital_gains = _pd.DataFrame(
@@ -583,7 +601,8 @@ def fix_Yahoo_returning_prepost_unrequested(quotes, interval, tradingPeriods):
quotes.index = idx
# "end" = end of regular trading hours (including any auction)
f_drop = quotes.index >= quotes["end"]
f_drop = f_drop | (quotes.index < quotes["start"])
td = _interval_to_timedelta(interval)
f_drop = f_drop | (quotes.index + td <= quotes["start"])
if f_drop.any():
# When printing report, ignore rows that were already NaNs:
# f_na = quotes[["Open","Close"]].isna().all(axis=1)
@@ -707,14 +726,10 @@ def fix_Yahoo_returning_live_separate(quotes, interval, tz_exchange, prepost, re
def safe_merge_dfs(df_main, df_sub, interval):
if df_sub.empty:
raise Exception("No data to merge")
if df_main.empty:
return df_main
data_cols = [c for c in df_sub.columns if c not in df_main]
if len(data_cols) > 1:
raise Exception("Expected 1 data col")
data_col = data_cols[0]
df_main = df_main.sort_index()
@@ -755,6 +770,13 @@ def safe_merge_dfs(df_main, df_sub, interval):
if df_sub.empty:
df_main['Dividends'] = 0.0
return df_main
# df_sub changed so recalc indices:
df_main['_date'] = df_main.index.date
df_sub['_date'] = df_sub.index.date
indices = _np.searchsorted(_np.append(df_main['_date'], [df_main['_date'].iloc[-1]+td]), df_sub['_date'], side='left')
df_main = df_main.drop('_date', axis=1)
df_sub = df_sub.drop('_date', axis=1)
else:
empty_row_data = {**{c:[_np.nan] for c in const._PRICE_COLNAMES_}, 'Volume':[0]}
if interval == '1d':
@@ -791,7 +813,7 @@ def safe_merge_dfs(df_main, df_sub, interval):
f_outOfRange = indices == -1
if f_outOfRange.any():
if intraday or interval in ['1d', '1wk']:
raise Exception(f"The following '{data_col}' events are out-of-range, did not expect with interval {interval}: {df_sub.index[f_outOfRange]}")
raise YFException(f"The following '{data_col}' events are out-of-range, did not expect with interval {interval}: {df_sub.index[f_outOfRange]}")
get_yf_logger().debug(f'Discarding these {data_col} events:' + '\n' + str(df_sub[f_outOfRange]))
df_sub = df_sub[~f_outOfRange].copy()
indices = indices[~f_outOfRange]
@@ -813,7 +835,7 @@ def safe_merge_dfs(df_main, df_sub, interval):
df = df.groupby("_NewIndex").prod()
df.index.name = None
else:
raise Exception(f"New index contains duplicates but unsure how to aggregate for '{data_col_name}'")
raise YFException(f"New index contains duplicates but unsure how to aggregate for '{data_col_name}'")
if "_NewIndex" in df.columns:
df = df.drop("_NewIndex", axis=1)
return df
@@ -825,7 +847,7 @@ def safe_merge_dfs(df_main, df_sub, interval):
f_na = df[data_col].isna()
data_lost = sum(~f_na) < df_sub.shape[0]
if data_lost:
raise Exception('Data was lost in merge, investigate')
raise YFException('Data was lost in merge, investigate')
return df

View File

@@ -1 +1 @@
version = "0.2.59"
version = "1.0"