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 )
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 :
0 29 41 (added row)
0 29.01 (added row)
The code I attempted :
spread = y;
for i = 2:numel(numUnits)
compare = abs(numUnits(i)-numUnits(i-1));
if compare == 2
temp = [temp(1:i-1,:); temp(i:i,:); temp(i: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!