Skip to content

Commit

Permalink
Update renaming of columns from yfinance and passing auto_adjust as o…
Browse files Browse the repository at this point in the history
…ptional parameter (#1301)

* Update renaming of columns from yfinance

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fix spelling with volume column

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
kuds and pre-commit-ci[bot] authored Dec 29, 2024
1 parent 7aea2b9 commit b632d6e
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions finrl/meta/preprocessor/yahoodownloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __init__(self, start_date: str, end_date: str, ticker_list: list):
self.end_date = end_date
self.ticker_list = ticker_list

def fetch_data(self, proxy=None) -> pd.DataFrame:
def fetch_data(self, proxy=None, auto_adjust=False) -> pd.DataFrame:
"""Fetches data from Yahoo API
Parameters
----------
Expand All @@ -49,7 +49,11 @@ def fetch_data(self, proxy=None) -> pd.DataFrame:
num_failures = 0
for tic in self.ticker_list:
temp_df = yf.download(
tic, start=self.start_date, end=self.end_date, proxy=proxy
tic,
start=self.start_date,
end=self.end_date,
proxy=proxy,
auto_adjust=auto_adjust,
)
if temp_df.columns.nlevels != 1:
temp_df.columns = temp_df.columns.droplevel(1)
Expand All @@ -65,16 +69,20 @@ def fetch_data(self, proxy=None) -> pd.DataFrame:
data_df = data_df.reset_index()
try:
# convert the column names to standardized names
data_df.columns = [
"date",
"open",
"high",
"low",
"close",
"adjcp",
"volume",
"tic",
]
data_df.rename(
columns={
"Date": "date",
"Adj Close": "adjcp",
"Close": "close",
"High": "high",
"Low": "low",
"Volume": "volume",
"Open": "open",
"tic": "tic",
},
inplace=True,
)

# use adjusted close price instead of close price
data_df["close"] = data_df["adjcp"]
# drop the adjusted close price column
Expand Down

0 comments on commit b632d6e

Please sign in to comment.