Backtesting simple moving average trading strategy

Hi everyone, I have to backtest a trading strategy based on the cross of 3 simple moving average of 4,9 and 18 periods. The script I wrote down is the foloowing:
% Construction of the 3 SMA for 4,9 and 18 periods.
SMA.sma4 = tsmovavg(EURUSD.price.pt1,'s',20,1);
SMA.sma9 = tsmovavg(EURUSD.price.pt1,'s',45,1);
SMA.sma18 = tsmovavg(EURUSD.price.pt1,'s',90,1);
% Construction of the buying/selling signals.
if SMA.sma4 >= SMA.sma9 && SMA.sma18 >= SMA.sma9;
SIGNALS.buy = 1;
else SIGNALS.buy = 0;
end
if SMA.sma4 <= SMA.sma9 && SMA.sma18 <= SMA.sma9; SIGNALS.sell = 1; else SIGNALS.sell = 0; end My problem consists on the fact that the SIGNALS.buy vector is always equal to 0 and I do not get signals for buying/selling assets.
Is there something wrong in the the script or the trading strategy does not work? Thanks for help.

 Respuesta aceptada

Roger Wohlwend
Roger Wohlwend el 9 de Sept. de 2014
Editada: Roger Wohlwend el 9 de Sept. de 2014
Indeed, your code could be the reason that you don't get any buy signal. Try the following:
SIGNALS.buy = (SMA.sma4 >= SMA.sma9) & (SMA.sma18 >= SMA.sma9);
SIGNALS.sell = (SMA.sma4 <= SMA.sma9) & (SMA.sma18 <= SMA.sma9);
That should do it. If you still do not get any buy signal then you should check your trading strategy.

4 comentarios

First of all, thanks for the answer and help @Roger Wohlwend. By coding as you suggested, I got the following error message:
Operands to the || and && operators must be convertible to logical scalar values.
What does it means?
Roger Wohlwend
Roger Wohlwend el 10 de Sept. de 2014
It means that you did not copy my code correctly. Use & instead of &&!
Quantopic
Quantopic el 10 de Sept. de 2014
ok! it works! thanks a lot for help!
Josh Perry
Josh Perry el 18 de Jul. de 2015
yes but how do you back test this???? I am so lost on the backtesting of something like this in matlab

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 8 de Sept. de 2014

Comentada:

el 18 de Jul. de 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by