Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config #2216

Open
wants to merge 23 commits into
base: dev
Choose a base branch
from
Open

Config #2216

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions doc/source/advanced/config.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
======
Config
======

`yfinance` allows you to set configurations for requests.

You can set:
* proxy
* timeout
* lang
* region
* session
* url

.. code-block:: python

yf.set_config(proxy=None, timeout=30, lang="en-US", region="US", session=None, url="finance.yahoo.com")

.. important::
DO NOT ADD `https://` to start of url. This will cause an error.
3 changes: 2 additions & 1 deletion doc/source/advanced/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ Advanced
logging
proxy
caching
multi_level_columns
multi_level_columns
config
11 changes: 0 additions & 11 deletions doc/source/advanced/proxy.rst

This file was deleted.

6 changes: 6 additions & 0 deletions doc/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ yfinance documentation
Download Market Data from Yahoo! Finance's API
----------------------------------------------

.. warning::

`session`, `proxy`, and `timeout` are deprecated.
Use `yfinance.set_config()` instead.
See :doc: `advanced/config` for more information.

.. admonition:: IMPORTANT LEGAL DISCLAIMER

**Yahoo!, Y!Finance, and Yahoo! finance are registered trademarks of Yahoo, Inc.**
Expand Down
36 changes: 19 additions & 17 deletions tests/test_price_repair.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class TestPriceRepairAssumptions(unittest.TestCase):

@classmethod
def setUpClass(cls):
cls.session = session_gbl
config = yf.set_config(session=session_gbl)
cls.session = config["session"]
cls.dp = os.path.dirname(__file__)

@classmethod
Expand All @@ -24,7 +25,7 @@ def tearDownClass(cls):

def test_resampling(self):
for tkr in ['GOOGL', 'GLEN.L', '2330.TW']:
dat = yf.Ticker(tkr, session=self.session)
dat = yf.Ticker(tkr)

intervals = ['1d', '1wk', '1mo', '3mo']
periods = ['1d', '5d', '1mo', '3mo', '6mo', '1y', '2y', '5y', '10y', 'ytd']#, 'max']
Expand Down Expand Up @@ -94,7 +95,8 @@ class TestPriceRepair(unittest.TestCase):

@classmethod
def setUpClass(cls):
cls.session = session_gbl
config = yf.set_config(session=session_gbl)
cls.session = config["session"]
cls.dp = os.path.dirname(__file__)

@classmethod
Expand All @@ -104,7 +106,7 @@ def tearDownClass(cls):

def test_types(self):
tkr = 'INTC'
dat = yf.Ticker(tkr, session=self.session)
dat = yf.Ticker(tkr)

data = dat.history(period="3mo", interval="1d", prepost=True, repair=True)
self.assertIsInstance(data, _pd.DataFrame, "data has wrong type")
Expand All @@ -128,15 +130,15 @@ def test_reconstruct_2m(self):
dt_now = dt_now.ceil("1h")

for tkr in tkrs:
dat = yf.Ticker(tkr, session=self.session)
dat = yf.Ticker(tkr)
end_dt = dt_now
start_dt = end_dt - td_60d
dat.history(start=start_dt, end=end_dt, interval="2m", repair=True)

def test_repair_100x_random_weekly(self):
# Setup:
tkr = "PNL.L"
dat = yf.Ticker(tkr, session=self.session)
dat = yf.Ticker(tkr)
tz_exchange = dat.fast_info["timezone"]
hist = dat._lazy_load_price_history()

Expand Down Expand Up @@ -191,7 +193,7 @@ def test_repair_100x_random_weekly_preSplit(self):
# PNL.L has a stock-split in 2022. Sometimes requesting data before 2022 is not split-adjusted.

tkr = "PNL.L"
dat = yf.Ticker(tkr, session=self.session)
dat = yf.Ticker(tkr)
tz_exchange = dat.fast_info["timezone"]
hist = dat._lazy_load_price_history()

Expand Down Expand Up @@ -250,7 +252,7 @@ def test_repair_100x_random_weekly_preSplit(self):

def test_repair_100x_random_daily(self):
tkr = "PNL.L"
dat = yf.Ticker(tkr, session=self.session)
dat = yf.Ticker(tkr)
tz_exchange = dat.fast_info["timezone"]
hist = dat._lazy_load_price_history()

Expand Down Expand Up @@ -308,7 +310,7 @@ def test_repair_100x_block_daily(self):

for tkr in tkrs:
for interval in intervals:
dat = yf.Ticker(tkr, session=self.session)
dat = yf.Ticker(tkr)
tz_exchange = dat.fast_info["timezone"]
hist = dat._lazy_load_price_history()

Expand Down Expand Up @@ -357,7 +359,7 @@ def test_repair_100x_block_daily(self):

def test_repair_zeroes_daily(self):
tkr = "BBIL.L"
dat = yf.Ticker(tkr, session=self.session)
dat = yf.Ticker(tkr)
hist = dat._lazy_load_price_history()
tz_exchange = dat.fast_info["timezone"]

Expand Down Expand Up @@ -405,7 +407,7 @@ def test_repair_zeroes_daily_adjClose(self):
_dt.datetime(2023, 2, 2)]))
df = df.sort_index()
df.index.name = "Date"
dat = yf.Ticker(tkr, session=self.session)
dat = yf.Ticker(tkr)
tz_exchange = dat.fast_info["timezone"]
df.index = df.index.tz_localize(tz_exchange)
hist = dat._lazy_load_price_history()
Expand All @@ -425,7 +427,7 @@ def test_repair_zeroes_daily_adjClose(self):

def test_repair_zeroes_hourly(self):
tkr = "INTC"
dat = yf.Ticker(tkr, session=self.session)
dat = yf.Ticker(tkr)
tz_exchange = dat.fast_info["timezone"]
hist = dat._lazy_load_price_history()

Expand Down Expand Up @@ -467,7 +469,7 @@ def test_repair_bad_stock_splits(self):
intervals = ['1d', '1wk', '1mo', '3mo']
for tkr in good_tkrs:
for interval in intervals:
dat = yf.Ticker(tkr, session=self.session)
dat = yf.Ticker(tkr)
tz_exchange = dat.fast_info["timezone"]
hist = dat._lazy_load_price_history()

Expand All @@ -491,7 +493,7 @@ def test_repair_bad_stock_splits(self):
bad_tkrs = ['4063.T', 'ALPHA.PA', 'AV.L', 'CNE.L', 'MOB.ST', 'SPM.MI']
bad_tkrs.append('LA.V') # special case - stock split error is 3 years ago! why not fixed?
for tkr in bad_tkrs:
dat = yf.Ticker(tkr, session=self.session)
dat = yf.Ticker(tkr)
tz_exchange = dat.fast_info["timezone"]
hist = dat._lazy_load_price_history()

Expand Down Expand Up @@ -531,7 +533,7 @@ def test_repair_bad_stock_splits(self):
intervals = ['1wk']
for tkr in sketchy_tkrs:
for interval in intervals:
dat = yf.Ticker(tkr, session=self.session)
dat = yf.Ticker(tkr)
tz_exchange = dat.fast_info["timezone"]
hist = dat._lazy_load_price_history()

Expand Down Expand Up @@ -613,7 +615,7 @@ def test_repair_bad_div_adjusts(self):

for tkr in false_positives:
# Nothing should change
dat = yf.Ticker(tkr, session=self.session)
dat = yf.Ticker(tkr)
hist = dat._lazy_load_price_history()
hist.history(period='1mo') # init metadata for currency
currency = hist._history_metadata['currency']
Expand Down Expand Up @@ -645,7 +647,7 @@ def test_repair_bad_div_adjusts(self):
raise

for tkr in bad_tkrs:
dat = yf.Ticker(tkr, session=self.session)
dat = yf.Ticker(tkr)
hist = dat._lazy_load_price_history()
hist.history(period='1mo') # init metadata for currency
currency = hist._history_metadata['currency']
Expand Down
44 changes: 22 additions & 22 deletions tests/test_prices.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class TestPriceHistory(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.session = session_gbl
cls.session = yf.set_config(session=session_gbl)["session"]

@classmethod
def tearDownClass(cls):
Expand All @@ -23,7 +23,7 @@ def test_daily_index(self):
tkrs = ["BHP.AX", "IMP.JO", "BP.L", "PNL.L", "INTC"]
intervals = ["1d", "1wk", "1mo"]
for tkr in tkrs:
dat = yf.Ticker(tkr, session=self.session)
dat = yf.Ticker(tkr)

for interval in intervals:
df = dat.history(period="5y", interval=interval)
Expand Down Expand Up @@ -64,7 +64,7 @@ def test_download_with_invalid_ticker(self):
def test_duplicatingHourly(self):
tkrs = ["IMP.JO", "BHG.JO", "SSW.JO", "BP.L", "INTC"]
for tkr in tkrs:
dat = yf.Ticker(tkr, session=self.session)
dat = yf.Ticker(tkr)
tz = dat._get_ticker_tz(proxy=None, timeout=None)

dt_utc = _pd.Timestamp.utcnow()
Expand All @@ -84,7 +84,7 @@ def test_duplicatingDaily(self):
tkrs = ["IMP.JO", "BHG.JO", "SSW.JO", "BP.L", "INTC"]
test_run = False
for tkr in tkrs:
dat = yf.Ticker(tkr, session=self.session)
dat = yf.Ticker(tkr)
tz = dat._get_ticker_tz(proxy=None, timeout=None)

dt_utc = _pd.Timestamp.utcnow()
Expand All @@ -110,7 +110,7 @@ def test_duplicatingWeekly(self):
tkrs = ['MSFT', 'IWO', 'VFINX', '^GSPC', 'BTC-USD']
test_run = False
for tkr in tkrs:
dat = yf.Ticker(tkr, session=self.session)
dat = yf.Ticker(tkr)
tz = dat._get_ticker_tz(proxy=None, timeout=None)

dt = _tz.timezone(tz).localize(_dt.datetime.now())
Expand All @@ -136,7 +136,7 @@ def test_pricesEventsMerge(self):
tkr = 'INTC'
start_d = _dt.date(2022, 1, 1)
end_d = _dt.date(2023, 1, 1)
df = yf.Ticker(tkr, session=self.session).history(interval='1d', start=start_d, end=end_d)
df = yf.Ticker(tkr).history(interval='1d', start=start_d, end=end_d)
div = 1.0
future_div_dt = df.index[-1] + _dt.timedelta(days=1)
if future_div_dt.weekday() in [5, 6]:
Expand Down Expand Up @@ -173,14 +173,14 @@ def test_intraDayWithEvents(self):
for tkr in tkrs:
start_d = _dt.date.today() - _dt.timedelta(days=59)
end_d = None
df_daily = yf.Ticker(tkr, session=self.session).history(start=start_d, end=end_d, interval="1d", actions=True)
df_daily = yf.Ticker(tkr).history(start=start_d, end=end_d, interval="1d", actions=True)
df_daily_divs = df_daily["Dividends"][df_daily["Dividends"] != 0]
if df_daily_divs.shape[0] == 0:
continue

start_d = df_daily_divs.index[0].date()
end_d = df_daily_divs.index[-1].date() + _dt.timedelta(days=1)
df_intraday = yf.Ticker(tkr, session=self.session).history(start=start_d, end=end_d, interval="15m", actions=True)
df_intraday = yf.Ticker(tkr).history(start=start_d, end=end_d, interval="15m", actions=True)
self.assertTrue((df_intraday["Dividends"] != 0.0).any())

df_intraday_divs = df_intraday["Dividends"][df_intraday["Dividends"] != 0]
Expand All @@ -200,14 +200,14 @@ def test_intraDayWithEvents_tase(self):
for tkr in tase_tkrs:
start_d = _dt.date.today() - _dt.timedelta(days=59)
end_d = None
df_daily = yf.Ticker(tkr, session=self.session).history(start=start_d, end=end_d, interval="1d", actions=True)
df_daily = yf.Ticker(tkr).history(start=start_d, end=end_d, interval="1d", actions=True)
df_daily_divs = df_daily["Dividends"][df_daily["Dividends"] != 0]
if df_daily_divs.shape[0] == 0:
continue

start_d = df_daily_divs.index[0].date()
end_d = df_daily_divs.index[-1].date() + _dt.timedelta(days=1)
df_intraday = yf.Ticker(tkr, session=self.session).history(start=start_d, end=end_d, interval="15m", actions=True)
df_intraday = yf.Ticker(tkr).history(start=start_d, end=end_d, interval="15m", actions=True)
self.assertTrue((df_intraday["Dividends"] != 0.0).any())

df_intraday_divs = df_intraday["Dividends"][df_intraday["Dividends"] != 0]
Expand All @@ -231,7 +231,7 @@ def test_dailyWithEvents(self):
_dt.date(2022, 2, 4)]}

for tkr, dates in tkr_div_dates.items():
df = yf.Ticker(tkr, session=self.session).history(interval='1d', start=start_d, end=end_d)
df = yf.Ticker(tkr).history(interval='1d', start=start_d, end=end_d)
df_divs = df[df['Dividends'] != 0].sort_index(ascending=False)
try:
self.assertTrue((df_divs.index.date == dates).all())
Expand Down Expand Up @@ -265,8 +265,8 @@ def test_dailyWithEvents_bugs(self):
# Test that index same with and without events:
tkrs = [tkr1, tkr2]
for tkr in tkrs:
df1 = yf.Ticker(tkr, session=self.session).history(start=start_d, end=end_d, interval="1d", actions=True)
df2 = yf.Ticker(tkr, session=self.session).history(start=start_d, end=end_d, interval="1d", actions=False)
df1 = yf.Ticker(tkr).history(start=start_d, end=end_d, interval="1d", actions=True)
df2 = yf.Ticker(tkr).history(start=start_d, end=end_d, interval="1d", actions=False)
self.assertTrue(((df1["Dividends"] > 0) | (df1["Stock Splits"] > 0)).any())
try:
self.assertTrue(df1.index.equals(df2.index))
Expand Down Expand Up @@ -308,8 +308,8 @@ def test_weeklyWithEvents(self):
# Test that index same with and without events:
tkrs = [tkr1, tkr2]
for tkr in tkrs:
df1 = yf.Ticker(tkr, session=self.session).history(start=start_d, end=end_d, interval="1wk", actions=True)
df2 = yf.Ticker(tkr, session=self.session).history(start=start_d, end=end_d, interval="1wk", actions=False)
df1 = yf.Ticker(tkr).history(start=start_d, end=end_d, interval="1wk", actions=True)
df2 = yf.Ticker(tkr).history(start=start_d, end=end_d, interval="1wk", actions=False)
self.assertTrue(((df1["Dividends"] > 0) | (df1["Stock Splits"] > 0)).any())
try:
self.assertTrue(df1.index.equals(df2.index))
Expand Down Expand Up @@ -341,8 +341,8 @@ def test_monthlyWithEvents(self):
# Test that index same with and without events:
tkrs = [tkr1, tkr2]
for tkr in tkrs:
df1 = yf.Ticker(tkr, session=self.session).history(start=start_d, end=end_d, interval="1mo", actions=True)
df2 = yf.Ticker(tkr, session=self.session).history(start=start_d, end=end_d, interval="1mo", actions=False)
df1 = yf.Ticker(tkr).history(start=start_d, end=end_d, interval="1mo", actions=True)
df2 = yf.Ticker(tkr).history(start=start_d, end=end_d, interval="1mo", actions=False)
self.assertTrue(((df1["Dividends"] > 0) | (df1["Stock Splits"] > 0)).any())
try:
self.assertTrue(df1.index.equals(df2.index))
Expand All @@ -365,7 +365,7 @@ def test_monthlyWithEvents2(self):
def test_tz_dst_ambiguous(self):
# Reproduce issue #1100
try:
yf.Ticker("ESLT.TA", session=self.session).history(start="2002-10-06", end="2002-10-09", interval="1d")
yf.Ticker("ESLT.TA").history(start="2002-10-06", end="2002-10-09", interval="1d")
except _tz.exceptions.AmbiguousTimeError:
raise Exception("Ambiguous DST issue not resolved")

Expand All @@ -378,7 +378,7 @@ def test_dst_fix(self):
# The correction is successful if no days are weekend, and weekly data begins Monday

tkr = "AGRO3.SA"
dat = yf.Ticker(tkr, session=self.session)
dat = yf.Ticker(tkr)
start = "2021-01-11"
end = "2022-11-05"

Expand All @@ -403,7 +403,7 @@ def test_prune_post_intraday_us(self):
tkr = "AMZN"
special_day = _dt.date(2023, 11, 24)
time_early_close = _dt.time(13)
dat = yf.Ticker(tkr, session=self.session)
dat = yf.Ticker(tkr)

# Run
start_d = special_day - _dt.timedelta(days=7)
Expand All @@ -424,7 +424,7 @@ def test_prune_post_intraday_asx(self):
# Setup
tkr = "BHP.AX"
# No early closes in 2023
dat = yf.Ticker(tkr, session=self.session)
dat = yf.Ticker(tkr)

# Test no other afternoons (or mornings) were pruned
start_d = _dt.date(2023, 1, 1)
Expand All @@ -446,7 +446,7 @@ def test_weekly_2rows_fix(self):
def test_aggregate_capital_gains(self):
# Setup
tkr = "FXAIX"
dat = yf.Ticker(tkr, session=self.session)
dat = yf.Ticker(tkr)
start = "2017-12-31"
end = "2019-12-31"
interval = "3mo"
Expand Down
Loading