mirror of
https://github.com/TauricResearch/TradingAgents.git
synced 2026-06-16 21:06:15 +03:00
Merge #567 — analysis-only crypto asset mode
feat: add analysis-only crypto asset mode
This commit is contained in:
12
cli/main.py
12
cli/main.py
@@ -510,6 +510,10 @@ def get_user_selections():
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
selected_ticker = get_ticker()
|
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
|
# Step 2: Analysis date
|
||||||
default_date = datetime.datetime.now().strftime("%Y-%m-%d")
|
default_date = datetime.datetime.now().strftime("%Y-%m-%d")
|
||||||
@@ -537,7 +541,7 @@ def get_user_selections():
|
|||||||
"Step 4: Analysts Team", "Select your LLM analyst agents for the analysis"
|
"Step 4: Analysts Team", "Select your LLM analyst agents for the analysis"
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
selected_analysts = select_analysts()
|
selected_analysts = select_analysts(asset_type)
|
||||||
console.print(
|
console.print(
|
||||||
f"[green]Selected analysts:[/green] {', '.join(analyst.value for analyst in selected_analysts)}"
|
f"[green]Selected analysts:[/green] {', '.join(analyst.value for analyst in selected_analysts)}"
|
||||||
)
|
)
|
||||||
@@ -620,6 +624,7 @@ def get_user_selections():
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
"ticker": selected_ticker,
|
"ticker": selected_ticker,
|
||||||
|
"asset_type": asset_type.value,
|
||||||
"analysis_date": analysis_date,
|
"analysis_date": analysis_date,
|
||||||
"analysts": selected_analysts,
|
"analysts": selected_analysts,
|
||||||
"research_depth": selected_research_depth,
|
"research_depth": selected_research_depth,
|
||||||
@@ -1071,6 +1076,7 @@ def run_analysis(checkpoint: bool = False):
|
|||||||
|
|
||||||
# Add initial messages
|
# Add initial messages
|
||||||
message_buffer.add_message("System", f"Selected ticker: {selections['ticker']}")
|
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(
|
message_buffer.add_message(
|
||||||
"System", f"Analysis date: {selections['analysis_date']}"
|
"System", f"Analysis date: {selections['analysis_date']}"
|
||||||
)
|
)
|
||||||
@@ -1094,7 +1100,9 @@ def run_analysis(checkpoint: bool = False):
|
|||||||
|
|
||||||
# Initialize state and get graph args with callbacks
|
# Initialize state and get graph args with callbacks
|
||||||
init_agent_state = graph.propagator.create_initial_state(
|
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
|
# Pass callbacks to graph config for tool execution tracking
|
||||||
# (LLM tracking is handled separately via LLM constructor)
|
# (LLM tracking is handled separately via LLM constructor)
|
||||||
|
|||||||
@@ -10,3 +10,8 @@ class AnalystType(str, Enum):
|
|||||||
SOCIAL = "social"
|
SOCIAL = "social"
|
||||||
NEWS = "news"
|
NEWS = "news"
|
||||||
FUNDAMENTALS = "fundamentals"
|
FUNDAMENTALS = "fundamentals"
|
||||||
|
|
||||||
|
|
||||||
|
class AssetType(str, Enum):
|
||||||
|
STOCK = "stock"
|
||||||
|
CRYPTO = "crypto"
|
||||||
|
|||||||
33
cli/utils.py
33
cli/utils.py
@@ -6,7 +6,7 @@ import questionary
|
|||||||
from dotenv import find_dotenv, set_key
|
from dotenv import find_dotenv, set_key
|
||||||
from rich.console import Console
|
from rich.console import Console
|
||||||
|
|
||||||
from cli.models import AnalystType
|
from cli.models import AnalystType, AssetType
|
||||||
from tradingagents.llm_clients.api_key_env import get_api_key_env
|
from tradingagents.llm_clients.api_key_env import get_api_key_env
|
||||||
from tradingagents.llm_clients.model_catalog import get_model_options
|
from tradingagents.llm_clients.model_catalog import get_model_options
|
||||||
|
|
||||||
@@ -21,6 +21,8 @@ ANALYST_ORDER = [
|
|||||||
("Fundamentals Analyst", AnalystType.FUNDAMENTALS),
|
("Fundamentals Analyst", AnalystType.FUNDAMENTALS),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
CRYPTO_SUFFIXES = ("-USD", "-USDT", "-USDC", "-BTC", "-ETH")
|
||||||
|
|
||||||
|
|
||||||
def get_ticker() -> str:
|
def get_ticker() -> str:
|
||||||
"""Prompt the user to enter a ticker symbol."""
|
"""Prompt the user to enter a ticker symbol."""
|
||||||
@@ -47,6 +49,25 @@ def normalize_ticker_symbol(ticker: str) -> str:
|
|||||||
return ticker.strip().upper()
|
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:
|
def get_analysis_date() -> str:
|
||||||
"""Prompt the user to enter a date in YYYY-MM-DD format."""
|
"""Prompt the user to enter a date in YYYY-MM-DD format."""
|
||||||
import re
|
import re
|
||||||
@@ -80,12 +101,18 @@ def get_analysis_date() -> str:
|
|||||||
return date.strip()
|
return date.strip()
|
||||||
|
|
||||||
|
|
||||||
def select_analysts() -> List[AnalystType]:
|
def select_analysts(asset_type: AssetType = AssetType.STOCK) -> List[AnalystType]:
|
||||||
"""Select analysts using an interactive checkbox."""
|
"""Select analysts using an interactive checkbox."""
|
||||||
|
available_analysts = filter_analysts_for_asset_type(
|
||||||
|
[value for _, value in ANALYST_ORDER],
|
||||||
|
asset_type,
|
||||||
|
)
|
||||||
choices = questionary.checkbox(
|
choices = questionary.checkbox(
|
||||||
"Select Your [Analysts Team]:",
|
"Select Your [Analysts Team]:",
|
||||||
choices=[
|
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",
|
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.",
|
validate=lambda x: len(x) > 0 or "You must select at least one analyst.",
|
||||||
|
|||||||
1
tests/__init__.py
Normal file
1
tests/__init__.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
|
||||||
56
tests/test_crypto_asset_mode.py
Normal file
56
tests/test_crypto_asset_mode.py
Normal file
@@ -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):
|
def market_analyst_node(state):
|
||||||
current_date = state["trade_date"]
|
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 = [
|
tools = [
|
||||||
get_stock_data,
|
get_stock_data,
|
||||||
|
|||||||
@@ -11,7 +11,11 @@ from tradingagents.dataflows.config import get_config
|
|||||||
def create_news_analyst(llm):
|
def create_news_analyst(llm):
|
||||||
def news_analyst_node(state):
|
def news_analyst_node(state):
|
||||||
current_date = state["trade_date"]
|
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 = [
|
tools = [
|
||||||
get_news,
|
get_news,
|
||||||
@@ -19,7 +23,7 @@ def create_news_analyst(llm):
|
|||||||
]
|
]
|
||||||
|
|
||||||
system_message = (
|
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."""
|
+ """ 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()
|
+ get_language_instruction()
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -12,8 +12,15 @@ def create_bear_researcher(llm):
|
|||||||
sentiment_report = state["sentiment_report"]
|
sentiment_report = state["sentiment_report"]
|
||||||
news_report = state["news_report"]
|
news_report = state["news_report"]
|
||||||
fundamentals_report = state["fundamentals_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)"
|
||||||
|
)
|
||||||
|
|
||||||
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:
|
Key points to focus on:
|
||||||
|
|
||||||
@@ -28,10 +35,10 @@ Resources available:
|
|||||||
Market research report: {market_research_report}
|
Market research report: {market_research_report}
|
||||||
Social media sentiment report: {sentiment_report}
|
Social media sentiment report: {sentiment_report}
|
||||||
Latest world affairs news: {news_report}
|
Latest world affairs news: {news_report}
|
||||||
Company fundamentals report: {fundamentals_report}
|
{fundamentals_label}: {fundamentals_report}
|
||||||
Conversation history of the debate: {history}
|
Conversation history of the debate: {history}
|
||||||
Last bull argument: {current_response}
|
Last bull argument: {current_response}
|
||||||
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.
|
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}.
|
||||||
""" + get_language_instruction()
|
""" + get_language_instruction()
|
||||||
|
|
||||||
response = llm.invoke(prompt)
|
response = llm.invoke(prompt)
|
||||||
|
|||||||
@@ -12,8 +12,15 @@ def create_bull_researcher(llm):
|
|||||||
sentiment_report = state["sentiment_report"]
|
sentiment_report = state["sentiment_report"]
|
||||||
news_report = state["news_report"]
|
news_report = state["news_report"]
|
||||||
fundamentals_report = state["fundamentals_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)"
|
||||||
|
)
|
||||||
|
|
||||||
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:
|
Key points to focus on:
|
||||||
- Growth Potential: Highlight the company's market opportunities, revenue projections, and scalability.
|
- Growth Potential: Highlight the company's market opportunities, revenue projections, and scalability.
|
||||||
@@ -26,7 +33,7 @@ Resources available:
|
|||||||
Market research report: {market_research_report}
|
Market research report: {market_research_report}
|
||||||
Social media sentiment report: {sentiment_report}
|
Social media sentiment report: {sentiment_report}
|
||||||
Latest world affairs news: {news_report}
|
Latest world affairs news: {news_report}
|
||||||
Company fundamentals report: {fundamentals_report}
|
{fundamentals_label}: {fundamentals_report}
|
||||||
Conversation history of the debate: {history}
|
Conversation history of the debate: {history}
|
||||||
Last bear argument: {current_response}
|
Last bear argument: {current_response}
|
||||||
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.
|
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.
|
||||||
|
|||||||
@@ -22,7 +22,8 @@ def create_trader(llm):
|
|||||||
|
|
||||||
def trader_node(state, name):
|
def trader_node(state, name):
|
||||||
company_name = state["company_of_interest"]
|
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"]
|
investment_plan = state["investment_plan"]
|
||||||
|
|
||||||
messages = [
|
messages = [
|
||||||
|
|||||||
@@ -45,6 +45,7 @@ class RiskDebateState(TypedDict):
|
|||||||
|
|
||||||
class AgentState(MessagesState):
|
class AgentState(MessagesState):
|
||||||
company_of_interest: Annotated[str, "Company that we are interested in trading"]
|
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"]
|
trade_date: Annotated[str, "What date we are trading at"]
|
||||||
|
|
||||||
sender: Annotated[str, "Agent that sent this message"]
|
sender: Annotated[str, "Agent that sent this message"]
|
||||||
|
|||||||
@@ -36,12 +36,19 @@ def get_language_instruction() -> str:
|
|||||||
return f" Write your entire response in {lang}."
|
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."""
|
"""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 (
|
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, "
|
"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():
|
def create_msg_delete():
|
||||||
|
|||||||
@@ -16,12 +16,17 @@ class Propagator:
|
|||||||
self.max_recur_limit = max_recur_limit
|
self.max_recur_limit = max_recur_limit
|
||||||
|
|
||||||
def create_initial_state(
|
def create_initial_state(
|
||||||
self, company_name: str, trade_date: str, past_context: str = ""
|
self,
|
||||||
|
company_name: str,
|
||||||
|
trade_date: str,
|
||||||
|
asset_type: str = "stock",
|
||||||
|
past_context: str = "",
|
||||||
) -> Dict[str, Any]:
|
) -> Dict[str, Any]:
|
||||||
"""Create the initial state for the agent graph."""
|
"""Create the initial state for the agent graph."""
|
||||||
return {
|
return {
|
||||||
"messages": [("human", company_name)],
|
"messages": [("human", company_name)],
|
||||||
"company_of_interest": company_name,
|
"company_of_interest": company_name,
|
||||||
|
"asset_type": asset_type,
|
||||||
"trade_date": str(trade_date),
|
"trade_date": str(trade_date),
|
||||||
"past_context": past_context,
|
"past_context": past_context,
|
||||||
"investment_debate_state": InvestDebateState(
|
"investment_debate_state": InvestDebateState(
|
||||||
|
|||||||
Reference in New Issue
Block a user