How to full vectorize this?
I write this little piece of code to syncronize two financial time series.
I download some forex data and there are some missing trades. The ideia
here is to get the biggest set and syncronize the others with this one.
For example i have a set with like this
a= [20010110 2310 10;
20010110 2311 20;
20010110 2313 30]
b= [20010110 2309 50;
20010110 2312 52]
and i want then i get this
c =[20010110 2310 50;;
20010110 2311 50
20010110 2313 52]
c is pretty much the same thing as a, but this is only a index.
so i write this
function [setAjustado] = ajustar(SetCorreto,SetParaAjustar)
dataCorreto = SetCorreto(:,1); % get the date from the correct set
dataAjustar = SetParaAjustar(:,1); % get the date from the set to be
corrected
minCorreto = SetCorreto(:,2); % get the timeframe from the correct set
minAjustar = SetParaAjustar(:,2);get the timeframe from the set to be
corrected
setAjustado = zeros(size(SetCorreto)); %corrected set
idxI = dataAjustar == dataCorreto(1); %generating the first range to
search
for i=2:size(SetCorreto,1)
try
if (i >1 && dataCorreto(i) ~= dataCorreto(i-1)) % if the dates are
the same, i dont need to look for the range again
idxI = dataAjustar == dataCorreto(i); % generate the range to search
idxIa = find(idxI==1,1); % find the first index
end
idx = find(minAjustar(idxI)>=minCorreto(i),1) +idxIa; % find the
nearest occurency in the set to be corrected to match the correct
set
setAjustado(i,:) = SetParaAjustar(idx,:); %replace all the line.
This line have prices close, max, low and open.
setAjustado(i,2) = minCorreto(i); %adjust the timeframe to match
the correct set
catch
if i==1 % in case of i to be greater then the size of set to be
corrected
a=i;
else
a= i-1;
end
setAjustado(i,:) = setAjustado(a,:); % will copy the last line
created in corrected set
end
end
But im thinking this thing is pretty slow... Can someone help me to speed
this thing?
Tks in advance!
No comments:
Post a Comment