Dynamically adding element to an array

22 vues (au cours des 30 derniers jours)
Darren Lim le 6 Jan 2021
Commenté : Darren Lim le 6 Jan 2021
Dear Community,
Still new to matlab and exploring, It may be a simple problem but I am stuck for sometime on trying to come out with a clean logic .
Say i have an array (N X 2 )
signal price
1 29.21
-1 29.41
-1 29.43
-1 29.63
1 29.01
0 29.21
1 28.62
1 28.82
I wanted to add a row if the consecutive row in column 1 is -1 to 1 or -1 to 1 , the row to be added is a 0 and column 2 will be the price in the next row.
Basically a 1 is a long signal and -1 is a short signal and 0 is exit a position (in finance). The problem with the original data is when the scenario is from long to short ( 1 to -1) , i need to exit it before shorting , so i need to add a row with 0 and the price to exit.
The result I am expecting will be :
signal price
1 29.21
-1 29.41
-1 29.43
-1 29.63
1 29.01
0 29.21
1 28.62
1 28.82
The code I attempted :
temp=NaN(length(spread), 2); % initialise N X 2 array
temp(:,2)=spread; % put price into 2nd column
%numUnits is a single row of signals (eg : -1,0,1...)
for i = 2:numel(numUnits)
temp(i,1)=numUnits(i);
compare = abs(numUnits(i)-numUnits(i-1));
if compare == 2
temp = [temp(1:i-1,:); temp(i:i,:); temp(i:end,:)];
temp(i,1)=0;
%temp(i,2)=i;
end
end
The result is incorrect because the for loop is always operating on the original NumUnits array and not the Temp where I am adding the extra rows. I dont have an elegant solution to add dynamically an array and operate it on the fly.
Anyone able to help or give some clue? Thanks in advance!
Darren
0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

Réponse acceptée

Timo Dietz le 6 Jan 2021
Modifié(e) : Timo Dietz le 6 Jan 2021
priceArray= ...
[1 29.21
-1 29.41
-1 29.43
-1 29.63
1 29.01
0 29.21
1 28.62
1 28.82]
y=priceArray(:, 2);
numUnits = priceArray(:, 1);
temp=NaN(2*length(spread), 2); % initialise N X 2 array TWICE the size of the origin
%temp(:,2)=spread; % put price into 2nd column
%numUnits is a single row of signals (eg : -1,0,1...)
idx = 1; % introduce second index variable
for i = 2:numel(numUnits)
%temp(i,1)=numUnits(i);
compare = abs(numUnits(i)-numUnits(i-1))
if compare == 2
temp(idx, :) = priceArray(i-1, :);
idx = idx + 1;
temp(idx, :) = [0 priceArray(i, 2)];
else
temp(idx, :) = priceArray(i-1, :);
end
idx = idx + 1;
end
temp(idx, :) = priceArray(i, :); % add last row which always equals the origin
temp = temp(1:idx, :)
Try these modifications. The point is that you have to introduce a second index variable since your temp array is bigger than
the origin. So you can't use one common index. Further, the initialization of temp must reserve more rows - I chose twice
the size for simplicity (+50% should suffice already).
1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Darren Lim le 6 Jan 2021
thank you Timo, this works! on my learning journey and this helps a lot!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Time Series dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by