How to Build a Multi-Agent Trading Research System with LangChain Deep Agents: A Comprehensive Handbook

A trading research agent can write strategy code, run a backtest, inspect the results, and iteratively refine the strategy. The real challenge is preventing this loop from devolving into an uncontrolled search for an attractive backtest. In this handbook, we'll build a multi-agent trading research system using LangChain Deep Agents. EODHD supplies historical market data, while a deterministic Python layer governs data splits, backtesting logic, benchmarks, experiment history, and strategy selection rules. A coordinator, a strategy engineer, and a research critic operate within these boundaries to develop and evaluate three strategy versions. The objective is not to prove that AI agents can reliably discover profitable strategies, but to establish a research workflow where agents can generate and challenge ideas without being allowed to manipulate the evidence used to judge them.


Table of Contents

  • Prerequisites
  • Design the Research Workflow
  • Set Up the Python Research Environment
  • Prepare the EODHD Research Data
  • Build a Deterministic Strategy Evaluation Layer
  • Create the Shared Backtesting Engine
  • Verify the Portfolio Accounting
  • Establish Fixed Benchmarks
  • Implement the Multi-Agent System with LangChain Deep Agents
  • Run the Research Workflow
  • Evaluate the Results and Iterate
  • Conclusion

Prerequisites

Before diving in, ensure you have the following:

  • Python 3.10 or later installed on your machine.
  • An EODHD API key for accessing historical market data.
  • A LangChain API key to use Deep Agents.
  • Basic familiarity with Python, backtesting concepts, and large language model (LLM) integration.

Design the Research Workflow

A successful multi-agent system requires clear roles and boundaries. In our design, three agents collaborate under strict rules:

  • Coordinator: Oversees the workflow, delegates tasks, and ensures that all steps adhere to the deterministic framework.
  • Strategy Engineer: Writes and modifies strategy code, focusing on generating new ideas based on data and previous feedback.
  • Research Critic: Reviews the performance of each strategy, identifies flaws, and recommends improvements.

The deterministic Python layer acts as a guardrail, controlling data splits (e.g., training, validation, test periods), backtesting parameters, benchmarks, and experiment history. This ensures that agents cannot inadvertently overfit or manipulate results.


Set Up the Python Research Environment

We'll start by creating a virtual environment and installing the necessary packages:

python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
pip install langchain eodhd pandas numpy matplotlib

Next, set up your API keys as environment variables for security:

export EODHD_API_KEY='your_key_here'
export LANGCHAIN_API_KEY='your_key_here'

Prepare the EODHD Research Data

EODHD provides comprehensive historical market data. We'll fetch daily OHLCV (Open, High, Low, Close, Volume) data for a set of liquid stocks over a specified period (e.g., 2015–2025).

from eodhd import EODHD
import pandas as pd

api = EODHD(api_key='your_key_here')
data = api.get_historical_data('AAPL', interval='d', start='2015-01-01', end='2025-12-31')
df = pd.DataFrame(data)
# Ensure proper data types and sorting
df['date'] = pd.to_datetime(df['date'])
df.sort_values('date', inplace=True)

Store the data in a clean format, merging additional symbols as needed, and split it into training, validation, and test sets. In this handbook, we'll use a fixed split (e.g., 60% training, 20% validation, 20% test) to ensure reproducibility.


Build a Deterministic Strategy Evaluation Layer

This layer is the backbone of the system, ensuring that all evaluations are consistent and unbiased. We'll create three components:


1. Create the Shared Backtesting Engine

We'll implement a simple backtesting engine that processes signals and simulates portfolio equity. The engine should accept a dictionary of signals (e.g., 'buy', 'sell', 'hold') and return performance metrics like total return, Sharpe ratio, and maximum drawdown.

class BacktestEngine:
    def __init__(self, data, initial_capital=100000):
        self.data = data
        self.initial_capital = initial_capital

    def run(self, signals):
        # Logic to execute trades based on signals
        # Return a result object with metrics
        pass

This engine must be deterministic – we fix transaction costs, slippage, and position sizing to avoid any hidden variability.


2. Verify the Portfolio Accounting

To ensure accuracy, we'll unit-test the accounting logic. For a simple buy-and-hold strategy, the equity curve should match manual calculations. This step catches bugs early and establishes trust in the engine.


3. Establish Fixed Benchmarks

We need a benchmark to compare strategies against. A common choice is a passive index fund or a simple equal-weight portfolio. We'll compute the benchmark's performance over the same periods and store it for later comparison.


Implement the Multi-Agent System with LangChain Deep Agents

Now, we'll use LangChain Deep Agents to create the three agents. Deep Agents allow the agents to use tools and maintain a conversation with the deterministic layer.

from langchain.agents import create_deep_agent, AgentExecutor, Tool
from langchain.llms import OpenAI

# Define tools that agents can use
backtest_tool = Tool(name="RunBacktest", func=run_backtest, description="Run a backtest with given signals")
load_data_tool = Tool(name="LoadData", func=load_data, description="Load historical data for a symbol")

# Initialize agents
llm = OpenAI(model="gpt-4")

coordinator = create_deep_agent(llm, tools=[backtest_tool, load_data_tool], system_message="You are a coordinator ...")
strategy_engineer = create_deep_agent(llm, tools=[backtest_tool], system_message="You are a strategy engineer ...")
critic = create_deep_agent(llm, tools=[backtest_tool], system_message="You are a research critic ...")

Each agent gets a clear system message describing its role, constraints, and expected output format. For example, the strategy engineer must produce Python code for signals, which the coordinator then validates before passing to the backtest tool.


Run the Research Workflow

We'll orchestrate the workflow programmatically. The coordinator starts by asking the strategy engineer to generate a baseline strategy. The signals are backtested, and the critic reviews the results. Based on feedback, the coordinator asks the engineer to refine the strategy. This loop continues for a fixed number of iterations or until the critic approves.

def run_research_loop():
    for iteration in range(3):
        # Step 1: Engineer proposes a strategy
        proposal = strategy_engineer.run("Generate a new strategy for AAPL")
        # Step 2: Coordinator validates and runs backtest
        result = coordinator.run(f"Run backtest for this strategy: {proposal}")
        # Step 3: Critic assesses results
        feedback = critic.run(f"Review these results: {result}")
        # Feedback is passed back to the engineer

All experiment data is logged to a structured file (e.g., CSV or JSON) for later analysis.


Evaluate the Results and Iterate

After the workflow completes, we analyze the performance of all strategy versions against the benchmark. Key metrics include:

  • Total return
  • Sharpe ratio
  • Maximum drawdown
  • Win rate

We also check for overfitting by comparing validation and test results. If a strategy performs significantly better in training than in test, it's likely overfitted and may be rejected.


Finally, we document the findings and iterate on the workflow design if needed. Perhaps we need stricter rules or additional tools.


Conclusion

In this handbook, we've built a multi-agent trading research system using LangChain Deep Agents. By confining agents within a deterministic evaluation layer, we ensure that their creativity is harnessed without compromising the integrity of the research. The result is a flexible, transparent, and reproducible research process.


As AI continues to evolve, such systems will become more sophisticated, but the core principle remains: agents should propose, but never dictate the evidence. This approach not only improves strategy development but also builds trust in AI-assisted research.


For more advanced applications, consider adding reinforcement learning, real-time data streaming, or risk management modules. The possibilities are vast, but the foundation we've built here is solid and scalable.

via FreeCodeCamp

Related