0

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.

Justin Fletcher
  • 2,319
  • 2
  • 17
  • 32
  • Did you try your code? Does it work? – nagyben Apr 25 '14 at 23:37
  • Tom, are you willing modify the input array architecture significantly? If so, I can give you a solution that's very robust, and will open up a lot of possibilities for interesting analysis. I's a struct-based solution, and i works very well for this sort of thing. Let me know if you're interested and I'll composes an answer. – Justin Fletcher Apr 25 '14 at 23:38
  • Exantas I did try my code, but took forever for one stock I was wondering if there was a better way to calculate the monthly return... Fletch if it helps me to get what i want i am willing to change the input or program some code that gives me the input you want to work with. In advance thank you very much. Best, Tom. – user3277835 Apr 26 '14 at 15:59

0 Answers0