Avoiding Look-Ahead Bias in a Moving Average Backtest
Summary
The discussion diagnoses an implausibly large cumulative return in a simple moving-average crossover backtest. The strategy uses closing prices and a 10-day simple moving average to set a long or short position, then applies that position to daily asset returns. The key issue is timing: a signal determined using a day’s closing price cannot earn the return that has already occurred by that close.
The accepted answer says to apply the position known at one close to the return from that close to the next close. This aligns the signal with the subsequent price movement and avoids look-ahead bias. Another response recommends simulating the trade sequence day by day and accounting for transaction costs. A separate example notes that costs can materially reduce performance, potentially overwhelming an otherwise attractive backtest.
The thread gives a timing correction and practical cautions rather than a complete backtest specification. It does not provide a verified strategy result or address details such as slippage, execution prices, or position sizing, which would also affect realistic performance.
Key ideas
- A closing-price signal should be applied to the return earned after that close.
- Using the same day’s return with a signal based on that day’s close introduces look-ahead bias.
- Cumulative strategy returns should be calculated from correctly aligned daily returns.
- Transaction costs can substantially change a moving-average strategy’s backtest performance.
- A step-by-step trade simulation can help expose timing and implementation errors.
Tags
Full text
# Simple Moving Average Backtest: Cumulative Return too high
# Simple Moving Average Backtest: Cumulative Return too high
I apologize if this is way too basic a question, but I'm an absolute beginner to trading and am in the process of learning the fundamentals.
Currently I'm trying to model a (10-day) SMA backtest in Excel, where signals are generated due to crossovers between the closing prices and the 10-day SMA. I calculate the daily and cumulative returns based on closing prices for the asset, and then get the daily returns of the strategy by multiplying the daily asset returns by 1 (if long) or -1 (if short) (this method of getting strategy daily returns has been suggested in a couple of blogs that I'd referred to previously).
Finally I calculate the cumulative returns for the strategy. The problem is that the cumulative returns grow insanely large (I guess up to the order of $10^{15}$). I can't understand what I'm doing wrong - whether the formulae to compute returns are wrong or if the logic itself is incorrect.
I would be grateful if someone could check out the excel sheet (google drive link below) and let me know or at least give me a hint of where I'm making a mistake (or mistakes).
Link to excel file
## Answer by Alex C (score 2, accepted)
https://quant.stackexchange.com/a/25473
The moving average is determined as of the close of a particular day. Then to calculate the P&L you have to multiply today's state (1 or -1) by TOMORROWs return, instead you are using todays! So for example the formula in H13 needs to be E14/E13-1 and not as you incorrectly have it E13/E12-1.
HTH
## Answer by Igor Dzama (score 1)
https://quant.stackexchange.com/a/30212
The logic behind cumulative P&L is pure scholar's. Follow it and you loss everything. Take spreadsheet and simulate reality of every day trade step by step (strongly recommend include fees) and find out difference! There is a lot of examples, a lot of discussions based on this "unverified?" assumption. Use your mind and check what it is written on Internet.
## Answer by Peter (score 1)
https://quant.stackexchange.com/a/36419
Some years ago, I've calculated a 29-day SMA backtesting in Excel with consideration that buying and selling is not able at the same close day where the signal is generated.
> Don't forget the transaction costs!
When transaction costs would be 0% (...or not included) the performance would look like this:
When transaction costs would be 0.45% the performance would look like this:
When transaction costs would be 1%, the strategy would go bankrupt just some years after launching (do you see the blue line at the bottom?):Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.