E.D.
E.D.

Reputation: 329

Speed upgrade for loop

I am analysing intra-day volume profiles on stocks. I have built a (rough) piece of code that does 2 things well, but slowly. One stock can have north of 200k trades over a given period and I want to analyse around 200 stocks.

My code looks over 3 months' worth of trade data, binning the data into 10 minute buckets for each day. I do this to make sure a stock trades at least x value per bucket. I then aggregate the intra day buckets to just time buckets to get a sense of the average volume distribution.

Code sample below just shows how I bin data and then aggregate by bin:

% Totals by time bucket

for i = 1:size(VALUE,1)
    MyDay = day(datenum(sprintf('%d',VALUE(i,1)),'yyyymmdd'));
    MyMonth = month(datenum(sprintf('%d',VALUE(i,1)),'yyyymmdd'));
    MyYear = year(datenum(sprintf('%d',VALUE(i,1)),'yyyymmdd'));
    StartHour = hour(VALUE(i,2));
    StartMinute = minute(VALUE(i,2));
    EndHour = hour(VALUE(i,3));
    EndMinute = minute(VALUE(i,3));
    if StartMinute ~= 50
        t = (day(MyTrades(:,1)) == MyDay & month(MyTrades(:,1)) == MyMonth & year(MyTrades(:,1)) == MyYear & hour(MyTrades(:,1)) == StartHour & minute(MyTrades(:,1)) >= StartMinute & minute(MyTrades(:,1)) <= EndMinute);
    else
        t = (day(MyTrades(:,1)) == MyDay & month(MyTrades(:,1)) == MyMonth & year(MyTrades(:,1)) == MyYear & hour(MyTrades(:,1)) == StartHour & hour(MyTrades(:,1)) < EndHour & minute(MyTrades(:,1)) >= StartMinute);
    end    
    tt = MyTrades(t,:);
    MyVALUE(i,1) = sum(tt(:,5));
end  





% Aggregate totals

for ii = 1:50
    VWAP(ii,1) = datenum(0,0,0,9,0,0)+datenum(0,0,0,0,10,0)*ii-datenum(0,0,0,0,10,0) ;
    VWAP(ii,2) = datenum(0,0,0,9,0,0)+datenum(0,0,0,0,10,0)*ii;
    StartTime = VWAP(ii,1);
    temp = (VALUE(:,2) == StartTime);
    temp2 = VALUE(temp,:);
    VWAP(ii,3) = sum(temp2(:,4))/100;
end

Is there a more elegant and (more importantly) faster way of calculating these types of "brute force" analyses?

Upvotes: 1

Views: 121

Answers (1)

Lesto
Lesto

Reputation: 2299

instead of using a complex data like DateNumber, use the timestamp and make the dateenum only one time for value.

you'll have to rewrite completly your code, but thinking with timestamp is more computing (and DB) friendly

Here some help to convert from timestamp in DateNumber: http://www.mathworks.it/matlabcentral/newsreader/view_thread/119237

Upvotes: 1

Related Questions