TradingAgents/tradingagents/services/news_service.py

112 lines
3.0 KiB
Python

"""
News service that provides structured news context.
"""
import logging
from datetime import datetime
from typing import Any
from tradingagents.clients.base import BaseClient
from tradingagents.models.context import (
ArticleData,
NewsContext,
SentimentScore,
)
from tradingagents.repositories.base import BaseRepository
from .base import BaseService
logger = logging.getLogger(__name__)
class NewsService(BaseService):
"""Service for news data and sentiment analysis."""
def __init__(
self,
finnhub_client: BaseClient | None = None,
google_client: BaseClient | None = None,
repository: BaseRepository | None = None,
online_mode: bool = True,
**kwargs,
):
"""
Initialize news service.
Args:
finnhub_client: Client for Finnhub news data
google_client: Client for Google News data
repository: Repository for cached news data
online_mode: Whether to use live data
**kwargs: Additional configuration
"""
super().__init__(online_mode, **kwargs)
self.finnhub_client = finnhub_client
self.google_client = google_client
self.repository = repository
def get_context(
self,
query: str,
start_date: str,
end_date: str,
symbol: str | None = None,
sources: list[str] | None = None,
force_refresh: bool = False,
**kwargs,
) -> NewsContext:
"""
Get news context for a query and date range.
Args:
query: Search query
start_date: Start date in YYYY-MM-DD format
end_date: End date in YYYY-MM-DD format
symbol: Stock ticker symbol if company-specific
sources: List of sources to use ('finnhub', 'google', or both)
force_refresh: If True, skip local data and fetch fresh from APIs
**kwargs: Additional parameters
Returns:
NewsContext: Structured news context
"""
pass
def get_company_news_context(
self, symbol: str, start_date: str, end_date: str, **kwargs
) -> NewsContext:
"""
Get news context specific to a company.
Args:
symbol: Stock ticker symbol
start_date: Start date in YYYY-MM-DD format
end_date: End date in YYYY-MM-DD format
**kwargs: Additional parameters
Returns:
NewsContext: Company-specific news context
"""
pass
def get_global_news_context(
self,
start_date: str,
end_date: str,
categories: list[str] | None = None,
**kwargs,
) -> NewsContext:
"""
Get global/macro news context.
Args:
start_date: Start date in YYYY-MM-DD format
end_date: End date in YYYY-MM-DD format
categories: News categories to search
**kwargs: Additional parameters
Returns:
NewsContext: Global news context
"""
pass