Building an AI Agent Platform for Stock Research: A Technical Practice

    This article walks through the engineering practice of building an AI agent platform for stock research, organized into four layers: data, computation, inference, and execution with risk control. It compares three deployment options (managed cloud, local containers, and self-hosted cloud hosts), explains how to unify data conventions before adding intelligence, shows how to split a complex task into a parallel workflow, and covers the inference layer with a vector knowledge base. It also details the risk-control and backtesting loop, plus deployment and operations, while noting that agent output is research assistance rather than investment advice.

    1. Why Rebuild a “Stock Trading Agent” with Engineering

    Many developers get stuck at the first attempt: the idea is ambitious, but there is no actionable layering. Treating stock research as an engineering problem, it can be split into four layers: data, computation, inference, and execution with risk control. The value of an AI agent is not to replace judgment, but to parallelize actions that originally required multiple tools and serial steps—fetching quotes, cleaning and adjusting prices, computing factors, generating summaries, and outputting structured conclusions can all be scheduled by an orchestration engine.

    It should be clear that the agent outputs research assistance material and does not constitute investment advice; any strategy must undergo out-of-sample backtesting and strict risk control before going live.

    2. Trade-offs Among Three Deployment Options

    1. Managed cloud: Register and start using it, with no operations overhead, but there are limits on the number of applications, team collaboration, and call quotas. It suits individuals validating ideas.
    2. Local containerized deployment: Use a container runtime on a personal computer to bring up the full set of services, with no quota constraints. The downside is that the workflow stops when the machine shuts down or containers stop, and it is affected by local compute and network stability.
    3. Self-hosted cloud host: Run with container orchestration on a cloud host, balancing availability and control. It suits scenarios that need to stay online long-term and run on schedule. After installation, the interface and parameter configuration logic are basically the same across the three options, so migration cost is low.

    If self-hosting, it is recommended to split services into stateless and stateful parts: the stateless orchestration and interface layer can scale horizontally; the stateful database, vector store, and object storage mount persistent volumes. Baidu AI Cloud can provide general capabilities such as a container platform, object storage, log service, and monitoring and alerting as the foundation.

    3. Data Layer: Solve “Data Trust” Before Intelligence

    A common reason stock research fails is inconsistent data conventions. It is recommended to unify four things: the trading calendar, adjustment method, financial report disclosure dates (to avoid look-ahead bias), and handling of suspensions and delistings. When storing data, keep two copies—the raw snapshot and the processed result—for traceability.

    1. # Example: unified data fetching and storage interface (pseudocode)
    2. def fetch_daily(symbols, start, end, adjust="qfq"):
    3. raw = market_api.batch_daily(symbols, start, end)
    4. df = normalize_calendar(raw) # align to trading calendar
    5. df = apply_adjust(df, mode=adjust) # unify price adjustment
    6. df = drop_suspended(df) # handle suspensions
    7. store.save_snapshot(df, layer="raw") # raw layer
    8. store.save(df, layer="clean") # cleaned layer
    9. return df

    4. Orchestration Layer: Split a Complex Task into a Parallel Workflow

    Take the “post-market daily review” as an example. It can be split into: quote fetching → factor computation → news and announcement summarization → structured writing → validation and archiving. The first four steps can be triggered in parallel when there are no dependencies, and finally converge at the validation node.

    1. workflow:
    2. - id: fetch
    3. parallel: [quotes, news, filings]
    4. - id: factor
    5. depends_on: quotes
    6. - id: summarize
    7. depends_on: [news, filings]
    8. - id: report
    9. depends_on: [factor, summarize]
    10. prompt: "Based on the given factor table and summary, output a structured review with risk points"
    11. - id: validate
    12. depends_on: report # numerical consistency and field completeness checks

    Key design principle: the model is only responsible for language and summarization, while numerical computation is handled by deterministic code; inject field descriptions and output schemas into prompts to reduce the probability of hallucination.

    5. Inference Layer and Knowledge Base

    Split the research framework, glossary, and historical review documents, then write them into a vector store. Retrieval augmentation can significantly improve the consistency of conclusions. For splitting, it is recommended to split by semantic paragraphs rather than a fixed word count, and keep timestamps and sources in metadata for citation and traceability.

    6. Risk Control and Backtesting: A Loop That Cannot Be Omitted

    • Backtesting must account for constraints such as fees, slippage, and the inability to trade at price limits;
    • Use rolling-window out-of-sample validation to avoid parameter overfitting;
    • Set maximum drawdown, single-target exposure, and stop-loss thresholds, enforced by code rather than left to model judgment;
    • Keep records of all signals to form an auditable chain.

    7. Deployment and Operations

    For a self-hosted environment, it is recommended to enable scheduled scheduling, failure retries, and alerting; collect logs centrally, and have a metrics dashboard covering task success rate, latency, and call cost. Put keys in environment variables or a key management service, and add authentication and rate limiting at the interface layer to prevent the workflow from being called directly by external parties. Overall, getting a minimal loop running first and then gradually replacing and extending nodes is the most reliable path to controlling complexity.