Daily Close above/below 200 Day SMA Backtest Report is Incorrect

Objective: Buy/Sell when the daily close of ^SPX is above/below the 200 Day Simple Moving Average.

My code below is producing incorrect results. Example...the most recent daily close of ^SPX from below to above the 200 SMA was on 03/20/23, closing price of 3951.57. My Back Test Report shows the last daily close of ^SPX from below to above the 200 SMA was on 01/20/23, closing price of 3972.61.

MASignal = MA(Close,200);

Buy = Cross(Close,MASignal);
Sell = Cross(MASignal,Close);

Hmmm...?

@MSJr , Here are a couple of suggestions that may help.

Use an Exploration to see/show the data you are working with.

See if you Reference the MASignal from prior day is what you really are looking for in terms of processing. Code sample below.

Buy = Cross(Close, Ref(MASignal, -1));
Sell = Cross(Ref(MASignal, -1), Close);

1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.