Skip to content
Python

refactor(dataflows): decide in one place whether a run is dated in the past

Yijia-Xiao660966d168b9
diff --git a/tests/test_undated_tools_as_of.py b/tests/test_undated_tools_as_of.py
index 3252f928a..a75a7276c 100644
--- a/tests/test_undated_tools_as_of.py
+++ b/tests/test_undated_tools_as_of.py
@@ -14,6 +14,7 @@ import pandas as pd
import pytest
from tradingagents.agents import tools
+from tradingagents.dataflows.date_window import get_current_date
from tradingagents.dataflows.vendors import polymarket
from tradingagents.dataflows.vendors.alpha_vantage import news as alpha_vantage_news
from tradingagents.dataflows.vendors.yahoo import (
@@ -77,7 +78,7 @@ def test_polymarket_withholds_live_odds_from_a_historical_run():
@pytest.mark.unit
def test_polymarket_serves_a_current_run():
with mock.patch.object(polymarket, "_request", return_value={"events": []}) as req:
- polymarket.get_prediction_markets("Fed rate cut", as_of_date=polymarket.get_current_date())
+ polymarket.get_prediction_markets("Fed rate cut", as_of_date=get_current_date())
req.assert_called_once()
diff --git a/tradingagents/agents/context.py b/tradingagents/agents/context.py
index c3eecafca..88a01b9d2 100644
--- a/tradingagents/agents/context.py
+++ b/tradingagents/agents/context.py
@@ -5,7 +5,7 @@ import logging
from collections.abc import Mapping
from typing import Any
-from tradingagents.dataflows.date_window import get_current_date
+from tradingagents.dataflows.date_window import is_historical
from tradingagents.dataflows.vendors.yahoo.fundamentals import get_company_profile
logger = logging.getLogger(__name__)
@@ -135,7 +135,7 @@ def build_instrument_context(
name = identity.get("company_name") or identity.get("name")
label = "Name" if is_crypto else "Company"
details = []
- if trade_date and str(trade_date) < get_current_date():
+ if is_historical(trade_date):
if name:
details.append(
f"{label}: {name} (its current name, given only to identify it; "
diff --git a/tradingagents/dataflows/date_window.py b/tradingagents/dataflows/date_window.py
index b74202d14..e799914a0 100644
--- a/tradingagents/dataflows/date_window.py
+++ b/tradingagents/dataflows/date_window.py
@@ -36,6 +36,11 @@ def get_current_date() -> str:
return date.today().strftime("%Y-%m-%d")
+def is_historical(run_date) -> bool:
+ """Whether a run is dated before today, so live-only data would come from after it."""
+ return bool(run_date) and str(run_date) < get_current_date()
+
+
def coverage_gap(
dates, start_date: str, end_date: str, source: str, subject: str
) -> str | None:
@@ -106,10 +111,7 @@ def withhold_live_profile(as_of_date: str | None, label: str) -> str | None:
Every fundamentals vendor withholds on this rule, so switching between them
cannot reintroduce the leak.
"""
- if not as_of_date:
- return None
- today = get_current_date()
- if as_of_date >= today:
+ if not is_historical(as_of_date):
return None
return (
f"# Company Fundamentals for {label}\n"
diff --git a/tradingagents/dataflows/vendors/polymarket.py b/tradingagents/dataflows/vendors/polymarket.py
index 0e5303dd1..2dae8f367 100644
--- a/tradingagents/dataflows/vendors/polymarket.py
+++ b/tradingagents/dataflows/vendors/polymarket.py
@@ -15,7 +15,7 @@ from datetime import datetime, timezone
import requests
-from tradingagents.dataflows.date_window import get_current_date
+from tradingagents.dataflows.date_window import is_historical
logger = logging.getLogger(__name__)
@@ -83,7 +83,7 @@ def get_prediction_markets(topic: str, limit: int | None = None, as_of_date: str
each with its implied probability, traded volume, resolution date, and
recent (1-week) move.
"""
- if as_of_date and as_of_date < get_current_date():
+ if is_historical(as_of_date):
return (
f"Prediction-market odds are withheld for {as_of_date}. Polymarket serves "
f"only live odds on open markets, with no historical vintage, so serving "
diff --git a/tradingagents/graph/trading_graph.py b/tradingagents/graph/trading_graph.py
index 820398c18..d2e29bdb6 100644
--- a/tradingagents/graph/trading_graph.py
+++ b/tradingagents/graph/trading_graph.py
@@ -11,7 +11,7 @@ import tradingagents
from tradingagents.agents.context import build_instrument_context, resolve_instrument_identity
from tradingagents.agents.rating import run_rating
from tradingagents.dataflows.config import run_config, run_config_context, set_config
-from tradingagents.dataflows.date_window import get_current_date
+from tradingagents.dataflows.date_window import get_current_date, is_historical
from tradingagents.dataflows.symbols import safe_ticker_component
from tradingagents.default_config import DEFAULT_CONFIG
from tradingagents.llm_clients import build_llm_kwargs, create_llm_client
@@ -152,8 +152,7 @@ class TradingAgentsGraph:
None, disabling the filter so live behavior and pre-migration entries
(which have no stored resolution date) are unaffected.
"""
- td = str(trade_date)
- return td if td < datetime.now().strftime("%Y-%m-%d") else None
+ return str(trade_date) if is_historical(trade_date) else None
def _run_signature(self, asset_type: str, portfolio=None) -> str:
"""Run inputs that must invalidate a checkpoint if changed.