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

1 visualización (últimos 30 días)
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 comentarios
Azzi Abdelmalek
Azzi Abdelmalek el 8 de Mayo de 2016
What to do if all values between 10h:30 and 11h are nan
Ekaterina Serikova
Ekaterina Serikova el 8 de Mayo de 2016
Then just NaN, because no tradings were during these 30 mins

Iniciar sesión para comentar.

Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 8 de Mayo de 2016
Editada: Azzi Abdelmalek el 8 de Mayo de 2016
% -----------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 comentarios
Ekaterina Serikova
Ekaterina Serikova el 10 de Mayo de 2016
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 el 10 de Mayo de 2016
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?

Iniciar sesión para comentar.

Más respuestas (1)

the cyclist
the cyclist el 8 de Mayo de 2016
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 comentario
Ekaterina Serikova
Ekaterina Serikova el 8 de Mayo de 2016
Editada: Ekaterina Serikova el 8 de Mayo de 2016
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

Iniciar sesión para comentar.

Categorías

Más información sobre Time Series Objects en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by