Building an Automated Data Analysis and Backtesting System with Large Language Models

    Learn how to build an automated data analysis and backtesting system with LLMs. This article covers data preprocessing with Markdown conversion, progressive task decomposition, prompt engineering for data ingestion and local storage, and practical engineering tips for handling model behavior in long-running workflows.

    As the reasoning and code generation capabilities of large language models (LLMs) continue to advance, developers are increasingly relying on AI assistance for complex data processing and analysis tasks. Recently, many developers have begun using the latest generation of large models to analyze personal historical trade statements and build backtesting systems, with the goal of extracting trading patterns and optimizing investment strategies. Based on this typical scenario, this article breaks down the end-to-end practice of using LLMs—from data cleaning to system construction—and discusses the technical challenges and best practices involved.

    1. Data Preprocessing: Converting Unstructured Data into Standardized Structured Formats

    The first step in building a backtesting system is obtaining and organizing raw trading data. Trade statements exported from various trading software are typically in Excel format. However, LLMs often struggle when directly reading and processing complex Excel files: due to intricate internal data structures, merged cells, multiple worksheets, and hidden formatting, the underlying parsing functions invoked by the model are prone to missing data, field misalignment, or type recognition errors.

    To address this pain point, introducing a file format conversion tool is a best practice. Developers can use widely adopted document conversion solutions to transform Excel files into Markdown plain-text tables. The flat text structure of Markdown aligns closely with the pretraining corpus of large models, ensuring that the model ingests every trade record and its corresponding field attributes completely and accurately. This seemingly simple preprocessing step is actually the foundation for reliable downstream AI analysis, preventing the classic “garbage in, garbage out” problem.

    2. Task Decomposition: Controlling Execution Pace and Accuracy of the Model

    Building a backtesting system involves complex requirements such as data cleaning, API integration, database design, and backtesting logic implementation. Many developers are accustomed to feeding a lengthy list of requirements to the model all at once. In practice, however, this “one-shot” approach frequently leads to uncontrolled output—especially for highly customized tasks, where the model may generate non-functional code due to misunderstandings of the requirements.

    A more robust approach is progressive task decomposition, breaking a large goal into verifiable small steps. For example, in the first step, the model is asked only to extract the list of all traded instrument names from the converted Markdown statement. The developer then manually reviews the list to ensure the accuracy of the base data. Once confirmed, the second step proceeds: writing the data retrieval logic based on the instrument list. This “small steps, continuous verification” collaboration style not only reduces the model’s reasoning burden, but also significantly improves the quality and controllability of the final delivered code.

    3. System Construction: Integrating Financial Data APIs with Local Storage

    When writing data retrieval and storage logic, having the model directly read and understand API documentation is a highly efficient approach. Developers can place the documentation of a widely used financial data API (typically in Markdown format) in the working directory, and guide the model with precise prompts to read the documentation and generate the calling code.

    A high-quality prompt should include the following key elements:

    1. Clear input: Specify the previously extracted instrument list as the input parameter.
    2. Clear data source: Instruct the model to first read the data API documentation at the specified path and use the authentication token provided in the documentation.
    3. Clear storage conventions: Require the model to design a well-structured local database schema, ensuring extensibility to accommodate additional data dimensions in the future.
    4. Clear progress feedback: Require the model to include progress logging in the data retrieval loop, so developers can monitor execution status at any time.

    Here is an illustrative prompt structure:

    1. Based on the following instrument list, refer to the data API in `/path/to/api_doc.md` (which contains authentication details) and build a data retrieval function.
    2. Requirements:
    3. 1. Retrieve all daily and intraday data for the instruments listed above.
    4. 2. Store the data in a local relational database. The table design should follow the third normal form and reserve extension fields.
    5. 3. During retrieval, output detailed progress to the console (e.g., "Fetching [instrument name], progress X/Y").

    With this approach, the LLM can quickly generate Python scripts that meet engineering standards, significantly shortening the development cycle from requirements to prototype.

    4. Model Behavior Analysis and Engineering Recommendations

    When using the latest generation of large models to execute the long-running workflows described above, developers can clearly perceive improvements in model capability, but certain issues also emerge that require engineering workarounds.

    The first is the “freezing” phenomenon. When performing time-consuming network requests or generating long pieces of code, the model occasionally stalls and stops responding. This means developers working in AI-assisted coding environments cannot fully rely on the model’s autonomous execution; instead, they should implement reasonable timeout interruption and manual restart mechanisms. Once a stall is detected, promptly interrupting and re-triggering execution usually restores the workflow quickly.

    The second concerns the model’s deep reasoning and boundary control. After the backtesting system produces results, developers can further ask the model to perform a macro-level summary analysis of the backtest data. When confronted with extremely unfavorable backtest results, the model may draw very harsh or even pessimistic conclusions (for example, determining that a trading strategy “carries a risk of ruin”). This reflects the strong data insight and logical reasoning capabilities of the new generation of models—they can see overall trends beyond individual data points. However, it also reminds developers that the model’s conclusions should be treated as risk references. Final business decisions still require human judgment informed by industry knowledge and the broader macro environment.

    5. Conclusion

    Large language models are evolving from simple conversational assistants into powerful engineering execution engines. Through sound data preprocessing, fine-grained task decomposition, precise prompt engineering, and engineering-level tolerance and intervention for model behavior, developers can significantly lower the barrier to building complex data analysis systems. In data-intensive scenarios such as quantitative financial backtesting, making good use of AI-assisted tools not only accelerates the delivery of business prototypes, but also provides fresh perspectives on business logic through the model’s deep reasoning capabilities. As model stability continues to improve, this AI-driven development paradigm is set to deliver core value across an increasing number of vertical domains.