Files
tradingagents/tradingagents/agents/tools.py
T
Yijia-Xiao 466d33ab3b fix(agents): answer a snapshot with no price rows as no data instead of ending the run
- build_verified_market_snapshot raises NoMarketDataError for an unknown symbol or no row by the date
- get_verified_market_snapshot returns the router's no-data answer, shared as router.no_data_available
2026-09-29 05:57:18 +00:00

283 lines
11 KiB
Python

"""The data tools the analysts call.
Each dated tool takes the run's ``trade_date`` from graph state (``InjectedState``)
and never serves data past it, whatever date the model asks for.
"""
from typing import Annotated
from langchain_core.tools import tool
from langgraph.prebuilt import InjectedState
from tradingagents.dataflows.date_window import as_of, as_of_window
from tradingagents.dataflows.errors import NoMarketDataError, VendorUnavailableError
from tradingagents.dataflows.router import no_data_available, route_to_vendor, vendor_unavailable
from tradingagents.dataflows.vendors.yahoo.snapshot import build_verified_market_snapshot
@tool
def get_stock_data(
symbol: Annotated[str, InjectedState("company_of_interest")],
start_date: Annotated[str, "Start date in yyyy-mm-dd format"],
end_date: Annotated[str, "End date in yyyy-mm-dd format"],
trade_date: Annotated[str, InjectedState("trade_date")] = "",
) -> str:
"""
Retrieve stock price data (OHLCV) for the instrument under analysis.
Uses the configured core_stock_apis vendor.
Args:
start_date (str): Start date in yyyy-mm-dd format
end_date (str): End date in yyyy-mm-dd format
Returns:
str: A formatted dataframe containing the price data for the instrument over the date range.
"""
start_date, end_date = as_of_window(start_date, end_date, trade_date)
return route_to_vendor("get_stock_data", symbol, start_date, end_date)
@tool
def get_indicators(
symbol: Annotated[str, InjectedState("company_of_interest")],
indicator: Annotated[str, "technical indicator to get the analysis and report of"],
curr_date: Annotated[str, "The current trading date you are trading on, YYYY-mm-dd"],
look_back_days: Annotated[int, "how many days to look back"] = 30,
trade_date: Annotated[str, InjectedState("trade_date")] = "",
) -> str:
"""
Retrieve a single technical indicator for the instrument under analysis.
Uses the configured technical_indicators vendor.
Args:
indicator (str): A single technical indicator name, e.g. 'rsi', 'macd'. Call this tool once per indicator.
curr_date (str): The current trading date you are trading on, YYYY-mm-dd
look_back_days (int): How many days to look back, default is 30
Returns:
str: A formatted dataframe containing the technical indicators for the instrument and indicator.
"""
# LLMs sometimes pass multiple indicators as a comma-separated string;
# split and process each individually.
curr_date = as_of(curr_date, trade_date)
indicators = [i.strip().lower() for i in indicator.split(",") if i.strip()]
results = []
for ind in indicators:
try:
results.append(route_to_vendor("get_indicators", symbol, ind, curr_date, look_back_days))
except ValueError as e:
results.append(str(e))
return "\n\n".join(results)
@tool
def get_verified_market_snapshot(
symbol: Annotated[str, InjectedState("company_of_interest")],
curr_date: Annotated[str, "the current trading date, YYYY-mm-dd"],
look_back_days: Annotated[
int, "number of recent trading rows to include for sanity-checking"
] = 30,
trade_date: Annotated[str, InjectedState("trade_date")] = "",
) -> str:
"""Deterministic verification snapshot for exact market-data claims.
Returns the latest OHLCV row on or before curr_date, common technical
indicators, and recent closes. Call this before making exact claims about
price levels, Bollinger bands, RSI, MACD, moving averages, support /
resistance, or historical comparisons, and treat it as the source of truth.
"""
# An exception out of a tool would end the run.
try:
return build_verified_market_snapshot(symbol, as_of(curr_date, trade_date), look_back_days)
except VendorUnavailableError as exc:
return vendor_unavailable("get_verified_market_snapshot", exc)
except NoMarketDataError as exc:
return no_data_available(exc)
@tool
def get_fundamentals(
ticker: Annotated[str, InjectedState("company_of_interest")],
curr_date: Annotated[str, "current date you are trading at, yyyy-mm-dd"],
trade_date: Annotated[str, InjectedState("trade_date")] = "",
) -> str:
"""
Retrieve comprehensive fundamental data for the instrument under analysis.
Uses the configured fundamental_data vendor.
Args:
curr_date (str): Current date you are trading at, yyyy-mm-dd
Returns:
str: A formatted report containing comprehensive fundamental data
"""
return route_to_vendor("get_fundamentals", ticker, as_of(curr_date, trade_date))
@tool
def get_balance_sheet(
ticker: Annotated[str, InjectedState("company_of_interest")],
freq: Annotated[str, "reporting frequency: annual/quarterly"] = "quarterly",
curr_date: Annotated[str, "current date you are trading at, yyyy-mm-dd"] = None,
trade_date: Annotated[str, InjectedState("trade_date")] = "",
) -> str:
"""
Retrieve balance sheet data for the instrument under analysis.
Uses the configured fundamental_data vendor.
Args:
freq (str): Reporting frequency: annual/quarterly (default quarterly)
curr_date (str): Current date you are trading at, yyyy-mm-dd
Returns:
str: A formatted report containing balance sheet data
"""
return route_to_vendor("get_balance_sheet", ticker, freq, as_of(curr_date, trade_date))
@tool
def get_cashflow(
ticker: Annotated[str, InjectedState("company_of_interest")],
freq: Annotated[str, "reporting frequency: annual/quarterly"] = "quarterly",
curr_date: Annotated[str, "current date you are trading at, yyyy-mm-dd"] = None,
trade_date: Annotated[str, InjectedState("trade_date")] = "",
) -> str:
"""
Retrieve cash flow statement data for the instrument under analysis.
Uses the configured fundamental_data vendor.
Args:
freq (str): Reporting frequency: annual/quarterly (default quarterly)
curr_date (str): Current date you are trading at, yyyy-mm-dd
Returns:
str: A formatted report containing cash flow statement data
"""
return route_to_vendor("get_cashflow", ticker, freq, as_of(curr_date, trade_date))
@tool
def get_income_statement(
ticker: Annotated[str, InjectedState("company_of_interest")],
freq: Annotated[str, "reporting frequency: annual/quarterly"] = "quarterly",
curr_date: Annotated[str, "current date you are trading at, yyyy-mm-dd"] = None,
trade_date: Annotated[str, InjectedState("trade_date")] = "",
) -> str:
"""
Retrieve income statement data for the instrument under analysis.
Uses the configured fundamental_data vendor.
Args:
freq (str): Reporting frequency: annual/quarterly (default quarterly)
curr_date (str): Current date you are trading at, yyyy-mm-dd
Returns:
str: A formatted report containing income statement data
"""
return route_to_vendor("get_income_statement", ticker, freq, as_of(curr_date, trade_date))
@tool
def get_news(
ticker: Annotated[str, InjectedState("company_of_interest")],
start_date: Annotated[str, "Start date in yyyy-mm-dd format"],
end_date: Annotated[str, "End date in yyyy-mm-dd format"],
trade_date: Annotated[str, InjectedState("trade_date")] = "",
) -> str:
"""
Retrieve news data for the instrument under analysis.
Uses the configured news_data vendor.
Args:
start_date (str): Start date in yyyy-mm-dd format
end_date (str): End date in yyyy-mm-dd format
Returns:
str: A formatted string containing news data
"""
start_date, end_date = as_of_window(start_date, end_date, trade_date)
return route_to_vendor("get_news", ticker, start_date, end_date)
@tool
def get_global_news(
curr_date: Annotated[str, "Current date in yyyy-mm-dd format"],
look_back_days: Annotated[int | None, "Days to look back; omit to use the configured default"] = None,
limit: Annotated[int | None, "Max articles to return; omit to use the configured default"] = None,
trade_date: Annotated[str, InjectedState("trade_date")] = "",
) -> str:
"""
Retrieve global news data.
Uses the configured news_data vendor. Defaults for look_back_days and
limit come from DEFAULT_CONFIG (global_news_lookback_days,
global_news_article_limit); pass explicit values to override.
Args:
curr_date (str): Current date in yyyy-mm-dd format
look_back_days (int): Number of days to look back; omit to inherit config
limit (int): Maximum number of articles to return; omit to inherit config
Returns:
str: A formatted string containing global news data
"""
return route_to_vendor("get_global_news", as_of(curr_date, trade_date), look_back_days, limit)
@tool
def get_insider_transactions(
ticker: Annotated[str, InjectedState("company_of_interest")],
trade_date: Annotated[str, InjectedState("trade_date")] = "",
) -> str:
"""
Retrieve insider transaction information about a company.
Uses the configured news_data vendor.
Returns:
str: A report of insider transaction data
"""
return route_to_vendor("get_insider_transactions", ticker, trade_date or None)
@tool
def get_macro_indicators(
indicator: Annotated[
str,
"Macro indicator: a friendly alias such as 'cpi', 'core_pce', "
"'unemployment', 'fed_funds_rate', '10y_treasury', 'yield_curve', "
"'real_gdp', 'vix', or a raw FRED series ID such as 'CPIAUCSL'.",
],
curr_date: Annotated[str, "Current date in yyyy-mm-dd format; the end of the window"],
look_back_days: Annotated[
int | None, "Trailing window length in days; omit for a 1-year window"
] = None,
trade_date: Annotated[str, InjectedState("trade_date")] = "",
) -> str:
"""
Retrieve a macroeconomic indicator time series from FRED (Federal Reserve
Economic Data): policy rates, Treasury yields, inflation, labor, and growth.
Returns the series title, units, frequency, the latest value, the change
over the window, and a recent observation table. Uses the configured
macro_data vendor.
Args:
indicator (str): Friendly alias or raw FRED series ID
curr_date (str): Current date in yyyy-mm-dd format
look_back_days (int): Trailing window length; omit for a 1-year window
Returns:
str: A formatted markdown report of the macro series
"""
return route_to_vendor("get_macro_indicators", indicator, as_of(curr_date, trade_date), look_back_days)
@tool
def get_prediction_markets(
topic: Annotated[
str,
"Event topic/keyword, e.g. 'Fed rate cut', 'recession 2026', "
"'US election', or a sector/company event.",
],
limit: Annotated[int | None, "Max markets to return; omit for a default of 6"] = None,
trade_date: Annotated[str, InjectedState("trade_date")] = "",
) -> str:
"""
Retrieve live, market-implied probabilities for forward-looking events from
prediction markets (Polymarket): Fed decisions, recession, elections,
geopolitics, crypto. Returns the most-traded open markets matching the
topic, each with its implied probability, traded volume, resolution date,
and recent move. Uses the configured prediction_markets vendor.
Args:
topic (str): Event keyword(s) to search
limit (int): Max markets to return; omit for a default of 6
Returns:
str: A formatted markdown report of matching prediction markets
"""
return route_to_vendor("get_prediction_markets", topic, limit, trade_date or None)