Failed to create signal form successive values

hi,
I'm trying to create a signal of 1 and -1 with a for loop but getting an error of "Subscript indices must either be real positive integers or logicals." My code is like this
for i=1:length(data)
if (MACD(i-1)>0) && (MACD(i)<0)
s(i,:)=-1; % Sell (short)
end
if (MACD(i-1)<0) && (MACD(i)>0)
s(i,:)=1; % buy (long)
end
end
Could you tell me where is the mistake please.

1 Commento

Hi KSVV,
I'm expecting a vector with zeros when there is no zero cross, with ones when MACD crosses zero from below (from negative to positive) and with minus ones when MACD crosses zero from above (from positive to negative).
Thanks for your help

Accedi per commentare.

 Risposta accettata

You can do it without the loop:
zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0); % Returns Approximate Zero-Crossing Indices Of Argument Vector
MACD = rand(1, 25)-0.5; % Create ‘MACD’ Data
zx = zci(MACD); % Find Zero-Crossings
zx = zx(1:end-1); % Eliminate Wrap-Around ‘End Effect’
sx = sign(MACD(zx+1)); % Set Signs Of Zero-Crossings
s = zeros(size(MACD)); % Define ‘s’
s(zx+1) = sx; % Insert Sign Vector Into ‘s’
My code uses the ‘zci’ anonymous function to return the zero-crossing indices of your ‘MACD’ vector, then assigns the sign by looking at the next value in the vector in the ‘sx’ vector. It then creates ‘s’ and assigns ‘sx’ to the appropriate positions in it to create the desired output for ‘s’.

6 Commenti

It works perfect for me. Thank you very much. Really appreciated.
My pleasure!
Hi Star Strider,
I simulated this & there seems to be bug?!
The last 4 values of my 25 random numbers were
-0.46 0.35 0.43 0.18 & the s values I got from
your code were ...
-1 0 0 0 when it should be (if I'm interpreting
the logic correctly)
-1 1 0 0
What do you think? The signs for the previous
21 were right.
Thanks
Damo
The ‘end-effect correction’ will produce that if the last zero-crossing is not at the end of the vector. A fix for that is to insert this if block in place of the second ‘zx’ assignment:
if zx(end) == length(MACD)
zx = zx(1:end-1); % Eliminate Wrap-Around ‘End Effect’
end
The purpose of that assignment is to eliminate the last index in the event that there is a false zero-crossing at the end of the vector, since the code works by shifting the vector relative to itself. If the last zero-crossing is not at the end of the vector, that line will now not execute and ‘s’ will be correct for all inputs.
The complete code is now:
zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0); % Returns Approximate Zero-Crossing Indices Of Argument Vector
MACD = rand(1, 25)-0.5; % Create ‘MACD’ Data
zx = zci(MACD); % Find Zero-Crossings
if zx(end) == length(MACD)
zx = zx(1:end-1); % Eliminate Wrap-Around ‘End Effect’
end
sx = sign(MACD(zx+1)); % Set Signs Of Zero-Crossings
s = zeros(size(MACD)); % Define ‘s’
s(zx+1) = sx;
Works well. I need to go over the logic carefully.
Thanks Damo.
My pleasure.
The logic is actually straightforward. The ‘zci’ utility function circularly shifts the column vector ‘v(:)’ up by one position, then multiplies it by itself. The result is a negative value at the approximate index where a zero-crossing occurs, and positive values everywhere else, because real values of like signs multiplied together are always positive, so a zero-crossing will always be a negative value. (Complex values behave differently and my simple function will not work for them.) Beyond that, the code assigns the signs as you want them, and shifts them to be in the appropriate places.
The ‘end-effect’ results when the end value of the vector is of one sign and the beginning value is another. Because of the circular shifting, this is not an actual zero-crossing, although it will initially be evaluated as one. The if block logic detects that condition and eliminates the ‘end-effect’ false zero-crossing.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su General Applications in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by