Search code examples
kdb

Are there any instances where a negative time type could give unexpected results?


Are there any instances where a negative time type could give unexpected results if used for specific purposes? When time deltas are calculated between negative time values and non-negative time values for example, there do not appear to be any issues.

time val
00:00:31.384 -0.3170017
00:06:00.139 0.9033492
00:07:01.099 -0.7661049

Then, for the purpose of a window join later over a 10-min window

win:00:10:00;
winForJoin: (neg win;00:00:00) +\:(exec time from data);

first[winForJoin] gives -00:09:28.616 -00:03:59.861 -00:02:58.901

winForJoin[1]-winForJoin[0] gives 10 minutes as expected


Solution

  • If I understand correctly, you're asking how would a window join behave if the opening interval was a negative time? (due to the interval subtraction taking the values into negative territory, relative to 00:00).

    The simple answer is that it won't behave any differently than if the times were numbers, but in practice you may see results you don't expect depending on how your table is set up and what you're trying to achieve.

    Taking the example in the official wiki as a starting point: https://code.kx.com/q/ref/wj/

    q)t:([]sym:3#`ibm;time:10:01:01 10:01:04 10:01:08;price:100 101 105)
    q)a:101 103 103 104 104 107 108 107 108
    q)b:98 99 102 103 103 104 106 106 107
    q)q:([]sym:`ibm; time:10:01:01+til 9; ask:a; bid:b)
    q)f:`sym`time
    q)w:-2 1+\:t.time
    
    /add volume too so it's easier to follow:
    q)s:908 360 522 257 858 585 90 683 90;
    q)update size:s from `q
    
    /add an alternative range which has negative starting time
    q)w2:(-11:00;1)+\:t.time
    

    The window join takes all rows in q whose times are between the pairs of time ranges:

    q)q[`time]within/:flip w
    110000000b
    011110000b
    000001111b
    

    Under the covers it's asking: are these positive numbers (the quote times) in between those two positive numbers (the window range). There's no reason it can't also ask: are these positive numbers in between this negative number and this positive number

    q)q[`time]within/:flip w2
    110000000b
    111110000b
    111111111b
    

    You'll notice that all of them are greater than the negative time - meaning that it will include all rows from the beginning of the q table, up until the end time of that pair. This can be considered expected behaviour - if your start time is negative you must mean "from the beginning of time" - aka, all rows from the beginning of the table.

    Comparing sum of size shows how the results differ:

    q)wj[w;f;t;(q;(sum;`size))]
    sym time     price size
    -----------------------
    ibm 10:01:01 100   1268
    ibm 10:01:04 101   1997
    ibm 10:01:08 105   1448
    q)wj[w2;f;t;(q;(sum;`size))]
    sym time     price size
    -----------------------
    ibm 10:01:01 100   1268
    ibm 10:01:04 101   2905
    ibm 10:01:08 105   4353
    

    Finally - where it might get complicated.....it depends on what "negative" time means in your table. If you're at 00:00 (midnight) and you subtract 10 minutes, are you trying to access data from 23:50 the day before? Or does 00:00 represent the starting time (row zero) of your table? If you're trying to access 23:50 from the day before then you will have problems because 23:50 is NOT in between your negative start time and your positive end time, e.g:

    q)23:50 within(-00:58:59;10:01:02)
    0b
    

    Again this all depends on how your data looks and what you're trying to do