Reputation: 145
I have the following data/sets (simplified version to make the example clear):
3*10
matrix of stocks identified by a number for a given period (3 stocks (my portfolio) in rows * by 10 days (in columns) named indexBest.10*10
matrix of returns for each period for each security in my universe named dailyret (my universe of stocks is 10).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
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