Assisted Development with LLMs: Building a Stock Data Fetching and Backtesting System from Scratch

    Evaluating the coding capabilities of Large Language Models (LLMs) requires more than just looking at benchmark scores; real-world projects provide better insights. This article documents the practical process of building a stock data fetching and backtesting analysis system from scratch based on personal stock trading statements. It covers essential engineering strategies such as converting complex data into Markdown format, using step-by-step prompts to prevent AI from going off track, letting the model digest API documentation to avoid hallucinations, and having humans guide architectural decisions like database design. Readers will learn how to effectively manage AI-assisted development by combining human planning with the model's execution capabilities.

    I. Why Use Real Projects to Evaluate LLM Coding Capabilities

    Evaluating a newly released Large Language Model (LLM) based solely on benchmark scores is of limited value. High scores do not always equate to good performance in real engineering scenarios. Real-world development tasks involve complex phases such as requirement comprehension, multi-step planning, exception handling, and interaction with local environments—elements that benchmark datasets often fail to cover.

    Therefore, I chose a small project with genuine personal demand: building a “stock data fetching + trading backtesting analysis” system based on personal stock trading statements. The advantages of this project include:

    • Real Data: The trading statements are exported from personal trading software, providing actual analytical value.
    • Complete Pipeline: It involves multiple technical steps including file parsing, data cleaning, API calling, database design, and progress feedback.
    • Moderate Complexity: An AI assistant can participate, but human oversight of the workflow is required, making it easy to observe the model’s compliance and stability.

    Below is a walkthrough of the entire practical process and pitfalls encountered, following the actual development sequence.

    II. Step One: Preprocessing Trading Statement Data

    2.1 From Spreadsheet Files to Structured Text

    Trading statements exported by trading software are typically spreadsheet files. Directly feeding such files to an LLM for analysis often results in incomplete reads—especially when there are many columns, complex formats, or merged cells. The parsing function invoked by the model might miss parts of the data.

    A more reliable approach is to first convert the spreadsheet file into a lightweight markup text format (Markdown) before handing it to the model for analysis. There are multiple open-source document conversion tools available that can transform common office documents (spreadsheets, documents, presentations, PDFs, etc.) into a unified Markdown format. The benefits of this include:

    1. Explicit Structure: The row and column relationships of tables are flattened into plain text, so the model doesn’t need to understand complex file encodings.
    2. No Parsing Ambiguity: It prevents errors when the model uses a code interpreter to parse binary file formats.
    3. Context-Friendly: Plain text is better suited for insertion into the conversation context, facilitating multi-turn analysis.

    2.2 Let the Model Extract the Target List First

    After obtaining the structured trading statement, my first task was not to have the model perform complex analysis directly. Instead, I had it extract all the stock names and codes involved in the statement to form a target list, which I then manually verified.

    This “small-step verification” approach is crucial: the target list is the foundation for all subsequent data fetching. If this step is wrong, everything that follows is wasted effort. Starting with low-risk, easily verifiable tasks allows for a quick assessment of whether the model understands the data accurately.

    III. Step Two: Step-by-Step Prompting to Prevent AI from “Losing Control”

    3.1 Dumping All Requirements vs. Issuing Tasks Step-by-Step

    Many developers are accustomed to throwing all requirements at an LLM at once: “Build me a backtesting system with data fetching, storage, analysis, visualization…”. Practice has proven that this approach easily loses control on personalized tasks. The model will hallucinate numerous structures you didn’t ask for, generate a bunch of unnecessary files, or drift off course midway.

    A more reliable working mode is to think through the complete workflow yourself first, then break the task down into clearly bounded sub-steps and issue them sequentially. For example, the breakdown for this project was:

    1. Extract the target list from the trading statement (manual verification).
    2. Fetch daily and intraday data based on the target list and store it in a database.
    3. Write data testing functions to verify the availability of each API.
    4. Build the backtesting analysis logic upon a complete dataset.

    After each step is completed, manually inspect the output and confirm it is correct before moving to the next step. This way, even if an error occurs in one step, the rollback cost is very low.

    3.2 Elements of a High-Quality Prompt

    Taking the “data fetching and database insertion” step as an example, a well-structured prompt should contain the following elements:

    • Project Background: Explain that a stock trading backtesting analysis system is being built and that the current phase is data preparation.
    • Clear Data Source and Priority: Specify the preferred data service API (include the local path to the API documentation in the prompt, which contains authentication credentials).
    • Task Boundary: Fetch daily and intraday data for all stocks in the target list.
    • Storage Requirements: Store in a local database, ensuring the database design is standardized and scalable.
    • Process Visibility: Display specific progress during the fetching process to keep track of the execution status at any time.

    An illustrative requirement description is as follows (credential info should be redacted):

    1. Please write the data fetching function based on the API documentation (local path: /path/to/api-docs.md):
    2. 1. Use the target list as the reference to fetch all daily and intraday data;
    3. 2. Store the data in a local database with a standardized, scalable table structure;
    4. 3. Output progress information in real-time during the fetching process.

    The “process visibility” point is particularly worth emphasizing. When an LLM executes a long-running task without progress output, the user cannot tell whether it is working normally or has hung. Requiring explicit progress feedback effectively adds a layer of “monitoring” to the AI’s execution process.

    IV. Step Three: Automatic API Documentation Digestion and Availability Verification

    4.1 Let the Model “Read the Docs” Itself

    This project utilized APIs from two market data services. My approach was to provide the model within the AI coding tool with the addresses of the API documentation for both services, letting it fetch the documentation content itself and compile it into a local reference document for subsequent coding.

    This essentially outsources the tedious task of “reading API documentation” to the model. The code generated by the model based on the documentation is far more accurate than asking it to write API calls from memory—the API details in an LLM’s training data might be outdated or inaccurate. Letting the model code based on the latest documentation is a key measure to avoid hallucinations.

    4.2 Test APIs One by One and Generate a Test Report

    No matter how well the API documentation is written, it does not guarantee that every endpoint is available under the current account’s permission level (many data services unlock APIs based on subscription tiers). Therefore, the next step is to have the model write a set of test functions to verify whether data can be successfully fetched for each endpoint, and output a test report.

    The value of the test report lies in:

    • Clarifying which APIs are available under current permissions, avoiding frequent errors in subsequent code.
    • Recording the return structure of each API, providing a basis for database table design.
    • Forming part of the project documentation, facilitating future maintenance and troubleshooting.

    This step also embodies the “step-by-step execution” philosophy: verify availability first, then write the formal data pipeline, rather than mixing verification and development together.

    V. Database Design Essentials

    Although the model is tasked with creating the database and tables, humans need to guide the design principles. Storage design for market data typically requires attention to:

    1. Standardized Table Structure: Explicit field types for stock code, trading date, OHLC (open, high, low, close) prices, and trading volume, with a unique primary key (e.g., a combined “code + date” primary key).
    2. Scalability: Reserve fields or use a combination of wide tables and detail tables to easily accommodate future data like technical indicators and capital flows.
    3. Incremental Updates: The fetching logic should support incremental writes by date range to avoid repeatedly fetching full historical data.
    4. Index Optimization: Establish indexes for the core query pattern of “querying time series by code”.

    Writing these principles into the prompt when interacting with the model allows the generated table creation scripts to succeed on the first try, reducing rework.

    VI. Stability Observations and Pitfall Records

    The practical process also exposed some issues. I encountered an “unresponsive” phenomenon multiple times when using new-generation models—after a task was issued, there was no response for a long time. The workaround was to manually pause and re-execute, which usually succeeded on the second attempt.

    To address such issues, the following engineering strategies are recommended:

    • Keep Tasks Small: The lighter the single task, the lower the retry cost for single-point failures.
    • Preserve Intermediate Artifacts: Ensure the output of each step is persisted (files, database records) so that reruns can resume from the breakpoint.
    • Report Issues Promptly: Instability during the initial rollout phase of a new model is common; reporting it to the service provider helps facilitate quick fixes.

    VII. Conclusion: The Right Approach to AI-Assisted Development

    Through this complete, small-scale project, several general practical experiences can be distilled:

    1. Use real-world demands to test model capabilities, which is more valuable than looking at benchmark scores.
    2. Data preprocessing first: Convert complex formats into model-friendly structured text.
    3. Issue tasks step-by-step with manual confirmation; this is the most effective means to prevent AI coding from going out of control.
    4. Let the model code based on the latest documentation and verify API availability one by one to avoid hallucinations and permission issues.
    5. Require observable execution processes: Progress output makes long tasks controllable and monitorable.
    6. Humans guide architectural decisions (like database design principles), while the model handles the specific implementation.

    LLMs are evolving from “code completion tools” to “engineering assistants capable of executing multi-step tasks,” but their reliability heavily depends on human workflow design. Combining human planning capabilities with the model’s execution capabilities is the correct path to improving development efficiency at the current stage.