Implementation:51bitquant Ai hedge fund crypto generate trading decision
| Knowledge Sources | |
|---|---|
| Domains | LLM_Integration, Decision_Making |
| Last Updated | 2026-10-28 14:00 GMT |
Overview
Concrete tool for constructing the LLM prompt and invoking the prompt-model-parser chain to produce trading decisions provided by the `generate_trading_decision` function.
Description
generate_trading_decision builds a `ChatPromptTemplate` with a system message (long/short trading rules, available actions, input descriptions) and a human message template listing `signals_by_ticker`, `current_prices`, `max_shares`, `portfolio_cash`, `portfolio_positions`, `margin_requirement`, and `total_margin_used`. It obtains the LLM via get_llm, composes the chain `prompt | llm | json_parser`, and invokes it with JSON-stringified inputs. The parsed result is a dict with a `decisions` key.
Usage
Called by PortfolioManagementNode.__call__ as the final step of the DAG. You generally do not call it directly, but it can be invoked standalone to test prompt construction and LLM responses.
Code Reference
Source Location
- Repository: ai-hedge-fund-crypto
- File: src/graph/portfolio_management_node.py
Signature
def generate_trading_decision(
tickers: List[str],
signals_by_ticker: Dict[str, Dict[str, Any]],
current_prices: Dict[str, float],
max_shares: Dict[str, float],
portfolio: Dict[str, float],
model_name: str,
model_provider: str,
model_base_url: Optional[str] = None,
) -> Dict:
Import
from src.graph.portfolio_management_node import generate_trading_decision
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| tickers | List[str] | Yes | Symbols to decide on |
| signals_by_ticker | Dict | Yes | Per-ticker aggregated signals from analyst agents |
| current_prices | Dict[str, float] | Yes | Latest close price per ticker |
| max_shares | Dict[str, float] | Yes | Max purchasable shares per ticker (position limit / price) |
| portfolio | Dict | Yes | Portfolio state with `cash`, `positions`, `margin_requirement`, `margin_used` |
| model_name | str | Yes | LLM model name |
| model_provider | str | Yes | LLM provider (openai/groq/openrouter/gemini/anthropic/ollama) |
| model_base_url | Optional[str] | No | LLM base URL override |
Outputs
| Name | Type | Description |
|---|---|---|
| return | Dict | `{"decisions": {ticker: {action, quantity, confidence, reasoning}}}` |
Usage Examples
Standalone Invocation
from src.graph.portfolio_management_node import generate_trading_decision
result = generate_trading_decision(
tickers=["BTCUSDT", "ETHUSDT"],
signals_by_ticker={
"BTCUSDT": {"technical_analyst_agent": {"signal": "bullish", "confidence": 75}},
"ETHUSDT": {"technical_analyst_agent": {"signal": "bearish", "confidence": 60}},
},
current_prices={"BTCUSDT": 60000.0, "ETHUSDT": 3000.0},
max_shares={"BTCUSDT": 0.33, "ETHUSDT": 6.6},
portfolio={"cash": 20000.0, "positions": {...}, "margin_requirement": 0.5, "margin_used": 0.0},
model_name="gpt-4o-mini",
model_provider="openai",
)
print(result["decisions"]["BTCUSDT"]["action"])