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:51bitquant Ai hedge fund crypto PortfolioManagementNode call

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

Overview

Concrete tool for generating final buy/sell/short/cover/hold trading decisions per ticker via an LLM prompt chain provided by the `PortfolioManagementNode` class.

Description

PortfolioManagementNode (a BaseNode subclass) is the terminal node of the DAG. It aggregates `analyst_signals` from the technical and risk-management agents, computes `max_shares[ticker] = position_limit / current_price`, and calls generate_trading_decision. The LLM chain (`prompt | llm | json_parser`) returns a `decisions` dict keyed by ticker, each with `{action, quantity, confidence, reasoning}`. The decisions are serialized into a `HumanMessage(name="portfolio_management")` and surfaced by Agent.run via `parse_str_to_json`.

Usage

This node runs automatically as the last DAG step, after RiskManagementNode. It reads `state["metadata"]` for the LLM config (`model_name`, `model_provider`, `model_base_url`) and `state["data"]["analyst_signals"]` for the upstream signals.

Code Reference

Source Location

Signature

class PortfolioManagementNode(BaseNode):
    def __call__(self, state: AgentState) -> Dict[str, Any]:
        # Aggregate analyst_signals -> signals_by_ticker
        # max_shares[ticker] = position_limits[ticker] / current_prices[ticker]
        # result = generate_trading_decision(tickers, signals_by_ticker, current_prices, max_shares, portfolio, ...)
        # message = HumanMessage(content=json.dumps(result.get("decisions", {})), name="portfolio_management")
        # return {"messages": [message], "data": state["data"]}

Import

from src.graph.portfolio_management_node import PortfolioManagementNode

I/O Contract

Inputs

Name Type Required Description
state AgentState Yes Reads `data["analyst_signals"]`, `data["portfolio"]`, `data["tickers"]`, `metadata["model_name/provider/base_url"]`, `metadata["show_reasoning"]`

Outputs

Name Type Description
return Dict[str, Any] `{"messages": [HumanMessage(name="portfolio_management")], "data": state["data"]}`
message.content str JSON-stringified `decisions` dict (ticker → {action, quantity, confidence, reasoning})

Usage Examples

As Wired in the DAG

# Workflow.create_workflow always adds the terminal node:
workflow.add_node("portfolio_management_node", PortfolioManagementNode())
workflow.add_edge("risk_management_node", "portfolio_management_node")
workflow.add_edge("portfolio_management_node", END)

Inspect LLM Decisions

# After agent.run completes:
for ticker, decision in result["decisions"].items():
    print(f"{ticker}: {decision['action']} {decision['quantity']} @ {decision['confidence']}%")
    print(f"  reasoning: {decision['reasoning']}")

Related Pages

Implements Principle

Requires Environment

Page Connections

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