From d0b48b47d5e5ae11a986ab3aafdf629f94fb39a3 Mon Sep 17 00:00:00 2001 From: Garrick Date: Tue, 24 Mar 2026 16:40:37 -0700 Subject: [PATCH] fix: preserve macro analyst context --- cli/main.py | 26 ++++++- cli/models.py | 1 + cli/utils.py | 1 + tests/test_macro_analyst.py | 68 +++++++++++++++++++ .../agents/analysts/macro_analyst.py | 9 --- .../agents/managers/portfolio_manager.py | 17 +++-- .../agents/managers/research_manager.py | 18 +++-- .../agents/researchers/bear_researcher.py | 18 +++-- .../agents/researchers/bull_researcher.py | 18 +++-- .../agents/risk_mgmt/aggressive_debator.py | 13 ++-- .../agents/risk_mgmt/conservative_debator.py | 13 ++-- .../agents/risk_mgmt/neutral_debator.py | 13 ++-- tradingagents/agents/trader/trader.py | 14 ++-- tradingagents/agents/utils/agent_states.py | 4 ++ tradingagents/agents/utils/agent_utils.py | 19 ++++++ tradingagents/graph/propagation.py | 2 + tradingagents/graph/reflection.py | 9 +-- tradingagents/graph/setup.py | 22 ++++-- tradingagents/graph/trading_graph.py | 4 +- 19 files changed, 200 insertions(+), 89 deletions(-) diff --git a/cli/main.py b/cli/main.py index c72e92e5..13a4ffd5 100644 --- a/cli/main.py +++ b/cli/main.py @@ -56,6 +56,7 @@ class MessageBuffer: "social": "Social Analyst", "news": "News Analyst", "fundamentals": "Fundamentals Analyst", + "macro": "Macro Analyst", } # Report section mapping: section -> (analyst_key for filtering, finalizing_agent) @@ -66,6 +67,7 @@ class MessageBuffer: "sentiment_report": ("social", "Social Analyst"), "news_report": ("news", "News Analyst"), "fundamentals_report": ("fundamentals", "Fundamentals Analyst"), + "macro_report": ("macro", "Macro Analyst"), "investment_plan": (None, "Research Manager"), "trader_investment_plan": (None, "Trader"), "final_trade_decision": (None, "Portfolio Manager"), @@ -174,6 +176,7 @@ class MessageBuffer: "sentiment_report": "Social Sentiment", "news_report": "News Analysis", "fundamentals_report": "Fundamentals Analysis", + "macro_report": "Macro Analysis", "investment_plan": "Research Team Decision", "trader_investment_plan": "Trading Team Plan", "final_trade_decision": "Portfolio Management Decision", @@ -189,7 +192,13 @@ class MessageBuffer: report_parts = [] # Analyst Team Reports - use .get() to handle missing sections - analyst_sections = ["market_report", "sentiment_report", "news_report", "fundamentals_report"] + analyst_sections = [ + "market_report", + "sentiment_report", + "news_report", + "fundamentals_report", + "macro_report", + ] if any(self.report_sections.get(section) for section in analyst_sections): report_parts.append("## Analyst Team Reports") if self.report_sections.get("market_report"): @@ -208,6 +217,10 @@ class MessageBuffer: report_parts.append( f"### Fundamentals Analysis\n{self.report_sections['fundamentals_report']}" ) + if self.report_sections.get("macro_report"): + report_parts.append( + f"### Macro Analysis\n{self.report_sections['macro_report']}" + ) # Research Team Reports if self.report_sections.get("investment_plan"): @@ -287,6 +300,7 @@ def update_display(layout, spinner_text=None, stats_handler=None, start_time=Non "Social Analyst", "News Analyst", "Fundamentals Analyst", + "Macro Analyst", ], "Research Team": ["Bull Researcher", "Bear Researcher", "Research Manager"], "Trading Team": ["Trader"], @@ -650,6 +664,10 @@ def save_report_to_disk(final_state, ticker: str, save_path: Path): analysts_dir.mkdir(exist_ok=True) (analysts_dir / "fundamentals.md").write_text(final_state["fundamentals_report"]) analyst_parts.append(("Fundamentals Analyst", final_state["fundamentals_report"])) + if final_state.get("macro_report"): + analysts_dir.mkdir(exist_ok=True) + (analysts_dir / "macro.md").write_text(final_state["macro_report"]) + analyst_parts.append(("Macro Analyst", final_state["macro_report"])) if analyst_parts: content = "\n\n".join(f"### {name}\n{text}" for name, text in analyst_parts) sections.append(f"## I. Analyst Team Reports\n\n{content}") @@ -731,6 +749,8 @@ def display_complete_report(final_state): analysts.append(("News Analyst", final_state["news_report"])) if final_state.get("fundamentals_report"): analysts.append(("Fundamentals Analyst", final_state["fundamentals_report"])) + if final_state.get("macro_report"): + analysts.append(("Macro Analyst", final_state["macro_report"])) if analysts: console.print(Panel("[bold]I. Analyst Team Reports[/bold]", border_style="cyan")) for title, content in analysts: @@ -785,18 +805,20 @@ def update_research_team_status(status): # Ordered list of analysts for status transitions -ANALYST_ORDER = ["market", "social", "news", "fundamentals"] +ANALYST_ORDER = ["market", "social", "news", "fundamentals", "macro"] ANALYST_AGENT_NAMES = { "market": "Market Analyst", "social": "Social Analyst", "news": "News Analyst", "fundamentals": "Fundamentals Analyst", + "macro": "Macro Analyst", } ANALYST_REPORT_MAP = { "market": "market_report", "social": "sentiment_report", "news": "news_report", "fundamentals": "fundamentals_report", + "macro": "macro_report", } diff --git a/cli/models.py b/cli/models.py index f68c3da1..8ac44008 100644 --- a/cli/models.py +++ b/cli/models.py @@ -8,3 +8,4 @@ class AnalystType(str, Enum): SOCIAL = "social" NEWS = "news" FUNDAMENTALS = "fundamentals" + MACRO = "macro" diff --git a/cli/utils.py b/cli/utils.py index 3cd418c2..c6094212 100644 --- a/cli/utils.py +++ b/cli/utils.py @@ -14,6 +14,7 @@ ANALYST_ORDER = [ ("Social Media Analyst", AnalystType.SOCIAL), ("News Analyst", AnalystType.NEWS), ("Fundamentals Analyst", AnalystType.FUNDAMENTALS), + ("Macro Analyst", AnalystType.MACRO), ] diff --git a/tests/test_macro_analyst.py b/tests/test_macro_analyst.py index 1b3775a2..40bb3529 100644 --- a/tests/test_macro_analyst.py +++ b/tests/test_macro_analyst.py @@ -1,3 +1,8 @@ +from inspect import signature + +from langchain_core.messages import AIMessage +from langchain_core.runnables import RunnableLambda + from tradingagents.graph.setup import GraphSetup from tradingagents.graph.trading_graph import TradingAgentsGraph @@ -180,3 +185,66 @@ def test_trading_graph_creates_macro_tool_node(monkeypatch): "get_yield_curve", "get_fed_calendar", ] + + +def test_macro_analyst_keeps_macro_output_out_of_news_report(): + from tradingagents.agents.analysts.macro_analyst import create_macro_analyst + + class FakeLLM: + def bind_tools(self, _tools): + return RunnableLambda( + lambda _inputs: AIMessage(content="macro summary", tool_calls=[]) + ) + + node = create_macro_analyst(FakeLLM()) + result = node( + { + "trade_date": "2026-03-24", + "company_of_interest": "AAPL", + "messages": [("human", "Analyze AAPL")], + "news_report": "existing news", + } + ) + + assert result["macro_report"] == "macro summary" + assert "news_report" not in result + + +def test_shared_analyst_context_and_state_contract_include_macro(): + from tradingagents.agents.utils.agent_states import AgentState + from tradingagents.agents.utils.agent_utils import build_analyst_report_context + from tradingagents.graph.propagation import Propagator + + context = build_analyst_report_context( + { + "market_report": "market", + "sentiment_report": "sentiment", + "news_report": "news", + "macro_report": "macro", + "fundamentals_report": "fundamentals", + } + ) + + assert "Macro Economic Report: macro" in context + assert "macro_report" in AgentState.__annotations__ + assert Propagator().create_initial_state("AAPL", "2026-03-24")["macro_report"] == "" + + +def test_macro_is_exposed_in_default_graph_and_cli_selection_paths(): + from cli.main import MessageBuffer + from cli.models import AnalystType + from cli.utils import ANALYST_ORDER as CLI_ANALYST_ORDER + + selected_analysts_default = signature(TradingAgentsGraph.__init__).parameters[ + "selected_analysts" + ].default + + assert AnalystType.MACRO.value == "macro" + assert ("Macro Analyst", AnalystType.MACRO) in CLI_ANALYST_ORDER + assert "macro" in selected_analysts_default + + message_buffer = MessageBuffer() + message_buffer.init_for_analysis(["macro"]) + + assert message_buffer.agent_status["Macro Analyst"] == "pending" + assert "macro_report" in message_buffer.report_sections diff --git a/tradingagents/agents/analysts/macro_analyst.py b/tradingagents/agents/analysts/macro_analyst.py index 69b95411..2d26d2d4 100644 --- a/tradingagents/agents/analysts/macro_analyst.py +++ b/tradingagents/agents/analysts/macro_analyst.py @@ -8,14 +8,6 @@ from tradingagents.agents.utils.agent_utils import ( ) -def _merge_with_news_report(existing_report: str, macro_report: str) -> str: - if not macro_report: - return existing_report - if not existing_report: - return macro_report - return f"{existing_report.rstrip()}\n\n## Macro Economic Overlay\n\n{macro_report}" - - def create_macro_analyst(llm): def macro_analyst_node(state): current_date = state["trade_date"] @@ -71,7 +63,6 @@ def create_macro_analyst(llm): return { "messages": [result], "macro_report": report, - "news_report": _merge_with_news_report(state.get("news_report", ""), report), } return macro_analyst_node diff --git a/tradingagents/agents/managers/portfolio_manager.py b/tradingagents/agents/managers/portfolio_manager.py index acdf940b..76ed3bd8 100644 --- a/tradingagents/agents/managers/portfolio_manager.py +++ b/tradingagents/agents/managers/portfolio_manager.py @@ -1,20 +1,21 @@ -from tradingagents.agents.utils.agent_utils import build_instrument_context +from tradingagents.agents.utils.agent_utils import ( + build_analyst_report_context, + build_instrument_context, +) def create_portfolio_manager(llm, memory): def portfolio_manager_node(state) -> dict: - instrument_context = build_instrument_context(state["company_of_interest"]) + analyst_report_context = build_analyst_report_context(state) + factor_rules_report = state.get("factor_rules_report", "") + factor_rules_context = f"Factor rules summary: {factor_rules_report}" history = state["risk_debate_state"]["history"] risk_debate_state = state["risk_debate_state"] - market_research_report = state["market_report"] - news_report = state["news_report"] - fundamentals_report = state["fundamentals_report"] - sentiment_report = state["sentiment_report"] trader_plan = state["investment_plan"] - curr_situation = f"{market_research_report}\n\n{sentiment_report}\n\n{news_report}\n\n{fundamentals_report}" + curr_situation = f"{analyst_report_context}\n{factor_rules_context}" past_memories = memory.get_memories(curr_situation, n_matches=2) past_memory_str = "" @@ -36,6 +37,8 @@ def create_portfolio_manager(llm, memory): **Context:** - Trader's proposed plan: **{trader_plan}** +- Source analyst reports: **{analyst_report_context}** +- {factor_rules_context} - Lessons from past decisions: **{past_memory_str}** **Required Output Structure:** diff --git a/tradingagents/agents/managers/research_manager.py b/tradingagents/agents/managers/research_manager.py index 2251bd50..c79a61c8 100644 --- a/tradingagents/agents/managers/research_manager.py +++ b/tradingagents/agents/managers/research_manager.py @@ -1,18 +1,20 @@ -from tradingagents.agents.utils.agent_utils import build_instrument_context +from tradingagents.agents.utils.agent_utils import ( + build_analyst_report_context, + build_instrument_context, +) def create_research_manager(llm, memory): def research_manager_node(state) -> dict: instrument_context = build_instrument_context(state["company_of_interest"]) + analyst_report_context = build_analyst_report_context(state) + factor_rules_report = state.get("factor_rules_report", "") + factor_rules_context = f"Factor rules summary: {factor_rules_report}" history = state["investment_debate_state"].get("history", "") - market_research_report = state["market_report"] - sentiment_report = state["sentiment_report"] - news_report = state["news_report"] - fundamentals_report = state["fundamentals_report"] investment_debate_state = state["investment_debate_state"] - curr_situation = f"{market_research_report}\n\n{sentiment_report}\n\n{news_report}\n\n{fundamentals_report}" + curr_situation = f"{analyst_report_context}\n{factor_rules_context}" past_memories = memory.get_memories(curr_situation, n_matches=2) past_memory_str = "" @@ -35,6 +37,10 @@ Here are your past reflections on mistakes: {instrument_context} +Source analyst reports: +{analyst_report_context} +{factor_rules_context} + Here is the debate: Debate History: {history}""" diff --git a/tradingagents/agents/researchers/bear_researcher.py b/tradingagents/agents/researchers/bear_researcher.py index 6634490a..8942206f 100644 --- a/tradingagents/agents/researchers/bear_researcher.py +++ b/tradingagents/agents/researchers/bear_researcher.py @@ -2,6 +2,8 @@ from langchain_core.messages import AIMessage import time import json +from tradingagents.agents.utils.agent_utils import build_analyst_report_context + def create_bear_researcher(llm, memory): def bear_node(state) -> dict: @@ -10,12 +12,10 @@ def create_bear_researcher(llm, memory): bear_history = investment_debate_state.get("bear_history", "") current_response = investment_debate_state.get("current_response", "") - market_research_report = state["market_report"] - sentiment_report = state["sentiment_report"] - news_report = state["news_report"] - fundamentals_report = state["fundamentals_report"] - - curr_situation = f"{market_research_report}\n\n{sentiment_report}\n\n{news_report}\n\n{fundamentals_report}" + analyst_report_context = build_analyst_report_context(state) + factor_rules_report = state.get("factor_rules_report", "") + factor_rules_context = f"Factor rules summary: {factor_rules_report}" + curr_situation = f"{analyst_report_context}\n{factor_rules_context}" past_memories = memory.get_memories(curr_situation, n_matches=2) past_memory_str = "" @@ -34,10 +34,8 @@ Key points to focus on: Resources available: -Market research report: {market_research_report} -Social media sentiment report: {sentiment_report} -Latest world affairs news: {news_report} -Company fundamentals report: {fundamentals_report} +{analyst_report_context} +{factor_rules_context} Conversation history of the debate: {history} Last bull argument: {current_response} Reflections from similar situations and lessons learned: {past_memory_str} diff --git a/tradingagents/agents/researchers/bull_researcher.py b/tradingagents/agents/researchers/bull_researcher.py index b03ef755..f2bbf4bc 100644 --- a/tradingagents/agents/researchers/bull_researcher.py +++ b/tradingagents/agents/researchers/bull_researcher.py @@ -2,6 +2,8 @@ from langchain_core.messages import AIMessage import time import json +from tradingagents.agents.utils.agent_utils import build_analyst_report_context + def create_bull_researcher(llm, memory): def bull_node(state) -> dict: @@ -10,12 +12,10 @@ def create_bull_researcher(llm, memory): bull_history = investment_debate_state.get("bull_history", "") current_response = investment_debate_state.get("current_response", "") - market_research_report = state["market_report"] - sentiment_report = state["sentiment_report"] - news_report = state["news_report"] - fundamentals_report = state["fundamentals_report"] - - curr_situation = f"{market_research_report}\n\n{sentiment_report}\n\n{news_report}\n\n{fundamentals_report}" + analyst_report_context = build_analyst_report_context(state) + factor_rules_report = state.get("factor_rules_report", "") + factor_rules_context = f"Factor rules summary: {factor_rules_report}" + curr_situation = f"{analyst_report_context}\n{factor_rules_context}" past_memories = memory.get_memories(curr_situation, n_matches=2) past_memory_str = "" @@ -32,10 +32,8 @@ Key points to focus on: - Engagement: Present your argument in a conversational style, engaging directly with the bear analyst's points and debating effectively rather than just listing data. Resources available: -Market research report: {market_research_report} -Social media sentiment report: {sentiment_report} -Latest world affairs news: {news_report} -Company fundamentals report: {fundamentals_report} +{analyst_report_context} +{factor_rules_context} Conversation history of the debate: {history} Last bear argument: {current_response} Reflections from similar situations and lessons learned: {past_memory_str} diff --git a/tradingagents/agents/risk_mgmt/aggressive_debator.py b/tradingagents/agents/risk_mgmt/aggressive_debator.py index 651114a7..9bda6a4d 100644 --- a/tradingagents/agents/risk_mgmt/aggressive_debator.py +++ b/tradingagents/agents/risk_mgmt/aggressive_debator.py @@ -1,6 +1,8 @@ import time import json +from tradingagents.agents.utils.agent_utils import build_analyst_report_context + def create_aggressive_debator(llm): def aggressive_node(state) -> dict: @@ -11,11 +13,7 @@ def create_aggressive_debator(llm): current_conservative_response = risk_debate_state.get("current_conservative_response", "") current_neutral_response = risk_debate_state.get("current_neutral_response", "") - market_research_report = state["market_report"] - sentiment_report = state["sentiment_report"] - news_report = state["news_report"] - fundamentals_report = state["fundamentals_report"] - + analyst_report_context = build_analyst_report_context(state) trader_decision = state["trader_investment_plan"] prompt = f"""As the Aggressive Risk Analyst, your role is to actively champion high-reward, high-risk opportunities, emphasizing bold strategies and competitive advantages. When evaluating the trader's decision or plan, focus intently on the potential upside, growth potential, and innovative benefits—even when these come with elevated risk. Use the provided market data and sentiment analysis to strengthen your arguments and challenge the opposing views. Specifically, respond directly to each point made by the conservative and neutral analysts, countering with data-driven rebuttals and persuasive reasoning. Highlight where their caution might miss critical opportunities or where their assumptions may be overly conservative. Here is the trader's decision: @@ -24,10 +22,7 @@ def create_aggressive_debator(llm): Your task is to create a compelling case for the trader's decision by questioning and critiquing the conservative and neutral stances to demonstrate why your high-reward perspective offers the best path forward. Incorporate insights from the following sources into your arguments: -Market Research Report: {market_research_report} -Social Media Sentiment Report: {sentiment_report} -Latest World Affairs Report: {news_report} -Company Fundamentals Report: {fundamentals_report} +{analyst_report_context} Here is the current conversation history: {history} Here are the last arguments from the conservative analyst: {current_conservative_response} Here are the last arguments from the neutral analyst: {current_neutral_response}. If there are no responses from the other viewpoints yet, present your own argument based on the available data. Engage actively by addressing any specific concerns raised, refuting the weaknesses in their logic, and asserting the benefits of risk-taking to outpace market norms. Maintain a focus on debating and persuading, not just presenting data. Challenge each counterpoint to underscore why a high-risk approach is optimal. Output conversationally as if you are speaking without any special formatting.""" diff --git a/tradingagents/agents/risk_mgmt/conservative_debator.py b/tradingagents/agents/risk_mgmt/conservative_debator.py index 7c8c0fd1..2ce2b841 100644 --- a/tradingagents/agents/risk_mgmt/conservative_debator.py +++ b/tradingagents/agents/risk_mgmt/conservative_debator.py @@ -2,6 +2,8 @@ from langchain_core.messages import AIMessage import time import json +from tradingagents.agents.utils.agent_utils import build_analyst_report_context + def create_conservative_debator(llm): def conservative_node(state) -> dict: @@ -12,11 +14,7 @@ def create_conservative_debator(llm): current_aggressive_response = risk_debate_state.get("current_aggressive_response", "") current_neutral_response = risk_debate_state.get("current_neutral_response", "") - market_research_report = state["market_report"] - sentiment_report = state["sentiment_report"] - news_report = state["news_report"] - fundamentals_report = state["fundamentals_report"] - + analyst_report_context = build_analyst_report_context(state) trader_decision = state["trader_investment_plan"] prompt = f"""As the Conservative Risk Analyst, your primary objective is to protect assets, minimize volatility, and ensure steady, reliable growth. You prioritize stability, security, and risk mitigation, carefully assessing potential losses, economic downturns, and market volatility. When evaluating the trader's decision or plan, critically examine high-risk elements, pointing out where the decision may expose the firm to undue risk and where more cautious alternatives could secure long-term gains. Here is the trader's decision: @@ -25,10 +23,7 @@ def create_conservative_debator(llm): Your task is to actively counter the arguments of the Aggressive and Neutral Analysts, highlighting where their views may overlook potential threats or fail to prioritize sustainability. Respond directly to their points, drawing from the following data sources to build a convincing case for a low-risk approach adjustment to the trader's decision: -Market Research Report: {market_research_report} -Social Media Sentiment Report: {sentiment_report} -Latest World Affairs Report: {news_report} -Company Fundamentals Report: {fundamentals_report} +{analyst_report_context} Here is the current conversation history: {history} Here is the last response from the aggressive analyst: {current_aggressive_response} Here is the last response from the neutral analyst: {current_neutral_response}. If there are no responses from the other viewpoints yet, present your own argument based on the available data. Engage by questioning their optimism and emphasizing the potential downsides they may have overlooked. Address each of their counterpoints to showcase why a conservative stance is ultimately the safest path for the firm's assets. Focus on debating and critiquing their arguments to demonstrate the strength of a low-risk strategy over their approaches. Output conversationally as if you are speaking without any special formatting.""" diff --git a/tradingagents/agents/risk_mgmt/neutral_debator.py b/tradingagents/agents/risk_mgmt/neutral_debator.py index 9ed490da..c8618d95 100644 --- a/tradingagents/agents/risk_mgmt/neutral_debator.py +++ b/tradingagents/agents/risk_mgmt/neutral_debator.py @@ -1,6 +1,8 @@ import time import json +from tradingagents.agents.utils.agent_utils import build_analyst_report_context + def create_neutral_debator(llm): def neutral_node(state) -> dict: @@ -11,11 +13,7 @@ def create_neutral_debator(llm): current_aggressive_response = risk_debate_state.get("current_aggressive_response", "") current_conservative_response = risk_debate_state.get("current_conservative_response", "") - market_research_report = state["market_report"] - sentiment_report = state["sentiment_report"] - news_report = state["news_report"] - fundamentals_report = state["fundamentals_report"] - + analyst_report_context = build_analyst_report_context(state) trader_decision = state["trader_investment_plan"] prompt = f"""As the Neutral Risk Analyst, your role is to provide a balanced perspective, weighing both the potential benefits and risks of the trader's decision or plan. You prioritize a well-rounded approach, evaluating the upsides and downsides while factoring in broader market trends, potential economic shifts, and diversification strategies.Here is the trader's decision: @@ -24,10 +22,7 @@ def create_neutral_debator(llm): Your task is to challenge both the Aggressive and Conservative Analysts, pointing out where each perspective may be overly optimistic or overly cautious. Use insights from the following data sources to support a moderate, sustainable strategy to adjust the trader's decision: -Market Research Report: {market_research_report} -Social Media Sentiment Report: {sentiment_report} -Latest World Affairs Report: {news_report} -Company Fundamentals Report: {fundamentals_report} +{analyst_report_context} Here is the current conversation history: {history} Here is the last response from the aggressive analyst: {current_aggressive_response} Here is the last response from the conservative analyst: {current_conservative_response}. If there are no responses from the other viewpoints yet, present your own argument based on the available data. Engage actively by analyzing both sides critically, addressing weaknesses in the aggressive and conservative arguments to advocate for a more balanced approach. Challenge each of their points to illustrate why a moderate risk strategy might offer the best of both worlds, providing growth potential while safeguarding against extreme volatility. Focus on debating rather than simply presenting data, aiming to show that a balanced view can lead to the most reliable outcomes. Output conversationally as if you are speaking without any special formatting.""" diff --git a/tradingagents/agents/trader/trader.py b/tradingagents/agents/trader/trader.py index 6298f239..58554396 100644 --- a/tradingagents/agents/trader/trader.py +++ b/tradingagents/agents/trader/trader.py @@ -2,20 +2,20 @@ import functools import time import json -from tradingagents.agents.utils.agent_utils import build_instrument_context +from tradingagents.agents.utils.agent_utils import ( + build_analyst_report_context, + build_instrument_context, +) def create_trader(llm, memory): def trader_node(state, name): company_name = state["company_of_interest"] instrument_context = build_instrument_context(company_name) + analyst_report_context = build_analyst_report_context(state) investment_plan = state["investment_plan"] - market_research_report = state["market_report"] - sentiment_report = state["sentiment_report"] - news_report = state["news_report"] - fundamentals_report = state["fundamentals_report"] - curr_situation = f"{market_research_report}\n\n{sentiment_report}\n\n{news_report}\n\n{fundamentals_report}" + curr_situation = analyst_report_context past_memories = memory.get_memories(curr_situation, n_matches=2) past_memory_str = "" @@ -27,7 +27,7 @@ def create_trader(llm, memory): context = { "role": "user", - "content": f"Based on a comprehensive analysis by a team of analysts, here is an investment plan tailored for {company_name}. {instrument_context} This plan incorporates insights from current technical market trends, macroeconomic indicators, and social media sentiment. Use this plan as a foundation for evaluating your next trading decision.\n\nProposed Investment Plan: {investment_plan}\n\nLeverage these insights to make an informed and strategic decision.", + "content": f"Based on a comprehensive analysis by a team of analysts, here is an investment plan tailored for {company_name}. {instrument_context} This plan incorporates insights from current technical market trends, macroeconomic indicators, and social media sentiment. Use this plan as a foundation for evaluating your next trading decision.\n\nSource Analyst Reports:\n{analyst_report_context}\n\nProposed Investment Plan: {investment_plan}\n\nLeverage these insights to make an informed and strategic decision.", } messages = [ diff --git a/tradingagents/agents/utils/agent_states.py b/tradingagents/agents/utils/agent_states.py index fbc9b27d..315e9780 100644 --- a/tradingagents/agents/utils/agent_states.py +++ b/tradingagents/agents/utils/agent_states.py @@ -168,7 +168,11 @@ class AgentState(MessagesState): news_report: Annotated[ str, "Report from the News Researcher of current world affairs" ] + macro_report: Annotated[str, "Report from the Macro Analyst"] fundamentals_report: Annotated[str, "Report from the Fundamentals Researcher"] + factor_rules_report: Annotated[ + str, "Summary from the optional factor rule analyst" + ] valuation_data: Annotated[ ValuationData, "Structured valuation underwriting output" ] diff --git a/tradingagents/agents/utils/agent_utils.py b/tradingagents/agents/utils/agent_utils.py index cd4a39f0..39028138 100644 --- a/tradingagents/agents/utils/agent_utils.py +++ b/tradingagents/agents/utils/agent_utils.py @@ -1,3 +1,6 @@ +from collections.abc import Mapping +from typing import Any + from langchain_core.messages import HumanMessage, RemoveMessage # Import tools from separate utility files @@ -27,6 +30,7 @@ from tradingagents.agents.utils.macro_data_tools import ( __all__ = [ "build_instrument_context", + "build_analyst_report_context", "create_msg_delete", "get_balance_sheet", "get_cashflow", @@ -52,6 +56,21 @@ def build_instrument_context(ticker: str) -> str: ) +def build_analyst_report_context(state: Mapping[str, Any]) -> str: + """Build a stable analyst context block for downstream prompts and memory.""" + sections = [ + ("Market Research Report", state.get("market_report", "")), + ("Social Media Sentiment Report", state.get("sentiment_report", "")), + ("Latest World Affairs Report", state.get("news_report", "")), + ("Macro Economic Report", state.get("macro_report", "")), + ("Company Fundamentals Report", state.get("fundamentals_report", "")), + ("Factor Rules Report", state.get("factor_rules_report", "")), + ] + return "\n".join( + f"{label}: {content}" for label, content in sections if content + ) + + def create_msg_delete(): def delete_messages(state): """Clear messages and add placeholder for Anthropic compatibility""" diff --git a/tradingagents/graph/propagation.py b/tradingagents/graph/propagation.py index 75b3be3b..9bb35081 100644 --- a/tradingagents/graph/propagation.py +++ b/tradingagents/graph/propagation.py @@ -52,6 +52,8 @@ class Propagator: "fundamentals_report": "", "sentiment_report": "", "news_report": "", + "factor_rules_report": "", + "macro_report": "", **make_default_structured_stock_underwriting_state(), } diff --git a/tradingagents/graph/reflection.py b/tradingagents/graph/reflection.py index 85438595..9a5d45ea 100644 --- a/tradingagents/graph/reflection.py +++ b/tradingagents/graph/reflection.py @@ -3,6 +3,8 @@ from typing import Dict, Any from langchain_openai import ChatOpenAI +from tradingagents.agents.utils.agent_utils import build_analyst_report_context + class Reflector: """Handles reflection on decisions and updating memory.""" @@ -48,12 +50,7 @@ Adhere strictly to these instructions, and ensure your output is detailed, accur def _extract_current_situation(self, current_state: Dict[str, Any]) -> str: """Extract the current market situation from the state.""" - curr_market_report = current_state["market_report"] - curr_sentiment_report = current_state["sentiment_report"] - curr_news_report = current_state["news_report"] - curr_fundamentals_report = current_state["fundamentals_report"] - - return f"{curr_market_report}\n\n{curr_sentiment_report}\n\n{curr_news_report}\n\n{curr_fundamentals_report}" + return build_analyst_report_context(current_state) def _reflect_on_component( self, component_type: str, report: str, situation: str, returns_losses diff --git a/tradingagents/graph/setup.py b/tradingagents/graph/setup.py index 47847055..334a5b5d 100644 --- a/tradingagents/graph/setup.py +++ b/tradingagents/graph/setup.py @@ -44,6 +44,9 @@ class GraphSetup: self.fundamentals_analyst_llm = self._get_role_llm( "fundamentals", self.quick_thinking_llm ) + self.factor_rules_analyst_llm = self._get_role_llm( + "factor_rules", self.quick_thinking_llm + ) self.macro_analyst_llm = self._get_role_llm("macro", self.quick_thinking_llm) self.bull_researcher_llm = self._get_role_llm( "bull_researcher", self.quick_thinking_llm @@ -90,7 +93,7 @@ class GraphSetup: return default_handler def setup_graph( - self, selected_analysts=["market", "social", "news", "fundamentals"] + self, selected_analysts=["market", "social", "news", "fundamentals", "macro"] ): """Set up and compile the agent workflow graph. @@ -100,6 +103,7 @@ class GraphSetup: - "social": Social media analyst - "news": News analyst - "fundamentals": Fundamentals analyst + - "factor_rules": Factor rule analyst - "macro": Macro analyst """ if len(selected_analysts) == 0: @@ -138,6 +142,12 @@ class GraphSetup: delete_nodes["fundamentals"] = create_msg_delete() tool_nodes["fundamentals"] = self.tool_nodes["fundamentals"] + if "factor_rules" in selected_analysts: + analyst_nodes["factor_rules"] = create_factor_rule_analyst( + self.factor_rules_analyst_llm + ) + delete_nodes["factor_rules"] = create_msg_delete() + if "macro" in selected_analysts: analyst_nodes["macro"] = create_macro_analyst(self.macro_analyst_llm) delete_nodes["macro"] = create_msg_delete() @@ -174,7 +184,8 @@ class GraphSetup: workflow.add_node( f"Msg Clear {analyst_type.capitalize()}", delete_nodes[analyst_type] ) - workflow.add_node(f"tools_{analyst_type}", tool_nodes[analyst_type]) + if analyst_type in tool_nodes: + workflow.add_node(f"tools_{analyst_type}", tool_nodes[analyst_type]) # Add other nodes workflow.add_node("Bull Researcher", bull_researcher_node) @@ -201,9 +212,12 @@ class GraphSetup: workflow.add_conditional_edges( current_analyst, self._get_continue_handler(analyst_type), - [current_tools, current_clear], + [current_tools, current_clear] + if analyst_type in tool_nodes + else [current_clear], ) - workflow.add_edge(current_tools, current_analyst) + if analyst_type in tool_nodes: + workflow.add_edge(current_tools, current_analyst) # Connect to next analyst or to Bull Researcher if this is the last analyst if i < len(selected_analysts) - 1: diff --git a/tradingagents/graph/trading_graph.py b/tradingagents/graph/trading_graph.py index b24e2268..294b25a5 100644 --- a/tradingagents/graph/trading_graph.py +++ b/tradingagents/graph/trading_graph.py @@ -61,6 +61,7 @@ class TradingAgentsGraph: "social", "news", "fundamentals", + "factor_rules", "macro", "bull_researcher", "bear_researcher", @@ -76,7 +77,7 @@ class TradingAgentsGraph: def __init__( self, - selected_analysts=["market", "social", "news", "fundamentals"], + selected_analysts=["market", "social", "news", "fundamentals", "macro"], debug=False, config: Dict[str, Any] = None, callbacks: Optional[List] = None, @@ -357,6 +358,7 @@ class TradingAgentsGraph: "sentiment_report": final_state["sentiment_report"], "news_report": final_state["news_report"], "fundamentals_report": final_state["fundamentals_report"], + "factor_rules_report": final_state.get("factor_rules_report", ""), "macro_report": final_state.get("macro_report", ""), "investment_debate_state": { "bull_history": final_state["investment_debate_state"]["bull_history"],