3.5

3.5 | 2 ratings Rate this file 19 Downloads (last 30 days) File Size: 2.76 KB File ID: #3398
image thumbnail

Tick2Bar

by Michael Robbins

 

07 May 2003 (Updated 30 Jul 2003)

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

| Watch this File

File Information
Description

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
% add some test-NANs
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

MATLAB release MATLAB 6.5 (R13)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (3)
17 Nov 2009 Robert  
17 Oct 2011 mathworks2011

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:

function trade =cce_Tick2Bar_synchronous(t,p,g)
% 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!
trade = timeseries(C(1:end-1), mySynchTime(2:end));

end

17 Oct 2011 mathworks2011

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

function trade =cce_Tick2Bar_synchronous(t,p,g)
% 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!
trade = timeseries(C(1:end-1), mySynchTime(2:end));

end

Please login to add a comment or rating.
Updates
29 Jul 2003

Speed increases on the order of 10x (input data with NaN's may produce different output than before change.

30 Jul 2003

Speed increases on the order of 10x (input data with NaN's may produce different output than before change.

Tag Activity for this File
Tag Applied By Date/Time
finance Michael Robbins 22 Oct 2008 07:01:09
modeling Michael Robbins 22 Oct 2008 07:01:09
analysis Michael Robbins 22 Oct 2008 07:01:09
highlow hloc time series volume high low open close finance plot Michael Robbins 22 Oct 2008 07:01:09

Contact us at files@mathworks.com