This commit is contained in:
Ubuntu
2026-07-03 16:58:28 +08:00
commit 32b43f85bd
6240 changed files with 1987678 additions and 0 deletions
@@ -0,0 +1,563 @@
"""
akshare data adapter for TradingAgents.
Replaces yfinance as the data vendor for Chinese (A-share), US, and HK stocks.
Uses akshare (https://github.com/akfamily/akshare) which sources data from
East Money (东方财富), providing reliable access without Yahoo Finance rate limits.
"""
import pandas as pd
from datetime import datetime, timedelta
from typing import Optional
from dateutil.relativedelta import relativedelta
import os
# Suppress akshare's internal tqdm progress bars
os.environ.setdefault('TQDM_DISABLE', '1')
import akshare as ak
# ═══════════════════════════════════════════════════════════════
# Symbol detection helpers
# ═══════════════════════════════════════════════════════════════
def _detect_market(symbol: str) -> str:
"""Detect market: 'cn' (A-share), 'us' (US), or 'hk' (HK).
A-share: 6-digit numeric (000001, 600519)
US: alphabetic tickers (AAPL, TSLA)
HK: 5-digit numeric (00700, 09988)
"""
sym = symbol.strip().upper().replace(".", "").replace("-", "")
if sym.isdigit():
if len(sym) == 6:
return "cn"
elif len(sym) == 5:
return "hk"
return "us"
def _get_exchange_prefix(symbol: str) -> str:
"""Get exchange prefix for A-share financial statement APIs.
Shanghai: 60xxxx, 68xxxx → 'SH'
Shenzhen: 00xxxx, 30xxxx → 'SZ'
Beijing: 8xxxxx, 4xxxxx → 'BJ'
"""
sym = symbol.strip()
first = sym[0]
if first == '6':
return "SH"
elif first in ('0', '3'):
return "SZ"
elif first in ('8', '4'):
return "BJ"
return "SZ"
# ═══════════════════════════════════════════════════════════════
# Data fetching helpers
# ═══════════════════════════════════════════════════════════════
COLUMN_MAP = {
"日期": "date",
"开盘": "open",
"收盘": "close",
"最高": "high",
"最低": "low",
"成交量": "volume",
"成交额": "amount",
"振幅": "amplitude",
"涨跌幅": "pct_change",
"涨跌额": "change",
"换手率": "turnover",
}
def _get_cn_stock_data(symbol: str, start_date: str, end_date: str) -> pd.DataFrame:
"""Fetch A-share OHLCV via akshare stock_zh_a_hist."""
df = ak.stock_zh_a_hist(
symbol=symbol.strip(),
period="daily",
start_date=start_date.replace("-", ""),
end_date=end_date.replace("-", ""),
adjust="qfq",
)
df = df.rename(columns=COLUMN_MAP)
needed = ["date", "open", "close", "high", "low", "volume"]
return df[[c for c in needed if c in df.columns]]
def _get_us_stock_data(symbol: str, start_date: str, end_date: str) -> pd.DataFrame:
"""Fetch US stock OHLCV via akshare stock_us_daily (no market code needed).
stock_us_daily returns all historical data; we filter by date range here.
"""
ticker = symbol.strip().upper()
# akshare US tickers use dot notation (BRK.B), not dash (BRK-B)
if "-" in symbol:
ticker = ticker.replace("-", ".")
df = ak.stock_us_daily(symbol=ticker, adjust="qfq")
df["date"] = pd.to_datetime(df["date"])
mask = (df["date"] >= pd.Timestamp(start_date)) & (df["date"] <= pd.Timestamp(end_date))
df = df[mask]
needed = ["date", "open", "close", "high", "low", "volume"]
return df[[c for c in needed if c in df.columns]]
def _get_hk_stock_data(symbol: str, start_date: str, end_date: str) -> pd.DataFrame:
"""Fetch HK stock OHLCV via akshare stock_hk_hist."""
df = ak.stock_hk_hist(
symbol=symbol.strip(),
period="daily",
start_date=start_date.replace("-", ""),
end_date=end_date.replace("-", ""),
adjust="qfq",
)
df = df.rename(columns=COLUMN_MAP)
needed = ["date", "open", "close", "high", "low", "volume"]
return df[[c for c in needed if c in df.columns]]
# ═══════════════════════════════════════════════════════════════
# Formatting helpers
# ═══════════════════════════════════════════════════════════════
def _fmt_stock_data(df: pd.DataFrame, symbol: str) -> str:
"""Format OHLCV DataFrame as Markdown table string."""
if df.empty:
return f"No stock data found for {symbol} in the given date range."
df = df.tail(60).copy()
df["date"] = df["date"].astype(str)
return df.to_markdown(index=False)
def _fmt_financial(df: pd.DataFrame, statement: str, symbol: str, freq: str) -> str:
"""Format financial statement DataFrame.
Handles both wide format (A-share: rows=periods, cols=items) and
long format (US: rows=item×period, cols=ITEM_NAME+AMOUNT).
"""
if df.empty:
return f"No {freq} {statement} data found for {symbol}."
# Long format (US stocks) — pivot to wide
if "ITEM_NAME" in df.columns and "AMOUNT" in df.columns:
pivot = df.pivot_table(
index="REPORT_DATE",
columns="ITEM_NAME",
values="AMOUNT",
aggfunc="first",
)
pivot = pivot.sort_index(ascending=False)
return pivot.head(8).to_markdown()
# Wide format (A-share) — select date + key items
key_items = {
"balance sheet": [
"REPORT_DATE", "TOTAL_ASSETS", "TOTAL_LIABILITIES",
"TOTAL_EQUITY", "MONETARYFUNDS", "INVENTORY",
"TOTAL_CURRENT_ASSETS", "TOTAL_CURRENT_LIAB",
"TOTAL_NONCURRENT_ASSETS", "TOTAL_NONCURRENT_LIAB",
"SHORT_LOAN", "LONG_LOAN", "GOODWILL",
],
"cash flow": [
"REPORT_DATE", "NETCASH_OPERATE", "NETCASH_INVEST",
"NETCASH_FINANCE", "CASH_EQUIVALENT_BEGIN",
"CASH_EQUIVALENT_END", "OPERATE_INFLOW",
"OPERATE_OUTFLOW", "INVEST_INFLOW", "INVEST_OUTFLOW",
"FINANCE_INFLOW", "FINANCE_OUTFLOW",
],
"income statement": [
"REPORT_DATE", "TOTAL_OPERATE_INCOME", "OPERATE_PROFIT",
"TOTAL_PROFIT", "NET_PROFIT", "BASIC_EPS",
"TOTAL_OPERATE_COST", "OPERATE_COST",
"RESEARCH_EXPENSE", "SELL_EXPENSE", "MANAGE_EXPENSE",
"FINANCE_EXPENSE",
],
}
stat_key = statement.lower().replace("_", " ")
items = key_items.get(stat_key, [])
available = [c for c in items if c in df.columns]
if not available:
# Fallback: show all columns but limit columns to first 15
result = df.head(8).iloc[:, :15]
else:
result = df[available].head(8)
return result.to_markdown(index=False)
# ═══════════════════════════════════════════════════════════════
# Public API — matches y_finance.py interface
# ═══════════════════════════════════════════════════════════════
def get_stock_data(symbol: str, start_date: str, end_date: str) -> str:
"""Get stock OHLCV price data. Returns Markdown table string."""
try:
market = _detect_market(symbol)
if market == "cn":
df = _get_cn_stock_data(symbol, start_date, end_date)
elif market == "us":
df = _get_us_stock_data(symbol, start_date, end_date)
else:
df = _get_hk_stock_data(symbol, start_date, end_date)
return _fmt_stock_data(df, symbol)
except Exception as e:
return f"[akshare] Error fetching stock data for {symbol}: {e}"
def get_indicators(
symbol: str,
indicator: str,
curr_date: str,
look_back_days: int = 365,
) -> str:
"""Get technical indicator values. Returns formatted string with date: value lines.
Uses stockstats for calculation (same as yfinance adapter).
"""
from stockstats import wrap
# Indicator descriptions (same as yfinance)
best_ind_params = {
"close_5_sma": (
"5-day SMA: A short-term simple moving average. "
"Usage: Identify short-term trend direction and potential entry/exit signals. "
"Tips: Often combined with longer SMAs (e.g., 20-day) for crossover strategies."
),
"close_10_sma": (
"10-day SMA: A short to medium-term simple moving average. "
"Usage: Useful for gauging immediate trend strength and potential support/resistance levels."
),
"close_20_sma": (
"20-day SMA: A medium-term simple moving average. "
"Usage: Commonly used to identify the intermediate trend and key swing levels."
),
"close_50_sma": (
"50-day SMA: A medium to long-term simple moving average. "
"Usage: Watched by many traders to gauge the intermediate trend strength."
),
"close_200_sma": (
"200-day SMA: A long-term simple moving average. "
"Usage: Often used to determine the overall bull/bear market trend."
),
"close_5_ema": (
"5-day EMA: A short-term exponential moving average giving more weight to recent prices. "
"Usage: Provides early signals for short-term trend changes."
),
"close_10_ema": (
"10-day EMA: A short to medium-term exponential moving average. "
"Usage: Reacts faster to price changes than SMA; good for active trading strategies."
),
"close_20_ema": (
"20-day EMA: A medium-term exponential moving average. "
"Usage: Commonly watched by swing and short-term traders for trend confirmation."
),
"close_50_ema": (
"50-day EMA: A medium to long-term exponential moving average. "
"Usage: Offers a balance between responsiveness and smoothing for trend analysis."
),
"close_200_ema": (
"200-day EMA: A long-term exponential moving average. "
"Usage: Often used by longer-term traders to gauge major trend turns."
),
"rsi": (
"RSI (14): The Relative Strength Index measures momentum on a scale of 0 to 100. "
"Usage: Identify overbought (>70) or oversold (<30) conditions. "
"Tips: Combine with trend indicators; divergences between RSI and price can signal reversals."
),
"rsi_6": (
"RSI (6): A faster-setting RSI. "
"Usage: More sensitive and generates earlier signals, ideal for quick momentum shifts."
),
"rsi_14": (
"RSI (14): The standard Relative Strength Index. "
"Usage: Same as RSI; widely used for identifying overbought/oversold and reversals."
),
"macd": (
"MACD (Moving Average Convergence Divergence): Trend-following momentum indicator. "
"Usage: Shows the relationship between two EMAs. "
"Tips: Crossovers of the MACD and signal line, as well as divergence from price, can indicate trades."
),
"macds": (
"MACD Signal: The 9-day EMA of the MACD line. "
"Usage: Acts as a trigger for buy/sell signals when the MACD line crosses above or below the signal line."
),
"macdh": (
"MACD Histogram: The difference between MACD and signal line. "
"Usage: Illustrates changes in momentum; a rising histogram indicates increasing momentum."
),
"boll": (
"Bollinger Bands Middle: 20-day SMA central line. "
"Usage: The baseline for volatility bands. "
"Tips: Prices reverting to the middle may suggest fading momentum."
),
"boll_ub": (
"Bollinger Bands Upper: Upper volatility band (2 std dev above middle). "
"Usage: May indicate overbought conditions. "
"Tips: Used with lower band to identify squeeze and breakout patterns."
),
"boll_lb": (
"Bollinger Bands Lower: Lower volatility band (2 std dev below middle). "
"Usage: May indicate oversold conditions. "
"Tips: Price touching/extending beyond the band can signal strong trend continuation."
),
"atr": (
"ATR: Average True Range measures market volatility. "
"Usage: High ATR indicates increased volatility; useful for setting stop-losses and position sizing."
),
"vwma": (
"VWMA: A moving average weighted by volume. "
"Usage: Confirm trends by integrating price action with volume data. "
"Tips: Watch for skewed results from volume spikes; use in combination with other volume analyses."
),
"mfi": (
"MFI: The Money Flow Index is a momentum indicator that uses both price and volume to measure buying and selling pressure. "
"Usage: Identify overbought (>80) or oversold (<20) conditions and confirm the strength of trends or reversals. "
"Tips: Use alongside RSI or MACD to confirm signals; divergence between price and MFI can indicate potential reversals."
),
}
if indicator not in best_ind_params:
raise ValueError(
f"Indicator {indicator} is not supported. Please choose from: {list(best_ind_params.keys())}"
)
end_date = datetime.strptime(curr_date, "%Y-%m-%d")
start_date = end_date - relativedelta(days=look_back_days + 200) # buffer for SMA calc
try:
# Fetch OHLCV via akshare
market = _detect_market(symbol)
if market == "cn":
df = _get_cn_stock_data(symbol, start_date.strftime("%Y-%m-%d"), curr_date)
elif market == "us":
df = _get_us_stock_data(symbol, start_date.strftime("%Y-%m-%d"), curr_date)
else:
df = _get_hk_stock_data(symbol, start_date.strftime("%Y-%m-%d"), curr_date)
# Prepare for stockstats (expects capitalized column names)
df = df.rename(columns={
"date": "Date", "open": "Open", "close": "Close",
"high": "High", "low": "Low", "volume": "Volume",
})
sdf = wrap(df)
sdf["Date"] = pd.to_datetime(sdf["Date"]).dt.strftime("%Y-%m-%d")
# Trigger calculation
sdf[indicator]
# Build dict: date → value
result_map = {}
for _, row in sdf.iterrows():
val = row[indicator]
result_map[row["Date"]] = f"{val:.4f}" if not pd.isna(val) and isinstance(val, float) else str(val) if not pd.isna(val) else "N/A"
except Exception as e:
# Fallback: date-by-date calculation (less efficient but more resilient)
print(f"[akshare] Bulk indicator calc failed: {e}, falling back to per-date")
ind_string = ""
curr_date_dt = datetime.strptime(curr_date, "%Y-%m-%d")
fallback_dt = curr_date_dt
while fallback_dt >= end_date - relativedelta(days=look_back_days):
indicator_value = _get_indicator_single(symbol, indicator, fallback_dt.strftime("%Y-%m-%d"))
ind_string += f"{fallback_dt.strftime('%Y-%m-%d')}: {indicator_value}\n"
fallback_dt -= relativedelta(days=1)
result_str = (
f"## {indicator} values from "
f"{end_date - relativedelta(days=look_back_days):%Y-%m-%d} to {curr_date}:\n\n"
+ ind_string
+ "\n\n"
+ best_ind_params.get(indicator, "")
)
return result_str
# Build output string
ind_string = ""
current_dt = end_date
before = end_date - relativedelta(days=look_back_days)
while current_dt >= before:
date_str = current_dt.strftime("%Y-%m-%d")
value = result_map.get(date_str, "N/A: Not a trading day (weekend or holiday)")
ind_string += f"{date_str}: {value}\n"
current_dt -= relativedelta(days=1)
result_str = (
f"## {indicator} values from {before.strftime('%Y-%m-%d')} to {curr_date}:\n\n"
+ ind_string
+ "\n\n"
+ best_ind_params.get(indicator, "")
)
return result_str
def _get_indicator_single(symbol: str, indicator: str, date_str: str) -> str:
"""Fallback: calculate a single indicator value for one date."""
from stockstats import wrap
try:
end_date = datetime.strptime(date_str, "%Y-%m-%d")
start_date = end_date - relativedelta(days=400)
market = _detect_market(symbol)
if market == "cn":
df = _get_cn_stock_data(symbol, start_date.strftime("%Y-%m-%d"), date_str)
elif market == "us":
df = _get_us_stock_data(symbol, start_date.strftime("%Y-%m-%d"), date_str)
else:
df = _get_hk_stock_data(symbol, start_date.strftime("%Y-%m-%d"), date_str)
df = df.rename(columns={
"date": "Date", "open": "Open", "close": "Close",
"high": "High", "low": "Low", "volume": "Volume",
})
sdf = wrap(df)
sdf["Date"] = pd.to_datetime(sdf["Date"]).dt.strftime("%Y-%m-%d")
sdf[indicator]
matching = sdf[sdf["Date"].str.startswith(date_str)]
if not matching.empty:
val = matching[indicator].values[0]
return f"{val:.4f}" if isinstance(val, float) else str(val)
return "N/A: Not a trading day (weekend or holiday)"
except Exception as e:
return f"N/A (error: {e})"
def get_fundamentals(ticker: str, curr_date: str = None) -> str:
"""Get company fundamentals. Returns Markdown string."""
try:
market = _detect_market(ticker)
if market == "cn":
df = ak.stock_individual_info_em(symbol=ticker.strip())
lines = [f"# Fundamentals for {ticker}\n"]
for _, row in df.head(30).iterrows():
lines.append(f"- **{row['item']}**: {row['value']}")
return "\n".join(lines)
elif market == "us":
t = ticker.strip().upper().replace("-", ".")
df = ak.stock_individual_basic_info_us_xq(symbol=t)
lines = [f"# Fundamentals for {ticker}\n"]
for _, row in df.head(25).iterrows():
lines.append(f"- **{row['item']}**: {row['value']}")
return "\n".join(lines)
else: # HK
try:
df = ak.stock_hk_company_profile_em(symbol=ticker.strip())
return df.to_markdown(index=False)
except Exception:
return f"Fundamentals not available for HK stock {ticker} via akshare."
except Exception as e:
return f"[akshare] Error fetching fundamentals for {ticker}: {e}"
def get_balance_sheet(ticker: str, freq: str = "annual", curr_date: str = None) -> str:
"""Get balance sheet. freq: 'annual' or 'quarterly'. Returns Markdown string."""
try:
market = _detect_market(ticker)
if market == "cn":
prefix = _get_exchange_prefix(ticker)
df = ak.stock_balance_sheet_by_report_em(symbol=f"{prefix}{ticker.strip()}")
if freq in ("annual", "yearly"):
df = df[df["REPORT_TYPE"] == "年报"]
else:
df = df[df["REPORT_TYPE"].isin(["一季报", "中报", "三季报"])]
if curr_date and not df.empty:
df = df[pd.to_datetime(df["REPORT_DATE"]) <= pd.Timestamp(curr_date)]
return _fmt_financial(df, "balance sheet", ticker, freq)
elif market == "us":
indicator = "年报" if freq in ("annual", "yearly") else "单季报"
t = ticker.strip().upper().replace("-", ".")
df = ak.stock_financial_us_report_em(
stock=t, symbol="资产负债表", indicator=indicator,
)
if curr_date and not df.empty:
df = df[pd.to_datetime(df["REPORT_DATE"]) <= pd.Timestamp(curr_date)]
return _fmt_financial(df, "balance sheet", ticker, freq)
else:
return f"Balance sheet not supported for HK stock {ticker} via akshare."
except Exception as e:
return f"[akshare] Error fetching balance sheet for {ticker}: {e}"
def get_cashflow(ticker: str, freq: str = "annual", curr_date: str = None) -> str:
"""Get cash flow statement. freq: 'annual' or 'quarterly'. Returns Markdown string."""
try:
market = _detect_market(ticker)
if market == "cn":
prefix = _get_exchange_prefix(ticker)
df = ak.stock_cash_flow_sheet_by_report_em(symbol=f"{prefix}{ticker.strip()}")
if freq in ("annual", "yearly"):
df = df[df["REPORT_TYPE"] == "年报"]
else:
df = df[df["REPORT_TYPE"].isin(["一季报", "中报", "三季报"])]
if curr_date and not df.empty:
df = df[pd.to_datetime(df["REPORT_DATE"]) <= pd.Timestamp(curr_date)]
return _fmt_financial(df, "cash flow", ticker, freq)
elif market == "us":
indicator = "年报" if freq in ("annual", "yearly") else "单季报"
t = ticker.strip().upper().replace("-", ".")
df = ak.stock_financial_us_report_em(
stock=t, symbol="现金流量表", indicator=indicator,
)
if curr_date and not df.empty:
df = df[pd.to_datetime(df["REPORT_DATE"]) <= pd.Timestamp(curr_date)]
return _fmt_financial(df, "cash flow", ticker, freq)
else:
return f"Cash flow statement not supported for HK stock {ticker} via akshare."
except Exception as e:
return f"[akshare] Error fetching cash flow for {ticker}: {e}"
def get_income_statement(ticker: str, freq: str = "annual", curr_date: str = None) -> str:
"""Get income statement. freq: 'annual' or 'quarterly'. Returns Markdown string."""
try:
market = _detect_market(ticker)
if market == "cn":
prefix = _get_exchange_prefix(ticker)
df = ak.stock_profit_sheet_by_report_em(symbol=f"{prefix}{ticker.strip()}")
if freq in ("annual", "yearly"):
df = df[df["REPORT_TYPE"] == "年报"]
else:
df = df[df["REPORT_TYPE"].isin(["一季报", "中报", "三季报"])]
if curr_date and not df.empty:
df = df[pd.to_datetime(df["REPORT_DATE"]) <= pd.Timestamp(curr_date)]
return _fmt_financial(df, "income statement", ticker, freq)
elif market == "us":
indicator = "年报" if freq in ("annual", "yearly") else "单季报"
t = ticker.strip().upper().replace("-", ".")
df = ak.stock_financial_us_report_em(
stock=t, symbol="综合损益表", indicator=indicator,
)
if curr_date and not df.empty:
df = df[pd.to_datetime(df["REPORT_DATE"]) <= pd.Timestamp(curr_date)]
return _fmt_financial(df, "income statement", ticker, freq)
else:
return f"Income statement not supported for HK stock {ticker} via akshare."
except Exception as e:
return f"[akshare] Error fetching income statement for {ticker}: {e}"
def get_insider_transactions(ticker: str) -> str:
"""Insider transactions not supported by akshare.
Raise AlphaVantageRateLimitError to trigger fallback to yfinance."""
from .alpha_vantage_common import AlphaVantageRateLimitError
raise AlphaVantageRateLimitError(
f"akshare does not support insider transactions for {ticker}. "
"Falling back to next vendor."
)
@@ -0,0 +1,165 @@
"""
akshare news adapter for TradingAgents.
Replaces yfinance_news for company and global news sourcing.
Uses:
- stock_news_em → individual stock news (East Money)
- news_cctv → CCTV daily news (macro/global)
- news_economic_baidu → economic calendar/events
"""
from datetime import datetime, timedelta
from typing import Optional
import os
import pandas as pd
from dateutil.relativedelta import relativedelta
# Suppress akshare's internal tqdm progress bars
os.environ.setdefault('TQDM_DISABLE', '1')
import akshare as ak
# ═══════════════════════════════════════════════════════════════
# Symbol detection
# ═══════════════════════════════════════════════════════════════
def _normalize_ticker(ticker: str) -> str:
"""Normalize ticker for akshare news API.
stock_news_em accepts: A-share 6-digit codes, or US tickers as-is.
"""
sym = ticker.strip().upper()
if sym.isdigit() and len(sym) == 6:
return sym
return sym
# ═══════════════════════════════════════════════════════════════
# Company news
# ═══════════════════════════════════════════════════════════════
def get_news(
ticker: str,
start_date: str,
end_date: str,
) -> str:
"""Get company-specific news from East Money.
Args:
ticker: Stock symbol (A-share code or US ticker).
start_date: YYYY-MM-DD format.
end_date: YYYY-MM-DD format.
Returns:
Markdown string containing news items.
"""
try:
sym = _normalize_ticker(ticker)
df = ak.stock_news_em(symbol=sym)
# Filter by date range
df["发布时间"] = pd.to_datetime(df["发布时间"])
mask = (df["发布时间"] >= pd.Timestamp(start_date)) & (
df["发布时间"] <= pd.Timestamp(end_date) + pd.Timedelta(days=1)
)
df = df[mask]
if df.empty:
return f"No news found for {ticker} between {start_date} and {end_date}."
# Select and format key columns
cols = [c for c in ["发布时间", "新闻标题", "新闻内容", "文章来源"] if c in df.columns]
result = df[cols].head(30) # Limit to 30 most recent
result["发布时间"] = result["发布时间"].astype(str)
return result.to_markdown(index=False)
except Exception as e:
return f"[akshare] Error fetching news for {ticker}: {e}"
# ═══════════════════════════════════════════════════════════════
# Global / macro news
# ═══════════════════════════════════════════════════════════════
def get_global_news(
curr_date: str,
look_back_days: int = 7,
limit: int = 20,
) -> str:
"""Get global macroeconomic news from multiple Chinese news sources.
Aggregates:
- CCTV daily news (political/economic headlines)
- Baidu economic calendar (economic events/data releases)
Args:
curr_date: Current reference date (YYYY-MM-DD).
look_back_days: Days to look back.
limit: Maximum total news items to return.
Returns:
Markdown string with global news and economic events.
"""
curr_dt = datetime.strptime(curr_date, "%Y-%m-%d")
sections = []
# — CCTV news —
try:
cctv_parts = []
for i in range(look_back_days + 1):
day = curr_dt - relativedelta(days=i)
day_str = day.strftime("%Y%m%d")
try:
df_day = ak.news_cctv(date=day_str)
if df_day is not None and not df_day.empty:
for _, row in df_day.head(10).iterrows():
if len(cctv_parts) >= limit:
break
title = str(row.get("title", "")) if "title" in df_day.columns else ""
content = str(row.get("content", "")) if "content" in df_day.columns else ""
brief = content[:120] if content else ""
cctv_parts.append(f"- [{day_str}] **{title}** — {brief}")
if len(cctv_parts) >= limit:
break
except Exception:
continue # skip days with no data
if cctv_parts:
sections.append("## CCTV News (央视新闻)\n\n" + "\n".join(cctv_parts[:limit]))
except Exception as e:
sections.append(f"## CCTV News\n\n*Error fetching CCTV news: {e}*")
# — Economic calendar —
try:
econ_parts = []
for i in range(look_back_days + 1):
day = curr_dt - relativedelta(days=i)
day_str = day.strftime("%Y%m%d")
try:
df_econ = ak.news_economic_baidu(date=day_str)
if df_econ is not None and not df_econ.empty:
for _, row in df_econ.head(10).iterrows():
if len(econ_parts) >= limit:
break
time_val = str(row.get("time", ""))
country = str(row.get("country", "")) if "country" in df_econ.columns else ""
event = str(row.get("event", "")) if "event" in df_econ.columns else ""
importance = str(row.get("importance", "")) if "importance" in df_econ.columns else ""
line = f"- **{country}** {importance} — {event} ({time_val})"
econ_parts.append(line)
if len(econ_parts) >= limit:
break
except Exception:
continue
if econ_parts:
sections.append("## Economic Calendar (经济日历)\n\n" + "\n".join(econ_parts[:limit]))
except Exception as e:
sections.append(f"## Economic Calendar\n\n*Error fetching economic calendar: {e}*")
if not sections:
return f"No global news found for the period ending {curr_date}."
header = f"**Global news and economic events from {curr_dt - relativedelta(days=look_back_days):%Y-%m-%d} to {curr_date}**\n\n"
return header + "\n\n".join(sections)
@@ -0,0 +1,5 @@
# Import functions from specialized modules
from .alpha_vantage_stock import get_stock
from .alpha_vantage_indicator import get_indicator
from .alpha_vantage_fundamentals import get_fundamentals, get_balance_sheet, get_cashflow, get_income_statement
from .alpha_vantage_news import get_news, get_global_news, get_insider_transactions
@@ -0,0 +1,122 @@
import os
import requests
import pandas as pd
import json
from datetime import datetime
from io import StringIO
API_BASE_URL = "https://www.alphavantage.co/query"
def get_api_key() -> str:
"""Retrieve the API key for Alpha Vantage from environment variables."""
api_key = os.getenv("ALPHA_VANTAGE_API_KEY")
if not api_key:
raise ValueError("ALPHA_VANTAGE_API_KEY environment variable is not set.")
return api_key
def format_datetime_for_api(date_input) -> str:
"""Convert various date formats to YYYYMMDDTHHMM format required by Alpha Vantage API."""
if isinstance(date_input, str):
# If already in correct format, return as-is
if len(date_input) == 13 and 'T' in date_input:
return date_input
# Try to parse common date formats
try:
dt = datetime.strptime(date_input, "%Y-%m-%d")
return dt.strftime("%Y%m%dT0000")
except ValueError:
try:
dt = datetime.strptime(date_input, "%Y-%m-%d %H:%M")
return dt.strftime("%Y%m%dT%H%M")
except ValueError:
raise ValueError(f"Unsupported date format: {date_input}")
elif isinstance(date_input, datetime):
return date_input.strftime("%Y%m%dT%H%M")
else:
raise ValueError(f"Date must be string or datetime object, got {type(date_input)}")
class AlphaVantageRateLimitError(Exception):
"""Exception raised when Alpha Vantage API rate limit is exceeded."""
pass
def _make_api_request(function_name: str, params: dict) -> dict | str:
"""Helper function to make API requests and handle responses.
Raises:
AlphaVantageRateLimitError: When API rate limit is exceeded
"""
# Create a copy of params to avoid modifying the original
api_params = params.copy()
api_params.update({
"function": function_name,
"apikey": get_api_key(),
"source": "trading_agents",
})
# Handle entitlement parameter if present in params or global variable
current_entitlement = globals().get('_current_entitlement')
entitlement = api_params.get("entitlement") or current_entitlement
if entitlement:
api_params["entitlement"] = entitlement
elif "entitlement" in api_params:
# Remove entitlement if it's None or empty
api_params.pop("entitlement", None)
response = requests.get(API_BASE_URL, params=api_params)
response.raise_for_status()
response_text = response.text
# Check if response is JSON (error responses are typically JSON)
try:
response_json = json.loads(response_text)
# Check for rate limit error
if "Information" in response_json:
info_message = response_json["Information"]
if "rate limit" in info_message.lower() or "api key" in info_message.lower():
raise AlphaVantageRateLimitError(f"Alpha Vantage rate limit exceeded: {info_message}")
except json.JSONDecodeError:
# Response is not JSON (likely CSV data), which is normal
pass
return response_text
def _filter_csv_by_date_range(csv_data: str, start_date: str, end_date: str) -> str:
"""
Filter CSV data to include only rows within the specified date range.
Args:
csv_data: CSV string from Alpha Vantage API
start_date: Start date in yyyy-mm-dd format
end_date: End date in yyyy-mm-dd format
Returns:
Filtered CSV string
"""
if not csv_data or csv_data.strip() == "":
return csv_data
try:
# Parse CSV data
df = pd.read_csv(StringIO(csv_data))
# Assume the first column is the date column (timestamp)
date_col = df.columns[0]
df[date_col] = pd.to_datetime(df[date_col])
# Filter by date range
start_dt = pd.to_datetime(start_date)
end_dt = pd.to_datetime(end_date)
filtered_df = df[(df[date_col] >= start_dt) & (df[date_col] <= end_dt)]
# Convert back to CSV string
return filtered_df.to_csv(index=False)
except Exception as e:
# If filtering fails, return original data with a warning
print(f"Warning: Failed to filter CSV data by date range: {e}")
return csv_data
@@ -0,0 +1,55 @@
from .alpha_vantage_common import _make_api_request
def _filter_reports_by_date(result, curr_date: str):
"""Filter annualReports/quarterlyReports to exclude entries after curr_date.
Prevents look-ahead bias by removing fiscal periods that end after
the simulation's current date.
"""
if not curr_date or not isinstance(result, dict):
return result
for key in ("annualReports", "quarterlyReports"):
if key in result:
result[key] = [
r for r in result[key]
if r.get("fiscalDateEnding", "") <= curr_date
]
return result
def get_fundamentals(ticker: str, curr_date: str = None) -> str:
"""
Retrieve comprehensive fundamental data for a given ticker symbol using Alpha Vantage.
Args:
ticker (str): Ticker symbol of the company
curr_date (str): Current date you are trading at, yyyy-mm-dd (not used for Alpha Vantage)
Returns:
str: Company overview data including financial ratios and key metrics
"""
params = {
"symbol": ticker,
}
return _make_api_request("OVERVIEW", params)
def get_balance_sheet(ticker: str, freq: str = "quarterly", curr_date: str = None):
"""Retrieve balance sheet data for a given ticker symbol using Alpha Vantage."""
result = _make_api_request("BALANCE_SHEET", {"symbol": ticker})
return _filter_reports_by_date(result, curr_date)
def get_cashflow(ticker: str, freq: str = "quarterly", curr_date: str = None):
"""Retrieve cash flow statement data for a given ticker symbol using Alpha Vantage."""
result = _make_api_request("CASH_FLOW", {"symbol": ticker})
return _filter_reports_by_date(result, curr_date)
def get_income_statement(ticker: str, freq: str = "quarterly", curr_date: str = None):
"""Retrieve income statement data for a given ticker symbol using Alpha Vantage."""
result = _make_api_request("INCOME_STATEMENT", {"symbol": ticker})
return _filter_reports_by_date(result, curr_date)
@@ -0,0 +1,222 @@
from .alpha_vantage_common import _make_api_request
def get_indicator(
symbol: str,
indicator: str,
curr_date: str,
look_back_days: int,
interval: str = "daily",
time_period: int = 14,
series_type: str = "close"
) -> str:
"""
Returns Alpha Vantage technical indicator values over a time window.
Args:
symbol: ticker symbol of the company
indicator: technical indicator to get the analysis and report of
curr_date: The current trading date you are trading on, YYYY-mm-dd
look_back_days: how many days to look back
interval: Time interval (daily, weekly, monthly)
time_period: Number of data points for calculation
series_type: The desired price type (close, open, high, low)
Returns:
String containing indicator values and description
"""
from datetime import datetime
from dateutil.relativedelta import relativedelta
supported_indicators = {
"close_50_sma": ("50 SMA", "close"),
"close_200_sma": ("200 SMA", "close"),
"close_10_ema": ("10 EMA", "close"),
"macd": ("MACD", "close"),
"macds": ("MACD Signal", "close"),
"macdh": ("MACD Histogram", "close"),
"rsi": ("RSI", "close"),
"boll": ("Bollinger Middle", "close"),
"boll_ub": ("Bollinger Upper Band", "close"),
"boll_lb": ("Bollinger Lower Band", "close"),
"atr": ("ATR", None),
"vwma": ("VWMA", "close")
}
indicator_descriptions = {
"close_50_sma": "50 SMA: A medium-term trend indicator. Usage: Identify trend direction and serve as dynamic support/resistance. Tips: It lags price; combine with faster indicators for timely signals.",
"close_200_sma": "200 SMA: A long-term trend benchmark. Usage: Confirm overall market trend and identify golden/death cross setups. Tips: It reacts slowly; best for strategic trend confirmation rather than frequent trading entries.",
"close_10_ema": "10 EMA: A responsive short-term average. Usage: Capture quick shifts in momentum and potential entry points. Tips: Prone to noise in choppy markets; use alongside longer averages for filtering false signals.",
"macd": "MACD: Computes momentum via differences of EMAs. Usage: Look for crossovers and divergence as signals of trend changes. Tips: Confirm with other indicators in low-volatility or sideways markets.",
"macds": "MACD Signal: An EMA smoothing of the MACD line. Usage: Use crossovers with the MACD line to trigger trades. Tips: Should be part of a broader strategy to avoid false positives.",
"macdh": "MACD Histogram: Shows the gap between the MACD line and its signal. Usage: Visualize momentum strength and spot divergence early. Tips: Can be volatile; complement with additional filters in fast-moving markets.",
"rsi": "RSI: Measures momentum to flag overbought/oversold conditions. Usage: Apply 70/30 thresholds and watch for divergence to signal reversals. Tips: In strong trends, RSI may remain extreme; always cross-check with trend analysis.",
"boll": "Bollinger Middle: A 20 SMA serving as the basis for Bollinger Bands. Usage: Acts as a dynamic benchmark for price movement. Tips: Combine with the upper and lower bands to effectively spot breakouts or reversals.",
"boll_ub": "Bollinger Upper Band: Typically 2 standard deviations above the middle line. Usage: Signals potential overbought conditions and breakout zones. Tips: Confirm signals with other tools; prices may ride the band in strong trends.",
"boll_lb": "Bollinger Lower Band: Typically 2 standard deviations below the middle line. Usage: Indicates potential oversold conditions. Tips: Use additional analysis to avoid false reversal signals.",
"atr": "ATR: Averages true range to measure volatility. Usage: Set stop-loss levels and adjust position sizes based on current market volatility. Tips: It's a reactive measure, so use it as part of a broader risk management strategy.",
"vwma": "VWMA: A moving average weighted by volume. Usage: Confirm trends by integrating price action with volume data. Tips: Watch for skewed results from volume spikes; use in combination with other volume analyses."
}
if indicator not in supported_indicators:
raise ValueError(
f"Indicator {indicator} is not supported. Please choose from: {list(supported_indicators.keys())}"
)
curr_date_dt = datetime.strptime(curr_date, "%Y-%m-%d")
before = curr_date_dt - relativedelta(days=look_back_days)
# Get the full data for the period instead of making individual calls
_, required_series_type = supported_indicators[indicator]
# Use the provided series_type or fall back to the required one
if required_series_type:
series_type = required_series_type
try:
# Get indicator data for the period
if indicator == "close_50_sma":
data = _make_api_request("SMA", {
"symbol": symbol,
"interval": interval,
"time_period": "50",
"series_type": series_type,
"datatype": "csv"
})
elif indicator == "close_200_sma":
data = _make_api_request("SMA", {
"symbol": symbol,
"interval": interval,
"time_period": "200",
"series_type": series_type,
"datatype": "csv"
})
elif indicator == "close_10_ema":
data = _make_api_request("EMA", {
"symbol": symbol,
"interval": interval,
"time_period": "10",
"series_type": series_type,
"datatype": "csv"
})
elif indicator == "macd":
data = _make_api_request("MACD", {
"symbol": symbol,
"interval": interval,
"series_type": series_type,
"datatype": "csv"
})
elif indicator == "macds":
data = _make_api_request("MACD", {
"symbol": symbol,
"interval": interval,
"series_type": series_type,
"datatype": "csv"
})
elif indicator == "macdh":
data = _make_api_request("MACD", {
"symbol": symbol,
"interval": interval,
"series_type": series_type,
"datatype": "csv"
})
elif indicator == "rsi":
data = _make_api_request("RSI", {
"symbol": symbol,
"interval": interval,
"time_period": str(time_period),
"series_type": series_type,
"datatype": "csv"
})
elif indicator in ["boll", "boll_ub", "boll_lb"]:
data = _make_api_request("BBANDS", {
"symbol": symbol,
"interval": interval,
"time_period": "20",
"series_type": series_type,
"datatype": "csv"
})
elif indicator == "atr":
data = _make_api_request("ATR", {
"symbol": symbol,
"interval": interval,
"time_period": str(time_period),
"datatype": "csv"
})
elif indicator == "vwma":
# Alpha Vantage doesn't have direct VWMA, so we'll return an informative message
# In a real implementation, this would need to be calculated from OHLCV data
return f"## VWMA (Volume Weighted Moving Average) for {symbol}:\n\nVWMA calculation requires OHLCV data and is not directly available from Alpha Vantage API.\nThis indicator would need to be calculated from the raw stock data using volume-weighted price averaging.\n\n{indicator_descriptions.get('vwma', 'No description available.')}"
else:
return f"Error: Indicator {indicator} not implemented yet."
# Parse CSV data and extract values for the date range
lines = data.strip().split('\n')
if len(lines) < 2:
return f"Error: No data returned for {indicator}"
# Parse header and data
header = [col.strip() for col in lines[0].split(',')]
try:
date_col_idx = header.index('time')
except ValueError:
return f"Error: 'time' column not found in data for {indicator}. Available columns: {header}"
# Map internal indicator names to expected CSV column names from Alpha Vantage
col_name_map = {
"macd": "MACD", "macds": "MACD_Signal", "macdh": "MACD_Hist",
"boll": "Real Middle Band", "boll_ub": "Real Upper Band", "boll_lb": "Real Lower Band",
"rsi": "RSI", "atr": "ATR", "close_10_ema": "EMA",
"close_50_sma": "SMA", "close_200_sma": "SMA"
}
target_col_name = col_name_map.get(indicator)
if not target_col_name:
# Default to the second column if no specific mapping exists
value_col_idx = 1
else:
try:
value_col_idx = header.index(target_col_name)
except ValueError:
return f"Error: Column '{target_col_name}' not found for indicator '{indicator}'. Available columns: {header}"
result_data = []
for line in lines[1:]:
if not line.strip():
continue
values = line.split(',')
if len(values) > value_col_idx:
try:
date_str = values[date_col_idx].strip()
# Parse the date
date_dt = datetime.strptime(date_str, "%Y-%m-%d")
# Check if date is in our range
if before <= date_dt <= curr_date_dt:
value = values[value_col_idx].strip()
result_data.append((date_dt, value))
except (ValueError, IndexError):
continue
# Sort by date and format output
result_data.sort(key=lambda x: x[0])
ind_string = ""
for date_dt, value in result_data:
ind_string += f"{date_dt.strftime('%Y-%m-%d')}: {value}\n"
if not ind_string:
ind_string = "No data available for the specified date range.\n"
result_str = (
f"## {indicator.upper()} values from {before.strftime('%Y-%m-%d')} to {curr_date}:\n\n"
+ ind_string
+ "\n\n"
+ indicator_descriptions.get(indicator, "No description available.")
)
return result_str
except Exception as e:
print(f"Error getting Alpha Vantage indicator data for {indicator}: {e}")
return f"Error retrieving {indicator} data: {str(e)}"
@@ -0,0 +1,71 @@
from .alpha_vantage_common import _make_api_request, format_datetime_for_api
def get_news(ticker, start_date, end_date) -> dict[str, str] | str:
"""Returns live and historical market news & sentiment data from premier news outlets worldwide.
Covers stocks, cryptocurrencies, forex, and topics like fiscal policy, mergers & acquisitions, IPOs.
Args:
ticker: Stock symbol for news articles.
start_date: Start date for news search.
end_date: End date for news search.
Returns:
Dictionary containing news sentiment data or JSON string.
"""
params = {
"tickers": ticker,
"time_from": format_datetime_for_api(start_date),
"time_to": format_datetime_for_api(end_date),
}
return _make_api_request("NEWS_SENTIMENT", params)
def get_global_news(curr_date, look_back_days: int = 7, limit: int = 50) -> dict[str, str] | str:
"""Returns global market news & sentiment data without ticker-specific filtering.
Covers broad market topics like financial markets, economy, and more.
Args:
curr_date: Current date in yyyy-mm-dd format.
look_back_days: Number of days to look back (default 7).
limit: Maximum number of articles (default 50).
Returns:
Dictionary containing global news sentiment data or JSON string.
"""
from datetime import datetime, timedelta
# Calculate start date
curr_dt = datetime.strptime(curr_date, "%Y-%m-%d")
start_dt = curr_dt - timedelta(days=look_back_days)
start_date = start_dt.strftime("%Y-%m-%d")
params = {
"topics": "financial_markets,economy_macro,economy_monetary",
"time_from": format_datetime_for_api(start_date),
"time_to": format_datetime_for_api(curr_date),
"limit": str(limit),
}
return _make_api_request("NEWS_SENTIMENT", params)
def get_insider_transactions(symbol: str) -> dict[str, str] | str:
"""Returns latest and historical insider transactions by key stakeholders.
Covers transactions by founders, executives, board members, etc.
Args:
symbol: Ticker symbol. Example: "IBM".
Returns:
Dictionary containing insider transaction data or JSON string.
"""
params = {
"symbol": symbol,
}
return _make_api_request("INSIDER_TRANSACTIONS", params)
@@ -0,0 +1,38 @@
from datetime import datetime
from .alpha_vantage_common import _make_api_request, _filter_csv_by_date_range
def get_stock(
symbol: str,
start_date: str,
end_date: str
) -> str:
"""
Returns raw daily OHLCV values, adjusted close values, and historical split/dividend events
filtered to the specified date range.
Args:
symbol: The name of the equity. For example: symbol=IBM
start_date: Start date in yyyy-mm-dd format
end_date: End date in yyyy-mm-dd format
Returns:
CSV string containing the daily adjusted time series data filtered to the date range.
"""
# Parse dates to determine the range
start_dt = datetime.strptime(start_date, "%Y-%m-%d")
today = datetime.now()
# Choose outputsize based on whether the requested range is within the latest 100 days
# Compact returns latest 100 data points, so check if start_date is recent enough
days_from_today_to_start = (today - start_dt).days
outputsize = "compact" if days_from_today_to_start < 100 else "full"
params = {
"symbol": symbol,
"outputsize": outputsize,
"datatype": "csv",
}
response = _make_api_request("TIME_SERIES_DAILY_ADJUSTED", params)
return _filter_csv_by_date_range(response, start_date, end_date)
@@ -0,0 +1,31 @@
import tradingagents.default_config as default_config
from typing import Dict, Optional
# Use default config but allow it to be overridden
_config: Optional[Dict] = None
def initialize_config():
"""Initialize the configuration with default values."""
global _config
if _config is None:
_config = default_config.DEFAULT_CONFIG.copy()
def set_config(config: Dict):
"""Update the configuration with custom values."""
global _config
if _config is None:
_config = default_config.DEFAULT_CONFIG.copy()
_config.update(config)
def get_config() -> Dict:
"""Get the current configuration."""
if _config is None:
initialize_config()
return _config.copy()
# Initialize with default config
initialize_config()
@@ -0,0 +1,182 @@
from typing import Annotated
# Import from vendor-specific modules
from .y_finance import (
get_YFin_data_online,
get_stock_stats_indicators_window,
get_fundamentals as get_yfinance_fundamentals,
get_balance_sheet as get_yfinance_balance_sheet,
get_cashflow as get_yfinance_cashflow,
get_income_statement as get_yfinance_income_statement,
get_insider_transactions as get_yfinance_insider_transactions,
)
from .yfinance_news import get_news_yfinance, get_global_news_yfinance
from .alpha_vantage import (
get_stock as get_alpha_vantage_stock,
get_indicator as get_alpha_vantage_indicator,
get_fundamentals as get_alpha_vantage_fundamentals,
get_balance_sheet as get_alpha_vantage_balance_sheet,
get_cashflow as get_alpha_vantage_cashflow,
get_income_statement as get_alpha_vantage_income_statement,
get_insider_transactions as get_alpha_vantage_insider_transactions,
get_news as get_alpha_vantage_news,
get_global_news as get_alpha_vantage_global_news,
)
from .alpha_vantage_common import AlphaVantageRateLimitError
from .akshare_data import (
get_stock_data as get_akshare_stock,
get_indicators as get_akshare_indicator,
get_fundamentals as get_akshare_fundamentals,
get_balance_sheet as get_akshare_balance_sheet,
get_cashflow as get_akshare_cashflow,
get_income_statement as get_akshare_income_statement,
get_insider_transactions as get_akshare_insider_transactions,
)
from .akshare_news import get_news as get_akshare_news, get_global_news as get_akshare_global_news
# Configuration and routing logic
from .config import get_config
# Tools organized by category
TOOLS_CATEGORIES = {
"core_stock_apis": {
"description": "OHLCV stock price data",
"tools": [
"get_stock_data"
]
},
"technical_indicators": {
"description": "Technical analysis indicators",
"tools": [
"get_indicators"
]
},
"fundamental_data": {
"description": "Company fundamentals",
"tools": [
"get_fundamentals",
"get_balance_sheet",
"get_cashflow",
"get_income_statement"
]
},
"news_data": {
"description": "News and insider data",
"tools": [
"get_news",
"get_global_news",
"get_insider_transactions",
]
}
}
VENDOR_LIST = [
"yfinance",
"alpha_vantage",
"akshare",
]
# Mapping of methods to their vendor-specific implementations
VENDOR_METHODS = {
# core_stock_apis
"get_stock_data": {
"alpha_vantage": get_alpha_vantage_stock,
"yfinance": get_YFin_data_online,
"akshare": get_akshare_stock,
},
# technical_indicators
"get_indicators": {
"alpha_vantage": get_alpha_vantage_indicator,
"yfinance": get_stock_stats_indicators_window,
"akshare": get_akshare_indicator,
},
# fundamental_data
"get_fundamentals": {
"alpha_vantage": get_alpha_vantage_fundamentals,
"yfinance": get_yfinance_fundamentals,
"akshare": get_akshare_fundamentals,
},
"get_balance_sheet": {
"alpha_vantage": get_alpha_vantage_balance_sheet,
"yfinance": get_yfinance_balance_sheet,
"akshare": get_akshare_balance_sheet,
},
"get_cashflow": {
"alpha_vantage": get_alpha_vantage_cashflow,
"yfinance": get_yfinance_cashflow,
"akshare": get_akshare_cashflow,
},
"get_income_statement": {
"alpha_vantage": get_alpha_vantage_income_statement,
"yfinance": get_yfinance_income_statement,
"akshare": get_akshare_income_statement,
},
# news_data
"get_news": {
"alpha_vantage": get_alpha_vantage_news,
"yfinance": get_news_yfinance,
"akshare": get_akshare_news,
},
"get_global_news": {
"yfinance": get_global_news_yfinance,
"alpha_vantage": get_alpha_vantage_global_news,
"akshare": get_akshare_global_news,
},
"get_insider_transactions": {
"akshare": get_akshare_insider_transactions,
"yfinance": get_yfinance_insider_transactions,
"alpha_vantage": get_alpha_vantage_insider_transactions,
},
}
def get_category_for_method(method: str) -> str:
"""Get the category that contains the specified method."""
for category, info in TOOLS_CATEGORIES.items():
if method in info["tools"]:
return category
raise ValueError(f"Method '{method}' not found in any category")
def get_vendor(category: str, method: str = None) -> str:
"""Get the configured vendor for a data category or specific tool method.
Tool-level configuration takes precedence over category-level.
"""
config = get_config()
# Check tool-level configuration first (if method provided)
if method:
tool_vendors = config.get("tool_vendors", {})
if method in tool_vendors:
return tool_vendors[method]
# Fall back to category-level configuration
return config.get("data_vendors", {}).get(category, "default")
def route_to_vendor(method: str, *args, **kwargs):
"""Route method calls to appropriate vendor implementation with fallback support."""
category = get_category_for_method(method)
vendor_config = get_vendor(category, method)
primary_vendors = [v.strip() for v in vendor_config.split(',')]
if method not in VENDOR_METHODS:
raise ValueError(f"Method '{method}' not supported")
# Build fallback chain: primary vendors first, then remaining available vendors
all_available_vendors = list(VENDOR_METHODS[method].keys())
fallback_vendors = primary_vendors.copy()
for vendor in all_available_vendors:
if vendor not in fallback_vendors:
fallback_vendors.append(vendor)
for vendor in fallback_vendors:
if vendor not in VENDOR_METHODS[method]:
continue
vendor_impl = VENDOR_METHODS[method][vendor]
impl_func = vendor_impl[0] if isinstance(vendor_impl, list) else vendor_impl
try:
return impl_func(*args, **kwargs)
except AlphaVantageRateLimitError:
continue # Only rate limits trigger fallback
raise RuntimeError(f"No available vendor for '{method}'")
@@ -0,0 +1,133 @@
import time
import logging
import pandas as pd
import yfinance as yf
from yfinance.exceptions import YFRateLimitError
from stockstats import wrap
from typing import Annotated
import os
from .config import get_config
from .utils import safe_ticker_component
logger = logging.getLogger(__name__)
def yf_retry(func, max_retries=3, base_delay=2.0):
"""Execute a yfinance call with exponential backoff on rate limits.
yfinance raises YFRateLimitError on HTTP 429 responses but does not
retry them internally. This wrapper adds retry logic specifically
for rate limits. Other exceptions propagate immediately.
"""
for attempt in range(max_retries + 1):
try:
return func()
except YFRateLimitError:
if attempt < max_retries:
delay = base_delay * (2 ** attempt)
logger.warning(f"Yahoo Finance rate limited, retrying in {delay:.0f}s (attempt {attempt + 1}/{max_retries})")
time.sleep(delay)
else:
raise
def _clean_dataframe(data: pd.DataFrame) -> pd.DataFrame:
"""Normalize a stock DataFrame for stockstats: parse dates, drop invalid rows, fill price gaps."""
data["Date"] = pd.to_datetime(data["Date"], errors="coerce")
data = data.dropna(subset=["Date"])
price_cols = [c for c in ["Open", "High", "Low", "Close", "Volume"] if c in data.columns]
data[price_cols] = data[price_cols].apply(pd.to_numeric, errors="coerce")
data = data.dropna(subset=["Close"])
data[price_cols] = data[price_cols].ffill().bfill()
return data
def load_ohlcv(symbol: str, curr_date: str) -> pd.DataFrame:
"""Fetch OHLCV data with caching, filtered to prevent look-ahead bias.
Downloads 15 years of data up to today and caches per symbol. On
subsequent calls the cache is reused. Rows after curr_date are
filtered out so backtests never see future prices.
"""
# Reject ticker values that would escape the cache directory when
# interpolated into the cache filename (e.g. ``../../tmp/x``).
safe_symbol = safe_ticker_component(symbol)
config = get_config()
curr_date_dt = pd.to_datetime(curr_date)
# Cache uses a fixed window (15y to today) so one file per symbol
today_date = pd.Timestamp.today()
start_date = today_date - pd.DateOffset(years=5)
start_str = start_date.strftime("%Y-%m-%d")
end_str = today_date.strftime("%Y-%m-%d")
os.makedirs(config["data_cache_dir"], exist_ok=True)
data_file = os.path.join(
config["data_cache_dir"],
f"{safe_symbol}-YFin-data-{start_str}-{end_str}.csv",
)
if os.path.exists(data_file):
data = pd.read_csv(data_file, on_bad_lines="skip", encoding="utf-8")
else:
data = yf_retry(lambda: yf.download(
symbol,
start=start_str,
end=end_str,
multi_level_index=False,
progress=False,
auto_adjust=True,
))
data = data.reset_index()
data.to_csv(data_file, index=False, encoding="utf-8")
data = _clean_dataframe(data)
# Filter to curr_date to prevent look-ahead bias in backtesting
data = data[data["Date"] <= curr_date_dt]
return data
def filter_financials_by_date(data: pd.DataFrame, curr_date: str) -> pd.DataFrame:
"""Drop financial statement columns (fiscal period timestamps) after curr_date.
yfinance financial statements use fiscal period end dates as columns.
Columns after curr_date represent future data and are removed to
prevent look-ahead bias.
"""
if not curr_date or data.empty:
return data
cutoff = pd.Timestamp(curr_date)
mask = pd.to_datetime(data.columns, errors="coerce") <= cutoff
return data.loc[:, mask]
class StockstatsUtils:
@staticmethod
def get_stock_stats(
symbol: Annotated[str, "ticker symbol for the company"],
indicator: Annotated[
str, "quantitative indicators based off of the stock data for the company"
],
curr_date: Annotated[
str, "curr date for retrieving stock price data, YYYY-mm-dd"
],
):
data = load_ohlcv(symbol, curr_date)
df = wrap(data)
df["Date"] = df["Date"].dt.strftime("%Y-%m-%d")
curr_date_str = pd.to_datetime(curr_date).strftime("%Y-%m-%d")
df[indicator] # trigger stockstats to calculate the indicator
matching_rows = df[df["Date"].str.startswith(curr_date_str)]
if not matching_rows.empty:
indicator_value = matching_rows[indicator].values[0]
return indicator_value
else:
return "N/A: Not a trading day (weekend or holiday)"
@@ -0,0 +1,74 @@
import os
import re
import json
import pandas as pd
from datetime import date, timedelta, datetime
from typing import Annotated
SavePathType = Annotated[str, "File path to save data. If None, data is not saved."]
# Tickers can contain letters, digits, dot, dash, underscore, and caret
# (for index symbols like ^GSPC). Anything else is rejected so the value
# never escapes a containing directory when interpolated into a path.
_TICKER_PATH_RE = re.compile(r"^[A-Za-z0-9._\-\^]+$")
def safe_ticker_component(value: str, *, max_len: int = 32) -> str:
"""Validate ``value`` is safe to interpolate into a filesystem path.
Tickers come from user CLI input or from LLM tool calls, both of which
can be influenced by attacker-controlled content (e.g. prompt injection
embedded in fetched news). Without validation, a value like
``"../../../etc/foo"`` flows into ``os.path.join`` / ``Path /`` and
escapes the configured cache, checkpoint, or results directory.
Returns ``value`` unchanged when it matches the allowed pattern; raises
``ValueError`` otherwise.
"""
if not isinstance(value, str) or not value:
raise ValueError(f"ticker must be a non-empty string, got {value!r}")
if len(value) > max_len:
raise ValueError(f"ticker exceeds {max_len} chars: {value!r}")
if not _TICKER_PATH_RE.fullmatch(value):
raise ValueError(
f"ticker contains characters not allowed in a filesystem path: {value!r}"
)
# The regex above allows '.', so values like '.', '..', '...' would pass,
# and as a path component they traverse the parent directory. Reject any
# value that's only dots.
if set(value) == {"."}:
raise ValueError(f"ticker cannot consist solely of dots: {value!r}")
return value
def save_output(data: pd.DataFrame, tag: str, save_path: SavePathType = None) -> None:
if save_path:
data.to_csv(save_path, encoding="utf-8")
print(f"{tag} saved to {save_path}")
def get_current_date():
return date.today().strftime("%Y-%m-%d")
def decorate_all_methods(decorator):
def class_decorator(cls):
for attr_name, attr_value in cls.__dict__.items():
if callable(attr_value):
setattr(cls, attr_name, decorator(attr_value))
return cls
return class_decorator
def get_next_weekday(date):
if not isinstance(date, datetime):
date = datetime.strptime(date, "%Y-%m-%d")
if date.weekday() >= 5:
days_to_add = 7 - date.weekday()
next_weekday = date + timedelta(days=days_to_add)
return next_weekday
else:
return date
@@ -0,0 +1,422 @@
from typing import Annotated
from datetime import datetime
from dateutil.relativedelta import relativedelta
import pandas as pd
import yfinance as yf
import os
from .stockstats_utils import StockstatsUtils, _clean_dataframe, yf_retry, load_ohlcv, filter_financials_by_date
def get_YFin_data_online(
symbol: Annotated[str, "ticker symbol of the company"],
start_date: Annotated[str, "Start date in yyyy-mm-dd format"],
end_date: Annotated[str, "End date in yyyy-mm-dd format"],
):
datetime.strptime(start_date, "%Y-%m-%d")
datetime.strptime(end_date, "%Y-%m-%d")
# Create ticker object
ticker = yf.Ticker(symbol.upper())
# Fetch historical data for the specified date range
data = yf_retry(lambda: ticker.history(start=start_date, end=end_date))
# Check if data is empty
if data.empty:
return (
f"No data found for symbol '{symbol}' between {start_date} and {end_date}"
)
# Remove timezone info from index for cleaner output
if data.index.tz is not None:
data.index = data.index.tz_localize(None)
# Round numerical values to 2 decimal places for cleaner display
numeric_columns = ["Open", "High", "Low", "Close", "Adj Close"]
for col in numeric_columns:
if col in data.columns:
data[col] = data[col].round(2)
# Convert DataFrame to CSV string
csv_string = data.to_csv()
# Add header information
header = f"# Stock data for {symbol.upper()} from {start_date} to {end_date}\n"
header += f"# Total records: {len(data)}\n"
header += f"# Data retrieved on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n"
return header + csv_string
def get_stock_stats_indicators_window(
symbol: Annotated[str, "ticker symbol of the company"],
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"],
) -> str:
best_ind_params = {
# Moving Averages
"close_50_sma": (
"50 SMA: A medium-term trend indicator. "
"Usage: Identify trend direction and serve as dynamic support/resistance. "
"Tips: It lags price; combine with faster indicators for timely signals."
),
"close_200_sma": (
"200 SMA: A long-term trend benchmark. "
"Usage: Confirm overall market trend and identify golden/death cross setups. "
"Tips: It reacts slowly; best for strategic trend confirmation rather than frequent trading entries."
),
"close_10_ema": (
"10 EMA: A responsive short-term average. "
"Usage: Capture quick shifts in momentum and potential entry points. "
"Tips: Prone to noise in choppy markets; use alongside longer averages for filtering false signals."
),
# MACD Related
"macd": (
"MACD: Computes momentum via differences of EMAs. "
"Usage: Look for crossovers and divergence as signals of trend changes. "
"Tips: Confirm with other indicators in low-volatility or sideways markets."
),
"macds": (
"MACD Signal: An EMA smoothing of the MACD line. "
"Usage: Use crossovers with the MACD line to trigger trades. "
"Tips: Should be part of a broader strategy to avoid false positives."
),
"macdh": (
"MACD Histogram: Shows the gap between the MACD line and its signal. "
"Usage: Visualize momentum strength and spot divergence early. "
"Tips: Can be volatile; complement with additional filters in fast-moving markets."
),
# Momentum Indicators
"rsi": (
"RSI: Measures momentum to flag overbought/oversold conditions. "
"Usage: Apply 70/30 thresholds and watch for divergence to signal reversals. "
"Tips: In strong trends, RSI may remain extreme; always cross-check with trend analysis."
),
# Volatility Indicators
"boll": (
"Bollinger Middle: A 20 SMA serving as the basis for Bollinger Bands. "
"Usage: Acts as a dynamic benchmark for price movement. "
"Tips: Combine with the upper and lower bands to effectively spot breakouts or reversals."
),
"boll_ub": (
"Bollinger Upper Band: Typically 2 standard deviations above the middle line. "
"Usage: Signals potential overbought conditions and breakout zones. "
"Tips: Confirm signals with other tools; prices may ride the band in strong trends."
),
"boll_lb": (
"Bollinger Lower Band: Typically 2 standard deviations below the middle line. "
"Usage: Indicates potential oversold conditions. "
"Tips: Use additional analysis to avoid false reversal signals."
),
"atr": (
"ATR: Averages true range to measure volatility. "
"Usage: Set stop-loss levels and adjust position sizes based on current market volatility. "
"Tips: It's a reactive measure, so use it as part of a broader risk management strategy."
),
# Volume-Based Indicators
"vwma": (
"VWMA: A moving average weighted by volume. "
"Usage: Confirm trends by integrating price action with volume data. "
"Tips: Watch for skewed results from volume spikes; use in combination with other volume analyses."
),
"mfi": (
"MFI: The Money Flow Index is a momentum indicator that uses both price and volume to measure buying and selling pressure. "
"Usage: Identify overbought (>80) or oversold (<20) conditions and confirm the strength of trends or reversals. "
"Tips: Use alongside RSI or MACD to confirm signals; divergence between price and MFI can indicate potential reversals."
),
}
if indicator not in best_ind_params:
raise ValueError(
f"Indicator {indicator} is not supported. Please choose from: {list(best_ind_params.keys())}"
)
end_date = curr_date
curr_date_dt = datetime.strptime(curr_date, "%Y-%m-%d")
before = curr_date_dt - relativedelta(days=look_back_days)
# Optimized: Get stock data once and calculate indicators for all dates
try:
indicator_data = _get_stock_stats_bulk(symbol, indicator, curr_date)
# Generate the date range we need
current_dt = curr_date_dt
date_values = []
while current_dt >= before:
date_str = current_dt.strftime('%Y-%m-%d')
# Look up the indicator value for this date
if date_str in indicator_data:
indicator_value = indicator_data[date_str]
else:
indicator_value = "N/A: Not a trading day (weekend or holiday)"
date_values.append((date_str, indicator_value))
current_dt = current_dt - relativedelta(days=1)
# Build the result string
ind_string = ""
for date_str, value in date_values:
ind_string += f"{date_str}: {value}\n"
except Exception as e:
print(f"Error getting bulk stockstats data: {e}")
# Fallback to original implementation if bulk method fails
ind_string = ""
curr_date_dt = datetime.strptime(curr_date, "%Y-%m-%d")
while curr_date_dt >= before:
indicator_value = get_stockstats_indicator(
symbol, indicator, curr_date_dt.strftime("%Y-%m-%d")
)
ind_string += f"{curr_date_dt.strftime('%Y-%m-%d')}: {indicator_value}\n"
curr_date_dt = curr_date_dt - relativedelta(days=1)
result_str = (
f"## {indicator} values from {before.strftime('%Y-%m-%d')} to {end_date}:\n\n"
+ ind_string
+ "\n\n"
+ best_ind_params.get(indicator, "No description available.")
)
return result_str
def _get_stock_stats_bulk(
symbol: Annotated[str, "ticker symbol of the company"],
indicator: Annotated[str, "technical indicator to calculate"],
curr_date: Annotated[str, "current date for reference"]
) -> dict:
"""
Optimized bulk calculation of stock stats indicators.
Fetches data once and calculates indicator for all available dates.
Returns dict mapping date strings to indicator values.
"""
from stockstats import wrap
data = load_ohlcv(symbol, curr_date)
df = wrap(data)
df["Date"] = df["Date"].dt.strftime("%Y-%m-%d")
# Calculate the indicator for all rows at once
df[indicator] # This triggers stockstats to calculate the indicator
# Create a dictionary mapping date strings to indicator values
result_dict = {}
for _, row in df.iterrows():
date_str = row["Date"]
indicator_value = row[indicator]
# Handle NaN/None values
if pd.isna(indicator_value):
result_dict[date_str] = "N/A"
else:
result_dict[date_str] = str(indicator_value)
return result_dict
def get_stockstats_indicator(
symbol: Annotated[str, "ticker symbol of the company"],
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"
],
) -> str:
curr_date_dt = datetime.strptime(curr_date, "%Y-%m-%d")
curr_date = curr_date_dt.strftime("%Y-%m-%d")
try:
indicator_value = StockstatsUtils.get_stock_stats(
symbol,
indicator,
curr_date,
)
except Exception as e:
print(
f"Error getting stockstats indicator data for indicator {indicator} on {curr_date}: {e}"
)
return ""
return str(indicator_value)
def get_fundamentals(
ticker: Annotated[str, "ticker symbol of the company"],
curr_date: Annotated[str, "current date (not used for yfinance)"] = None
):
"""Get company fundamentals overview from yfinance."""
try:
ticker_obj = yf.Ticker(ticker.upper())
info = yf_retry(lambda: ticker_obj.info)
if not info:
return f"No fundamentals data found for symbol '{ticker}'"
fields = [
("Name", info.get("longName")),
("Sector", info.get("sector")),
("Industry", info.get("industry")),
("Market Cap", info.get("marketCap")),
("PE Ratio (TTM)", info.get("trailingPE")),
("Forward PE", info.get("forwardPE")),
("PEG Ratio", info.get("pegRatio")),
("Price to Book", info.get("priceToBook")),
("EPS (TTM)", info.get("trailingEps")),
("Forward EPS", info.get("forwardEps")),
("Dividend Yield", info.get("dividendYield")),
("Beta", info.get("beta")),
("52 Week High", info.get("fiftyTwoWeekHigh")),
("52 Week Low", info.get("fiftyTwoWeekLow")),
("50 Day Average", info.get("fiftyDayAverage")),
("200 Day Average", info.get("twoHundredDayAverage")),
("Revenue (TTM)", info.get("totalRevenue")),
("Gross Profit", info.get("grossProfits")),
("EBITDA", info.get("ebitda")),
("Net Income", info.get("netIncomeToCommon")),
("Profit Margin", info.get("profitMargins")),
("Operating Margin", info.get("operatingMargins")),
("Return on Equity", info.get("returnOnEquity")),
("Return on Assets", info.get("returnOnAssets")),
("Debt to Equity", info.get("debtToEquity")),
("Current Ratio", info.get("currentRatio")),
("Book Value", info.get("bookValue")),
("Free Cash Flow", info.get("freeCashflow")),
]
lines = []
for label, value in fields:
if value is not None:
lines.append(f"{label}: {value}")
header = f"# Company Fundamentals for {ticker.upper()}\n"
header += f"# Data retrieved on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n"
return header + "\n".join(lines)
except Exception as e:
return f"Error retrieving fundamentals for {ticker}: {str(e)}"
def get_balance_sheet(
ticker: Annotated[str, "ticker symbol of the company"],
freq: Annotated[str, "frequency of data: 'annual' or 'quarterly'"] = "quarterly",
curr_date: Annotated[str, "current date in YYYY-MM-DD format"] = None
):
"""Get balance sheet data from yfinance."""
try:
ticker_obj = yf.Ticker(ticker.upper())
if freq.lower() == "quarterly":
data = yf_retry(lambda: ticker_obj.quarterly_balance_sheet)
else:
data = yf_retry(lambda: ticker_obj.balance_sheet)
data = filter_financials_by_date(data, curr_date)
if data.empty:
return f"No balance sheet data found for symbol '{ticker}'"
# Convert to CSV string for consistency with other functions
csv_string = data.to_csv()
# Add header information
header = f"# Balance Sheet data for {ticker.upper()} ({freq})\n"
header += f"# Data retrieved on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n"
return header + csv_string
except Exception as e:
return f"Error retrieving balance sheet for {ticker}: {str(e)}"
def get_cashflow(
ticker: Annotated[str, "ticker symbol of the company"],
freq: Annotated[str, "frequency of data: 'annual' or 'quarterly'"] = "quarterly",
curr_date: Annotated[str, "current date in YYYY-MM-DD format"] = None
):
"""Get cash flow data from yfinance."""
try:
ticker_obj = yf.Ticker(ticker.upper())
if freq.lower() == "quarterly":
data = yf_retry(lambda: ticker_obj.quarterly_cashflow)
else:
data = yf_retry(lambda: ticker_obj.cashflow)
data = filter_financials_by_date(data, curr_date)
if data.empty:
return f"No cash flow data found for symbol '{ticker}'"
# Convert to CSV string for consistency with other functions
csv_string = data.to_csv()
# Add header information
header = f"# Cash Flow data for {ticker.upper()} ({freq})\n"
header += f"# Data retrieved on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n"
return header + csv_string
except Exception as e:
return f"Error retrieving cash flow for {ticker}: {str(e)}"
def get_income_statement(
ticker: Annotated[str, "ticker symbol of the company"],
freq: Annotated[str, "frequency of data: 'annual' or 'quarterly'"] = "quarterly",
curr_date: Annotated[str, "current date in YYYY-MM-DD format"] = None
):
"""Get income statement data from yfinance."""
try:
ticker_obj = yf.Ticker(ticker.upper())
if freq.lower() == "quarterly":
data = yf_retry(lambda: ticker_obj.quarterly_income_stmt)
else:
data = yf_retry(lambda: ticker_obj.income_stmt)
data = filter_financials_by_date(data, curr_date)
if data.empty:
return f"No income statement data found for symbol '{ticker}'"
# Convert to CSV string for consistency with other functions
csv_string = data.to_csv()
# Add header information
header = f"# Income Statement data for {ticker.upper()} ({freq})\n"
header += f"# Data retrieved on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n"
return header + csv_string
except Exception as e:
return f"Error retrieving income statement for {ticker}: {str(e)}"
def get_insider_transactions(
ticker: Annotated[str, "ticker symbol of the company"]
):
"""Get insider transactions data from yfinance."""
try:
ticker_obj = yf.Ticker(ticker.upper())
data = yf_retry(lambda: ticker_obj.insider_transactions)
if data is None or data.empty:
return f"No insider transactions data found for symbol '{ticker}'"
# Convert to CSV string for consistency with other functions
csv_string = data.to_csv()
# Add header information
header = f"# Insider Transactions data for {ticker.upper()}\n"
header += f"# Data retrieved on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n"
return header + csv_string
except Exception as e:
return f"Error retrieving insider transactions for {ticker}: {str(e)}"
@@ -0,0 +1,197 @@
"""yfinance-based news data fetching functions."""
import yfinance as yf
from datetime import datetime
from dateutil.relativedelta import relativedelta
from .stockstats_utils import yf_retry
def _extract_article_data(article: dict) -> dict:
"""Extract article data from yfinance news format (handles nested 'content' structure)."""
# Handle nested content structure
if "content" in article:
content = article["content"]
title = content.get("title", "No title")
summary = content.get("summary", "")
provider = content.get("provider", {})
publisher = provider.get("displayName", "Unknown")
# Get URL from canonicalUrl or clickThroughUrl
url_obj = content.get("canonicalUrl") or content.get("clickThroughUrl") or {}
link = url_obj.get("url", "")
# Get publish date
pub_date_str = content.get("pubDate", "")
pub_date = None
if pub_date_str:
try:
pub_date = datetime.fromisoformat(pub_date_str.replace("Z", "+00:00"))
except (ValueError, AttributeError):
pass
return {
"title": title,
"summary": summary,
"publisher": publisher,
"link": link,
"pub_date": pub_date,
}
else:
# Fallback for flat structure
return {
"title": article.get("title", "No title"),
"summary": article.get("summary", ""),
"publisher": article.get("publisher", "Unknown"),
"link": article.get("link", ""),
"pub_date": None,
}
def get_news_yfinance(
ticker: str,
start_date: str,
end_date: str,
) -> str:
"""
Retrieve news for a specific stock ticker using yfinance.
Args:
ticker: Stock ticker symbol (e.g., "AAPL")
start_date: Start date in yyyy-mm-dd format
end_date: End date in yyyy-mm-dd format
Returns:
Formatted string containing news articles
"""
try:
stock = yf.Ticker(ticker)
news = yf_retry(lambda: stock.get_news(count=20))
if not news:
return f"No news found for {ticker}"
# Parse date range for filtering
start_dt = datetime.strptime(start_date, "%Y-%m-%d")
end_dt = datetime.strptime(end_date, "%Y-%m-%d")
news_str = ""
filtered_count = 0
for article in news:
data = _extract_article_data(article)
# Filter by date if publish time is available
if data["pub_date"]:
pub_date_naive = data["pub_date"].replace(tzinfo=None)
if not (start_dt <= pub_date_naive <= end_dt + relativedelta(days=1)):
continue
news_str += f"### {data['title']} (source: {data['publisher']})\n"
if data["summary"]:
news_str += f"{data['summary']}\n"
if data["link"]:
news_str += f"Link: {data['link']}\n"
news_str += "\n"
filtered_count += 1
if filtered_count == 0:
return f"No news found for {ticker} between {start_date} and {end_date}"
return f"## {ticker} News, from {start_date} to {end_date}:\n\n{news_str}"
except Exception as e:
return f"Error fetching news for {ticker}: {str(e)}"
def get_global_news_yfinance(
curr_date: str,
look_back_days: int = 7,
limit: int = 10,
) -> str:
"""
Retrieve global/macro economic news using yfinance Search.
Args:
curr_date: Current date in yyyy-mm-dd format
look_back_days: Number of days to look back
limit: Maximum number of articles to return
Returns:
Formatted string containing global news articles
"""
# Search queries for macro/global news
search_queries = [
"stock market economy",
"Federal Reserve interest rates",
"inflation economic outlook",
"global markets trading",
]
all_news = []
seen_titles = set()
try:
for query in search_queries:
search = yf_retry(lambda q=query: yf.Search(
query=q,
news_count=limit,
enable_fuzzy_query=True,
))
if search.news:
for article in search.news:
# Handle both flat and nested structures
if "content" in article:
data = _extract_article_data(article)
title = data["title"]
else:
title = article.get("title", "")
# Deduplicate by title
if title and title not in seen_titles:
seen_titles.add(title)
all_news.append(article)
if len(all_news) >= limit:
break
if not all_news:
return f"No global news found for {curr_date}"
# Calculate date range
curr_dt = datetime.strptime(curr_date, "%Y-%m-%d")
start_dt = curr_dt - relativedelta(days=look_back_days)
start_date = start_dt.strftime("%Y-%m-%d")
news_str = ""
for article in all_news[:limit]:
# Handle both flat and nested structures
if "content" in article:
data = _extract_article_data(article)
# Skip articles published after curr_date (look-ahead guard)
if data.get("pub_date"):
pub_naive = data["pub_date"].replace(tzinfo=None) if hasattr(data["pub_date"], "replace") else data["pub_date"]
if pub_naive > curr_dt + relativedelta(days=1):
continue
title = data["title"]
publisher = data["publisher"]
link = data["link"]
summary = data["summary"]
else:
title = article.get("title", "No title")
publisher = article.get("publisher", "Unknown")
link = article.get("link", "")
summary = ""
news_str += f"### {title} (source: {publisher})\n"
if summary:
news_str += f"{summary}\n"
if link:
news_str += f"Link: {link}\n"
news_str += "\n"
return f"## Global Market News, from {start_date} to {curr_date}:\n\n{news_str}"
except Exception as e:
return f"Error fetching global news: {str(e)}"