# How can i speed up this code

2 views (last 30 days)
Leonardo Hermoso on 17 Apr 2013
Hello fellows,
I need some help to speed up my code.
Its basically a backtesting system, my array have 32M elements, and in my calculation the script will take 17 days to end the processing.
The code look for some patterns in matrix padroes, read each line and find the pattern in other array called direcao
in this code i look for 10 pips in 10 minutes and a maximum stop loss of 5 pips.
here is the code
if true
barra = waitbar(0,'Iniciando Backtests');
nAcertos=0;
incremento = 0.1/10000;
waitbar(perc,barra,sprintf(' %f%%concluidos',perc))
if ( length(posicoes) >=30)
nAcertos=0;
for j=1:length(posicoes)
% Here i calculate what is the minutes thats ends the array
% for example i want to close the trade in 10 minutes and
% the minutes found in the array is 40 so i ll look for the
% next occurrency o of (40+11) 51, 10 minutes of trade
if ( minutos(posicoes(j)+15) + 11 <60 )
quantosMinutos = minutos(posicoes(j)+15) + 11;
else
quantosMinutos = (minutos(posicoes (j) +15 )) +11 - 60 ;
end
if ( posicoes(j) +2000> length(direcao))
break;
end
r= find( minutos(posicoes(j)+15: posicoes(j) +2000)>quantosMinutos,1,'first');
if (~isempty(r) && ( horas( r +(posicoes(j)+15 )) - horas(posicoes(j)+15) ) <2 )
posicaoFinalVetor = r + posicoes(j)+15 ;
end %
r=[];
resultado = bid(posicoes(j)+16)+ (10/10000) ; % here i calculate what is my price for profit.. and the look its index in the array
if ( ~isempty(r))
posicaoGain = r+ posicoes(j)+15;
% sprintf('Operação de compra')
r=[];
resultado = bid(posicoes(j)+15)- 0.0005;here i calculate what is my price for stop.. and the look its index in the array
% [~,ia,~] = unique(c,'first');
if ( ~isempty(r) )
posicaoStop = r + posicoes(j)+15;
else
posicaoStop = posicaoGain +1 ;
% sprintf('Gain Atingido %d',bid(posicaoGain))
end
% if posicaogain< posicaostop i assume that this is a winning
if ( posicaoGain < posicaoStop)
nAcertos = nAcertos+1;
end
if ( (nAcertos + (length(posicoes) -j) /length(posicoes) < 0.6))
break;
end
end
end
achou=0;
% nAcertos
% length(posicoes);
%((nAcertos / length(posicoes)))*100
if ((nAcertos / length(posicoes) >=0.60)&& nAcertos ~= 0)
nAcertos =0;
end
nAcertos =0;
stopou =0;
end
end
close(barra) end
I appreciate the help in advance, thank you!