diff --git a/cli/main.py b/cli/main.py index adda48fc..6d2c25a1 100644 --- a/cli/main.py +++ b/cli/main.py @@ -505,6 +505,10 @@ def get_user_selections(): ) ) selected_ticker = get_ticker() + asset_type = detect_asset_type(selected_ticker) + console.print( + f"[green]Detected asset type:[/green] {asset_type.value}" + ) # Step 2: Analysis date default_date = datetime.datetime.now().strftime("%Y-%m-%d") @@ -523,7 +527,7 @@ def get_user_selections(): "Step 3: Analysts Team", "Select your LLM analyst agents for the analysis" ) ) - selected_analysts = select_analysts() + selected_analysts = select_analysts(asset_type) console.print( f"[green]Selected analysts:[/green] {', '.join(analyst.value for analyst in selected_analysts)}" ) @@ -577,6 +581,7 @@ def get_user_selections(): return { "ticker": selected_ticker, + "asset_type": asset_type.value, "analysis_date": analysis_date, "analysts": selected_analysts, "research_depth": selected_research_depth, @@ -989,6 +994,7 @@ def run_analysis(): # Add initial messages message_buffer.add_message("System", f"Selected ticker: {selections['ticker']}") + message_buffer.add_message("System", f"Detected asset type: {selections['asset_type']}") message_buffer.add_message( "System", f"Analysis date: {selections['analysis_date']}" ) @@ -1011,7 +1017,9 @@ def run_analysis(): # Initialize state and get graph args with callbacks init_agent_state = graph.propagator.create_initial_state( - selections["ticker"], selections["analysis_date"] + selections["ticker"], + selections["analysis_date"], + asset_type=selections["asset_type"], ) # Pass callbacks to graph config for tool execution tracking # (LLM tracking is handled separately via LLM constructor) diff --git a/cli/models.py b/cli/models.py index f68c3da1..6b6c2734 100644 --- a/cli/models.py +++ b/cli/models.py @@ -8,3 +8,8 @@ class AnalystType(str, Enum): SOCIAL = "social" NEWS = "news" FUNDAMENTALS = "fundamentals" + + +class AssetType(str, Enum): + STOCK = "stock" + CRYPTO = "crypto" diff --git a/cli/utils.py b/cli/utils.py index 5a8ec16c..6a02280c 100644 --- a/cli/utils.py +++ b/cli/utils.py @@ -3,7 +3,7 @@ from typing import List, Optional, Tuple, Dict from rich.console import Console -from cli.models import AnalystType +from cli.models import AnalystType, AssetType console = Console() @@ -14,6 +14,8 @@ ANALYST_ORDER = [ ("Fundamentals Analyst", AnalystType.FUNDAMENTALS), ] +CRYPTO_SUFFIXES = ("-USD", "-USDT", "-USDC", "-BTC", "-ETH") + def get_ticker() -> str: """Prompt the user to enter a ticker symbol.""" @@ -35,6 +37,25 @@ def get_ticker() -> str: return ticker.strip().upper() +def detect_asset_type(ticker: str) -> AssetType: + normalized_ticker = ticker.strip().upper() + if normalized_ticker.endswith(CRYPTO_SUFFIXES): + return AssetType.CRYPTO + return AssetType.STOCK + + +def filter_analysts_for_asset_type( + analysts: List[AnalystType], asset_type: AssetType +) -> List[AnalystType]: + if asset_type != AssetType.CRYPTO: + return analysts + return [ + analyst + for analyst in analysts + if analyst != AnalystType.FUNDAMENTALS + ] + + def get_analysis_date() -> str: """Prompt the user to enter a date in YYYY-MM-DD format.""" import re @@ -68,12 +89,18 @@ def get_analysis_date() -> str: return date.strip() -def select_analysts() -> List[AnalystType]: +def select_analysts(asset_type: AssetType = AssetType.STOCK) -> List[AnalystType]: """Select analysts using an interactive checkbox.""" + available_analysts = filter_analysts_for_asset_type( + [value for _, value in ANALYST_ORDER], + asset_type, + ) choices = questionary.checkbox( "Select Your [Analysts Team]:", choices=[ - questionary.Choice(display, value=value) for display, value in ANALYST_ORDER + questionary.Choice(display, value=value) + for display, value in ANALYST_ORDER + if value in available_analysts ], instruction="\n- Press Space to select/unselect analysts\n- Press 'a' to select/unselect all\n- Press Enter when done", validate=lambda x: len(x) > 0 or "You must select at least one analyst.", diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1 @@ + diff --git a/tests/test_crypto_asset_mode.py b/tests/test_crypto_asset_mode.py new file mode 100644 index 00000000..d9b54972 --- /dev/null +++ b/tests/test_crypto_asset_mode.py @@ -0,0 +1,56 @@ +import unittest + +from cli.models import AnalystType, AssetType +from cli.utils import detect_asset_type, filter_analysts_for_asset_type +from tradingagents.graph.propagation import Propagator + + +class CryptoAssetModeTests(unittest.TestCase): + def test_detects_crypto_pair_symbols(self): + self.assertEqual(detect_asset_type("BTC-USD"), AssetType.CRYPTO) + self.assertEqual(detect_asset_type("eth-usd"), AssetType.CRYPTO) + + def test_defaults_non_crypto_symbols_to_stock(self): + self.assertEqual(detect_asset_type("AAPL"), AssetType.STOCK) + self.assertEqual(detect_asset_type("SPY"), AssetType.STOCK) + + def test_filters_out_fundamentals_analyst_for_crypto(self): + analysts = [ + AnalystType.MARKET, + AnalystType.SOCIAL, + AnalystType.NEWS, + AnalystType.FUNDAMENTALS, + ] + + self.assertEqual( + filter_analysts_for_asset_type(analysts, AssetType.CRYPTO), + [ + AnalystType.MARKET, + AnalystType.SOCIAL, + AnalystType.NEWS, + ], + ) + + def test_keeps_all_analysts_for_stock(self): + analysts = [ + AnalystType.MARKET, + AnalystType.SOCIAL, + AnalystType.NEWS, + AnalystType.FUNDAMENTALS, + ] + + self.assertEqual( + filter_analysts_for_asset_type(analysts, AssetType.STOCK), + analysts, + ) + + def test_propagator_includes_asset_type_in_initial_state(self): + state = Propagator().create_initial_state( + "BTC-USD", "2026-04-18", asset_type=AssetType.CRYPTO.value + ) + + self.assertEqual(state["asset_type"], AssetType.CRYPTO.value) + + +if __name__ == "__main__": + unittest.main() diff --git a/tradingagents/agents/analysts/market_analyst.py b/tradingagents/agents/analysts/market_analyst.py index e175b94e..2ebbb149 100644 --- a/tradingagents/agents/analysts/market_analyst.py +++ b/tradingagents/agents/analysts/market_analyst.py @@ -10,7 +10,8 @@ def create_market_analyst(llm): def market_analyst_node(state): current_date = state["trade_date"] ticker = state["company_of_interest"] - company_name = state["company_of_interest"] + asset_type = state.get("asset_type", "stock") + asset_label = "stock" if asset_type == "stock" else "crypto asset" tools = [ get_stock_data, @@ -57,7 +58,7 @@ Volume-Based Indicators: " 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}", + "For your reference, the current date is {current_date}. The {asset_label} we want to look at is {ticker}", ), MessagesPlaceholder(variable_name="messages"), ] @@ -67,6 +68,7 @@ Volume-Based Indicators: prompt = prompt.partial(tool_names=", ".join([tool.name for tool in tools])) prompt = prompt.partial(current_date=current_date) prompt = prompt.partial(ticker=ticker) + prompt = prompt.partial(asset_label=asset_label) chain = prompt | llm.bind_tools(tools) diff --git a/tradingagents/agents/analysts/news_analyst.py b/tradingagents/agents/analysts/news_analyst.py index 03b4fae4..f271d0d9 100644 --- a/tradingagents/agents/analysts/news_analyst.py +++ b/tradingagents/agents/analysts/news_analyst.py @@ -9,6 +9,8 @@ def create_news_analyst(llm): def news_analyst_node(state): current_date = state["trade_date"] ticker = state["company_of_interest"] + asset_type = state.get("asset_type", "stock") + asset_label = "company" if asset_type == "stock" else "asset" tools = [ get_news, @@ -16,7 +18,7 @@ def create_news_analyst(llm): ] 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." + 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 {asset_label}-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.""" ) @@ -31,7 +33,7 @@ def create_news_analyst(llm): " 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}", + "For your reference, the current date is {current_date}. We are looking at the {asset_label} {ticker}", ), MessagesPlaceholder(variable_name="messages"), ] @@ -41,6 +43,7 @@ def create_news_analyst(llm): prompt = prompt.partial(tool_names=", ".join([tool.name for tool in tools])) prompt = prompt.partial(current_date=current_date) prompt = prompt.partial(ticker=ticker) + prompt = prompt.partial(asset_label=asset_label) chain = prompt | llm.bind_tools(tools) result = chain.invoke(state["messages"]) diff --git a/tradingagents/agents/analysts/social_media_analyst.py b/tradingagents/agents/analysts/social_media_analyst.py index b25712d7..6552456d 100644 --- a/tradingagents/agents/analysts/social_media_analyst.py +++ b/tradingagents/agents/analysts/social_media_analyst.py @@ -9,14 +9,15 @@ def create_social_media_analyst(llm): def social_media_analyst_node(state): current_date = state["trade_date"] ticker = state["company_of_interest"] - company_name = state["company_of_interest"] + asset_type = state.get("asset_type", "stock") + subject_label = "company" if asset_type == "stock" else "asset" tools = [ get_news, ] 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." + f"You are a social media and targeted news researcher/analyst tasked with analyzing social media posts, recent {subject_label} news, and public sentiment for a specific {subject_label} over the past week. You will be given an asset identifier and your objective is to write a comprehensive long report detailing your analysis, insights, and implications for traders and investors after looking at social media, sentiment, and recent news related to that {subject_label}. Use the get_news(query, start_date, end_date) tool to search for {subject_label}-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.""", ) @@ -31,7 +32,7 @@ def create_social_media_analyst(llm): " 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}", + "For your reference, the current date is {current_date}. The current {subject_label} we want to analyze is {ticker}", ), MessagesPlaceholder(variable_name="messages"), ] @@ -41,6 +42,7 @@ def create_social_media_analyst(llm): prompt = prompt.partial(tool_names=", ".join([tool.name for tool in tools])) prompt = prompt.partial(current_date=current_date) prompt = prompt.partial(ticker=ticker) + prompt = prompt.partial(subject_label=subject_label) chain = prompt | llm.bind_tools(tools) diff --git a/tradingagents/agents/researchers/bear_researcher.py b/tradingagents/agents/researchers/bear_researcher.py index 6634490a..4ebbe73e 100644 --- a/tradingagents/agents/researchers/bear_researcher.py +++ b/tradingagents/agents/researchers/bear_researcher.py @@ -14,6 +14,13 @@ def create_bear_researcher(llm, memory): sentiment_report = state["sentiment_report"] news_report = state["news_report"] fundamentals_report = state["fundamentals_report"] + asset_type = state.get("asset_type", "stock") + target_label = "stock" if asset_type == "stock" else "asset" + fundamentals_label = ( + "Company fundamentals report" + if asset_type == "stock" + else "Asset fundamentals report (may be unavailable for crypto)" + ) curr_situation = f"{market_research_report}\n\n{sentiment_report}\n\n{news_report}\n\n{fundamentals_report}" past_memories = memory.get_memories(curr_situation, n_matches=2) @@ -22,7 +29,7 @@ def create_bear_researcher(llm, memory): 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 {target_label}. 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: @@ -37,11 +44,11 @@ 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} +{fundamentals_label}: {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. +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 {target_label}. You must also address reflections and learn from lessons and mistakes you made in the past. """ response = llm.invoke(prompt) diff --git a/tradingagents/agents/researchers/bull_researcher.py b/tradingagents/agents/researchers/bull_researcher.py index b03ef755..03332d05 100644 --- a/tradingagents/agents/researchers/bull_researcher.py +++ b/tradingagents/agents/researchers/bull_researcher.py @@ -14,6 +14,13 @@ def create_bull_researcher(llm, memory): sentiment_report = state["sentiment_report"] news_report = state["news_report"] fundamentals_report = state["fundamentals_report"] + asset_type = state.get("asset_type", "stock") + target_label = "stock" if asset_type == "stock" else "asset" + fundamentals_label = ( + "Company fundamentals report" + if asset_type == "stock" + else "Asset fundamentals report (may be unavailable for crypto)" + ) curr_situation = f"{market_research_report}\n\n{sentiment_report}\n\n{news_report}\n\n{fundamentals_report}" past_memories = memory.get_memories(curr_situation, n_matches=2) @@ -22,7 +29,7 @@ def create_bull_researcher(llm, memory): 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 {target_label}. 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. @@ -35,7 +42,7 @@ 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} +{fundamentals_label}: {fundamentals_report} Conversation history of the debate: {history} Last bear argument: {current_response} Reflections from similar situations and lessons learned: {past_memory_str} diff --git a/tradingagents/agents/trader/trader.py b/tradingagents/agents/trader/trader.py index 1b05c35d..a1a28533 100644 --- a/tradingagents/agents/trader/trader.py +++ b/tradingagents/agents/trader/trader.py @@ -6,6 +6,8 @@ import json def create_trader(llm, memory): def trader_node(state, name): company_name = state["company_of_interest"] + asset_type = state.get("asset_type", "stock") + target_label = "company" if asset_type == "stock" else "asset" investment_plan = state["investment_plan"] market_research_report = state["market_report"] sentiment_report = state["sentiment_report"] @@ -24,7 +26,7 @@ def create_trader(llm, memory): context = { "role": "user", - "content": f"Based on a comprehensive analysis by a team of analysts, here is an investment plan tailored for {company_name}. 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 the {target_label} {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.", } messages = [ diff --git a/tradingagents/agents/utils/agent_states.py b/tradingagents/agents/utils/agent_states.py index 813b00ee..7fcbee89 100644 --- a/tradingagents/agents/utils/agent_states.py +++ b/tradingagents/agents/utils/agent_states.py @@ -49,6 +49,7 @@ class RiskDebateState(TypedDict): class AgentState(MessagesState): company_of_interest: Annotated[str, "Company that we are interested in trading"] + asset_type: Annotated[str, "Asset type under analysis such as stock or crypto"] trade_date: Annotated[str, "What date we are trading at"] sender: Annotated[str, "Agent that sent this message"] diff --git a/tradingagents/graph/propagation.py b/tradingagents/graph/propagation.py index 0fd10c0c..a3caaa82 100644 --- a/tradingagents/graph/propagation.py +++ b/tradingagents/graph/propagation.py @@ -16,12 +16,13 @@ class Propagator: 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, asset_type: str = "stock" ) -> Dict[str, Any]: """Create the initial state for the agent graph.""" return { "messages": [("human", company_name)], "company_of_interest": company_name, + "asset_type": asset_type, "trade_date": str(trade_date), "investment_debate_state": InvestDebateState( {