diff --git a/tradingagents/agents/analysts/social_media_analyst.py b/tradingagents/agents/analysts/social_media_analyst.py index 8a6677e5..444b1615 100644 --- a/tradingagents/agents/analysts/social_media_analyst.py +++ b/tradingagents/agents/analysts/social_media_analyst.py @@ -1,7 +1,7 @@ from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder import time import json -from tradingagents.agents.utils.agent_utils import get_news +from tradingagents.agents.utils.agent_utils import get_news, get_fear_and_greed from tradingagents.dataflows.config import get_config @@ -13,6 +13,7 @@ def create_social_media_analyst(llm): tools = [ get_news, + get_fear_and_greed, ] system_message = ( diff --git a/tradingagents/agents/utils/agent_utils.py b/tradingagents/agents/utils/agent_utils.py index 6cf294a1..2d74fbbc 100644 --- a/tradingagents/agents/utils/agent_utils.py +++ b/tradingagents/agents/utils/agent_utils.py @@ -19,6 +19,9 @@ from tradingagents.agents.utils.news_data_tools import ( get_insider_transactions, get_global_news ) +from tradingagents.agents.utils.sentiment_tools import ( + get_fear_and_greed, +) def create_msg_delete(): def delete_messages(state): diff --git a/tradingagents/agents/utils/sentiment_tools.py b/tradingagents/agents/utils/sentiment_tools.py new file mode 100644 index 00000000..22f39c08 --- /dev/null +++ b/tradingagents/agents/utils/sentiment_tools.py @@ -0,0 +1,17 @@ +from langchain_core.tools import tool +from typing import Annotated +from tradingagents.dataflows.interface import route_to_vendor + +@tool +def get_fear_and_greed( + look_back_days: Annotated[int, "how many days to look back"] = 30, +) -> str: + """ + Retrieve the latest Fear and Greed Index. + Uses the configured sentiment_analysis vendor. + Args: + look_back_days (int): How many days to look back, default is 30 + Returns: + str: A formatted string containing the Fear and Greed Index. + """ + return route_to_vendor("get_fear_and_greed", look_back_days) \ No newline at end of file diff --git a/tradingagents/dataflows/interface.py b/tradingagents/dataflows/interface.py index 4cd5ddef..77c9fb5b 100644 --- a/tradingagents/dataflows/interface.py +++ b/tradingagents/dataflows/interface.py @@ -1,7 +1,7 @@ from typing import Annotated # Import from vendor-specific modules -from .local import get_YFin_data, get_finnhub_news, get_finnhub_company_insider_sentiment, get_finnhub_company_insider_transactions, get_simfin_balance_sheet, get_simfin_cashflow, get_simfin_income_statements, get_reddit_global_news, get_reddit_company_news +from .local import get_YFin_data, get_finnhub_news, get_finnhub_company_insider_sentiment, get_finnhub_company_insider_transactions, get_simfin_balance_sheet, get_simfin_cashflow, get_simfin_income_statements, get_reddit_global_news, get_reddit_company_news, get_fear_and_greed from .y_finance import get_YFin_data_online, get_stock_stats_indicators_window, 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 .google import get_google_news from .openai import get_stock_news_openai, get_global_news_openai, get_fundamentals_openai @@ -50,6 +50,7 @@ TOOLS_CATEGORIES = { "get_global_news", "get_insider_sentiment", "get_insider_transactions", + "get_fear_and_greed" ] } } @@ -114,6 +115,9 @@ VENDOR_METHODS = { "yfinance": get_yfinance_insider_transactions, "local": get_finnhub_company_insider_transactions, }, + "get_fear_and_greed": { + "local": get_fear_and_greed, + }, } def get_category_for_method(method: str) -> str: diff --git a/tradingagents/dataflows/local.py b/tradingagents/dataflows/local.py index 502bc43a..c17ca230 100644 --- a/tradingagents/dataflows/local.py +++ b/tradingagents/dataflows/local.py @@ -1,6 +1,7 @@ from typing import Annotated import pandas as pd import os +import requests from .config import DATA_DIR from datetime import datetime from dateutil.relativedelta import relativedelta @@ -472,4 +473,30 @@ def get_reddit_company_news( else: news_str += f"### {post['title']}\n\n{post['content']}\n\n" - return f"##{query} News Reddit, from {start_date} to {end_date}:\n\n{news_str}" \ No newline at end of file + return f"##{query} News Reddit, from {start_date} to {end_date}:\n\n{news_str}" + + +def get_fear_and_greed( + look_back_days: Annotated[int, "how many days to look back"] = 30, +) -> str: + """ + Retrieve the latest Fear and Greed Index. + Args: + look_back_days (int): How many days to look back, default is 30 + Returns: + str: A formatted string containing the Fear and Greed Index. + """ + + url = f"https://api.alternative.me/fng/?limit={look_back_days}&date_format=world" + response = requests.get(url) + + data = response.json().get("data", []) + + if len(data) == 0: + return "" + + result_str = "## Fear and Greed Index Data:\n\n" + for entry in data: + result_str += f"### Date: {entry['timestamp']}\nFear and Greed Index: {entry['value']}\nClassification: {entry['value_classification']}\n\n" + + return result_str \ No newline at end of file