如何在KDB+/Q中的ticker函数中对表选择运行透视操作
我正在尝试对ticker函数中的选择运行透视操作,如下所示:如何在KDB+/Q中的ticker函数中对表选择运行透视操作,kdb,ticker,Kdb,Ticker,我正在尝试对ticker函数中的选择运行透视操作,如下所示: // Trades table trades:( []time:`timespan$(); sym:`symbol$(); exch:`symbol$(); side:`symbol$(); price:`float$(); size:`float$()); // Generic pivot function piv:{[t;k;p;v]f:{[v;P]`${raze "_" sv x} each string raze
// Trades table
trades:(
[]time:`timespan$();
sym:`symbol$();
exch:`symbol$();
side:`symbol$();
price:`float$();
size:`float$());
// Generic pivot function
piv:{[t;k;p;v]f:{[v;P]`${raze "_" sv x} each string raze P,'/:v};v:(),v; k:(),k; p:(),p;G:group flip k!(t:.Q.v t)k;F:group flip p!t p;key[G]!flip(C:f[v]P:flip value flip key F)!raze{[i;j;k;x;y]a:count[x]#x 0N;a[y]:x y;b:count[x]#0b;b[y]:1b;c:a i;c[k]:first'[a[j]@'where'[b j]];c}[I[;0];I J;J:where 1<>count'[I:value G]]/:\:[t v;value F]};
// Ticker function that "ticks" every 5 seconds
if[not system"t";system"t 5000";
.z.ts:{
// Selection is made from the trades table with the index reset
trds:0!select first_price:first price, last_price:last price, mean_size:avg size, volume:sum size, min_price:min price, max_price:max price by time:1 xbar time.second, exch, sym from trades;
// Check if selection returns at least one row and run pivot
if[(count trds)>0; (
ptrds:piv[`trds;`time;`exch`sym;`first_price`last_price`mean_size`min_price`max_price`volume];
show ptrds;
)];
}];
但是,这不起作用,因为未拾取pivot函数中的trds参考,可能是因为trds选择不是全局的,但是,当使用trds而不作为参数时,piv函数不知何故未应用,并返回结果,就好像未执行任何操作一样。此外,必须使用ptrds创建一个附加变量似乎是不必要的,并且还会引发一个引用错误。有人能告诉我如何才能以一种规范的方式有效地实现这个逻辑吗。谢谢我认为您的错误源于您最后的if语句中的括号。问题是,当多个参数被括在括号内时,对象会变成一个列表。在kdb中,列表的元素从右到左求值,例如
q)(a:10;a+15)
'a
[0] (a:10;a+15)
^
q)(a+15;a:10)
25 10
要解决您的问题,只需删除括号,使其显示
if[(count trades)>0;ptrds:piv[...];show ptrds;]
我认为你的错误源于你最后的if语句中的括号。问题是,当多个参数被括在括号内时,对象会变成一个列表。在kdb中,列表的元素从右到左求值,例如
q)(a:10;a+15)
'a
[0] (a:10;a+15)
^
q)(a+15;a:10)
25 10
要解决您的问题,只需删除括号,使其显示
if[(count trades)>0;ptrds:piv[...];show ptrds;]