mirror of
https://github.com/farcasclaudiu/TradingAgents.git
synced 2026-06-28 21:01:16 +03:00
bba147798f
Extends the canonical structured-output pattern from the Portfolio Manager to the other two decision-making agents. Each of the three agents now returns a typed Pydantic instance via llm.with_structured_output() in a single primary call, and a render helper turns the result into the same markdown shape downstream agents and saved reports already consume. - ResearchPlan: 5-tier recommendation, conversational rationale, concrete strategic actions for the trader. - TraderProposal: 3-tier action (transaction direction is naturally Buy / Hold / Sell — position sizing happens later at the Portfolio Manager), reasoning, and optional entry_price / stop_loss / position_sizing. Rendered output preserves the trailing "FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL**" line for backward compatibility with the analyst stop-signal text. - PortfolioDecision: 5-tier rating, executive summary, investment thesis, optional price_target / time_horizon (unchanged). The shared try-structured-then-fallback pattern is extracted into tradingagents/agents/utils/structured.py (bind_structured + invoke_structured_or_freetext) so all three agents go through the same code path and log the same warning when a provider lacks structured output and the agent falls back to free-text generation. Net effect for users: every saved markdown report (research/manager.md, trading/trader.md, portfolio/decision.md) now has consistent section headers across runs and providers, easier to scan. Net effect for the runtime: the rating extraction round-trip is gone — the rating comes from the structured response itself, not a second LLM call. SignalProcessor was already simplified to a heuristic adapter in the previous commit. 11 new tests in tests/test_structured_agents.py cover the Trader and Research Manager render functions, structured-output happy paths, and free-text fallback. Full suite: 88 tests pass in ~2s without API keys.
62 lines
2.3 KiB
Python
62 lines
2.3 KiB
Python
"""Trader: turns the Research Manager's investment plan into a concrete transaction proposal."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import functools
|
|
|
|
from langchain_core.messages import AIMessage
|
|
|
|
from tradingagents.agents.schemas import TraderProposal, render_trader_proposal
|
|
from tradingagents.agents.utils.agent_utils import build_instrument_context
|
|
from tradingagents.agents.utils.structured import (
|
|
bind_structured,
|
|
invoke_structured_or_freetext,
|
|
)
|
|
|
|
|
|
def create_trader(llm):
|
|
structured_llm = bind_structured(llm, TraderProposal, "Trader")
|
|
|
|
def trader_node(state, name):
|
|
company_name = state["company_of_interest"]
|
|
instrument_context = build_instrument_context(company_name)
|
|
investment_plan = state["investment_plan"]
|
|
|
|
messages = [
|
|
{
|
|
"role": "system",
|
|
"content": (
|
|
"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. "
|
|
"Anchor your reasoning in the analysts' reports and the research plan."
|
|
),
|
|
},
|
|
{
|
|
"role": "user",
|
|
"content": (
|
|
f"Based on a comprehensive analysis by a team of analysts, here is an investment "
|
|
f"plan tailored for {company_name}. {instrument_context} This plan incorporates "
|
|
f"insights from current technical market trends, macroeconomic indicators, and "
|
|
f"social media sentiment. Use this plan as a foundation for evaluating your next "
|
|
f"trading decision.\n\nProposed Investment Plan: {investment_plan}\n\n"
|
|
f"Leverage these insights to make an informed and strategic decision."
|
|
),
|
|
},
|
|
]
|
|
|
|
trader_plan = invoke_structured_or_freetext(
|
|
structured_llm,
|
|
llm,
|
|
messages,
|
|
render_trader_proposal,
|
|
"Trader",
|
|
)
|
|
|
|
return {
|
|
"messages": [AIMessage(content=trader_plan)],
|
|
"trader_investment_plan": trader_plan,
|
|
"sender": name,
|
|
}
|
|
|
|
return functools.partial(trader_node, name="Trader")
|