Calculation of monthly returns
조회 수: 3 (최근 30일)
이전 댓글 표시
Hello,
I want to calculate monthly returns for single stocks. The input consists of a matrix which contains the single daily stocks values (stocks-> columns; daily values -> rows) and a vector which contains the single dates.
I want to calculate the return between two stock values if their month differ. So my loops log in the start value, which is the first value of each input and then search in the date vector for the first date which has a different month than the one which is logged in. Between those two values (the corresponding to the dates) the return should be calculated. After this the latest value, so the one which was the end value for previous return calculation should be the new start value...
My function gives me as output just the predefined matrix of zeros..
I don't think that my code has any big faults, but I'm new at MATLAB and therefore do not know if I made any syntax mistakes....
This is my code: if true
%Code
function [ Returns ] = Return_calc_month( Stock_Prices, Dates_daily )
% Calculation of monthly returns
% calculating monthly returns by checking if month of date differs from
% following date
% Input-matrix, size of matrix
A = size (Stock_Prices);
r=A(1,1); %rows input
b=floor((r/20)+20); %rows output
c=A(1,2); %columns = number of stocks
Returns=zeros(b,c);
% writer for output matrix
l=1;
% reader index
h=0;
%columns
for i=1:c
%rows of input matrix
for j=1:r-1
h=h+j;
%counter till month change
for k=1:31
if h+k<=j && Dates_daily(h,2)~= Dates_daily(h+k,2)
%calculate monthly return
Returns(l,i)=(Stock_Prices(h+k,i)/Stock_Prices(h,i))-1;
%increment writer by one
l=l+1;
%
h=h+k;
%set current month end as new beginning
end
end
end
end
end
end
Please help me :)
Thanks in advance! Tobi
댓글 수: 0
답변 (2개)
Iain
2013년 9월 2일
Your if statement condition looks hinky.
"h+k<=j" is ALWAYS false, because you've coded h to ALWAYS be higher or equal to j and for k to always be greater than one.
댓글 수: 2
Iain
2013년 9월 2일
Its the same problem. - That if condition is hinky, and looks like it will stay so if you keep using "h" in that way. Why are you setting h = h + j?
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!