kdb+ 测试以检查我们没有来回翻转

kdb+ tests to check we're not flipping back and forth

我们如何知道符号是否来回翻转? 一旦 syms 更改为其他 sym,它不应返回到历史记录中先前出现的符号。 每次符号 flips/changes 到新符号时,新符号应该是唯一的并且不应该出现在以前的符号中。

我们想要的是:

  1. 如果发生翻转,我们希望有一个布尔值列 1b
  2. 我们希望另一个布尔值列 1b 发生翻转并且 sym 来回翻转。 (当前符号已经出现在之前或未来的日期符号中。)
//To create a sample table
tmp:([]sdate:`date$();sym:`symbol$();name:`symbol$());
{`tmp insert (x;`VXV2;`someName1)} each tdate;
{`tmp insert (x;`VXJ2;`someName2)} each tdate;
{`tmp insert (x;`VXG8;`someName3)} each tdate;
{`tmp insert (x;`VXZ4;`someName4)} each tdate;

//update the dataset to get the scenario
tmp:`sdate`sym xasc tmp;
tmp:update sym:`VXG8 from tmp where sdate=2020.04.11;
tmp:update sym:`VXN6 from tmp where sdate>=2020.04.21;

//the below query is we are interested in to find the rollover and sym flipping back and forth.
select from tmp where name=`someName1

//notes
from 2020-03-30 till 2020-04-10 then sym is VXV2
from 2020-04-11 till 2020-04-11 then sym is VXG8  // the sym was updated to VXG8  
from 2020-04-12 till 2020-04-20 then sym is VXV2  // the sym was flipped back to VXV2 from VXG8
from 2020-04-21 till 2021-03-29 then sym is VXN6  // the sym was updated to VXN6  

您可以在列表上使用 differ 到 return 它发生变化的地方。对于第二列,您还可以使用 differ 并过滤掉列表中第一次出现的元素。

q)list:`A`A`A`A`B`A`A`A`C`C
q)differ list
1000110010b
q)differ[list] and not @[count[list]#0b; list?distinct list; :; 1b]
0000010000b

这完全取决于您希望布尔值的外观 - 翻转时只有一个真布尔值或每个重复符号的所有真布尔值 - 但这是前者:

update roll:differ sym,dup:differ[sym]&not i=group[sym][;0]sym from select from tmp where name=`someName1

类似于 Cathans 的回答