Merge 99ec63f966 into fa4d01c23a
This commit is contained in:
commit
d09a0dadd5
12
cli/main.py
12
cli/main.py
|
|
@ -508,6 +508,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")
|
||||
|
|
@ -535,7 +539,7 @@ def get_user_selections():
|
|||
"Step 4: 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)}"
|
||||
)
|
||||
|
|
@ -598,6 +602,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,
|
||||
|
|
@ -1022,6 +1027,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']}"
|
||||
)
|
||||
|
|
@ -1044,7 +1050,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)
|
||||
|
|
|
|||
|
|
@ -8,3 +8,8 @@ class AnalystType(str, Enum):
|
|||
SOCIAL = "social"
|
||||
NEWS = "news"
|
||||
FUNDAMENTALS = "fundamentals"
|
||||
|
||||
|
||||
class AssetType(str, Enum):
|
||||
STOCK = "stock"
|
||||
CRYPTO = "crypto"
|
||||
|
|
|
|||
33
cli/utils.py
33
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
|
||||
from tradingagents.llm_clients.model_catalog import get_model_options
|
||||
|
||||
console = Console()
|
||||
|
|
@ -17,6 +17,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."""
|
||||
|
|
@ -43,6 +45,25 @@ def normalize_ticker_symbol(ticker: str) -> 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
|
||||
|
|
@ -76,12 +97,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.",
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
|
||||
|
|
@ -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()
|
||||
|
|
@ -12,7 +12,10 @@ def create_market_analyst(llm):
|
|||
|
||||
def market_analyst_node(state):
|
||||
current_date = state["trade_date"]
|
||||
instrument_context = build_instrument_context(state["company_of_interest"])
|
||||
asset_type = state.get("asset_type", "stock")
|
||||
instrument_context = build_instrument_context(
|
||||
state["company_of_interest"], asset_type
|
||||
)
|
||||
|
||||
tools = [
|
||||
get_stock_data,
|
||||
|
|
|
|||
|
|
@ -11,7 +11,11 @@ from tradingagents.dataflows.config import get_config
|
|||
def create_news_analyst(llm):
|
||||
def news_analyst_node(state):
|
||||
current_date = state["trade_date"]
|
||||
instrument_context = build_instrument_context(state["company_of_interest"])
|
||||
asset_type = state.get("asset_type", "stock")
|
||||
asset_label = "company" if asset_type == "stock" else "asset"
|
||||
instrument_context = build_instrument_context(
|
||||
state["company_of_interest"], asset_type
|
||||
)
|
||||
|
||||
tools = [
|
||||
get_news,
|
||||
|
|
@ -19,7 +23,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. Provide specific, actionable insights with supporting evidence to help traders make informed 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. Provide specific, actionable insights with supporting evidence to help traders make informed 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."""
|
||||
+ get_language_instruction()
|
||||
)
|
||||
|
|
|
|||
|
|
@ -6,14 +6,18 @@ from tradingagents.dataflows.config import get_config
|
|||
def create_social_media_analyst(llm):
|
||||
def social_media_analyst_node(state):
|
||||
current_date = state["trade_date"]
|
||||
instrument_context = build_instrument_context(state["company_of_interest"])
|
||||
asset_type = state.get("asset_type", "stock")
|
||||
subject_label = "company" if asset_type == "stock" else "asset"
|
||||
instrument_context = build_instrument_context(
|
||||
state["company_of_interest"], asset_type
|
||||
)
|
||||
|
||||
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. Provide specific, actionable insights with supporting evidence to help traders make informed 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. Provide specific, actionable insights with supporting evidence to help traders make informed 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."""
|
||||
+ get_language_instruction()
|
||||
)
|
||||
|
|
|
|||
|
|
@ -11,6 +11,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)
|
||||
|
|
@ -19,7 +26,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:
|
||||
|
||||
|
|
@ -34,11 +41,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)
|
||||
|
|
|
|||
|
|
@ -11,6 +11,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)
|
||||
|
|
@ -19,7 +26,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.
|
||||
|
|
@ -32,7 +39,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}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,8 @@ from tradingagents.agents.utils.agent_utils import build_instrument_context
|
|||
def create_trader(llm, memory):
|
||||
def trader_node(state, name):
|
||||
company_name = state["company_of_interest"]
|
||||
instrument_context = build_instrument_context(company_name)
|
||||
asset_type = state.get("asset_type", "stock")
|
||||
instrument_context = build_instrument_context(company_name, asset_type)
|
||||
investment_plan = state["investment_plan"]
|
||||
market_research_report = state["market_report"]
|
||||
sentiment_report = state["sentiment_report"]
|
||||
|
|
|
|||
|
|
@ -45,6 +45,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"]
|
||||
|
|
|
|||
|
|
@ -34,12 +34,19 @@ def get_language_instruction() -> str:
|
|||
return f" Write your entire response in {lang}."
|
||||
|
||||
|
||||
def build_instrument_context(ticker: str) -> str:
|
||||
def build_instrument_context(ticker: str, asset_type: str = "stock") -> str:
|
||||
"""Describe the exact instrument so agents preserve exchange-qualified tickers."""
|
||||
instrument_label = "asset" if asset_type == "crypto" else "instrument"
|
||||
extra_hint = (
|
||||
" Treat it as a crypto asset rather than a company, and do not assume company fundamentals are available."
|
||||
if asset_type == "crypto"
|
||||
else ""
|
||||
)
|
||||
return (
|
||||
f"The instrument to analyze is `{ticker}`. "
|
||||
f"The {instrument_label} to analyze is `{ticker}`. "
|
||||
"Use this exact ticker in every tool call, report, and recommendation, "
|
||||
"preserving any exchange suffix (e.g. `.TO`, `.L`, `.HK`, `.T`)."
|
||||
"preserving any exchange suffix (e.g. `.TO`, `.L`, `.HK`, `.T`, `-USD`)."
|
||||
+ extra_hint
|
||||
)
|
||||
|
||||
def create_msg_delete():
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue