diff --git a/.env.example b/.env.example index 1e257c3c53..68ad0d51ff 100644 --- a/.env.example +++ b/.env.example @@ -1,2 +1,7 @@ ALPHA_VANTAGE_API_KEY=alpha_vantage_api_key_placeholder -OPENAI_API_KEY=openai_api_key_placeholder \ No newline at end of file +# only select 1 to your .env +OPENAI_API_KEY=openai_api_key_placeholder +OPENROUTER_API_KEY=openrouter_api_key_placeholder +ANTHROPIC_API_KEY=anthropic_api_key_placeholder +GOOGLE_API_KEY=google_api_key_placeholder +XAI_API_KEY=xai_api_key_placeholder \ No newline at end of file diff --git a/.gitignore b/.gitignore index 3369bad9e9..07e653f58b 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,5 @@ eval_results/ eval_data/ *.egg-info/ .env +.history +.idea \ No newline at end of file diff --git a/cli/main.py b/cli/main.py index 2e06d50cea..4a1e3d4a5e 100644 --- a/cli/main.py +++ b/cli/main.py @@ -1,16 +1,15 @@ -from typing import Optional import datetime -import typer -from pathlib import Path from functools import wraps -from rich.console import Console +from pathlib import Path + +import typer from dotenv import load_dotenv +from rich.console import Console # Load environment variables from .env file load_dotenv() from rich.panel import Panel from rich.spinner import Spinner -from rich.live import Live from rich.columns import Columns from rich.markdown import Markdown from rich.layout import Layout @@ -18,15 +17,11 @@ from rich.live import Live from rich.table import Table from collections import deque -import time -from rich.tree import Tree from rich import box from rich.align import Align -from rich.rule import Rule from tradingagents.graph.trading_graph import TradingAgentsGraph from tradingagents.default_config import DEFAULT_CONFIG -from cli.models import AnalystType from cli.utils import * console = Console() @@ -66,13 +61,13 @@ def __init__(self, max_length=100): } self.current_agent = None self.report_sections = { - "market_report": None, - "sentiment_report": None, - "news_report": None, - "fundamentals_report": None, - "investment_plan": None, - "trader_investment_plan": None, - "final_trade_decision": None, + "market_analysis": None, + "sentiment_analysis": None, + "news_analysis": None, + "fundamentals_analysis": None, + "research_team_decision": None, + "trader_team_plan": None, + "final_portfolio_management_decision": None, } def add_message(self, message_type, content): @@ -103,17 +98,17 @@ def _update_current_report(self): if content is not None: latest_section = section latest_content = content - + if latest_section and latest_content: # Format the current section for display section_titles = { - "market_report": "Market Analysis", - "sentiment_report": "Social Sentiment", - "news_report": "News Analysis", - "fundamentals_report": "Fundamentals Analysis", - "investment_plan": "Research Team Decision", - "trader_investment_plan": "Trading Team Plan", - "final_trade_decision": "Portfolio Management Decision", + "market_analysis": "Market Analysis", + "sentiment_analysis": "Social Sentiment", + "news_analysis": "News Analysis", + "fundamentals_analysis": "Fundamentals Analysis", + "research_team_decision": "Research Team Decision", + "trader_team_plan": "Trading Team Plan", + "final_portfolio_management_decision": "Portfolio Management Decision", } self.current_report = ( f"### {section_titles[latest_section]}\n{latest_content}" @@ -127,46 +122,46 @@ def _update_final_report(self): # Analyst Team Reports if any( - self.report_sections[section] - for section in [ - "market_report", - "sentiment_report", - "news_report", - "fundamentals_report", - ] + self.report_sections[section] + for section in [ + "market_analysis", + "sentiment_analysis", + "news_analysis", + "fundamentals_analysis", + ] ): report_parts.append("## Analyst Team Reports") - if self.report_sections["market_report"]: + if self.report_sections["market_analysis"]: report_parts.append( - f"### Market Analysis\n{self.report_sections['market_report']}" + f"### Market Analysis\n{self.report_sections['market_analysis']}" ) - if self.report_sections["sentiment_report"]: + if self.report_sections["sentiment_analysis"]: report_parts.append( - f"### Social Sentiment\n{self.report_sections['sentiment_report']}" + f"### Social Sentiment\n{self.report_sections['sentiment_analysis']}" ) - if self.report_sections["news_report"]: + if self.report_sections["news_analysis"]: report_parts.append( - f"### News Analysis\n{self.report_sections['news_report']}" + f"### News Analysis\n{self.report_sections['news_analysis']}" ) - if self.report_sections["fundamentals_report"]: + if self.report_sections["fundamentals_analysis"]: report_parts.append( - f"### Fundamentals Analysis\n{self.report_sections['fundamentals_report']}" + f"### Fundamentals Analysis\n{self.report_sections['fundamentals_analysis']}" ) # Research Team Reports - if self.report_sections["investment_plan"]: + if self.report_sections["research_team_decision"]: report_parts.append("## Research Team Decision") - report_parts.append(f"{self.report_sections['investment_plan']}") + report_parts.append(f"{self.report_sections['research_team_decision']}") # Trading Team Reports - if self.report_sections["trader_investment_plan"]: + if self.report_sections["trader_team_plan"]: report_parts.append("## Trading Team Plan") - report_parts.append(f"{self.report_sections['trader_investment_plan']}") + report_parts.append(f"{self.report_sections['trader_team_plan']}") # Portfolio Management Decision - if self.report_sections["final_trade_decision"]: + if self.report_sections["final_portfolio_management_decision"]: report_parts.append("## Portfolio Management Decision") - report_parts.append(f"{self.report_sections['final_trade_decision']}") + report_parts.append(f"{self.report_sections['final_portfolio_management_decision']}") self.final_report = "\n\n".join(report_parts) if report_parts else None @@ -317,7 +312,7 @@ def update_display(layout, spinner_text=None): content_str = ' '.join(text_parts) elif not isinstance(content_str, str): content_str = str(content) - + # Truncate message content if too long if len(content_str) > 200: content_str = content_str[:197] + "..." @@ -429,6 +424,14 @@ def create_question_box(title, prompt, default=None): box_content += f"\n[dim]Default: {default}[/dim]" return Panel(box_content, border_style="blue", padding=(1, 2)) + # Step 0: Language Selection + console.print( + create_question_box( + "Step 0: Language Selection", "Choose language for agent outputs" + ) + ) + selected_language = select_language() + # Step 1: Ticker symbol console.print( create_question_box( @@ -474,7 +477,7 @@ def create_question_box(title, prompt, default=None): ) ) selected_llm_provider, backend_url = select_llm_provider() - + # Step 6: Thinking agents console.print( create_question_box( @@ -493,6 +496,7 @@ def create_question_box(title, prompt, default=None): "backend_url": backend_url, "shallow_thinker": selected_shallow_thinker, "deep_thinker": selected_deep_thinker, + "language": selected_language, } @@ -528,10 +532,10 @@ def display_complete_report(final_state): analyst_reports = [] # Market Analyst Report - if final_state.get("market_report"): + if final_state.get("market_analysis"): analyst_reports.append( Panel( - Markdown(final_state["market_report"]), + Markdown(final_state["market_analysis"]), title="Market Analyst", border_style="blue", padding=(1, 2), @@ -539,10 +543,10 @@ def display_complete_report(final_state): ) # Social Analyst Report - if final_state.get("sentiment_report"): + if final_state.get("sentiment_analysis"): analyst_reports.append( Panel( - Markdown(final_state["sentiment_report"]), + Markdown(final_state["sentiment_analysis"]), title="Social Analyst", border_style="blue", padding=(1, 2), @@ -550,10 +554,10 @@ def display_complete_report(final_state): ) # News Analyst Report - if final_state.get("news_report"): + if final_state.get("news_analysis"): analyst_reports.append( Panel( - Markdown(final_state["news_report"]), + Markdown(final_state["news_analysis"]), title="News Analyst", border_style="blue", padding=(1, 2), @@ -561,10 +565,10 @@ def display_complete_report(final_state): ) # Fundamentals Analyst Report - if final_state.get("fundamentals_report"): + if final_state.get("fundamentals_analysis"): analyst_reports.append( Panel( - Markdown(final_state["fundamentals_report"]), + Markdown(final_state["fundamentals_analysis"]), title="Fundamentals Analyst", border_style="blue", padding=(1, 2), @@ -630,11 +634,11 @@ def display_complete_report(final_state): ) # III. Trading Team Reports - if final_state.get("trader_investment_plan"): + if final_state.get("trader_team_plan"): console.print( Panel( Panel( - Markdown(final_state["trader_investment_plan"]), + Markdown(final_state["trader_team_plan"]), title="Trader", border_style="blue", padding=(1, 2), @@ -716,6 +720,7 @@ def update_research_team_status(status): for agent in research_team: message_buffer.update_agent_status(agent, status) + def extract_content_string(content): """Extract string content from various message formats.""" if isinstance(content, str): @@ -735,12 +740,14 @@ def extract_content_string(content): else: return str(content) + def run_analysis(): # First get all user selections selections = get_user_selections() # Create config with selected research depth config = DEFAULT_CONFIG.copy() + config["output_language"] = selections["language"] config["max_debate_rounds"] = selections["research_depth"] config["max_risk_discuss_rounds"] = selections["research_depth"] config["quick_think_llm"] = selections["shallow_thinker"] @@ -763,6 +770,7 @@ def run_analysis(): def save_message_decorator(obj, func_name): func = getattr(obj, func_name) + @wraps(func) def wrapper(*args, **kwargs): func(*args, **kwargs) @@ -770,10 +778,12 @@ def wrapper(*args, **kwargs): content = content.replace("\n", " ") # Replace newlines with spaces with open(log_file, "a") as f: f.write(f"{timestamp} [{message_type}] {content}\n") + return wrapper - + def save_tool_call_decorator(obj, func_name): func = getattr(obj, func_name) + @wraps(func) def wrapper(*args, **kwargs): func(*args, **kwargs) @@ -781,10 +791,12 @@ def wrapper(*args, **kwargs): args_str = ", ".join(f"{k}={v}" for k, v in args.items()) with open(log_file, "a") as f: f.write(f"{timestamp} [Tool Call] {tool_name}({args_str})\n") + return wrapper def save_report_section_decorator(obj, func_name): func = getattr(obj, func_name) + @wraps(func) def wrapper(section_name, content): func(section_name, content) @@ -794,6 +806,7 @@ def wrapper(section_name, content): file_name = f"{section_name}.md" with open(report_dir / file_name, "w") as f: f.write(content) + return wrapper message_buffer.add_message = save_message_decorator(message_buffer, "add_message") @@ -861,7 +874,7 @@ def wrapper(section_name, content): msg_type = "System" # Add message to buffer - message_buffer.add_message(msg_type, content) + message_buffer.add_message(msg_type, content) # If it's a tool call, add it to tool calls if hasattr(last_message, "tool_calls"): @@ -876,9 +889,9 @@ def wrapper(section_name, content): # Update reports and agent status based on chunk content # Analyst Team Reports - if "market_report" in chunk and chunk["market_report"]: + if "market_analysis" in chunk and chunk["market_analysis"]: message_buffer.update_report_section( - "market_report", chunk["market_report"] + "market_analysis", chunk["market_analysis"] ) message_buffer.update_agent_status("Market Analyst", "completed") # Set next analyst to in_progress @@ -887,9 +900,9 @@ def wrapper(section_name, content): "Social Analyst", "in_progress" ) - if "sentiment_report" in chunk and chunk["sentiment_report"]: + if "sentiment_analysis" in chunk and chunk["sentiment_analysis"]: message_buffer.update_report_section( - "sentiment_report", chunk["sentiment_report"] + "sentiment_analysis", chunk["sentiment_analysis"] ) message_buffer.update_agent_status("Social Analyst", "completed") # Set next analyst to in_progress @@ -898,9 +911,9 @@ def wrapper(section_name, content): "News Analyst", "in_progress" ) - if "news_report" in chunk and chunk["news_report"]: + if "news_analysis" in chunk and chunk["news_analysis"]: message_buffer.update_report_section( - "news_report", chunk["news_report"] + "news_analysis", chunk["news_analysis"] ) message_buffer.update_agent_status("News Analyst", "completed") # Set next analyst to in_progress @@ -909,9 +922,9 @@ def wrapper(section_name, content): "Fundamentals Analyst", "in_progress" ) - if "fundamentals_report" in chunk and chunk["fundamentals_report"]: + if "fundamentals_analysis" in chunk and chunk["fundamentals_analysis"]: message_buffer.update_report_section( - "fundamentals_report", chunk["fundamentals_report"] + "fundamentals_analysis", chunk["fundamentals_analysis"] ) message_buffer.update_agent_status( "Fundamentals Analyst", "completed" @@ -921,8 +934,8 @@ def wrapper(section_name, content): # Research Team - Handle Investment Debate State if ( - "investment_debate_state" in chunk - and chunk["investment_debate_state"] + "investment_debate_state" in chunk + and chunk["investment_debate_state"] ): debate_state = chunk["investment_debate_state"] @@ -937,7 +950,7 @@ def wrapper(section_name, content): message_buffer.add_message("Reasoning", latest_bull) # Update research report with bull's latest analysis message_buffer.update_report_section( - "investment_plan", + "research_team_decision", f"### Bull Researcher Analysis\n{latest_bull}", ) @@ -952,14 +965,14 @@ def wrapper(section_name, content): message_buffer.add_message("Reasoning", latest_bear) # Update research report with bear's latest analysis message_buffer.update_report_section( - "investment_plan", - f"{message_buffer.report_sections['investment_plan']}\n\n### Bear Researcher Analysis\n{latest_bear}", + "research_team_decision", + f"{message_buffer.report_sections['research_team_decision']}\n\n### Bear Researcher Analysis\n{latest_bear}", ) # Update Research Manager status and final decision if ( - "judge_decision" in debate_state - and debate_state["judge_decision"] + "judge_decision" in debate_state + and debate_state["judge_decision"] ): # Keep all research team members in progress until final decision update_research_team_status("in_progress") @@ -969,8 +982,8 @@ def wrapper(section_name, content): ) # Update research report with final decision message_buffer.update_report_section( - "investment_plan", - f"{message_buffer.report_sections['investment_plan']}\n\n### Research Manager Decision\n{debate_state['judge_decision']}", + "research_team_decision", + f"{message_buffer.report_sections['research_team_decision']}\n\n### Research Manager Decision\n{debate_state['judge_decision']}", ) # Mark all research team members as completed update_research_team_status("completed") @@ -981,11 +994,11 @@ def wrapper(section_name, content): # Trading Team if ( - "trader_investment_plan" in chunk - and chunk["trader_investment_plan"] + "trader_team_plan" in chunk + and chunk["trader_team_plan"] ): message_buffer.update_report_section( - "trader_investment_plan", chunk["trader_investment_plan"] + "trader_team_plan", chunk["trader_team_plan"] ) # Set first risk analyst to in_progress message_buffer.update_agent_status("Risky Analyst", "in_progress") @@ -996,8 +1009,8 @@ def wrapper(section_name, content): # Update Risky Analyst status and report if ( - "current_risky_response" in risk_state - and risk_state["current_risky_response"] + "current_risky_response" in risk_state + and risk_state["current_risky_response"] ): message_buffer.update_agent_status( "Risky Analyst", "in_progress" @@ -1008,14 +1021,14 @@ def wrapper(section_name, content): ) # Update risk report with risky analyst's latest analysis only message_buffer.update_report_section( - "final_trade_decision", + "final_portfolio_management_decision", f"### Risky Analyst Analysis\n{risk_state['current_risky_response']}", ) # Update Safe Analyst status and report if ( - "current_safe_response" in risk_state - and risk_state["current_safe_response"] + "current_safe_response" in risk_state + and risk_state["current_safe_response"] ): message_buffer.update_agent_status( "Safe Analyst", "in_progress" @@ -1026,14 +1039,14 @@ def wrapper(section_name, content): ) # Update risk report with safe analyst's latest analysis only message_buffer.update_report_section( - "final_trade_decision", + "final_portfolio_management_decision", f"### Safe Analyst Analysis\n{risk_state['current_safe_response']}", ) # Update Neutral Analyst status and report if ( - "current_neutral_response" in risk_state - and risk_state["current_neutral_response"] + "current_neutral_response" in risk_state + and risk_state["current_neutral_response"] ): message_buffer.update_agent_status( "Neutral Analyst", "in_progress" @@ -1044,7 +1057,7 @@ def wrapper(section_name, content): ) # Update risk report with neutral analyst's latest analysis only message_buffer.update_report_section( - "final_trade_decision", + "final_portfolio_management_decision", f"### Neutral Analyst Analysis\n{risk_state['current_neutral_response']}", ) @@ -1059,7 +1072,7 @@ def wrapper(section_name, content): ) # Update risk report with final decision only message_buffer.update_report_section( - "final_trade_decision", + "final_portfolio_management_decision", f"### Portfolio Manager Decision\n{risk_state['judge_decision']}", ) # Mark risk analysts as completed @@ -1079,7 +1092,7 @@ def wrapper(section_name, content): # Get final state and decision final_state = trace[-1] - decision = graph.process_signal(final_state["final_trade_decision"]) + decision = graph.process_signal(final_state["final_portfolio_management_decision"]) # Update all agent statuses to completed for agent in message_buffer.agent_status: diff --git a/cli/utils.py b/cli/utils.py index 7b9682a6b0..e4cf10d6fa 100644 --- a/cli/utils.py +++ b/cli/utils.py @@ -1,6 +1,8 @@ +from typing import List + import questionary -from typing import List, Optional, Tuple, Dict +from cli.main import console from cli.models import AnalystType ANALYST_ORDER = [ @@ -48,7 +50,7 @@ def validate_date(date_str: str) -> bool: date = questionary.text( "Enter the analysis date (YYYY-MM-DD):", validate=lambda x: validate_date(x.strip()) - or "Please enter a valid date in YYYY-MM-DD format.", + or "Please enter a valid date in YYYY-MM-DD format.", style=questionary.Style( [ ("text", "fg:green"), @@ -146,12 +148,17 @@ def select_shallow_thinking_agent(provider) -> str: ], "openrouter": [ ("Meta: Llama 4 Scout", "meta-llama/llama-4-scout:free"), - ("Meta: Llama 3.3 8B Instruct - A lightweight and ultra-fast variant of Llama 3.3 70B", "meta-llama/llama-3.3-8b-instruct:free"), - ("google/gemini-2.0-flash-exp:free - Gemini Flash 2.0 offers a significantly faster time to first token", "google/gemini-2.0-flash-exp:free"), + ("Meta: Llama 3.3 8B Instruct - A lightweight and ultra-fast variant of Llama 3.3 70B", + "meta-llama/llama-3.3-8b-instruct:free"), + ("google/gemini-2.0-flash-exp:free - Gemini Flash 2.0 offers a significantly faster time to first token", + "google/gemini-2.0-flash-exp:free"), ], "ollama": [ ("llama3.1 local", "llama3.1"), ("llama3.2 local", "llama3.2"), + ], + "xai": [ + ("grok-4-fast-non-reasoning", "grok-4-fast-non-reasoning"), ] } @@ -209,14 +216,18 @@ def select_deep_thinking_agent(provider) -> str: ], "openrouter": [ ("DeepSeek V3 - a 685B-parameter, mixture-of-experts model", "deepseek/deepseek-chat-v3-0324:free"), - ("Deepseek - latest iteration of the flagship chat model family from the DeepSeek team.", "deepseek/deepseek-chat-v3-0324:free"), + ("Deepseek - latest iteration of the flagship chat model family from the DeepSeek team.", + "deepseek/deepseek-chat-v3-0324:free"), ], "ollama": [ ("llama3.1 local", "llama3.1"), ("qwen3", "qwen3"), + ], + "xai": [ + ("grok-4-fast-reasoning", "grok-4-fast-reasoning"), ] } - + choice = questionary.select( "Select Your [Deep-Thinking LLM Engine]:", choices=[ @@ -239,17 +250,19 @@ def select_deep_thinking_agent(provider) -> str: return choice + def select_llm_provider() -> tuple[str, str]: """Select the OpenAI api url using interactive selection.""" # Define OpenAI api options with their corresponding endpoints BASE_URLS = [ + ("XAI", "https://api.x.ai/v1"), ("OpenAI", "https://api.openai.com/v1"), ("Anthropic", "https://api.anthropic.com/"), ("Google", "https://generativelanguage.googleapis.com/v1"), ("Openrouter", "https://openrouter.ai/api/v1"), - ("Ollama", "http://localhost:11434/v1"), + ("Ollama", "http://localhost:11434/v1"), ] - + choice = questionary.select( "Select your LLM Provider:", choices=[ @@ -265,12 +278,42 @@ def select_llm_provider() -> tuple[str, str]: ] ), ).ask() - + if choice is None: console.print("\n[red]no OpenAI backend selected. Exiting...[/red]") exit(1) - + display_name, url = choice print(f"You selected: {display_name}\tURL: {url}") - + return display_name, url + + +def select_language() -> int: + """Select output language for agent responses.""" + LANGUAGE_OPTIONS = [ + ("English", "en"), + ("Traditional Chinese", "zh-tw"), + ("Simplified Chinese", "zh-cn"), + ] + + choice = questionary.select( + "Select Output Language for Agents:", + choices=[ + questionary.Choice(display, value=value) for display, value in LANGUAGE_OPTIONS + ], + instruction="\n- Use arrow keys to navigate\n- Press Enter to select", + style=questionary.Style( + [ + ("selected", "fg:yellow noinherit"), + ("highlighted", "fg:yellow noinherit"), + ("pointer", "fg:yellow noinherit"), + ] + ), + ).ask() + + if choice is None: + console.print("\n[red]No langauge selected. Exiting...[/red]") + exit(1) + + return choice diff --git a/requirements.txt b/requirements.txt index a6154cd26e..57451615fa 100644 --- a/requirements.txt +++ b/requirements.txt @@ -24,3 +24,5 @@ rich questionary langchain_anthropic langchain-google-genai +langchain-xai +sentence-transformers \ No newline at end of file diff --git a/tradingagents/agents/analysts/fundamentals_analyst.py b/tradingagents/agents/analysts/fundamentals_analyst.py index e20139cbb3..3cb1148478 100644 --- a/tradingagents/agents/analysts/fundamentals_analyst.py +++ b/tradingagents/agents/analysts/fundamentals_analyst.py @@ -1,11 +1,12 @@ from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder -import time -import json -from tradingagents.agents.utils.agent_utils import get_fundamentals, get_balance_sheet, get_cashflow, get_income_statement, get_insider_sentiment, get_insider_transactions -from tradingagents.dataflows.config import get_config +from tradingagents.agents.utils.agent_utils import get_fundamentals, get_balance_sheet, get_cashflow, \ + get_income_statement + + +def create_fundamentals_analyst(llm, config): + """Create the fundamentals analyst node with language support.""" -def create_fundamentals_analyst(llm): def fundamentals_analyst_node(state): current_date = state["trade_date"] ticker = state["company_of_interest"] @@ -18,34 +19,49 @@ def fundamentals_analyst_node(state): get_income_statement, ] + language = config["output_language"] + language_prompts = { + "en": "", + "zh-tw": "Use Traditional Chinese as the output.", + "zh-cn": "Use Simplified Chinese as the output.", + } + language_prompt = language_prompts.get(language, "") + system_message = ( - "You are a researcher tasked with analyzing fundamental information over the past week about a company. Please write a comprehensive report of the company's fundamental information such as financial documents, company profile, basic company financials, and company financial history to gain a full view of the company's fundamental information to inform traders. Make sure to include as much detail as possible. Do not simply state the trends are mixed, provide detailed and finegrained analysis and insights that may help traders make decisions." - + " Make sure to append a Markdown table at the end of the report to organize key points in the report, organized and easy to read." - + " Use the available tools: `get_fundamentals` for comprehensive company analysis, `get_balance_sheet`, `get_cashflow`, and `get_income_statement` for specific financial statements.", + f""" +You are a researcher tasked with analyzing fundamental information over the past week about a company. +Please write a comprehensive report of the company's fundamental information such as financial documents, company profile, basic company financials, and company financial history to gain a full view of the company's fundamental information to inform traders. +Make sure to include as much detail as possible. +Do not simply state the trends are mixed, provide detailed and finegrained analysis and insights that may help traders make decisions. +Make sure to append a Markdown table at the end of the report to organize key points in the report, organized and easy to read. +Use the available tools: `get_fundamentals` for comprehensive company analysis, `get_balance_sheet`, `get_cashflow`, and `get_income_statement` for specific financial statements. +""" ) prompt = ChatPromptTemplate.from_messages( [ ( "system", - "You are a helpful AI assistant, collaborating with other assistants." - " Use the provided tools to progress towards answering the question." - " If you are unable to fully answer, that's OK; another assistant with different tools" - " will help where you left off. Execute what you can to make progress." - " If you or any other assistant has the FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** or deliverable," - " prefix your response with FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** so the team knows to stop." - " You have access to the following tools: {tool_names}.\n{system_message}" - "For your reference, the current date is {current_date}. The company we want to look at is {ticker}", + f""" +You are a helpful AI assistant, collaborating with other assistants. +Use the provided tools to progress towards answering the question. +If you are unable to fully answer, that's OK; another assistant with different tools will help where you left off. Execute what you can to make progress. +If you or any other assistant has the FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** or deliverable, prefix your response with FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** so the team knows to stop. + +You have access to the following tools: {tools}. + +{system_message} + +For your reference, the current date is {current_date}. +The company we want to look at is {ticker} + +Output language: ***{language_prompt}*** +""", ), MessagesPlaceholder(variable_name="messages"), ] ) - prompt = prompt.partial(system_message=system_message) - prompt = prompt.partial(tool_names=", ".join([tool.name for tool in tools])) - prompt = prompt.partial(current_date=current_date) - prompt = prompt.partial(ticker=ticker) - chain = prompt | llm.bind_tools(tools) result = chain.invoke(state["messages"]) @@ -57,7 +73,7 @@ def fundamentals_analyst_node(state): return { "messages": [result], - "fundamentals_report": report, + "fundamentals_analysis": report, } return fundamentals_analyst_node diff --git a/tradingagents/agents/analysts/market_analyst.py b/tradingagents/agents/analysts/market_analyst.py index c955dd76a6..6037cd3c9b 100644 --- a/tradingagents/agents/analysts/market_analyst.py +++ b/tradingagents/agents/analysts/market_analyst.py @@ -1,11 +1,10 @@ from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder -import time -import json + from tradingagents.agents.utils.agent_utils import get_stock_data, get_indicators -from tradingagents.dataflows.config import get_config -def create_market_analyst(llm): +def create_market_analyst(llm, config): + """Create the market analyst node with language support.""" def market_analyst_node(state): current_date = state["trade_date"] @@ -17,57 +16,69 @@ def market_analyst_node(state): get_indicators, ] - system_message = ( - """You are a trading assistant tasked with analyzing financial markets. Your role is to select the **most relevant indicators** for a given market condition or trading strategy from the following list. The goal is to choose up to **8 indicators** that provide complementary insights without redundancy. Categories and each category's indicators are: - -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. + language = config["output_language"] + language_prompts = { + "en": "", + "zh-tw": "Use Traditional Chinese as the output.", + "zh-cn": "Use Simplified Chinese as the output.", + } + language_prompt = language_prompts.get(language, "") -- Select indicators that provide diverse and complementary information. Avoid redundancy (e.g., do not select both rsi and stochrsi). Also briefly explain why they are suitable for the given market context. When you tool call, please use the exact name of the indicators provided above as they are defined parameters, otherwise your call will fail. Please make sure to call get_stock_data first to retrieve the CSV that is needed to generate indicators. Then use get_indicators with the specific indicator names. Write a very detailed and nuanced report of the trends you observe. Do not simply state the trends are mixed, provide detailed and finegrained analysis and insights that may help traders make decisions.""" - + """ Make sure to append a Markdown table at the end of the report to organize key points in the report, organized and easy to read.""" + system_message = ( + f"""You are a trading assistant tasked with analyzing financial markets. Your role is to select the **most relevant indicators** for a given market condition or trading strategy from the following list. The goal is to choose up to **8 indicators** that provide complementary insights without redundancy. Categories and each category's indicators are: + + 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. + + - Select indicators that provide diverse and complementary information. Avoid redundancy (e.g., do not select both rsi and stochrsi). Also briefly explain why they are suitable for the given market context. When you tool call, please use the exact name of the indicators provided above as they are defined parameters, otherwise your call will fail. Please make sure to call get_stock_data first to retrieve the CSV that is needed to generate indicators. Then use get_indicators with the specific indicator names. Write a very detailed and nuanced report of the trends you observe. Do not simply state the trends are mixed, provide detailed and finegrained analysis and insights that may help traders make decisions. + + Make sure to append a Markdown table at the end of the report to organize key points in the report, organized and easy to read. +""" ) prompt = ChatPromptTemplate.from_messages( [ ( "system", - "You are a helpful AI assistant, collaborating with other assistants." - " Use the provided tools to progress towards answering the question." - " If you are unable to fully answer, that's OK; another assistant with different tools" - " will help where you left off. Execute what you can to make progress." - " If you or any other assistant has the FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** or deliverable," - " prefix your response with FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** so the team knows to stop." - " You have access to the following tools: {tool_names}.\n{system_message}" - "For your reference, the current date is {current_date}. The company we want to look at is {ticker}", + f""" +You are a helpful AI assistant, collaborating with other assistants. +Use the provided tools to progress towards answering the question. +If you are unable to fully answer, that's OK; another assistant with different tools will help where you left off. Execute what you can to make progress. +If you or any other assistant has the FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** or deliverable, prefix your response with FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** so the team knows to stop. + +You have access to the following tools: {tools}. + +{system_message} + +For your reference, the current date is {current_date}. +The company we want to look at is {ticker} + +Output language: ***{language_prompt}*** +""", ), MessagesPlaceholder(variable_name="messages"), ] ) - prompt = prompt.partial(system_message=system_message) - prompt = prompt.partial(tool_names=", ".join([tool.name for tool in tools])) - prompt = prompt.partial(current_date=current_date) - prompt = prompt.partial(ticker=ticker) - chain = prompt | llm.bind_tools(tools) result = chain.invoke(state["messages"]) @@ -76,10 +87,10 @@ def market_analyst_node(state): if len(result.tool_calls) == 0: report = result.content - + return { "messages": [result], - "market_report": report, + "market_analysis": report, } return market_analyst_node diff --git a/tradingagents/agents/analysts/news_analyst.py b/tradingagents/agents/analysts/news_analyst.py index 03b4fae449..7f5cb720ef 100644 --- a/tradingagents/agents/analysts/news_analyst.py +++ b/tradingagents/agents/analysts/news_analyst.py @@ -1,11 +1,11 @@ from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder -import time -import json + from tradingagents.agents.utils.agent_utils import get_news, get_global_news -from tradingagents.dataflows.config import get_config -def create_news_analyst(llm): +def create_news_analyst(llm, config): + """Create the news analyst node with language support.""" + def news_analyst_node(state): current_date = state["trade_date"] ticker = state["company_of_interest"] @@ -15,33 +15,48 @@ def news_analyst_node(state): get_global_news, ] + language = config["output_language"] + language_prompts = { + "en": "", + "zh-tw": "Use Traditional Chinese as the output.", + "zh-cn": "Use Simplified Chinese as the output.", + } + language_prompt = language_prompts.get(language, "") + system_message = ( - "You are a news researcher tasked with analyzing recent news and trends over the past week. Please write a comprehensive report of the current state of the world that is relevant for trading and macroeconomics. Use the available tools: get_news(query, start_date, end_date) for company-specific or targeted news searches, and get_global_news(curr_date, look_back_days, limit) for broader macroeconomic news. Do not simply state the trends are mixed, provide detailed and finegrained analysis and insights that may help traders make decisions." - + """ Make sure to append a Markdown table at the end of the report to organize key points in the report, organized and easy to read.""" + f""" +You are a news researcher tasked with analyzing recent news and trends over the past week. +Please write a comprehensive report of the current state of the world that is relevant for trading and macroeconomics. +Use the available tools: get_news(query, start_date, end_date) for company-specific or targeted news searches, and get_global_news(curr_date, look_back_days, limit) for broader macroeconomic news. +Do not simply state the trends are mixed, provide detailed and finegrained analysis and insights that may help traders make decisions. +Make sure to append a Markdown table at the end of the report to organize key points in the report, organized and easy to read. +""" ) prompt = ChatPromptTemplate.from_messages( [ ( "system", - "You are a helpful AI assistant, collaborating with other assistants." - " Use the provided tools to progress towards answering the question." - " If you are unable to fully answer, that's OK; another assistant with different tools" - " will help where you left off. Execute what you can to make progress." - " If you or any other assistant has the FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** or deliverable," - " prefix your response with FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** so the team knows to stop." - " You have access to the following tools: {tool_names}.\n{system_message}" - "For your reference, the current date is {current_date}. We are looking at the company {ticker}", + f""" +You are a helpful AI assistant, collaborating with other assistants. +Use the provided tools to progress towards answering the question. +If you are unable to fully answer, that's OK; another assistant with different tools will help where you left off. Execute what you can to make progress. +If you or any other assistant has the FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** or deliverable, prefix your response with FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** so the team knows to stop. + +You have access to the following tools: {tools}. + +{system_message} + +For your reference, the current date is {current_date}. +The company we want to look at is {ticker} + +Output language: ***{language_prompt}*** +""", ), MessagesPlaceholder(variable_name="messages"), ] ) - prompt = prompt.partial(system_message=system_message) - prompt = prompt.partial(tool_names=", ".join([tool.name for tool in tools])) - prompt = prompt.partial(current_date=current_date) - prompt = prompt.partial(ticker=ticker) - chain = prompt | llm.bind_tools(tools) result = chain.invoke(state["messages"]) @@ -52,7 +67,7 @@ def news_analyst_node(state): return { "messages": [result], - "news_report": report, + "news_analysis": report, } return news_analyst_node diff --git a/tradingagents/agents/analysts/social_media_analyst.py b/tradingagents/agents/analysts/social_media_analyst.py index b25712d748..420d2ba8d5 100644 --- a/tradingagents/agents/analysts/social_media_analyst.py +++ b/tradingagents/agents/analysts/social_media_analyst.py @@ -1,11 +1,11 @@ from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder -import time -import json + from tradingagents.agents.utils.agent_utils import get_news -from tradingagents.dataflows.config import get_config -def create_social_media_analyst(llm): +def create_social_media_analyst(llm, config): + """Create the social media analyst node with language support.""" + def social_media_analyst_node(state): current_date = state["trade_date"] ticker = state["company_of_interest"] @@ -15,33 +15,48 @@ def social_media_analyst_node(state): get_news, ] + language = config["output_language"] + language_prompts = { + "en": "", + "zh-tw": "Use Traditional Chinese as the output.", + "zh-cn": "Use Simplified Chinese as the output.", + } + language_prompt = language_prompts.get(language, "") + system_message = ( - "You are a social media and company specific news researcher/analyst tasked with analyzing social media posts, recent company news, and public sentiment for a specific company over the past week. You will be given a company's name your objective is to write a comprehensive long report detailing your analysis, insights, and implications for traders and investors on this company's current state after looking at social media and what people are saying about that company, analyzing sentiment data of what people feel each day about the company, and looking at recent company news. Use the get_news(query, start_date, end_date) tool to search for company-specific news and social media discussions. Try to look at all sources possible from social media to sentiment to news. Do not simply state the trends are mixed, provide detailed and finegrained analysis and insights that may help traders make decisions." - + """ Make sure to append a Markdown table at the end of the report to organize key points in the report, organized and easy to read.""", + f""" +You are a social media and company specific news researcher/analyst tasked with analyzing social media posts, recent company news, and public sentiment for a specific company over the past week. +You will be given a company's name your objective is to write a comprehensive long report detailing your analysis, insights, and implications for traders and investors on this company's current state after looking at social media and what people are saying about that company, analyzing sentiment data of what people feel each day about the company, and looking at recent company news. +Use the get_news(query, start_date, end_date) tool to search for company-specific news and social media discussions. +Try to look at all sources possible from social media to sentiment to news. Do not simply state the trends are mixed, provide detailed and finegrained analysis and insights that may help traders make decisions. +Make sure to append a Markdown table at the end of the report to organize key points in the report, organized and easy to read. +""" ) prompt = ChatPromptTemplate.from_messages( [ ( "system", - "You are a helpful AI assistant, collaborating with other assistants." - " Use the provided tools to progress towards answering the question." - " If you are unable to fully answer, that's OK; another assistant with different tools" - " will help where you left off. Execute what you can to make progress." - " If you or any other assistant has the FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** or deliverable," - " prefix your response with FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** so the team knows to stop." - " You have access to the following tools: {tool_names}.\n{system_message}" - "For your reference, the current date is {current_date}. The current company we want to analyze is {ticker}", + f""" +You are a helpful AI assistant, collaborating with other assistants. +Use the provided tools to progress towards answering the question. +If you are unable to fully answer, that's OK; another assistant with different tools will help where you left off. Execute what you can to make progress. +If you or any other assistant has the FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** or deliverable, prefix your response with FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** so the team knows to stop. + +You have access to the following tools: {tools}. + +{system_message} + +For your reference, the current date is {current_date}. +The company we want to look at is {ticker} + +Output language: ***{language_prompt}*** +""", ), MessagesPlaceholder(variable_name="messages"), ] ) - prompt = prompt.partial(system_message=system_message) - prompt = prompt.partial(tool_names=", ".join([tool.name for tool in tools])) - prompt = prompt.partial(current_date=current_date) - prompt = prompt.partial(ticker=ticker) - chain = prompt | llm.bind_tools(tools) result = chain.invoke(state["messages"]) @@ -53,7 +68,7 @@ def social_media_analyst_node(state): return { "messages": [result], - "sentiment_report": report, + "sentiment_analysis": report, } return social_media_analyst_node diff --git a/tradingagents/agents/managers/research_manager.py b/tradingagents/agents/managers/research_manager.py index c537fa2ff2..a1d2b48509 100644 --- a/tradingagents/agents/managers/research_manager.py +++ b/tradingagents/agents/managers/research_manager.py @@ -1,28 +1,32 @@ -import time -import json +def create_research_manager(llm, memory, config): + """Create the research manager node with language support.""" + language = config["output_language"] + language_prompts = { + "en": "", + "zh-tw": "Use Traditional Chinese as the output.", + "zh-cn": "Use Simplified Chinese as the output.", + } + language_prompt = language_prompts.get(language, "") - -def create_research_manager(llm, memory): def research_manager_node(state) -> dict: 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"] + market_research_report = state["market_analysis"] + sentiment_analysis = state["sentiment_analysis"] + news_analysis = state["news_analysis"] + fundamentals_analysis = state["fundamentals_analysis"] 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"{market_research_report}\n\n{sentiment_analysis}\n\n{news_analysis}\n\n{fundamentals_analysis}" past_memories = memory.get_memories(curr_situation, n_matches=2) past_memory_str = "" for i, rec in enumerate(past_memories, 1): past_memory_str += rec["recommendation"] + "\n\n" - prompt = f"""As the portfolio manager and debate facilitator, your role is to critically evaluate this round of debate and make a definitive decision: align with the bear analyst, the bull analyst, or choose Hold only if it is strongly justified based on the arguments presented. - + prompt = f""" +As the portfolio manager and debate facilitator, your role is to critically evaluate this round of debate and make a definitive decision: align with the bear analyst, the bull analyst, or choose Hold only if it is strongly justified based on the arguments presented. Summarize the key points from both sides concisely, focusing on the most compelling evidence or reasoning. Your recommendation—Buy, Sell, or Hold—must be clear and actionable. Avoid defaulting to Hold simply because both sides have valid points; commit to a stance grounded in the debate's strongest arguments. - Additionally, develop a detailed investment plan for the trader. This should include: Your Recommendation: A decisive stance supported by the most convincing arguments. @@ -35,7 +39,10 @@ def research_manager_node(state) -> dict: Here is the debate: Debate History: -{history}""" +{history} + +Output language: ***{language_prompt}*** +""" response = llm.invoke(prompt) new_investment_debate_state = { @@ -49,7 +56,7 @@ def research_manager_node(state) -> dict: return { "investment_debate_state": new_investment_debate_state, - "investment_plan": response.content, + "research_team_decision": response.content, } return research_manager_node diff --git a/tradingagents/agents/managers/risk_manager.py b/tradingagents/agents/managers/risk_manager.py index fba763d6c0..2ef120c076 100644 --- a/tradingagents/agents/managers/risk_manager.py +++ b/tradingagents/agents/managers/risk_manager.py @@ -1,28 +1,33 @@ -import time -import json +def create_risk_manager(llm, memory, config): + """Create the risk manager node with language support.""" + language = config["output_language"] + language_prompts = { + "en": "", + "zh-tw": "Use Traditional Chinese as the output.", + "zh-cn": "Use Simplified Chinese as the output.", + } + language_prompt = language_prompts.get(language, "") - -def create_risk_manager(llm, memory): def risk_manager_node(state) -> dict: - company_name = state["company_of_interest"] 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["news_report"] - sentiment_report = state["sentiment_report"] - trader_plan = state["investment_plan"] + market_research_report = state["market_analysis"] + news_analysis = state["news_analysis"] + fundamentals_analysis = state["news_analysis"] + sentiment_analysis = state["sentiment_analysis"] + trader_plan = state["research_team_decision"] - curr_situation = f"{market_research_report}\n\n{sentiment_report}\n\n{news_report}\n\n{fundamentals_report}" + curr_situation = f"{market_research_report}\n\n{sentiment_analysis}\n\n{news_analysis}\n\n{fundamentals_analysis}" past_memories = memory.get_memories(curr_situation, n_matches=2) past_memory_str = "" for i, rec in enumerate(past_memories, 1): past_memory_str += rec["recommendation"] + "\n\n" - prompt = f"""As the Risk Management Judge and Debate Facilitator, your goal is to evaluate the debate between three risk analysts—Risky, Neutral, and Safe/Conservative—and determine the best course of action for the trader. Your decision must result in a clear recommendation: Buy, Sell, or Hold. Choose Hold only if strongly justified by specific arguments, not as a fallback when all sides seem valid. Strive for clarity and decisiveness. + prompt = f""" +As the Risk Management Judge and Debate Facilitator, your goal is to evaluate the debate between three risk analysts—Risky, Neutral, and Safe/Conservative—and determine the best course of action for the trader. Your decision must result in a clear recommendation: Buy, Sell, or Hold. Choose Hold only if strongly justified by specific arguments, not as a fallback when all sides seem valid. Strive for clarity and decisiveness. Guidelines for Decision-Making: 1. **Summarize Key Arguments**: Extract the strongest points from each analyst, focusing on relevance to the context. @@ -41,7 +46,10 @@ def risk_manager_node(state) -> dict: --- -Focus on actionable insights and continuous improvement. Build on past lessons, critically evaluate all perspectives, and ensure each decision advances better outcomes.""" +Focus on actionable insights and continuous improvement. Build on past lessons, critically evaluate all perspectives, and ensure each decision advances better outcomes. + +Output language: ***{language_prompt}*** +""" response = llm.invoke(prompt) @@ -60,7 +68,7 @@ def risk_manager_node(state) -> dict: return { "risk_debate_state": new_risk_debate_state, - "final_trade_decision": response.content, + "final_portfolio_management_decision": response.content, } return risk_manager_node diff --git a/tradingagents/agents/researchers/bear_researcher.py b/tradingagents/agents/researchers/bear_researcher.py index 6634490a55..3d080948d7 100644 --- a/tradingagents/agents/researchers/bear_researcher.py +++ b/tradingagents/agents/researchers/bear_researcher.py @@ -1,28 +1,35 @@ -from langchain_core.messages import AIMessage -import time -import json +def create_bear_researcher(llm, memory, config): + """Create the bear researcher node with language support.""" + language = config["output_language"] + language_prompts = { + "en": "", + "zh-tw": "Use Traditional Chinese as the output.", + "zh-cn": "Use Simplified Chinese as the output.", + } + language_prompt = language_prompts.get(language, "") - -def create_bear_researcher(llm, memory): def bear_node(state) -> dict: investment_debate_state = state["investment_debate_state"] history = investment_debate_state.get("history", "") 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"] + market_research_report = state["market_analysis"] + sentiment_analysis = state["sentiment_analysis"] + news_analysis = state["news_analysis"] + fundamentals_analysis = state["fundamentals_analysis"] - curr_situation = f"{market_research_report}\n\n{sentiment_report}\n\n{news_report}\n\n{fundamentals_report}" + curr_situation = f"{market_research_report}\n\n{sentiment_analysis}\n\n{news_analysis}\n\n{fundamentals_analysis}" past_memories = memory.get_memories(curr_situation, n_matches=2) past_memory_str = "" for i, rec in enumerate(past_memories, 1): past_memory_str += rec["recommendation"] + "\n\n" - prompt = f"""You are a Bear Analyst making the case against investing in the stock. Your goal is to present a well-reasoned argument emphasizing risks, challenges, and negative indicators. Leverage the provided research and data to highlight potential downsides and counter bullish arguments effectively. + prompt = f""" +You are a Bear Analyst making the case against investing in the stock. +Your goal is to present a well-reasoned argument emphasizing risks, challenges, and negative indicators. +Leverage the provided research and data to highlight potential downsides and counter bullish arguments effectively. Key points to focus on: @@ -33,15 +40,45 @@ def bear_node(state) -> dict: - Engagement: Present your argument in a conversational style, directly engaging with the bull analyst's points and debating effectively rather than simply listing facts. Resources available: +Market research report: +{market_research_report} + + +-------------------------------------- +Social media sentiment report: +{sentiment_analysis} + + +-------------------------------------- +Latest world affairs news: +{news_analysis} + + +-------------------------------------- +Company fundamentals report: +{fundamentals_analysis} + + +-------------------------------------- +Conversation history of the debate: +{history} + + +-------------------------------------- +Last bull argument: +{current_response} + + +-------------------------------------- +Reflections from similar situations and lessons learned: +{past_memory_str} + + +-------------------------------------- +Use this information to deliver a compelling bear argument, refute the bull's claims, and engage in a dynamic debate that demonstrates the risks and weaknesses of investing in the stock. +You must also address reflections and learn from lessons and mistakes you made in the past. -Market research report: {market_research_report} -Social media sentiment report: {sentiment_report} -Latest world affairs news: {news_report} -Company fundamentals report: {fundamentals_report} -Conversation history of the debate: {history} -Last bull argument: {current_response} -Reflections from similar situations and lessons learned: {past_memory_str} -Use this information to deliver a compelling bear argument, refute the bull's claims, and engage in a dynamic debate that demonstrates the risks and weaknesses of investing in the stock. You must also address reflections and learn from lessons and mistakes you made in the past. +Output language: ***{language_prompt}*** """ response = llm.invoke(prompt) diff --git a/tradingagents/agents/researchers/bull_researcher.py b/tradingagents/agents/researchers/bull_researcher.py index b03ef7553a..34b057d98a 100644 --- a/tradingagents/agents/researchers/bull_researcher.py +++ b/tradingagents/agents/researchers/bull_researcher.py @@ -1,28 +1,35 @@ -from langchain_core.messages import AIMessage -import time -import json +def create_bull_researcher(llm, memory, config): + """Create the bull researcher node with language support.""" + language = config["output_language"] + language_prompts = { + "en": "", + "zh-tw": "Use Traditional Chinese as the output.", + "zh-cn": "Use Simplified Chinese as the output.", + } + language_prompt = language_prompts.get(language, "") - -def create_bull_researcher(llm, memory): def bull_node(state) -> dict: investment_debate_state = state["investment_debate_state"] history = investment_debate_state.get("history", "") 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"] + market_research_report = state["market_analysis"] + sentiment_analysis = state["sentiment_analysis"] + news_analysis = state["news_analysis"] + fundamentals_analysis = state["fundamentals_analysis"] - curr_situation = f"{market_research_report}\n\n{sentiment_report}\n\n{news_report}\n\n{fundamentals_report}" + curr_situation = f"{market_research_report}\n\n{sentiment_analysis}\n\n{news_analysis}\n\n{fundamentals_analysis}" past_memories = memory.get_memories(curr_situation, n_matches=2) past_memory_str = "" for i, rec in enumerate(past_memories, 1): past_memory_str += rec["recommendation"] + "\n\n" - prompt = f"""You are a Bull Analyst advocating for investing in the stock. Your task is to build a strong, evidence-based case emphasizing growth potential, competitive advantages, and positive market indicators. Leverage the provided research and data to address concerns and counter bearish arguments effectively. + prompt = f""" +You are a Bull Analyst advocating for investing in the stock. +Your task is to build a strong, evidence-based case emphasizing growth potential, competitive advantages, and positive market indicators. +Leverage the provided research and data to address concerns and counter bearish arguments effectively. Key points to focus on: - Growth Potential: Highlight the company's market opportunities, revenue projections, and scalability. @@ -32,14 +39,44 @@ def bull_node(state) -> dict: - 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} -Conversation history of the debate: {history} -Last bear argument: {current_response} -Reflections from similar situations and lessons learned: {past_memory_str} -Use this information to deliver a compelling bull argument, refute the bear's concerns, and engage in a dynamic debate that demonstrates the strengths of the bull position. You must also address reflections and learn from lessons and mistakes you made in the past. +Market research report: +{market_research_report} + +-------------------------------------- +Social media sentiment report: +{sentiment_analysis} + + +-------------------------------------- +Latest world affairs news: +{news_analysis} + + +-------------------------------------- +Company fundamentals report: +{fundamentals_analysis} + + +-------------------------------------- +Conversation history of the debate: +{history} + + +-------------------------------------- +Last bear argument: +{current_response} + + +-------------------------------------- +Reflections from similar situations and lessons learned: +{past_memory_str} + + +-------------------------------------- +Use this information to deliver a compelling bull argument, refute the bear's concerns, and engage in a dynamic debate that demonstrates the strengths of the bull position. +You must also address reflections and learn from lessons and mistakes you made in the past. + +Output language: ***{language_prompt}*** """ response = llm.invoke(prompt) diff --git a/tradingagents/agents/risk_mgmt/aggresive_debator.py b/tradingagents/agents/risk_mgmt/aggresive_debator.py index 7e2b4937a7..1257238855 100644 --- a/tradingagents/agents/risk_mgmt/aggresive_debator.py +++ b/tradingagents/agents/risk_mgmt/aggresive_debator.py @@ -1,8 +1,13 @@ -import time -import json +def create_risky_debator(llm, config): + """Create the risky debator node with language support.""" + language = config["output_language"] + language_prompts = { + "en": "", + "zh-tw": "Use Traditional Chinese as the output.", + "zh-cn": "Use Simplified Chinese as the output.", + } + language_prompt = language_prompts.get(language, "") - -def create_risky_debator(llm): def risky_node(state) -> dict: risk_debate_state = state["risk_debate_state"] history = risk_debate_state.get("history", "") @@ -11,26 +16,69 @@ def risky_node(state) -> dict: current_safe_response = risk_debate_state.get("current_safe_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"] - - trader_decision = state["trader_investment_plan"] + market_research_report = state["market_analysis"] + sentiment_analysis = state["sentiment_analysis"] + news_analysis = state["news_analysis"] + fundamentals_analysis = state["fundamentals_analysis"] - prompt = f"""As the Risky 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: + trader_decision = state["trader_team_plan"] + prompt = f""" +As the Risky 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: {trader_decision} -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: +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_analysis} + + +-------------------------------------- +Latest World Affairs Report: +{news_analysis} + + +-------------------------------------- +Company Fundamentals Report: +{fundamentals_analysis} + + +-------------------------------------- +Here is the current conversation history: +{history} + + +-------------------------------------- +Here are the last arguments from the conservative analyst: +{current_safe_response} + + +-------------------------------------- +Here are the last arguments from the neutral analyst: +{current_neutral_response} + + +-------------------------------------- +If there are no responses from the other viewpoints, do not halluncinate and just present your point. -Market Research Report: {market_research_report} -Social Media Sentiment Report: {sentiment_report} -Latest World Affairs Report: {news_report} -Company Fundamentals Report: {fundamentals_report} -Here is the current conversation history: {history} Here are the last arguments from the conservative analyst: {current_safe_response} Here are the last arguments from the neutral analyst: {current_neutral_response}. If there are no responses from the other viewpoints, do not halluncinate and just present your point. +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. -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.""" +Output language: ***{language_prompt}*** +""" response = llm.invoke(prompt) diff --git a/tradingagents/agents/risk_mgmt/conservative_debator.py b/tradingagents/agents/risk_mgmt/conservative_debator.py index c56e16ad98..6faf55274b 100644 --- a/tradingagents/agents/risk_mgmt/conservative_debator.py +++ b/tradingagents/agents/risk_mgmt/conservative_debator.py @@ -1,9 +1,14 @@ -from langchain_core.messages import AIMessage -import time -import json +def create_safe_debator(llm, config): + """Create the safe debator node with language support.""" + language = config["output_language"] + language = config["output_language"] + language_prompts = { + "en": "", + "zh-tw": "Use Traditional Chinese as the output.", + "zh-cn": "Use Simplified Chinese as the output.", + } + language_prompt = language_prompts.get(language, "") - -def create_safe_debator(llm): def safe_node(state) -> dict: risk_debate_state = state["risk_debate_state"] history = risk_debate_state.get("history", "") @@ -12,26 +17,67 @@ def safe_node(state) -> dict: current_risky_response = risk_debate_state.get("current_risky_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"] - - trader_decision = state["trader_investment_plan"] + market_research_report = state["market_analysis"] + sentiment_analysis = state["sentiment_analysis"] + news_analysis = state["news_analysis"] + fundamentals_analysis = state["fundamentals_analysis"] - prompt = f"""As the Safe/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: + trader_decision = state["trader_team_plan"] + prompt = f""" +As the Safe/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: {trader_decision} -Your task is to actively counter the arguments of the Risky 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: +Your task is to actively counter the arguments of the Risky 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_analysis} + + +-------------------------------------- +Latest World Affairs Report: +{news_analysis} + + +-------------------------------------- +Company Fundamentals Report: +{fundamentals_analysis} + + +-------------------------------------- +Here is the current conversation history: +{history} + + +-------------------------------------- +Here is the last response from the risky analyst: +{current_risky_response} + + +-------------------------------------- +Here is the last response from the neutral analyst: +{current_neutral_response} + -Market Research Report: {market_research_report} -Social Media Sentiment Report: {sentiment_report} -Latest World Affairs Report: {news_report} -Company Fundamentals Report: {fundamentals_report} -Here is the current conversation history: {history} Here is the last response from the risky analyst: {current_risky_response} Here is the last response from the neutral analyst: {current_neutral_response}. If there are no responses from the other viewpoints, do not halluncinate and just present your point. +-------------------------------------- +If there are no responses from the other viewpoints, do not halluncinate and just present your point. +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. -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.""" +Output language: ***{language_prompt}*** +""" response = llm.invoke(prompt) diff --git a/tradingagents/agents/risk_mgmt/neutral_debator.py b/tradingagents/agents/risk_mgmt/neutral_debator.py index a6d2ef5c1a..ca7387e263 100644 --- a/tradingagents/agents/risk_mgmt/neutral_debator.py +++ b/tradingagents/agents/risk_mgmt/neutral_debator.py @@ -1,8 +1,13 @@ -import time -import json +def create_neutral_debator(llm, config): + """Create the neutral debator node with language support.""" + language = config["output_language"] + language_prompts = { + "en": "", + "zh-tw": "Use Traditional Chinese as the output.", + "zh-cn": "Use Simplified Chinese as the output.", + } + language_prompt = language_prompts.get(language, "") - -def create_neutral_debator(llm): def neutral_node(state) -> dict: risk_debate_state = state["risk_debate_state"] history = risk_debate_state.get("history", "") @@ -11,26 +16,66 @@ def neutral_node(state) -> dict: current_risky_response = risk_debate_state.get("current_risky_response", "") current_safe_response = risk_debate_state.get("current_safe_response", "") - market_research_report = state["market_report"] - sentiment_report = state["sentiment_report"] - news_report = state["news_report"] - fundamentals_report = state["fundamentals_report"] + market_research_report = state["market_analysis"] + sentiment_analysis = state["sentiment_analysis"] + news_analysis = state["news_analysis"] + fundamentals_analysis = state["fundamentals_analysis"] - trader_decision = state["trader_investment_plan"] + trader_decision = state["trader_team_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: + 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: {trader_decision} -Your task is to challenge both the Risky and Safe 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: +Your task is to challenge both the Risky and Safe 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_analysis} + + +-------------------------------------- +Latest World Affairs Report: +{news_analysis} + + +-------------------------------------- +Company Fundamentals Report: +{fundamentals_analysis} + + +-------------------------------------- +Here is the current conversation history: +{history} + + +-------------------------------------- +Here is the last response from the risky analyst: +{current_risky_response} + + +-------------------------------------- +Here is the last response from the safe analyst: +{current_safe_response} + -Market Research Report: {market_research_report} -Social Media Sentiment Report: {sentiment_report} -Latest World Affairs Report: {news_report} -Company Fundamentals Report: {fundamentals_report} -Here is the current conversation history: {history} Here is the last response from the risky analyst: {current_risky_response} Here is the last response from the safe analyst: {current_safe_response}. If there are no responses from the other viewpoints, do not halluncinate and just present your point. +-------------------------------------- +If there are no responses from the other viewpoints, do not halluncinate and just present your point. +Engage actively by analyzing both sides critically, addressing weaknesses in the risky 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. -Engage actively by analyzing both sides critically, addressing weaknesses in the risky 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.""" +Output language: ***{language_prompt}*** +""" response = llm.invoke(prompt) diff --git a/tradingagents/agents/trader/trader.py b/tradingagents/agents/trader/trader.py index 1b05c35d5f..95b7b1861b 100644 --- a/tradingagents/agents/trader/trader.py +++ b/tradingagents/agents/trader/trader.py @@ -1,18 +1,25 @@ import functools -import time -import json -def create_trader(llm, memory): +def create_trader(llm, memory, config): + """Create the trader node with language support.""" + language = config["output_language"] + language_prompts = { + "en": "", + "zh-tw": "Use Traditional Chinese as the output.", + "zh-cn": "Use Simplified Chinese as the output.", + } + language_prompt = language_prompts.get(language, "") + def trader_node(state, name): company_name = state["company_of_interest"] - 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"] + research_team_decision = state["research_team_decision"] + market_research_report = state["market_analysis"] + sentiment_analysis = state["sentiment_analysis"] + news_analysis = state["news_analysis"] + fundamentals_analysis = state["fundamentals_analysis"] - curr_situation = f"{market_research_report}\n\n{sentiment_report}\n\n{news_report}\n\n{fundamentals_report}" + curr_situation = f"{market_research_report}\n\n{sentiment_analysis}\n\n{news_analysis}\n\n{fundamentals_analysis}" past_memories = memory.get_memories(curr_situation, n_matches=2) past_memory_str = "" @@ -24,13 +31,36 @@ def trader_node(state, name): context = { "role": "user", - "content": f"Based on a comprehensive analysis by a team of analysts, here is an investment plan tailored for {company_name}. 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}. +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. + +Proposed Investment Plan: +{research_team_decision} + + +-------------------------------------- +Leverage these insights to make an informed and strategic decision. +""", } messages = [ { "role": "system", - "content": f"""You are a trading agent analyzing market data to make investment decisions. Based on your analysis, provide a specific recommendation to buy, sell, or hold. End with a firm decision and always conclude your response with 'FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL**' to confirm your recommendation. Do not forget to utilize lessons from past decisions to learn from your mistakes. Here is some reflections from similar situatiosn you traded in and the lessons learned: {past_memory_str}""", + "content": f""" +You are a trading agent analyzing market data to make investment decisions. +Based on your analysis, provide a specific recommendation to buy, sell, or hold. +End with a firm decision and always conclude your response with 'FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL**' to confirm your recommendation. +Do not forget to utilize lessons from past decisions to learn from your mistakes. + +Here is some reflections from similar situations you traded in and the lessons learned: +{past_memory_str} + + +-------------------------------------- +Output language: ***{language_prompt}*** +""", }, context, ] @@ -39,7 +69,7 @@ def trader_node(state, name): return { "messages": [result], - "trader_investment_plan": result.content, + "trader_team_plan": result.content, "sender": name, } diff --git a/tradingagents/agents/utils/agent_states.py b/tradingagents/agents/utils/agent_states.py index 3a859ea193..af672b5ae3 100644 --- a/tradingagents/agents/utils/agent_states.py +++ b/tradingagents/agents/utils/agent_states.py @@ -1,10 +1,7 @@ -from typing import Annotated, Sequence -from datetime import date, timedelta, datetime -from typing_extensions import TypedDict, Optional -from langchain_openai import ChatOpenAI -from tradingagents.agents import * -from langgraph.prebuilt import ToolNode -from langgraph.graph import END, StateGraph, START, MessagesState +from typing import Annotated + +from langgraph.graph import MessagesState +from typing_extensions import TypedDict # Researcher team state @@ -54,23 +51,23 @@ class AgentState(MessagesState): sender: Annotated[str, "Agent that sent this message"] # research step - market_report: Annotated[str, "Report from the Market Analyst"] - sentiment_report: Annotated[str, "Report from the Social Media Analyst"] - news_report: Annotated[ + market_analysis: Annotated[str, "Report from the Market Analyst"] + sentiment_analysis: Annotated[str, "Report from the Social Media Analyst"] + news_analysis: Annotated[ str, "Report from the News Researcher of current world affairs" ] - fundamentals_report: Annotated[str, "Report from the Fundamentals Researcher"] + fundamentals_analysis: Annotated[str, "Report from the Fundamentals Researcher"] # researcher team discussion step investment_debate_state: Annotated[ InvestDebateState, "Current state of the debate on if to invest or not" ] - investment_plan: Annotated[str, "Plan generated by the Analyst"] + research_team_decision: Annotated[str, "Plan generated by the Analyst"] - trader_investment_plan: Annotated[str, "Plan generated by the Trader"] + trader_team_plan: Annotated[str, "Plan generated by the Trader"] # risk management team discussion step risk_debate_state: Annotated[ RiskDebateState, "Current state of the debate on evaluating risk" ] - final_trade_decision: Annotated[str, "Final decision made by the Risk Analysts"] + final_portfolio_management_decision: Annotated[str, "Final decision made by the Risk Analysts"] diff --git a/tradingagents/agents/utils/memory.py b/tradingagents/agents/utils/memory.py index 69b8ab8c96..02a2733c46 100644 --- a/tradingagents/agents/utils/memory.py +++ b/tradingagents/agents/utils/memory.py @@ -1,25 +1,41 @@ import chromadb from chromadb.config import Settings from openai import OpenAI +from sentence_transformers import SentenceTransformer class FinancialSituationMemory: def __init__(self, name, config): - if config["backend_url"] == "http://localhost:11434/v1": + # Based on llm_provider to select embeddings + if config["llm_provider"] == "ollama": self.embedding = "nomic-embed-text" - else: + self.embedding_client = OpenAI(base_url="http://localhost:11434/v1") + self.use_local_embedding = False + elif config["llm_provider"] == "openai": + self.client = OpenAI(base_url=config["backend_url"], api_key=config.get("api_key")) self.embedding = "text-embedding-3-small" - self.client = OpenAI(base_url=config["backend_url"]) + self.embedding_client = self.client + self.use_local_embedding = False + else: + # use local Hugging Face model + self.embedding_model = SentenceTransformer('all-MiniLM-L6-v2') + self.use_local_embedding = True + self.chroma_client = chromadb.Client(Settings(allow_reset=True)) self.situation_collection = self.chroma_client.create_collection(name=name) def get_embedding(self, text): - """Get OpenAI embedding for a text""" - - response = self.client.embeddings.create( - model=self.embedding, input=text - ) - return response.data[0].embedding + """Get embedding for a text""" + if self.use_local_embedding: + # use local Hugging Face model + return self.embedding_model.encode(text).tolist() + else: + # use API + response = self.embedding_client.embeddings.create( + model=self.embedding, + input=text + ) + return response.data[0].embedding def add_situations(self, situations_and_advice): """Add financial situations and their corresponding advice. Parameter is a list of tuples (situation, rec)""" diff --git a/tradingagents/default_config.py b/tradingagents/default_config.py index 1f40a2a283..61b5e2aefa 100644 --- a/tradingagents/default_config.py +++ b/tradingagents/default_config.py @@ -30,4 +30,5 @@ # Example: "get_stock_data": "alpha_vantage", # Override category default # Example: "get_news": "openai", # Override category default }, + "output_language": "en", } diff --git a/tradingagents/graph/propagation.py b/tradingagents/graph/propagation.py index 58ebd0a8c5..e98a68e257 100644 --- a/tradingagents/graph/propagation.py +++ b/tradingagents/graph/propagation.py @@ -1,8 +1,8 @@ # TradingAgents/graph/propagation.py from typing import Dict, Any + from tradingagents.agents.utils.agent_states import ( - AgentState, InvestDebateState, RiskDebateState, ) @@ -16,7 +16,7 @@ def __init__(self, max_recur_limit=100): self.max_recur_limit = max_recur_limit def create_initial_state( - self, company_name: str, trade_date: str + self, company_name: str, trade_date: str ) -> Dict[str, Any]: """Create the initial state for the agent graph.""" return { @@ -35,10 +35,10 @@ def create_initial_state( "count": 0, } ), - "market_report": "", - "fundamentals_report": "", - "sentiment_report": "", - "news_report": "", + "market_analysis": "", + "fundamentals_analysis": "", + "sentiment_analysis": "", + "news_analysis": "", } def get_graph_args(self) -> Dict[str, Any]: diff --git a/tradingagents/graph/reflection.py b/tradingagents/graph/reflection.py index 333032317c..0137772ed4 100644 --- a/tradingagents/graph/reflection.py +++ b/tradingagents/graph/reflection.py @@ -1,20 +1,29 @@ # TradingAgents/graph/reflection.py from typing import Dict, Any -from langchain_openai import ChatOpenAI + +from langchain_core.language_models.chat_models import BaseChatModel class Reflector: """Handles reflection on decisions and updating memory.""" - def __init__(self, quick_thinking_llm: ChatOpenAI): + def __init__(self, quick_thinking_llm: BaseChatModel, config): """Initialize the reflector with an LLM.""" + language = config["output_language"] + language_prompts = { + "en": "", + "zh-tw": "Use Traditional Chinese as the output.", + "zh-cn": "Use Simplified Chinese as the output.", + } + self.language_prompt = language_prompts.get(language, "") + self.quick_thinking_llm = quick_thinking_llm self.reflection_system_prompt = self._get_reflection_prompt() def _get_reflection_prompt(self) -> str: """Get the system prompt for reflection.""" - return """ + return f""" You are an expert financial analyst tasked with reviewing trading decisions/analysis and providing a comprehensive, step-by-step analysis. Your goal is to deliver detailed insights into investment decisions and highlight opportunities for improvement, adhering strictly to the following guidelines: @@ -44,26 +53,36 @@ def _get_reflection_prompt(self) -> str: - Ensure the condensed sentence captures the essence of the lessons and reasoning for easy reference. Adhere strictly to these instructions, and ensure your output is detailed, accurate, and actionable. You will also be given objective descriptions of the market from a price movements, technical indicator, news, and sentiment perspective to provide more context for your analysis. + +Output language: ***{self.language_prompt}*** """ 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"] + curr_market_report = current_state["market_analysis"] + curr_sentiment_report = current_state["sentiment_analysis"] + curr_news_report = current_state["news_analysis"] + curr_fundamentals_report = current_state["fundamentals_analysis"] return f"{curr_market_report}\n\n{curr_sentiment_report}\n\n{curr_news_report}\n\n{curr_fundamentals_report}" def _reflect_on_component( - self, component_type: str, report: str, situation: str, returns_losses + self, component_type: str, report: str, situation: str, returns_losses ) -> str: """Generate reflection for a component.""" messages = [ ("system", self.reflection_system_prompt), ( "human", - f"Returns: {returns_losses}\n\nAnalysis/Decision: {report}\n\nObjective Market Reports for Reference: {situation}", + f""" +Returns: {returns_losses} + + +Analysis/Decision: {report} + + +Objective Market Reports for Reference: {situation} +""", ), ] @@ -93,7 +112,7 @@ def reflect_bear_researcher(self, current_state, returns_losses, bear_memory): def reflect_trader(self, current_state, returns_losses, trader_memory): """Reflect on trader's decision and update memory.""" situation = self._extract_current_situation(current_state) - trader_decision = current_state["trader_investment_plan"] + trader_decision = current_state["trader_team_plan"] result = self._reflect_on_component( "TRADER", trader_decision, situation, returns_losses diff --git a/tradingagents/graph/setup.py b/tradingagents/graph/setup.py index b270ffc046..ea6f7e5d46 100644 --- a/tradingagents/graph/setup.py +++ b/tradingagents/graph/setup.py @@ -1,13 +1,13 @@ # TradingAgents/graph/setup.py from typing import Dict, Any -from langchain_openai import ChatOpenAI + +from langchain_core.language_models.chat_models import BaseChatModel from langgraph.graph import END, StateGraph, START from langgraph.prebuilt import ToolNode from tradingagents.agents import * from tradingagents.agents.utils.agent_states import AgentState - from .conditional_logic import ConditionalLogic @@ -15,16 +15,17 @@ class GraphSetup: """Handles the setup and configuration of the agent graph.""" def __init__( - self, - quick_thinking_llm: ChatOpenAI, - deep_thinking_llm: ChatOpenAI, - tool_nodes: Dict[str, ToolNode], - bull_memory, - bear_memory, - trader_memory, - invest_judge_memory, - risk_manager_memory, - conditional_logic: ConditionalLogic, + self, + quick_thinking_llm: BaseChatModel, + deep_thinking_llm: BaseChatModel, + tool_nodes: Dict[str, ToolNode], + bull_memory, + bear_memory, + trader_memory, + invest_judge_memory, + risk_manager_memory, + conditional_logic: ConditionalLogic, + config: Dict[str, Any], ): """Initialize with required components.""" self.quick_thinking_llm = quick_thinking_llm @@ -36,9 +37,10 @@ def __init__( self.invest_judge_memory = invest_judge_memory self.risk_manager_memory = risk_manager_memory self.conditional_logic = conditional_logic + self.config = config def setup_graph( - self, selected_analysts=["market", "social", "news", "fundamentals"] + self, selected_analysts=["market", "social", "news", "fundamentals"] ): """Set up and compile the agent workflow graph. @@ -59,50 +61,50 @@ def setup_graph( if "market" in selected_analysts: analyst_nodes["market"] = create_market_analyst( - self.quick_thinking_llm + self.quick_thinking_llm, self.config ) delete_nodes["market"] = create_msg_delete() tool_nodes["market"] = self.tool_nodes["market"] if "social" in selected_analysts: analyst_nodes["social"] = create_social_media_analyst( - self.quick_thinking_llm + self.quick_thinking_llm, self.config ) delete_nodes["social"] = create_msg_delete() tool_nodes["social"] = self.tool_nodes["social"] if "news" in selected_analysts: analyst_nodes["news"] = create_news_analyst( - self.quick_thinking_llm + self.quick_thinking_llm, self.config ) delete_nodes["news"] = create_msg_delete() tool_nodes["news"] = self.tool_nodes["news"] if "fundamentals" in selected_analysts: analyst_nodes["fundamentals"] = create_fundamentals_analyst( - self.quick_thinking_llm + self.quick_thinking_llm, self.config ) delete_nodes["fundamentals"] = create_msg_delete() tool_nodes["fundamentals"] = self.tool_nodes["fundamentals"] # Create researcher and manager nodes bull_researcher_node = create_bull_researcher( - self.quick_thinking_llm, self.bull_memory + self.quick_thinking_llm, self.bull_memory, self.config ) bear_researcher_node = create_bear_researcher( - self.quick_thinking_llm, self.bear_memory + self.quick_thinking_llm, self.bear_memory, self.config ) research_manager_node = create_research_manager( - self.deep_thinking_llm, self.invest_judge_memory + self.deep_thinking_llm, self.invest_judge_memory, self.config ) - trader_node = create_trader(self.quick_thinking_llm, self.trader_memory) + trader_node = create_trader(self.quick_thinking_llm, self.trader_memory, self.config) # Create risk analysis nodes - risky_analyst = create_risky_debator(self.quick_thinking_llm) - neutral_analyst = create_neutral_debator(self.quick_thinking_llm) - safe_analyst = create_safe_debator(self.quick_thinking_llm) + risky_analyst = create_risky_debator(self.quick_thinking_llm, self.config) + neutral_analyst = create_neutral_debator(self.quick_thinking_llm, self.config) + safe_analyst = create_safe_debator(self.quick_thinking_llm, self.config) risk_manager_node = create_risk_manager( - self.deep_thinking_llm, self.risk_manager_memory + self.deep_thinking_llm, self.risk_manager_memory, self.config ) # Create workflow @@ -147,7 +149,7 @@ def setup_graph( # Connect to next analyst or to Bull Researcher if this is the last analyst if i < len(selected_analysts) - 1: - next_analyst = f"{selected_analysts[i+1].capitalize()} Analyst" + next_analyst = f"{selected_analysts[i + 1].capitalize()} Analyst" workflow.add_edge(current_clear, next_analyst) else: workflow.add_edge(current_clear, "Bull Researcher") diff --git a/tradingagents/graph/signal_processing.py b/tradingagents/graph/signal_processing.py index 903e85293a..2cc3d8756f 100644 --- a/tradingagents/graph/signal_processing.py +++ b/tradingagents/graph/signal_processing.py @@ -1,13 +1,22 @@ # TradingAgents/graph/signal_processing.py -from langchain_openai import ChatOpenAI + +from langchain_core.language_models.chat_models import BaseChatModel class SignalProcessor: """Processes trading signals to extract actionable decisions.""" - def __init__(self, quick_thinking_llm: ChatOpenAI): + def __init__(self, quick_thinking_llm: BaseChatModel, config): """Initialize with an LLM for processing.""" + language = config["output_language"] + language_prompts = { + "en": "", + "zh-tw": "Use Traditional Chinese as the output.", + "zh-cn": "Use Simplified Chinese as the output.", + } + self.language_prompt = language_prompts.get(language, "") + self.quick_thinking_llm = quick_thinking_llm def process_signal(self, full_signal: str) -> str: @@ -23,7 +32,13 @@ def process_signal(self, full_signal: str) -> str: messages = [ ( "system", - "You are an efficient assistant designed to analyze paragraphs or financial reports provided by a group of analysts. Your task is to extract the investment decision: SELL, BUY, or HOLD. Provide only the extracted decision (SELL, BUY, or HOLD) as your output, without adding any additional text or information.", + f""" +You are an efficient assistant designed to analyze paragraphs or financial reports provided by a group of analysts. +Your task is to extract the investment decision: SELL, BUY, or HOLD. +Provide only the extracted decision (SELL, BUY, or HOLD) as your output, without adding any additional text or information. + +Output language: ***{self.language_prompt}*** + """, ), ("human", full_signal), ] diff --git a/tradingagents/graph/trading_graph.py b/tradingagents/graph/trading_graph.py index 40cdff755d..2b48dda569 100644 --- a/tradingagents/graph/trading_graph.py +++ b/tradingagents/graph/trading_graph.py @@ -1,27 +1,16 @@ # TradingAgents/graph/trading_graph.py +import json import os from pathlib import Path -import json -from datetime import date -from typing import Dict, Any, Tuple, List, Optional +from typing import Dict, Any -from langchain_openai import ChatOpenAI from langchain_anthropic import ChatAnthropic from langchain_google_genai import ChatGoogleGenerativeAI - +from langchain_openai import ChatOpenAI +from langchain_xai import ChatXAI from langgraph.prebuilt import ToolNode -from tradingagents.agents import * -from tradingagents.default_config import DEFAULT_CONFIG -from tradingagents.agents.utils.memory import FinancialSituationMemory -from tradingagents.agents.utils.agent_states import ( - AgentState, - InvestDebateState, - RiskDebateState, -) -from tradingagents.dataflows.config import set_config - # Import the new abstract tool methods from agent_utils from tradingagents.agents.utils.agent_utils import ( get_stock_data, @@ -35,11 +24,13 @@ get_insider_transactions, get_global_news ) - +from tradingagents.agents.utils.memory import FinancialSituationMemory +from tradingagents.dataflows.config import set_config +from tradingagents.default_config import DEFAULT_CONFIG from .conditional_logic import ConditionalLogic -from .setup import GraphSetup from .propagation import Propagator from .reflection import Reflector +from .setup import GraphSetup from .signal_processing import SignalProcessor @@ -47,10 +38,10 @@ class TradingAgentsGraph: """Main class that orchestrates the trading agents framework.""" def __init__( - self, - selected_analysts=["market", "social", "news", "fundamentals"], - debug=False, - config: Dict[str, Any] = None, + self, + selected_analysts=["market", "social", "news", "fundamentals"], + debug=False, + config: Dict[str, Any] = None, ): """Initialize the trading agents graph and components. @@ -72,18 +63,26 @@ def __init__( ) # Initialize LLMs - if self.config["llm_provider"].lower() == "openai" or self.config["llm_provider"] == "ollama" or self.config["llm_provider"] == "openrouter": - self.deep_thinking_llm = ChatOpenAI(model=self.config["deep_think_llm"], base_url=self.config["backend_url"]) - self.quick_thinking_llm = ChatOpenAI(model=self.config["quick_think_llm"], base_url=self.config["backend_url"]) + if self.config["llm_provider"].lower() == "openai" or self.config["llm_provider"] == "ollama" or self.config[ + "llm_provider"] == "openrouter": + self.deep_thinking_llm = ChatOpenAI(model=self.config["deep_think_llm"], + base_url=self.config["backend_url"]) + self.quick_thinking_llm = ChatOpenAI(model=self.config["quick_think_llm"], + base_url=self.config["backend_url"]) elif self.config["llm_provider"].lower() == "anthropic": - self.deep_thinking_llm = ChatAnthropic(model=self.config["deep_think_llm"], base_url=self.config["backend_url"]) - self.quick_thinking_llm = ChatAnthropic(model=self.config["quick_think_llm"], base_url=self.config["backend_url"]) + self.deep_thinking_llm = ChatAnthropic(model=self.config["deep_think_llm"], + base_url=self.config["backend_url"]) + self.quick_thinking_llm = ChatAnthropic(model=self.config["quick_think_llm"], + base_url=self.config["backend_url"]) elif self.config["llm_provider"].lower() == "google": self.deep_thinking_llm = ChatGoogleGenerativeAI(model=self.config["deep_think_llm"]) self.quick_thinking_llm = ChatGoogleGenerativeAI(model=self.config["quick_think_llm"]) + elif self.config["llm_provider"].lower() == "xai": + self.deep_thinking_llm = ChatXAI(model=self.config["deep_think_llm"]) + self.quick_thinking_llm = ChatXAI(model=self.config["quick_think_llm"]) else: raise ValueError(f"Unsupported LLM provider: {self.config['llm_provider']}") - + # Initialize memories self.bull_memory = FinancialSituationMemory("bull_memory", self.config) self.bear_memory = FinancialSituationMemory("bear_memory", self.config) @@ -106,11 +105,12 @@ def __init__( self.invest_judge_memory, self.risk_manager_memory, self.conditional_logic, + config=self.config, ) self.propagator = Propagator() - self.reflector = Reflector(self.quick_thinking_llm) - self.signal_processor = SignalProcessor(self.quick_thinking_llm) + self.reflector = Reflector(self.quick_thinking_llm, self.config) + self.signal_processor = SignalProcessor(self.quick_thinking_llm, self.config) # State tracking self.curr_state = None @@ -190,17 +190,17 @@ def propagate(self, company_name, trade_date): self._log_state(trade_date, final_state) # Return decision and processed signal - return final_state, self.process_signal(final_state["final_trade_decision"]) + return final_state, self.process_signal(final_state["final_portfolio_management_decision"]) def _log_state(self, trade_date, final_state): """Log the final state to a JSON file.""" self.log_states_dict[str(trade_date)] = { "company_of_interest": final_state["company_of_interest"], "trade_date": final_state["trade_date"], - "market_report": final_state["market_report"], - "sentiment_report": final_state["sentiment_report"], - "news_report": final_state["news_report"], - "fundamentals_report": final_state["fundamentals_report"], + "market_analysis": final_state["market_analysis"], + "sentiment_analysis": final_state["sentiment_analysis"], + "news_analysis": final_state["news_analysis"], + "fundamentals_analysis": final_state["fundamentals_analysis"], "investment_debate_state": { "bull_history": final_state["investment_debate_state"]["bull_history"], "bear_history": final_state["investment_debate_state"]["bear_history"], @@ -212,7 +212,7 @@ def _log_state(self, trade_date, final_state): "judge_decision" ], }, - "trader_investment_decision": final_state["trader_investment_plan"], + "trader_investment_decision": final_state["trader_team_plan"], "risk_debate_state": { "risky_history": final_state["risk_debate_state"]["risky_history"], "safe_history": final_state["risk_debate_state"]["safe_history"], @@ -220,8 +220,8 @@ def _log_state(self, trade_date, final_state): "history": final_state["risk_debate_state"]["history"], "judge_decision": final_state["risk_debate_state"]["judge_decision"], }, - "investment_plan": final_state["investment_plan"], - "final_trade_decision": final_state["final_trade_decision"], + "research_team_decision": final_state["research_team_decision"], + "final_portfolio_management_decision": final_state["final_portfolio_management_decision"], } # Save to file @@ -229,8 +229,8 @@ def _log_state(self, trade_date, final_state): directory.mkdir(parents=True, exist_ok=True) with open( - f"eval_results/{self.ticker}/TradingAgentsStrategy_logs/full_states_log_{trade_date}.json", - "w", + f"eval_results/{self.ticker}/TradingAgentsStrategy_logs/full_states_log_{trade_date}.json", + "w", ) as f: json.dump(self.log_states_dict, f, indent=4)