我正在尝试对自动收报功能中的选择进行以下操作:
// 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;
)];
}];
但是,它不能用作trds reference made in the pivot function is not picked up, presumably because the trds selection is not global, however whilst using trds (without
)作为参数,因此piv函数不会以某种方式被应用,并返回结果,就好像没有执行任何操作一样。此外,必须使用ptrds创建一个附加变量似乎效率低下,并且还会引发参考错误。有人可以建议我如何有效地以规范的方式实现此逻辑。谢谢
答案 0 :(得分:3)
我认为您错误的根源是您的最终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;]