fix: make news article limits and lookback window configurable (fixes #562)
Three parameters were hardcoded with no way to override via config: - count=20 in yf.Ticker.get_news() inside get_news_yfinance - look_back_days=7 default in get_global_news_yfinance - limit=10 default in get_global_news_yfinance Add three new keys to DEFAULT_CONFIG: - news_article_limit (default 20): max articles per ticker for get_news - global_news_lookback_days (default 7): lookback window for get_global_news - global_news_article_limit (default 10): max global articles for get_global_news Both yfinance and alpha_vantage news implementations now read these values from config, allowing users running weekly/monthly strategies to increase coverage without modifying library source code.
This commit is contained in:
parent
fa4d01c23a
commit
09a9062ec9
|
|
@ -1,4 +1,5 @@
|
|||
from .alpha_vantage_common import _make_api_request, format_datetime_for_api
|
||||
from .config import get_config
|
||||
|
||||
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.
|
||||
|
|
@ -22,21 +23,27 @@ def get_news(ticker, start_date, end_date) -> dict[str, str] | str:
|
|||
|
||||
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:
|
||||
def get_global_news(curr_date, look_back_days: int = None, limit: int = None) -> 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).
|
||||
look_back_days: Number of days to look back (defaults to global_news_lookback_days config).
|
||||
limit: Maximum number of articles (defaults to global_news_article_limit config).
|
||||
|
||||
Returns:
|
||||
Dictionary containing global news sentiment data or JSON string.
|
||||
"""
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
config = get_config()
|
||||
if look_back_days is None:
|
||||
look_back_days = config.get("global_news_lookback_days", 7)
|
||||
if limit is None:
|
||||
limit = config.get("global_news_article_limit", 50)
|
||||
|
||||
# Calculate start date
|
||||
curr_dt = datetime.strptime(curr_date, "%Y-%m-%d")
|
||||
start_dt = curr_dt - timedelta(days=look_back_days)
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ from datetime import datetime
|
|||
from dateutil.relativedelta import relativedelta
|
||||
|
||||
from .stockstats_utils import yf_retry
|
||||
from .config import get_config
|
||||
|
||||
|
||||
def _extract_article_data(article: dict) -> dict:
|
||||
|
|
@ -66,7 +67,8 @@ def get_news_yfinance(
|
|||
"""
|
||||
try:
|
||||
stock = yf.Ticker(ticker)
|
||||
news = yf_retry(lambda: stock.get_news(count=20))
|
||||
count = get_config().get("news_article_limit", 20)
|
||||
news = yf_retry(lambda: stock.get_news(count=count))
|
||||
|
||||
if not news:
|
||||
return f"No news found for {ticker}"
|
||||
|
|
@ -106,20 +108,25 @@ def get_news_yfinance(
|
|||
|
||||
def get_global_news_yfinance(
|
||||
curr_date: str,
|
||||
look_back_days: int = 7,
|
||||
limit: int = 10,
|
||||
look_back_days: int = None,
|
||||
limit: int = None,
|
||||
) -> 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
|
||||
look_back_days: Number of days to look back (defaults to global_news_lookback_days config)
|
||||
limit: Maximum number of articles to return (defaults to global_news_article_limit config)
|
||||
|
||||
Returns:
|
||||
Formatted string containing global news articles
|
||||
"""
|
||||
config = get_config()
|
||||
if look_back_days is None:
|
||||
look_back_days = config.get("global_news_lookback_days", 7)
|
||||
if limit is None:
|
||||
limit = config.get("global_news_article_limit", 10)
|
||||
# Search queries for macro/global news
|
||||
search_queries = [
|
||||
"stock market economy",
|
||||
|
|
|
|||
|
|
@ -22,6 +22,10 @@ DEFAULT_CONFIG = {
|
|||
"max_debate_rounds": 1,
|
||||
"max_risk_discuss_rounds": 1,
|
||||
"max_recur_limit": 100,
|
||||
# News fetching configuration
|
||||
"news_article_limit": 20, # max articles per ticker for get_news
|
||||
"global_news_lookback_days": 7, # lookback window (days) for get_global_news
|
||||
"global_news_article_limit": 10, # max articles returned by get_global_news
|
||||
# Data vendor configuration
|
||||
# Category-level configuration (default for all tools in category)
|
||||
"data_vendors": {
|
||||
|
|
|
|||
Loading…
Reference in New Issue