Search code examples
kdb

KDB how to get previous business day


What is the most efficient way to find a previous' day entry in a table and join that entry?. So, if I have

t:flip `date`sym`close!(`t1`t1`t2`t2`t3`t3`t4`t4`t5`t5`t1`t1;`A`B`A`B`A`B`A`B`A`B`A`B; 5 10 6 11 5 11 4 12 5 11 6 13)

date    sym close
t1  A   5
t1  B   10
t2  A   6
t2  B   11
t3  A   5
t3  B   11
t4  A   4
t4  B   12
t5  A   5
t5  B   11
t1  A   6
t1  B   13

how can I get to

t:flip `date`sym`close`last_date`last_close!(`t1`t1`t2`t2`t3`t3`t4`t4`t5`t5`t1`t1;`A`B`A`B`A`B`A`B`A`B`A`B; 5 10 6 11 5 11 4 12 5 11 6 13; ```t1`t1`t2`t2`t3`t3`t4`t4`t5`t5; 0n 0n 5 10 6 11 5 11 4 12 5 11)

date    sym close   last_date   last_close
t1  A   5       
t1  B   10      
t2  A   6   t1  5.0
t2  B   11  t1  10.0
t3  A   5   t2  6.0
t3  B   11  t2  11.0
t4  A   4   t3  5.0
t4  B   12  t3  11.0
t5  A   5   t4  4.0
t5  B   11  t4  12.0
t1  A   6   t5  5.0
t1  B   13  t5  11.0

Solution

  • update last_date:prev date,last_close:prev close by sym from t