Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:24mlight A Share investment Agent portfolio management agent

From Leeroopedia
Knowledge Sources
Domains Finance, Portfolio_Management, Decision_Making
Last Updated 2026-10-10 14:00 GMT

Overview

Concrete tool for synthesizing multi-agent investment signals into a final LLM-driven portfolio trading decision (buy/sell/hold) with quantity and confidence, provided by the A_Share_investment_Agent repository.

Description

The portfolio_management_agent function is the terminal node of the LangGraph workflow. It deduplicates incoming messages by agent name (keeping latest), extracts signals from 7 agents (technical, fundamentals, sentiment, valuation, risk, macro_analyst, macro_news), normalizes macro news payload, and feeds all into an LLM with a system prompt defining signal weights (valuation 30%, fundamentals 25%, technicals 20%, macro 15%, sentiment 10%) and hard risk constraints.

The LLM returns a JSON decision with action, quantity, confidence, agent_signals, and reasoning. The module includes helper functions for message lookup, macro news normalization, JSON parsing with fallback, and Chinese-language report formatting.

Usage

Import when you need to:

  • Generate the final trading decision from multiple agent signals
  • Run as the terminal node in the LangGraph workflow

Decorated with @agent_endpoint("portfolio_management", "负责投资组合管理和最终交易决策").

Code Reference

Source Location

  • Repository: A_Share_investment_Agent
  • File: src/agents/portfolio_manager.py
  • Lines: L21-424 (helpers L21-87, agent L91-309, format_decision L312-424)

Signature

def get_latest_message_by_name(messages: list, name: str, *, log_missing: bool = True):
    """Get latest message by agent name."""

def _normalize_macro_news_payload(raw_value: Any) -> Dict[str, Any]:
    """Normalize macro news data with defaults."""

def _parse_decision_json(raw_text: str) -> Dict[str, Any]:
    """Parse LLM JSON response with fallback extraction."""

@agent_endpoint("portfolio_management", "负责投资组合管理和最终交易决策")
def portfolio_management_agent(state: AgentState) -> dict:
    """Responsible for portfolio management"""

def format_decision(action: str, quantity: int, confidence: float, agent_signals: list, reasoning: str, market_wide_news_summary: str = "未提供") -> dict:
    """Format trading decision into Chinese investment analysis report."""

Import

from src.agents.portfolio_manager import portfolio_management_agent, format_decision

I/O Contract

Inputs

Name Type Required Description
state AgentState Yes Reads messages from 7 agents + data["portfolio"] + data["macro_news_analysis_result"]

Outputs

Name Type Description
messages list HumanMessage with LLM-generated JSON {action, quantity, confidence, agent_signals, reasoning}
metadata["portfolio_management_agent_decision_details"] dict Parsed {action, quantity, confidence, reasoning_snippet}

Usage Examples

from src.agents.portfolio_manager import portfolio_management_agent

# Terminal node: runs after [macro_analyst_agent, macro_news_agent] fan-in
# 1. Deduplicates messages by agent name (keeps latest)
# 2. Normalizes macro_news_analysis_result from data
# 3. Formats system + user prompt with all 7 agent signals
# 4. Calls LLM via log_llm_interaction(state)(get_chat_completion)(messages)
# 5. Parses JSON response with _parse_decision_json (handles code fences, bracket extraction)
# 6. Fallback: conservative "hold" with 0.7 confidence if LLM fails

Related Pages

Implements Principle

Requires Environment

Uses Heuristics

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment