I am working with daily returns of 100 stocks from 1970 to 2012. The thing is that I want to calculate the monthly return of each stock in every year . The data I have is a cell array 1x100 stocks, and for each stock cell I have another cell array which contains the dates, a vector of doubles with the stock returns for every single date and cell array with the ticker of the stock. To solve the problem I was thinking of doing the following:
for r=1:1:100
name=res.ticker{1,r};
for t=1:1:length(res{1,r}.dates)
for j=1970:1:2012
fecha=datestr(res{1,r}.dates(t),'yyyy/mm/dd');
year =fecha(1:4);
month = fecha(6:7);
anio= num2str(j);
if (strcmp(anio,year)==1)
switch month
case '01'
if (isnan(res{1,r}.cs(t))~=1)
return1=[return1;res{1,r}.cs(t)];
end
case '02'
if (isnan(res{1,r}.cs(t))~=1)
return2=[return2;res{1,r}.cs(t)];
end
.
.
.
case '12'
if (isnan(res{1,r}.cs(t))~=1)
return12=[return12;res{1,r}.cs(t)];
end
end
else
prom1=mean(return1);
prom2=mean(return2);
.
.
.
prom12=mean(return12);
ansyear=[prom1;prom2;...;prom11;prom12];
end
end
end
end
The result I want is a cell array 1x100 where each cell have 1x42 (where those are the 42 years of data I have) and inside of those 42 cells a 12x1 vector with the mean of the returns of each months. I hope I make myself clear, in advance thank you very much. Best, Tom.