Skip to content

Commit 2ad2977

Browse files
committed
Update the historical model overriding yfinance messages and ensuring Adj Close is reported
1 parent dd1b122 commit 2ad2977

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

financetoolkit/historical_model.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@
2424
ENABLE_TQDM = False
2525

2626
try:
27+
import logging
28+
2729
import yfinance as yf
2830

31+
logging.disable(logging.ERROR)
32+
2933
ENABLE_YFINANCE = True
3034
except ImportError:
3135
ENABLE_YFINANCE = False
@@ -497,12 +501,15 @@ def get_historical_data_from_yahoo_finance(
497501
interval = "1d"
498502

499503
try:
500-
historical_data = yf.Ticker(ticker).history(
504+
historical_data = yf.download(
505+
tickers=ticker,
506+
progress=False,
507+
actions=True,
501508
start=start_timestamp,
502509
end=end_timestamp,
503510
interval=interval,
504-
)
505-
except (HTTPError, URLError, RemoteDisconnected):
511+
).droplevel(level=1, axis=1)
512+
except (HTTPError, URLError, RemoteDisconnected, IndexError):
506513
return pd.DataFrame()
507514

508515
if historical_data.loc[start:end].empty:
@@ -526,8 +533,6 @@ def get_historical_data_from_yahoo_finance(
526533
~historical_data.index.duplicated(keep="first")
527534
]
528535

529-
historical_data["Adj Close"] = historical_data["Close"]
530-
531536
if "Stock Splits" in historical_data and "Capital Gains" in historical_data:
532537
historical_data = historical_data.drop(
533538
columns=["Stock Splits", "Capital Gains"]
@@ -537,6 +542,10 @@ def get_historical_data_from_yahoo_finance(
537542
else:
538543
historical_data = historical_data.drop(columns=["Capital Gains"])
539544

545+
historical_data = historical_data[
546+
["Open", "High", "Low", "Close", "Adj Close", "Volume", "Dividends"]
547+
]
548+
540549
historical_data = enrich_historical_data(
541550
historical_data=historical_data,
542551
start=start,

0 commit comments

Comments
 (0)