Building Quant Data Pipelines with LLMs: A Practical Guide

    This article presents a practical approach to building quantitative data pipelines with large language models. Instead of packing every requirement into a single prompt, it treats the model as an execution unit and breaks the work into four verifiable atomic steps: symbol list confirmation, data contract definition, ingestion and storage, and validation with monitoring. It covers prompt structure, credential security, normalized data modeling, troubleshooting stalled model responses, and a progress and acceptance checklist. The goal is a reliable data foundation for backtesting.

    1. Why “One-Shot Requirements” Get Out of Control

    When using large language models (LLMs) for data engineering, a common mistake is to compress every requirement into a single prompt and expect a usable system in one shot. In practice, the opposite happens: the generated code has a messy directory structure, inconsistent field naming, missing exception branches, and sometimes multiple configuration-reading approaches mixed within the same script. For highly customized, tightly constrained tasks—such as storing securities market data—this loss of control is amplified, because any ambiguity in data definitions, adjustment methods, trading calendars, or time zone handling can make later backtest conclusions meaningless.

    A more reliable approach is to treat the model as an “execution unit” rather than a “decision maker”: a human first defines the workflow and acceptance criteria, then dispatches atomic tasks step by step, with each step required to be observable, rollback-capable, and reviewable. This aligns with small commits and contract-first principles in software engineering, and makes it easier to quickly locate whether a deviation comes from a prompt issue, an interface issue, or a data issue.

    2. Workflow Breakdown: Splitting the Goal into Four Verifiable Atomic Steps

    1. Confirm the symbol list. First extract security names and codes from existing trading records, then have a human double-check them, handling delistings, renamings, and multiple listings of the same company across markets, to produce a versioned symbol list file.
    2. Define the data contract. Specify the granularity, field meanings, adjustment conventions, time zone, and missing-value strategy for daily and intraday data, and output a field description document that serves as the single source of truth for all subsequent code.
    3. Implement ingestion and storage. Prefer already-authorized market data interfaces, establish a normalized database structure, and support incremental writes and resumable transfers.
    4. Validate and monitor. Perform sampled comparisons, row-count fluctuation checks, and failure-retry statistics, and present progress in a readable way in real time.

    Each step should end with a clear deliverable, such as a list file, a field document, table creation statements, or a validation report, avoiding the vague state of “it looks like it finished.”

    3. Prompt Design: Using Constraints Instead of Natural Language Descriptions

    The core of a high-quality prompt is making implicit assumptions explicit. A recommended structure is “Role + Goal + Input + Constraints + Output + Acceptance,” for example:

    1. Role: Senior data engineer
    2. Goal: Build daily and intraday market data storage for the symbol list
    3. Input: Local symbol list file, interface documentation (credentials read from environment variables)
    4. Constraints:
    5. 1) Use only the standard library and common database drivers; do not introduce heavy frameworks
    6. 2) Credentials must not be hardcoded or written to logs
    7. 3) Support resumable transfers and failure retries with exponential backoff
    8. 4) Print progress every 50 symbols (completed/total/failed/elapsed)
    9. Output: Table creation script, ingestion script, configuration sample, run instructions
    10. Acceptance: Runs end-to-end on an empty database; repeated runs produce no duplicate primary keys

    Credential security deserves special emphasis: interface tokens should be injected via environment variables or a key management service, with only placeholders kept in code and logs. Example code:

    1. import os
    2. TOKEN = os.environ["MARKET_DATA_TOKEN"] # No plaintext credentials in source code

    4. Data Modeling: Normalization and Extensibility in Practice

    Normalization does not mean cramming everything into one wide table. A “dimension + fact” split is recommended: a securities dimension table stores code, name, listing status, industry, and sector; the daily fact table uses (symbol, trade_date) as the primary key; the intraday fact table uses (symbol, trade_time) as the primary key; and a separate ingestion task table records batches, status, and error summaries to support reruns and auditing.

    Extensibility shows up in three places: first, primary keys and unique constraints are declared explicitly, relying on the database itself for deduplication; second, numeric fields use unified precision, avoiding accumulated floating-point errors for prices and volumes; third, a metadata table records data versions and adjustment conventions, so the same symbol can hold different conventions side by side without contaminating each other. Example table creation snippet:

    1. CREATE TABLE daily_bar (
    2. symbol VARCHAR(16) NOT NULL,
    3. trade_date DATE NOT NULL,
    4. open DECIMAL(18,4),
    5. high DECIMAL(18,4),
    6. low DECIMAL(18,4),
    7. close DECIMAL(18,4),
    8. volume BIGINT,
    9. adjust_type VARCHAR(8) NOT NULL,
    10. PRIMARY KEY (symbol, trade_date, adjust_type)
    11. );

    5. Troubleshooting Stability: Handling Model “Freezes”

    During interactive generation and execution, you may encounter long periods with no output. This is usually not a data problem, but may relate to overly long context, tasks that are too large in a single pass, tool-call timeouts, or network jitter. General countermeasures include: narrowing the scope of each task and splitting long context across multiple rounds; requiring the prompt to output a plan before execution; setting explicit timeouts and retry limits for external calls; retaining the ability to interrupt and resume from a checkpoint; and running static checks on generated results before execution. If you use engineering-capable AI coding tools or general cloud services from Baidu AI Cloud (such as logging or monitoring and alerting) for observability, you can more quickly distinguish whether the model response is blocked or the interface is rate-limited.

    6. Progress Visualization and Acceptance Checklist

    Progress output should include total, completed, failed, skipped, current symbol, and estimated remaining time, and should be persisted as structured logs for later success-rate statistics. Acceptance should cover: a full run on an empty database; idempotent repeated execution; resuming from a checkpoint after a network outage; sampled data matching authoritative sources; and no sensitive information appearing in any output. Once this loop is complete, the backtesting system has a trustworthy data foundation, and subsequent factor calculation and strategy evaluation conclusions have engineering assurance.