Noob_1
Noob_1

Reputation: 145

How do I loop a portfolio return calculation for multiple periods in matlab?

I have the following data/sets (simplified version to make the example clear):

I want to create a loop where I am able to calculate the sum returns of each portfolio for each period into one matrix ideally 1*10 or 10*1 (returns * date or vice versa).

I have done this for a single period for a portfolio, see below: but I want to be able to automate this process instead of doing all this 10* over for each portfolio for each period. Please help!

Portfolio_1_L= indexBest(:,1); %helps me get the portfolio constituents for period one (3 stocks basically).
Returns_1_L= dailyret(Portfolio_1(:,1));%to calculate returns of the portfolio for period 1 I have referenced  the extraction of returns to the portfolio constituents.
Sum_ret_Port_1_L=sum(Returns_1_L); %sum return of the portfolio for period one

How do I loop this process for all other 9 periods?

Upvotes: 1

Views: 1043

Answers (1)

shoelzer
shoelzer

Reputation: 10708

Use a for loop and use the index variable in place of hardcoded 1 in your example code. Also index the output to store the result for each day.

for day = 1:10
    Portfolio_1_L = indexBest(:,day);
    Returns_1_L = dailyret(Portfolio_1_L);
    Sum_ret_Port_1_L(day) = sum(Returns_1_L);
end

Upvotes: 3

Related Questions