Backtest Demo: Test a Trading Strategy on Past Prices
This demo replays a simple rule on prices that already happened. It does not say what happens next. You paste a price series, pick one rule, and see the return, the drawdown, and the trades next to buy and hold, with fees. It is illustrative. It is not a profit claim.
Illustrative demo. The sample prices are made up. Results describe that series only.
How it works
The demo is long only, one position at a time, with no leverage and no shorting. A moving-average signal at a row compares that close with the average of the previous N closes, then fills at that close. That is the lookahead check: the average does not include the current close, and no later close is used. Buy with all cash when the close is above the average and you are out. Sell all when the close is below the average and you are in. Buy and hold buys at the first row and sells at the last, with the fee on both sides. Fixed-amount buying spends the amount every N rows until cash cannot cover another buy, then keeps the position through the last price. The comparison for every rule is the same cash invested at the first row and sold at the last. RSI uses Wilder smoothing. A buy is a cross below the low level while you are out. A sell is a cross above the high level while you are in. The signal uses only earlier closes.
Buy units = cash x (1 - fee) / price. Sell cash = units x price x (1 - fee). Total return = final equity / starting cash - 1.
Things this demo leaves out
- Spreads, unless you enter slippage.
- Taxes, liquidity, and partial fills.
- Overfitting: a rule can be tuned until it fits one series.
- Survivorship: one price series is one path, and it is a path you chose.
- Anything after the last price. The result is not a claim about what happens next.
Worked example
Example, made-up numbers. Sample closes, moving average length 3, cash $1,000, fee 0.75% per side, no slippage. Rows are numbered from 0.
| Row | Close | Prior-3 average | Action | Result |
|---|---|---|---|---|
| 3 | 105 | 101.00 | Close above average: buy | 9.4524 units after fee |
| 6 | 104 | 107.67 | Below: sell | cash 975.67 |
| 9 | 109 | 102.00 | Above: buy | 8.8840 units after fee |
| 11 | 107 | 108.00 | Below: sell | cash 943.46 |
Summary: 2 closed trades, 0 winners (win rate 0%), final equity $943.46 (-5.65%), max drawdown -9.26% (peak $1,039.76 at row 4). Buy and hold on the same series with fees on both sides: 1,000 x 1.07 x 0.9925 x 0.9925 = $1,054.01 (+5.40%).
A rule that sounds sensible lost to buy and hold on this series. One series proves nothing either way. A backtest is a way to ask questions about the past.
Equity table for the sample (text fallback)
| Row | Close | What the rule did |
|---|---|---|
| 0 to 2 | 100, 102, 101 | No signal yet. Cash stays $1,000.00. |
| 3 | 105 | Buy. About 9.4524 units. Equity is the units marked at 105, after the fee. |
| 4 | 110 | Still in. This is the equity peak, about $1,039.76. |
| 6 | 104 | Sell. Cash about $975.67. |
| 9 | 109 | Buy again. About 8.8840 units. |
| 11 | 107 | Sell. Cash $943.46. Drawdown from the peak is -9.26%. |
Edge cases and limits
- Fewer than 5 prices, or more than 5,000, is rejected. If the moving-average length needs more rows than you pasted, the page says you need at least n + 1 prices.
- A blank line is skipped. A zero, negative, or non-numeric close stops at that row number. Oldest price first.
- A constant series has no moving-average cross and no RSI signal, so the return is 0.00% when the rule never trades. Buy and hold still buys and sells, so the fee changes the result.
- If the rule never trades, that is not an error. The line is: The rule never triggered. Total return 0.00%.
- An open position at the end is marked at the last close and left out of the win rate. Fixed-amount buying that cannot spend the next amount shows the idle cash.
- The file stays in the browser. Nothing is uploaded. There is no price download from a network.
Frequently asked questions
What is backtesting a trading strategy?
Backtesting trading strategies means applying fixed rules to past prices and reading what would have happened. You bring the prices, the rule, and the fees. The result is a return, a drawdown, and a list of trades for that series.
Can a backtest tell you later returns?
No. Markets change. No AI can predict markets. A backtest describes the past under the assumptions you enter.
What is lookahead bias and how does this demo avoid it?
Lookahead bias is using information that was not known yet. In this demo a moving-average signal compares the current close with the average of earlier closes only, and the fill is at that same close.
Why include fees and slippage?
Fees and slippage turn many small apparent winners into losers. A tight rule can look fine before costs and poor after them. The trading fee calculator is the dollar view of the same idea.
What is overfitting?
Overfitting is tuning a rule until it fits one history. Try other periods and other series. One result is not evidence.
For how AI tools relate to backtesting, see backtesting with AI.
Related tools
Price a fee on the trading fee calculator and a target on the risk reward calculator. The wider frame is risk management for trading.
Try it with a Moby Agent
A backtest does not trade for you. If you want an agent to act inside limits you set, Moby Agents trade on Solana, paid in USDC. You set your agent's per-trade and daily spending limits and market-cap range; Moby blocks red-rated tokens.
