Interesting to see is that the speed up is coming from the vectorization of the vwap calculation. It does not have anything to do with aj.<o:p></o:p>
<o:p> </o:p>
q)N:100000<o:p></o:p>
q)time:asc N?.z.t;prx:N?100+0.01*1+N?100;qty:1+N?1000<o:p></o:p>
q)trades:(time;prx;qty)<o:p></o:p>
<o:p> </o:p>
q)\t:10 t:update sqty:sums qty,sqtyprx:sums qty*prx from trades; a:(select time from t),'select vwap:sqtyprx%sqty from (sqtyprx
sqty#t)-0^sqtyprx
sqty#aj[`time;select time:time-00:01 from t;t]<o:p></o:p>
466<o:p></o:p>
q)\t:10 b:update vwap:{(x - 0^x z) % y- 0^y z}[sums qty*prx;sums qty]{ x bin (x - 00:01)}time from trades<o:p></o:p>
420<o:p></o:p>
q)a[vwap]~b
vwap<o:p></o:p>
1b<o:p></o:p>
<o:p> </o:p>
Kim<o:p></o:p>
<o:p> </o:p>
Von: personal-kdbplus@googlegroups.com [mailto:personal-kdbplus@googlegroups.com] Im Auftrag von Joshua Shields (AquaQ Analytics)
Gesendet: Montag, 25. Januar 2016 16:01
An: Kdb+ Personal Developers
Betreff: [personal kdb+] Re: Sliding window computation in tick table with irregular timestamp<o:p></o:p>
<o:p> </o:p>
Hi Gary,<o:p></o:p>
<o:p> </o:p>
Another way of dealing with this question is to use aj, which should perform faster.<o:p></o:p>
<o:p> </o:p>
Setting:<o:p></o:p>
q)N:100000<o:p></o:p>
q)time:asc N?.z.t;prx:N?100+0.01*1+N?100;qty:1+N?1000<o:p></o:p>
q)trades:(time;prx;qty)<o:p></o:p>
<o:p> </o:p>
To get the vwap use:<o:p></o:p>
<o:p> </o:p>
q)t:update sqty:sums qty,sqtyprx:sums qty*prx from trades; <o:p></o:p>
q)(select time from t),'select vwap:sqtyprx%sqty from (sqtyprx
sqty#t)-0^sqtyprx
sqty#aj[`time;select time:time-00:01 from t;t]<o:p></o:p>
<o:p> </o:p>
Breaking this down <o:p></o:p>
<o:p> </o:p>
t:update sqty:sums qty,sqtyprx:sums qty*prx from trades / create a new table t with running sums of qty and qty*prx<o:p></o:p>
(sqtyprx
sqty#t)-0^sqtyprx
sqty#aj[`time;select time:time-00:01 from t;t] / use aj to create running sum values stopping 1 minute before the actual time of the record<o:p></o:p>
/ and subtract it from the original running sums at that time<o:p></o:p>
/ this gives the total sums for the preceeding minute<o:p></o:p>
<o:p> </o:p>
(select time from t),'select vwap:sqtyprx%sqty from (sqtyprx
sqty#t)-0^sqtyprx
sqty#aj[`time;select time:time-00:01 from t;t]<o:p></o:p>
<o:p></o:p>
We can then use this to calculate the vwap for the preceeding minute at each trade record, and join back to its original time.<o:p></o:p>
<o:p> </o:p>
I hope this makes sense.<o:p></o:p>
<o:p> </o:p>
Josh<o:p></o:p>
–
Submitted via Google Groups