How to make 30min intervals from 1min data taking the previous value for NaNs?

조회 수: 1 (최근 30일)
Hello,
I have a matrix of type double(below).
It consists of 1min stock prices (StockA, StockB, etc.).
Year Mo Day Hour Min StockA StockB
2012 3 22 9 58 NaN 9.98
2012 3 22 9 59 NaN 9.40
2012 3 22 10 00 NaN NaN
2012 3 22 10 01 NaN 9.59
2012 3 22 10 02 NaN NaN
2012 3 22 10 03 10.03 9.91
2012 3 22 10 04 NaN NaN
I would like to aggregate the data in 30min intervals in such a way that if no price is available for exact 09.30,10.00, 10.30 and so on, it takes the last available value before 9.30,10.00, 10.30 and so on column by column.
Could you please advise how can I do that?
I tried to create 30mins intervals separately and use min(abs... function, but it seems not to work for the dates.
  댓글 수: 4
Azzi Abdelmalek
Azzi Abdelmalek 2016년 5월 8일
What to do if all values between 10h:30 and 11h are nan
Ekaterina Serikova
Ekaterina Serikova 2016년 5월 8일
Then just NaN, because no tradings were during these 30 mins

댓글을 달려면 로그인하십시오.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2016년 5월 8일
편집: Azzi Abdelmalek 2016년 5월 8일
% -----------Example------------------------
v1=datenum('01-01-2016','dd-mm-yyyy');
pas=1/(24*60);
v=datestr(v1:pas:v1+pas*89);
n=size(v,1);
w=randi(10,n,2);
w(randperm(n*2,fix(3*n/2)))=nan;
A=[datevec(v) w];
%------------------------------------
m=size(A,2);
nn=30;
B=permute(reshape(A',m,nn,[]),[2 1 3]);
for k=1:size(B,3)
ii1=min([nn nn-find(~isnan(flipud(B(:,7,k))),1)+1])
ii2=min([nn nn-find(~isnan(flipud(B(:,8,k))),1)+1])
B(nn,7,k)=B(ii1,7,k)
B(nn,8,k)=B(ii2,8,k);
end
out=reshape(permute(B,[2 1 3]),size(A,2),size(A,1))'
  댓글 수: 6
Ekaterina Serikova
Ekaterina Serikova 2016년 5월 10일
Dear Azzi, could you please also explain what is the idea that 30mins values are in 29th and 59th minutes? In this case, if there is a price at exactly 30th minute, it will count it for the next 30 mins instead of just leaving it. How can I adjust the code so that 30th and 00th values were included? Thanks a lot.
Ekaterina Serikova
Ekaterina Serikova 2016년 5월 10일
Dear Azzi,ok, I solved that by taking 30th and 00th value and adding a condition that if that is NA, then take the previous (which is 29th). Another question - this code works only for matrices, the length of which are multiples of 30. Thus, I have to delete some 29th and 30th lines containing the values that I also need. How can I still save these values?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

the cyclist
the cyclist 2016년 5월 8일
I think this does what you want. I've commented it to show the algorithm.
% Create some pretend prices. Use your actual price matrix here.
% Needs to be multiple of 30 in length along dimension 1.
% If it isn't, trim as needed.
originalPriceMatrix = reshape(1:240,60,4);
% Set last-minute price to NaN for testing
originalPriceMatrix(end,:) = NaN;
% Reshape to get each 30-minute segment along dimension 1
price = reshape(originalPriceMatrix,30,2,4);
% Carry forward price if NaN.
lastMinutePrices = price(1,:,:);
for nt = 2:30
thisMinutePrices = price(nt,:,:);
thisMinutePrices(isnan(thisMinutePrices)) = lastMinutePrices(isnan(thisMinutePrices));
lastMinutePrices = thisMinutePrices;
end
% Permute to get (time X stock)
lastMinutePrices = permute(lastMinutePrices,[2 3 1]);
  댓글 수: 1
Ekaterina Serikova
Ekaterina Serikova 2016년 5월 8일
편집: Ekaterina Serikova 2016년 5월 8일
Dear the cyclist, the output does not seem to give "time X stock" result, as you indicated. I get lastMinutePrices=
30 90 150 210
59 119 179 239

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Time Series Objects에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by