Implementation:51bitquant Ai hedge fund crypto Agent run
| Knowledge Sources | |
|---|---|
| Domains | Workflow_Orchestration, Trading_Agent |
| Last Updated | 2026-10-28 14:00 GMT |
Overview
Concrete tool for invoking the compiled LangGraph trading pipeline with portfolio state and LLM metadata, returning parsed trading decisions provided by the `Agent` class.
Description
The Agent.run method invokes the compiled `StateGraph` (`self.agent`) with an initial `HumanMessage`, populating the graph's `data` channel with `primary_interval`, `intervals`, `tickers`, `portfolio`, `end_date`, and an empty `analyst_signals` dict. The `metadata` channel carries `show_reasoning` and the LLM model config (`model_name`, `model_provider`, `model_base_url`). After the DAG executes (DataNode → strategy nodes → RiskManagementNode → PortfolioManagementNode), the final `HumanMessage` content is parsed via `parse_str_to_json` into the `decisions` dict, and `analyst_signals` is surfaced from state.
Usage
Call this method once per backtest bar (inside `Backtester.run_backtest`) or once for a live trading run. Pass the current bar's close time as `end_date`, the live portfolio dict by reference, and the LLM settings from `Settings.model`.
Code Reference
Source Location
- Repository: ai-hedge-fund-crypto
- File: src/agent/agent.py
Signature
class Agent:
def __init__(self, intervals: List[Interval], strategies: List[str], show_agent_graph: bool = True):
workflow = Workflow.create_workflow(intervals=intervals, strategies=strategies)
self.intervals = intervals
self.strategies = strategies
self.agent = workflow.compile()
if show_agent_graph:
save_graph_as_png(self.agent, ...)
def run(
self,
primary_interval: Interval,
tickers: List[str],
end_date: datetime,
portfolio: Dict,
show_reasoning: bool = False,
model_name: str = "gpt-4o",
model_provider: str = "openai",
model_base_url: Optional[str] = None,
) -> Dict:
Import
from src.agent.agent import Agent
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| primary_interval | Interval | Yes | Decision timeframe |
| tickers | List[str] | Yes | Symbols to trade |
| end_date | datetime | Yes | "Current" time for data fetching (bar close_time in backtest) |
| portfolio | Dict | Yes | Portfolio state (cash, positions, realized_gains); mutated by reference in backtest |
| show_reasoning | bool | No | Toggle reasoning display (default `False`) |
| model_name | str | No | LLM model name (default `"gpt-4o"`) |
| model_provider | str | No | LLM provider (default `"openai"`) |
| model_base_url | Optional[str] | No | LLM base URL override |
Outputs
| Name | Type | Description |
|---|---|---|
| return | Dict | `{"decisions": Dict[str, {action, quantity, confidence, reasoning}], "analyst_signals": Dict}` |
| decisions | Dict | Parsed from last message content via `parse_str_to_json` |
| analyst_signals | Dict | From `state["data"]["analyst_signals"]` (technical_analyst_agent, risk_management_agent) |
Usage Examples
Live Trading Run
from datetime import datetime
from src.agent.agent import Agent
from src.utils.settings import settings
# 1. Build the agent (compiles the DAG)
agent = Agent(
intervals=settings.signals.intervals,
strategies=settings.signals.strategies,
show_agent_graph=settings.show_agent_graph,
)
# 2. Run the pipeline once with current portfolio state
result = agent.run(
primary_interval=settings.primary_interval,
tickers=settings.signals.tickers,
end_date=datetime.now(),
portfolio=portfolio,
show_reasoning=settings.show_reasoning,
model_name=settings.model.name,
model_provider=settings.model.provider,
model_base_url=settings.model.base_url,
)
# 3. Inspect decisions
for ticker, decision in result["decisions"].items():
print(ticker, decision["action"], decision["quantity"], decision["confidence"])
Per-Bar Backtest Invocation
# Inside Backtester.run_backtest loop
output = agent.run(
primary_interval=self.primary_interval,
tickers=self.tickers,
end_date=current_time, # bar close_time
portfolio=self.portfolio, # mutated in place
model_name=self.model_name,
model_provider=self.model_provider,
model_base_url=self.model_base_url,
show_reasoning=self.show_reasoning,
)
decisions = output.get("decisions")
Related Pages
Implements Principle
- Principle:51bitquant_Ai_hedge_fund_crypto_DAG_Pipeline_Orchestration
- Principle:51bitquant_Ai_hedge_fund_crypto_LLM_Driven_Decision_Making