fix(dataflows): include the analysis day in the Alpha Vantage news window

- time_to was midnight starting the end date, dropping that day's news
This commit is contained in:
Yijia-Xiao
2026-09-17 23:37:43 +00:00
parent c3bb991974
commit 04b691804c
3 changed files with 27 additions and 5 deletions
@@ -34,8 +34,13 @@ def get_api_key() -> str:
)
return api_key
def format_datetime_for_api(date_input) -> str:
"""Convert various date formats to YYYYMMDDTHHMM format required by Alpha Vantage API."""
def format_datetime_for_api(date_input, end_of_day: bool = False) -> str:
"""Convert various date formats to the YYYYMMDDTHHMM Alpha Vantage expects.
A plain date means midnight, which is the start of that day. For the end of
a window pass ``end_of_day`` so the day itself is inside it, rather than
dropping everything published on the analysis date.
"""
if isinstance(date_input, str):
# If already in correct format, return as-is
if len(date_input) == 13 and 'T' in date_input:
@@ -43,7 +48,7 @@ def format_datetime_for_api(date_input) -> str:
# Try to parse common date formats
try:
dt = datetime.strptime(date_input, "%Y-%m-%d")
return dt.strftime("%Y%m%dT0000")
return dt.strftime("%Y%m%dT2359" if end_of_day else "%Y%m%dT0000")
except ValueError:
try:
dt = datetime.strptime(date_input, "%Y-%m-%d %H:%M")
@@ -21,7 +21,7 @@ def get_news(ticker, start_date, end_date) -> dict[str, str] | str:
params = {
"tickers": ticker,
"time_from": format_datetime_for_api(start_date),
"time_to": format_datetime_for_api(end_date),
"time_to": format_datetime_for_api(end_date, end_of_day=True),
}
return _make_api_request("NEWS_SENTIMENT", params)
@@ -56,7 +56,7 @@ def get_global_news(curr_date, look_back_days: int | None = None, limit: int | N
params = {
"topics": "financial_markets,economy_macro,economy_monetary",
"time_from": format_datetime_for_api(start_date),
"time_to": format_datetime_for_api(curr_date),
"time_to": format_datetime_for_api(curr_date, end_of_day=True),
"limit": str(limit),
}