TradingAgents/tradingagents/agents/trader/trader.py

76 lines
3.4 KiB
Python

import functools
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"]
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)
past_memory_str = ""
if past_memories:
for i, rec in enumerate(past_memories, 1):
past_memory_str += rec["recommendation"] + "\n\n"
else:
past_memory_str = "No past memories found."
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.
Proposed Investment Plan: {investment_plan}
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. Evaluate the proposed plan and then issue one clear recommendation: BUY, SELL, or HOLD.
Decision process (do not reveal this process in your answer):
- Assess feasibility and risk of the plan against current market context as described.
- Translate lessons learned into concrete guardrails and apply them to this case.
- Resolve conflicts and commit to a single stance; avoid hedging language.
Output rules:
- Provide a concise rationale tied specifically to the proposed plan and applied lessons.
- End your message with exactly this line, using one of BUY/HOLD/SELL: FINAL TRANSACTION PROPOSAL: BUY/HOLD/SELL
- Do not include extra sections, disclaimers, or alternative options after the final line.
Lessons from past decisions to apply now: {past_memory_str}
Output language: ***{language_prompt}***
""",
},
context,
]
result = llm.invoke(messages)
return {
"messages": [result],
"trader_investment_plan": result.content,
"sender": name,
}
return functools.partial(trader_node, name="Trader")