An alternative method to this is to use window join, wj1
, which allows you to pass in custom windows for each time and perform an aggregation on the data within that window. A simplified explanation of the syntax is:
wj[window pairs;common columns;table 1;(table 2;(function;column))]
Taking the following tables as an example:
q)trade / simplified trade table
time sym
---------
09:00 a
09:30 a
10:00 a
q)quote / simplified quote table
time sym px
------------------
09:12 a 9.420396
09:29 a 6.416515
10:07 a 8.53406
To sums all the quote
prices within a 20 minute window either side of the trade
time for each sym
we use the following method. First create pairs of start and end times for the windows:
q)show window:-20 20+\:x`time
08:40 09:10 09:40
09:20 09:50 10:20
Where each list has the same length as the trade
table. Then pass this in to wj
, alongside the aggregation function sum
for the prices px
:
q)wj1[window;`sym`time;x;(y;(sum;`px))]
time sym px
------------------
09:00 a 9.420396
09:30 a 15.83691
10:00 a 8.53406
To investigate the values being aggregated in each window we can use the null function ::
:
q)wj1[window;`sym`time;x;(y;(::;`px))]
time sym px
---------------------------
09:00 a ,9.420396
09:30 a 9.420396 6.416515
10:00 a ,8.53406
It should be noted that wj1
only considers value inside the window, whereas wj
considers prevailing values to be part of the window.