Building a Personal Trading Data Backtesting System with LLM Assistance
Learn how to build a personal trading backtesting and analysis system with LLM-assisted coding: normalize trade data into Markdown, break tasks into verifiable steps, design a market data database, and generate objective performance reports—letting data, not emotion, evaluate your trading behavior.
1. Project Background: From Trade Statements to a Backtesting System
Many individual investors share the same experience: their accounts fluctuate over the long run, losses outnumber gains, yet they rarely conduct a systematic review. A common piece of wisdom in investing circles is to repeatedly study your own trade statements and distill a trading pattern that suits you. But manually reviewing hundreds or thousands of executions is practically infeasible, so “using code to backtest your own trading behavior” becomes a natural technical proposition.
At its core, this project is a typical data engineering pipeline with four stages:
- Data acquisition: export trade statements from your trading software;
- Data preprocessing: convert the files into a format the LLM can understand easily;
- Data enrichment: pull historical market data for the traded symbols;
- Analysis and backtesting: generate an objective analysis report on top of a unified data foundation.
Throughout the entire workflow, the LLM’s role is not to “make decisions for you” but to “write the engineering code for you.” This positioning is critical and is the premise of all the methodology that follows.
2. Data Preprocessing: Why Convert Excel to Markdown
Trade statements can usually be exported from a trading terminal as Excel files. Letting an LLM read Excel directly carries a hidden risk: under the hood, the model typically relies on a code interpreter or parsing library to read spreadsheets. When there are many columns, merged cells, or irregular formatting, problems like dropped rows or misaligned columns can occur—and such errors are often silent. The model doesn’t raise an error; the analysis results simply drift out of accuracy.
A common industry practice is to introduce a layer of format normalization: first use a document conversion tool (several open-source options can convert Office documents to Markdown) to turn the Excel file into Markdown tables, then feed it to the model. Markdown is plain text with explicitly visible table structure, which significantly improves the model’s comprehension accuracy—and makes it easy for a human to spot-check.
The lesson from this step generalizes to every scenario where you feed data to an LLM: never assume the model can perfectly parse binary formats; always prefer a lossless, human-verifiable plain-text intermediate representation.
3. Task Decomposition: Don’t Dump the Entire Requirement on the Model at Once
Many developers using AI coding tools follow the same habit: write a long requirement description and wait for the model to produce a complete system in one shot. Practice shows this works passably for standardized tasks, but for tasks with personalized logic it easily spirals out of control—the output looks complete, yet the details deviate from expectations everywhere.
A more reliable approach is the human defines the workflow first, and the model executes it step by step:
- Step 1: only ask the model to extract all stock codes and names from the trade statement, and manually confirm the list before proceeding. This establishes a “factual baseline,” preventing downstream analysis from being built on the wrong symbols.
- Step 2: based on the confirmed symbol list, have the model write the market data retrieval functionality.
- Step 3: design the backtesting logic and generate the analysis report.
The output of each step is verifiable and reversible, and the cost of locating errors is minimal. This is essentially the same idea as “small commits, continuous verification” in software engineering.
One more caution: when a new generation of LLM services is freshly released, inference peak hours can cause a “frozen” phenomenon where responses produce no output for a long time. If this happens, manually interrupting and resending the request usually resolves it. Engineering-wise, implement timeout and retry mechanisms rather than waiting indefinitely.
4. Market Data Retrieval and Database Design
The prompt design for Step 2 is worth elaborating on. A good instruction should include the following elements:
Goal: Build the data layer of a stock buy/sell backtesting system.Input: The confirmed list of stock symbols.Data source: Prefer the locally available market data API documentation(the documentation includes authentication credentials); interfaces withoutpermission fall back to alternative sources.Requirements:1. Retrieve daily and minute-level market data for all symbols;2. Store the data in a local database with a well-designed, extensible schema;3. Output real-time progress logs during retrieval so execution statuscan be observed at any time.
The phrase “the database should be well-designed and extensible” is not a courtesy—it’s the key to avoiding rework later. A minimal viable market data schema can look like this:
| Table | Purpose |
|---|---|
| stock_basic | Symbol code, name, exchange |
| daily_kline | Daily OHLCV data (date, open, high, low, close, volume) |
| minute_kline | Intraday data (timestamp, price, volume) |
| trade_records | Personal trade statements (side, price, quantity, time) |
For primary keys, a composite unique constraint of “code + date” is recommended, combined with upsert writes so that repeated retrievals are idempotent. If you later need to handle larger data volumes or team collaboration, this kind of structured data can also be smoothly migrated to general-purpose storage categories such as cloud object storage and data warehouses, without rebuilding the architecture from scratch.
Requiring “visible progress” matters just as much: having the model add progress output to the script (e.g., “Fetching symbol 3/42”) is both an observability design and a fallback for diagnosing state when the service appears frozen.
5. Backtesting Analysis: Let the Data Speak, Not Emotions
With the data layer ready, Step 3 is the actual backtesting analysis. For personal trade statements, common and valuable analysis dimensions include:
- Win rate and profit/loss ratio: statistics by symbol, by holding period, and by time-of-day of entry;
- Pattern recognition: do winning trades share common traits (e.g., certain chart patterns, certain time windows), and do losses concentrate in some type of impulsive action;
- Counterfactual experiments: how would the equity curve change with strict stop-losses, without chasing rallies, or by delaying exits by one day?
These analyses are performed by model-generated scripts, but the conclusions must be reviewed by a human. A real example: after one user handed their trading records to a model for comprehensive evaluation, the model’s conclusion was “the current trading pattern is heading toward destruction.” That judgment sounds harsh, yet it is precisely the value of data—it has no emotion, saves no face, and only reflects statistical facts. For an investor, such a cold shower is far more useful than self-comfort.
6. Conclusion
Looking back at the whole project, the methodological gains far outweigh the experience of any specific tool:
- Normalize the data first: eliminate parsing uncertainty with a plain-text intermediate representation;
- Break tasks into small pieces: the human defines the process, the model executes, and every step gets verified;
- Write complete instructions: goal, input, data source, storage specification, and observability—none can be missing;
- Keep conclusions under human review: the model computes; the human judges and decides.
Whether it’s quantitative backtesting, log analysis, or business reporting, this “human-in-the-loop data analysis” paradigm applies equally well. Tools will keep evolving, but engineering-style thinking is what compounds over the long term.
