Good evening,
I have to create a daily return matrix based on 174 daily prices of the S&P index. The table from which I fetch such prices is called "prices", and the objective matrix I have to plug the values in is called "stockreturns". I have tried setting up a nested for - loop after pre-allocating a 173*500 array with zeroes (otherwise the process takes 1+ hours), but the output I get is a zero matrix.
Could anyone help me out? This is the code I am using:
stockreturns = zeros(173,500)
for k = 1:500;
for h = 1:173;
stockreturns(h,k) = ((prices{h+1,k}/prices{(h),k})-1)
end
end
Assuming prices
is a 174 x 500 table, you can just use MATLAB's in-built element-wise operations.
prices = table2array(prices);
stockreturns = prices(2:end, :) ./ prices(1:end-1, :) - 1;