From 7fd3de1f333e1b6f194b4044081eae0fc269ac66 Mon Sep 17 00:00:00 2001 From: Marcus Read Date: Fri, 18 Oct 2024 15:16:37 +0100 Subject: [PATCH] Fix bug filling most recent sessions for funds Fixes bug where `base.fill_reindexed_daily` was forward filling prices for the most recent sessions of a fund even if those sessions would not be expected to have prices available given long delays in publishing price data (which for some funds can stretch to various days). With fix, `base.fill_reindexed_daily` now looks at sesions prior to the most recent whenever the most recent (or prior) session would not be expected to have price data yet available. --- src/market_prices/prices/base.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/market_prices/prices/base.py b/src/market_prices/prices/base.py index c3d9be6..490a346 100644 --- a/src/market_prices/prices/base.py +++ b/src/market_prices/prices/base.py @@ -179,10 +179,20 @@ def fill_reindexed_daily( if not na_rows.any(): return df, warnings - if na_rows.iloc[-1] and helpers.now() <= cal.session_open(df.index[-1]) + delay: - na_rows.iloc[-1] = False - if not na_rows.any(): - return df, warnings + # do not fill prices for any most recent sessions for which prices would not be + # expected to be available. NB this considers more than one session as for some + # funds prices many not be available for a number of days (in which case can pass + # the corresponding delay as a very high value). + i = 1 + len_df = len(df) + while i <= len_df: + if na_rows.iloc[-i] and helpers.now() <= cal.session_open(df.index[-i]) + delay: + na_rows.iloc[-i] = False + if not na_rows.any(): + return df, warnings + else: + break + i += 1 # fill adj_close = df["close"].ffill()