Search code examples
databasekdb

Window Function in KDB+


I am working through an example from Q for Mortals (Section 9.9.9), and cannot understand how the window function is extracting the ask/bid price on the final row.

aapl 09:30:08 101 104 103 102 103 100 100

enter image description here

The window for the last trade @ 09:30:08 (price 101 from trades table) should be from 09:30:06 to 09:30:09. This is shown in the w variable (last column).

However when I run the window example (verbatim for the book) it includes askof 104 and bid of 103. I am not sure why this sample shows up as the timestamp is 09:30:05 which is outside the window range. Is this a bug or am I missing something here?


Solution

  • The wj function takes the prevailing quote at the start of each window to be valid for aggregations. If you want to only aggregate what is strictly within your windows use wj1 which has the same syntax as wj.

    Jeff Borror demonstrates this later in section 9.9.9 of Q for Mortals.

    The internal behavior of wj and wj1 is elaborated on code.kx.com.