MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

# Write a date loop in matlab

Asked by Kevin van Berkel on 23 Apr 2013

Hello all,

I am very new to Matlab so I hope you guys can help me out.

The thing is,

I have to create a loop which calculate portfolio means on an annual basis. The assets in the portfolio are mkt, hml and mom. The starting date is 19261103(yyyymmdd), ending date is 20121231 (yyyymmdd). So from 19271103 onwards, I need to retrieve the means per asset.

My problem is that I am just not able to create a loop that proceeds onwards, because not every year November 3rd, is a trading day (it can be a Saturday or a Sunday). How can I avoid this problem and retrieve annual 1x3 matrices from 1927 till 2012?

Hope I formulated this problem well.

Thank you very much in advance.

Kevin

Jan Simon on 23 Apr 2013

You have explained, what is not working as wanted: You want a loop over dates, but some dates are not valid. But you did not explain, what shgould happen instead: When a specific date is not wanted, which date should be used instead? How can the validity be determined automatically? Is it only the weekend, or are there more exceptions? If you calculate the values on an "annual basis", why do certain days matter? I though this would concerns complete years only.

Kevin van Berkel on 23 Apr 2013

Hello Jan,

I forgot to mention that in my raw data weekends are already excluded. So a series can for example be:

Nov 3, 1926 Nov 4, 1926, Nov 5, 1926 Nov 9, 1926, and so on.

if November 3, 19XX is not available, than it should be Nov 4 or Nov 5. What matters is that this should always be the startingdate of a new year.

Indeed, it only concerns complete years, so I need to get the average of a whole year. And whether this year ends on November 3 or November 4/5 does not matter.

Do you understand my problem now?

## Products

No products are associated with this question.

Answer by Azzi Abdelmalek on 23 Apr 2013
```d1=datenum('1926-11-03','yyyy-mm-dd'),
d2=datenum('2012-12-31','yyyy-mm-dd'),
d=d1:d2
for k=1:numel(d)
%do
end
```

Jan Simon on 23 Apr 2013

This is a loop of the the period of time with step size 1 day. How does this concern the question? It is a loop and it contain dates, but the actual problem does not appear in any way.

Kevin van Berkel on 23 Apr 2013

Hello Azzi, thank you for your attempt to solve the problem. However, as Jan said, it constructs date ID's with one day steps.

With some adjustments I came a bit closer solving my problems.

Look at this:

```d1=datenum('1926-11-03','yyyy-mm-dd'),
d2=datenum('1927-11-03','yyyy-mm-dd'),
d=d1:d2
for k=1:numel(d)
y1927 = [mean2(mkt) mean2(hml) mean2(mom)]
end
```
```d2=datenum('1927-11-03','yyyy-mm-dd'),
d3=datenum('1928-11-03','yyyy-mm-dd'),
d=d2:d3
for k=1:numel(d)
y1928 = [mean2(mkt) mean2(hml) mean2(mom)]
end
```

First problem that comes up is that the returns yielded by Matlab are returns from the whole period 1926-2012, for every asset. How to adjust the code so that for every year November 3, 1926- November 3 1927 the means are given in separate 1x3 matrices?

The next problem is: by continuing like this, I have to repeat this step about 100 times. How can I compress it to a smaller amount of steps?

Thanks again!

Azzi Abdelmalek on 23 Apr 2013

Jan, it was not my intention to give a complete answer, because it was not completly clear for me. I gave a hint that could help.

Answer by Andrei Bobrov on 23 Apr 2013
Edited by Andrei Bobrov on 23 Apr 2013
```d = [19261103; 20121231];
ddte = datenum(num2str(d),'yyyymmdd');
ndte = (ddte(1):ddte(2))';
t = weekday(ndte);
ndte = ndte(t ~= 1 & t ~= 7);
yourdata = [ndte, randi(125,numel(ndte),3)]; % create your data as
% yourdata =  [date,mkt,hml,mom]
```
```[yy,mm,dd] = datevec(yourdata(:,1));
ymd = [yy,mm,dd];
im = mm == 11 & dd >= 3;
ii = strfind([~im(1),im(:)'],[0 1]);
sb = zeros(numel(ndte),1);
sb(ii) = 1;
sbc = cumsum(sb);
t = sbc > 0 & sbc ~= max(sbc);
sbb = sbc(t);
sb1 = find(sb(t));
wdta = yourdata(t,:);
[r, c] = ndgrid(sbb,1:size(wdta,2)-1);
out1 = accumarray([r(:) c(:)],reshape(wdta(:,2:4),[],1),[],@mean);
out = [ymd(ii(1:end-1),:),out1] ;
```

## 1 Comment

Kevin van Berkel on 23 Apr 2013

Hello Andrei,

Thank you so much. This code works perfect. By mistake a accepted Azzi's codde, but ofcourse it has to be your code that must be accepted!

Thanks again for your effort, I can proceed!