Implementation:51bitquant Ai hedge fund crypto PortfolioManagementNode call
| 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
- Repository: ai-hedge-fund-crypto
- File: src/graph/portfolio_management_node.py
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
- Principle:51bitquant_Ai_hedge_fund_crypto_LLM_Driven_Decision_Making
- Principle:51bitquant_Ai_hedge_fund_crypto_DAG_Pipeline_Orchestration