Effacer les filtres
Effacer les filtres

Calculation of monthly returns

3 vues (au cours des 30 derniers jours)
Tobi
Tobi le 2 Sep 2013
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

Réponses (2)

Iain
Iain le 2 Sep 2013
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 commentaires
Tobi
Tobi le 2 Sep 2013
Hi lain,
thank you!
So it was a was stupid mistake.. I change the code a bid and now it works more than before! But only the returns for the first stock are calculated.. It seems that the first loop (i) is ignored..
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);
Dates = datevec(Dates_daily);
% 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<=r-1 && Dates(h,2)~= Dates(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
Can you help me once again?
Thank you very much!
Iain
Iain le 2 Sep 2013
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?

Connectez-vous pour commenter.


Tobi
Tobi le 2 Sep 2013
Hi lain,
thank you!
So it was a was stupid mistake.. I change the code a bid and now it works more than before! But only the returns for the first stock are calculated.. It seems that the first loop (i) is ignored..
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);
Dates = datevec(Dates_daily);
% 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<=r-1 && Dates(h,2)~= Dates(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
Can you help me once again?
Thank you very much!
  1 commentaire
Iain
Iain le 2 Sep 2013
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?

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by