Skip to content
New issue

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

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

Already on GitHub? # to your account

Added auto_adjust flag to YfinanceDataProvider.get_history #208

Merged
merged 3 commits into from
Feb 18, 2025
Merged
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
6 changes: 5 additions & 1 deletion qf_lib/data_providers/yfinance/yfinance_data_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ def price_field_to_str_map(self, *args) -> Dict[PriceField, str]:

def get_history(self, tickers: Union[YFinanceTicker, Sequence[YFinanceTicker]], fields: Union[None, str, Sequence[str]],
start_date: datetime, end_date: datetime = None, frequency: Frequency = None,
look_ahead_bias: bool = False, **kwargs) -> Union[QFSeries, QFDataFrame, QFDataArray]:
look_ahead_bias: bool = False, auto_adjust: bool = True, **kwargs) \
-> Union[QFSeries, QFDataFrame, QFDataArray]:
"""
Gets historical attributes (fields) of different securities (tickers).

Expand All @@ -87,6 +88,8 @@ def get_history(self, tickers: Union[YFinanceTicker, Sequence[YFinanceTicker]],
frequencies at the following intervals: 60, 30, 15, 5 and 1 minute.
look_ahead_bias: bool
if set to False, the look-ahead bias will be taken care of to make sure no future data is returned
auto_adjust: bool
Adjust back all OHLC prices automatically. Default values is True.

Returns
-------
Expand Down Expand Up @@ -117,6 +120,7 @@ def get_history(self, tickers: Union[YFinanceTicker, Sequence[YFinanceTicker]],
tickers_str = [t.as_string() for t in tickers]
df = yf.download(list(set(tickers_str)), start_date, end_date, keepna=True,
interval=self._frequency_to_period(frequency),
auto_adjust=auto_adjust,
progress=False)
df = df.reindex(columns=MultiIndex.from_product([fields, tickers_str]))
values = df.values.reshape(len(df), len(tickers), len(fields))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def assert_equal(result, expected_value, decimals=2):
)
def test_get_history__daily__real_timer(tickers, fields, start_date, end_date, expected_values, data_provider):
result = data_provider.get_history(YFinanceTicker.from_string(tickers), fields, str_to_date(start_date),
str_to_date(end_date))
str_to_date(end_date), auto_adjust=False)
assert_equal(result, expected_values)


Expand All @@ -107,7 +107,7 @@ def test_get_history__daily__real_timer(tickers, fields, start_date, end_date, e
)
def test_incorrect_inputs(tickers, fields, start_date, end_date, expected_values, data_provider):
result = data_provider.get_history(YFinanceTicker.from_string(tickers), fields, str_to_date(start_date),
str_to_date(end_date))
str_to_date(end_date), auto_adjust=False)

assert_equal(result, expected_values)

Expand All @@ -123,7 +123,8 @@ def test_incorrect_inputs(tickers, fields, start_date, end_date, expected_values
)
def test_get_history__various_frequencies_real_timer(tickers, fields, start_date, end_date, frequency,
expected_values, data_provider):
result = data_provider.get_history(YFinanceTicker.from_string(tickers), fields, start_date, end_date, frequency)
result = data_provider.get_history(YFinanceTicker.from_string(tickers), fields, start_date, end_date, frequency,
auto_adjust=False)
assert_equal(result, expected_values)


Expand Down Expand Up @@ -151,5 +152,6 @@ def test_get_history__settable_timer(tickers, fields, start_date, end_date, freq
MarketOpenEvent.set_trigger_time({"hour": 9, "minute": 30, "second": 0, "microsecond": 0})

data_provider.timer = SettableTimer(current_time)
result = data_provider.get_history(YFinanceTicker.from_string(tickers), fields, start_date, end_date, frequency)
result = data_provider.get_history(YFinanceTicker.from_string(tickers), fields, start_date, end_date, frequency,
auto_adjust=False)
assert_equal(result, expected_values)
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def data_provider():
def mock_daily_yfinance_download():
""" Fixture to mock yfinance.download to return hardcoded data. """

def _mock_download(tickers, start, end, interval, keepna, progress):
def _mock_download(tickers, start, end, **kwargs):
# Hardcoded data for testing
data = {
"AAPL": pd.DataFrame({
Expand Down