Generate a Valid Trading Signal based on Simple Moving Average

I am trying to generate a LONG Trading Signal based on Simple Moving Average(SMA) (using CLOSE prices) using the following rule:
LONG Signal:
close(today) > SMA(today) & close(yesterday) < SMA(today)
Both above statements must be satisfied in order to generate a valid trading signal.
I have attached a picture in which there is a green arrow. That is the point where I should be able to generate a trading signal. Purple line is a 100 days SMA.
The code I am using is as follows:
signal = zeros(numel(close),1);
for i = 2:numel(close)
signal = close(i-1) < sma100 & close > sma100;
end
The above code does create a logical vector but all items are 0(false).
Any help to remedy the above situation would be of great help.
Thank you.

Answers (0)

Products

Release

R2021a

Asked:

on 7 Feb 2022

Community Treasure Hunt

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

Start Hunting!