File Exchange

Tick2Bar

version 1.0 (2.76 KB) by

Divides a time series into periods and returns high, low, open, close, volume.

Updated

TICK2BAR
[T,H,L,O,C,V]=Tick2Bar(t,p,v,g)
Divides a time series into periods of g days
(may be less than one day).
It is useful in conjunction with HIGHLOW
[T,H,L,O,C,V]=Tick2Bar(t,p,v,g);
highlow(H(:),L(:),C(:),O(:),'k',T(:));

It returns:

T date/time of the period
H high of the period
L low of the period
O open (first datum of the period)
C close (last datum of the period)
V volume of the period

given

t date/time
p price
v volume
g time granularity [days]

USAGE

% test data
N=1e4;
g=1/(24*60*60); % 1 minute granularity
t=([1:N]+rand(1,N)).*g; % time
p=100+rand(1,N).*10; % price
v=1e4.*rand(1,N); % volume
a=randperm(N);
v(a(1:10))=nan;

g=1/(24*60*60); % 1 minute granularity
[T,H,L,O,C,V]=Tick2Bar(t,p,v,g);

subplot(6,1,1:2);
plot(t,p,'k');
datetick;
title('plot(t,p,''k'');')

subplot(6,1,3);
plot(t,v,'k');
datetick;
title('plot(t,v,''k'');');

subplot(6,1,4:5);
highlow(H(:),L(:),C(:),O(:),'k',T(:));
datetick;
title('highlow(H(:),L(:),C(:),O(:),''k'',T(:));');

subplot(6,1,6);
plot(T,V,'k');
datetick;
title('plot(T,V,''k'');');

h=suptitle('[T,H,L,O,C,V]=Tick2Bar(t,p,v,g);');
set(h,'FontSize',get(h,'FontSize').*2, ...
'FontWeight','Bold');

IT'S NOT FANCY BUT IT WORKS

mathworks2011

mathworks2011 (view profile)

sorry - copied an old version in by mistake. Pls see below:

% error check
[t,tix]=sort(t);
p=p(tix);
vix=~isnan(p);
t=t(vix);
p=p(vix);

% create bins
mySynchTime=[floor(min(t)):g:ceil(max(t))];
[n,bin]=histc(t,mySynchTime); % n is the number of obs per bin

% pre-allocate
C=NaN(numel(n),1);
for i = 1: length(mySynchTime)
if(i==1)
from = 1;
elseif(i == length(mySynchTime))
from = cumsum(n(1:i-1));
from = from(end);
else
from = cumsum(n(1: i-1));
from = from(end) + 1;
end
to =cumsum(n(1:i));
to = to(end);

% These are the index numbers you are accessing
%disp([num2str(from) ' ' num2str(to)]);

%Observe which timestamps you are taking.
%disp(datestr(t(from:to)));

% As you are interested in the close, just take the last value
tmp = p(from:to);
if(isempty(tmp) && i ~=1)
%we have no observations in that window. Thus no price change.
tmp = C(i-1,1);
elseif(isempty(tmp) && i ==1)
%Special case. we are right at the start.
tmp = p(1);
end
C(i,1) = tmp(end);
clear tmp;
end

% Make sure you align correctly!

end

mathworks2011

mathworks2011 (view profile)

a good effort. Doesnt support synchronous data though. ie you may have a security which trades infrequently, yet you still want to map it on a discrete grid:

% error check
[t,tix]=sort(t);
p=p(tix);
vix=~isnan(p);
t=t(vix);
p=p(vix);

% create bins
mySynchTime=[floor(min(t)):g:ceil(max(t))];
[n,bin]=histc(t,mySynchTime); % n is the number of obs per bin

% pre-allocate
C=NaN(n,1);
for i = 1: length(mySynchTime)
if(i==1)
from = 1;
to = cumsum(n(1:i));
to = to(end);
elseif(i == length(mySynchTime))
from = cumsum(n(1:i-1));
from = from(end);
to =cumsum(n(1:i));
to = to(end);
else
from = cumsum(n(1: i-1));
from = from(end) + 1;
to =cumsum(n(1:i));
to = to(end);
end
% These are the index numbers you are accessing
%disp([num2str(from) ' ' num2str(to)]);

%Observe which timestamps you are taking.
%disp(datestr(t(from:to)));

% As you are interested in the close, just take the last value
tmp = p(from:to);
if(isempty(tmp))
%we have no observations in that window. Thus no price change.
tmp = C(i-1,1);
end
C(i,1) = tmp(end);
clear tmp;
end

% Make sure you align correctly!

end

Robert