1
$\begingroup$

I am using R moving average crossover backtest script from eickonomics. But I have a question about this section.

#Calculate the moving averages and lag them one day to prevent lookback bias
PreviousSMA_50 <- lag(SMA(Cl(data[['SPY']]),50))  
PreviousSMA_200 <- lag(SMA(Cl(data[['SPY']]),200))
. 
.
data$weight[] = ifelse(as.integer(PreviousSMA_50>PreviousSMA_200)==1,1,0)  #If price of SPY is above the SMA then buy

Why does the author need to lag the SMA values? Why can't he simply use unlagged values?

PreviousSMA_50 <- SMA(Cl(data[['SPY']]),50) 
PreviousSMA_200 <- SMA(Cl(data[['SPY']]),200)
. 
.
data$weight[] = ifelse(as.integer(PreviousSMA_50>PreviousSMA_200)==1,1,0)

Why would we use previous SMA value instead of current SMA value? How does it cause lookahead bias?

If we need other indicators do we have to lag those values as well?

rsi<-RSI(Cl(data[['SPY']]),50) 
roc<-ROC(Cl(data[['SPY']]),50) 
$\endgroup$

1 Answer 1

3
$\begingroup$

Because SMA value for a certain day includes that day's closing price. But before the market closing time, obviously it isn't known (because it's in the future), you only know yesterday's closing value. You can not use today's closing price (or any other indicator which is calculated using it) to make any trading decisions or forecasts for this same day.

$\endgroup$
4
  • $\begingroup$ so is this logic wrong PreviousSMA_50 <- SMA(Cl(data[['SPY']]),50)..... data$weight[] = ifelse(as.integer(Cl(data[['SPY']])>PreviousSMA_50)==1,1,0) I have used this in my backtest so this backtest result are wrong? $\endgroup$
    – Eka
    Sep 3, 2016 at 12:38
  • $\begingroup$ @Eka Yes, this is technically wrong, you might be capturing signals before they are observed, although your backtest might still be close to reality, well... it depends... Think about it on a second-by-second basis: you see the trading signal, you send a trade, but it will only be executed in the next second, and the price might be very different. In your case, one day is the smallest observable unit of time, so you have to deal with it same way as well. $\endgroup$
    – sashkello
    Sep 4, 2016 at 8:20
  • $\begingroup$ @sashkello Yes, but the fact that the price can move in a few seconds (and therefore you buy at a different price than you thought) I would consider to be "slippage", not "technically wrong". There are other slippages like bid-ask spread, commissions, etc. But as you say "it still might be close to reality" in a continuous and liquid market. $\endgroup$
    – Alex C
    Sep 5, 2016 at 16:35
  • $\begingroup$ @AlexC Well, one could consider not taking slippage into account "technically wrong", but in general I do agree. However, sharp moves (exactly the times when you might lose or gain a lot) might have a big impact and produce incorrect backtests which earn (or avoid loss) by performing such unrealistic trades. If a backtest makes money without a lag, but loses with a lag, it is a red flag that something might be wrong. $\endgroup$
    – sashkello
    Sep 5, 2016 at 23:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.