An AI tool produces an attractive equity curve. You can read the final return, but can you explain where the account's cash and shares came from on any particular day? If you cannot, the chart is still an output you have not reconciled.
A backtest reconstructs a simulated account using past data, written rules, and execution assumptions. It does not turn those assumptions into actual trades. Before asking whether a strategy is worthwhile, Lin checks a smaller question: Does the implementation produce the account history its rules require?
What you will be able to do
- Separate a signal, a target position, an order, and a fill in time.
- Recalculate account equity from cash, shares, prices, and fees.
- Save a comparison sheet that identifies the first discrepancy between an expected ledger and an implementation.
Write the account rules before calculating returns
This is C4-S1-v1, a synthetic exercise with one fictional asset, X. It is separate from the book's longer C2 study of SPY. Nothing in this five-day example verifies that study or establishes a profitable strategy.
Start with $1,000 in cash and no shares. The account can hold X or cash. It cannot borrow or sell more shares than it owns. After each day's close, compare the closing price with 11. If the close is strictly greater than 11, the target is 20 shares. Otherwise, including a close of exactly 11, the target is zero.
The order is the target minus the shares already held. A target of 20 with 20 already held produces no order. A nonzero order created after the close fills in full at the next listed trading day's opening price. Each order with a fill incurs a $1 fee. An order with no fill incurs no fee.
Before a purchase, check whether cash covers both the purchase and its fee. If it does not, reject the whole order: no partial purchase, no fee, and no negative cash. A rejected or unfilled order does not change the position. Preserve the rejected order and its status: an empty position does not mean that no purchase was ever intended. These rules are supplied teaching assumptions, not a description of a broker's execution policy. Actual order types have different execution and price constraints.1
For this main ledger, no price is missing between the listed days. Slippage, taxes, cash interest, external deposits and withdrawals, and settlement delays are zero. There are no corporate actions. Later examples change one assumption at a time and do not alter this ledger. The $1,000 is a strategy subaccount, not a household's total wealth; 20 shares is an exercise input, not a suggested position size.
| Day | Open | Close |
|---|---|---|
| D1 | 10 | 10 |
| D2 | 10 | 12 |
| D3 | 13 | 11 |
| D4 | 10 | 10 |
| D5 | 10 | 10 |
Read across the dates before calculating. The close that produces an order and the open that fills it are different observations. A system also needs the time at which a bar becomes available, not merely the date or time printed at its start.2
Follow one order into the account
At D1's close, 10 is not greater than 11. The target is zero, matching the existing position. Cash and equity both remain $1,000.
At D2's close, 12 is greater than 11. The target becomes 20 shares, creating an order to buy 20. The account still owns no shares at that close. It has not earned the earlier rise from 10 to 12. A signal has changed what the account intends to do, not what it already owns.
At D3's open, buy 20 shares at 13. The purchase costs $260 and the fee is $1, leaving cash of $739. Immediately after the fill, marking the shares at 13 gives equity of $999. If a ledger says $1,000, investigate the missing fee. If it says $998, investigate a duplicated fee or another explicitly recorded cost; the number alone does not establish the cause.
At D3's close, the shares are worth 20 × $11 = $220. Equity is cash of $739 plus that position value, or $959. Equity is a valuation of the account; selling is not required to calculate it.
The close is exactly 11, so the new target is zero. An order to sell 20 shares is created after the close. It cannot lock in that closing price under our next-open rule.
At D4's open, sell 20 shares at 10. Gross proceeds are $200; after the $1 fee, $199 enters cash. The account now has $938 and no shares. D4's and D5's closes create no new orders. No additional fees are charged.
| Close | Cash | Shares | Position value | Equity | Order created after close |
|---|---|---|---|---|---|
| D1 | $1,000 | 0 | $0 | $1,000 | None |
| D2 | $1,000 | 0 | $0 | $1,000 | Buy 20 |
| D3 | $739 | 20 | $220 | $959 | Sell 20 |
| D4 | $938 | 0 | $0 | $938 | None |
| D5 | $938 | 0 | $0 | $938 | None |
The final return is ($938 − $1,000) / $1,000 = −6.2%. Reconcile it a second way: buying at 13 and selling at 10 loses 20 × $3 = $60; the two fees add $2. The $62 total loss agrees with the change in equity. This bridge checks whether the return has an understandable economic source.
A better-looking result can violate the rules
Suppose an implementation buys at D2's close of 12 and sells at D3's close of 11. Buying costs $241 including the fee, leaving $759. Selling brings in $219 after the fee. Final cash is $978, a −2.2% return.
That result is $40 better than the correct ledger, but it uses the closing observation to decide and execute at that same close. Our rules permit neither fill. This is a timing error, not an improvement in the strategy. Incorrect timing can also make another example look worse. Whether the chart improves is not a test of whether the implementation is correct.
Now keep the correct timing but change the target to 100 shares. At D3's open, 100 × $13 + $1 = $1,301 exceeds available cash. Reject the whole purchase without a fee. Silently allowing a negative balance would introduce borrowing that the specification forbids.
Treat additional mechanisms as separate branches
Partial fills require their own policy. In a separate example, suppose only 12 of the requested 20 shares fill at 13. Charge the $1 fee on the first fill of that order: cash becomes $843 and the position becomes 12 shares. Eight shares remain unfilled. Whether that remainder stays active or is canceled needs an explicit rule. Under this branch's fee assumption, a later fill of the same order does not incur another $1 fee. Do not insert these numbers into the full-fill main ledger.
At the sample's end, an order without a subsequent opening observation remains unfilled. A missing price in the middle of the sample is a different problem: this specification has not defined order expiration, a delayed fill, or conflicts between an old order and a new signal. Stop and supply those rules before assigning a convenient price or postponing the order yourself.
Corporate actions also affect the account's components. Consider a separate snapshot of 20 shares at a reference price of $10. In a two-for-one split, 40 shares at a reference price of $5 still have a value of $200. This isolates the arithmetic; an actual market price can move for other reasons, and outstanding-order adjustments depend on the system.3
For a separate dividend snapshot, assume the account qualifies and a $0.50-per-share cash dividend on 20 shares has already been paid. Cash increases by $10. Ignoring differences among ex-dividend, record, and payment dates solely for this illustration, a reference price moving from $10 to $9.50 leaves $190 in shares plus $10 in cash: $200 in total. It does not predict an actual price change. If an account return already uses a series that includes reinvested distributions, adding the same dividend cash again can double count it. Check shares, prices, and cash together.
Compare a tool with an expectation you saved first
Before running a tool, write down three checkpoints: no shares at D2's close; cash of $739 and equity of $959 at D3's close; cash of $938 at D4's close. Saving them first makes it harder to adjust the expected answer to whatever appears on the screen.
Request the signal, order, fill, fee, cash, position, and equity records. Work from the first difference, rather than changing the last number until it reads 938. A correct final value can conceal errors that cancel each other.
Use this sequence:
- Save the price table and complete rule set, including rejection and fee policies.
- Calculate the expected ledger independently by hand or in a spreadsheet.
- Run the implementation, retaining its data and rule versions.
- Find the earliest date and field that differ.
- Investigate the underlying records, change one identified cause, and rerun.
- Save the original output, explanation, revised output, and version information.
You can give an AI or another tool this request, together with the entire price table and rules:
First restate the supplied assumptions and identify anything you cannot implement. Return a daily ledger with date, close, signal, target, order created after the close, next-open fill, fee, cash, shares, and closing equity. Do not use a close before it is available, fill a new order at that same close, or silently change missing-data, rejection, or fee policies. Reconcile the ledger before reporting the return. State any execution or export limitations.
The tool does not automatically see this chapter, and it may not support every assumption. If it cannot provide a usable ledger, request an export and reconstruct the account in a table. Without that evidence, record its implementation as unchecked. A hand calculation verifies your own ledger; it does not show that a platform or CoTrading implementation was tested.
The supplied comparison sheet has expected cash, shares, and equity on the left. Enter actual values on the right, then calculate actual minus expected for each field. Start investigating at the first nonzero difference. Record whether “actual” came from a hand calculation, a spreadsheet, or a named tool and version. A blank actual field means not checked, not zero and not a pass.
Explain the ledger, then change a cost
Explanation. D2 has a buy signal. Why is closing equity still $1,000?
Reference answers · reveal
Answer. The signal creates an order after the close. No fill has occurred and the position is still zero. Neither the earlier price rise nor the intended purchase changes existing holdings.
Find a discrepancy. D1 and D2 match, but the reported D3 closing record is cash $740, shares 20, and equity $960. What should you investigate first?
Answer. D3 is the first differing row. Cash and equity are each $1 too high, while the share count and the $220 position value are correct. Investigate whether the purchase fee was omitted. Confirm with the fee and transaction records; a final number alone cannot uniquely diagnose the implementation.
Transfer. Start again from the original rules and prices, changing only the fee from $1 to $2 per filled order. Calculate D3 closing cash and equity, final cash, and return before reading on.
Answer. The D3 purchase costs $262, leaving cash $738. Add the $220 closing position value for equity of $958. The D4 sale contributes $198, leaving $936. The return is −6.4%. Dates and share counts remain the same because the account can still afford the purchase. A difference caused by a deliberately changed assumption is not, by itself, a program defect.
Save a ledger another person can reconstruct
Complete the daily comparison sheet, including actual values, differences, and their source. Attach the $60 trading-loss plus $2 fee reconciliation. Save the D3 discrepancy as a separate investigation record, and save the $2-fee transfer calculation separately from the original expected ledger.
Keep the input prices, written rules, expected results, actual results if available, and versions together. Someone reviewing the file should be able to recover the first signal, first fill, first fee, and first difference without relying on your memory or an AI conversation.
The key checks run in two directions: follow time forward from available information to a permissible fill, then reconstruct equity from cash and positions. Once the implementation is understandable, you can ask what its performance statistics mean. That is the next chapter's task.
References
- Investor.gov: Types of Orders. Order execution and price constraints; our next-open fill is a separate teaching assumption.
- QuantConnect: Custom Securities—Key Concepts, “Avoid Look-Ahead Bias.” Availability and bar end times.
- QuantConnect: US Equity Corporate Actions. Its documented split, dividend, normalization, and order handling illustrate implementation choices; they are not universal rules for every engine.
Editorial status
AI-assisted draft synchronized to the accepted second-round Chinese source. All account examples are synthetic. See the author review. A separate AI reviewer has completed an independent full-text review. Human financial review and target-reader testing have not been performed and are not scheduled for this edition.