kdb 2-way asof join(合并 bid/ask 表)

kdb 2-way asof join (merge bid/ask tables)

我使用了 http://code.kx.com/svn/kx/kdb+/tick/tba.q 中定义的模式:

贸易:买价:卖价:([]时间:timespan$();sym:symbol$();价格:float$();size:int$())

我想加入 bid 并要求 tables 变成单引号 table 但不知道该怎么做。 aj[] 仅使用 table 的时间列之一,但我需要合并两个时间列。

例如

出价

time     size price
----------------
10:00:00 10 0.05
10:00:03 10 0.06
10:00:06 20 0.06

询问

time     size price
----------------
10:00:00 10 0.06
10:00:02 10 0.07
10:00:04 20 0.07

结果应该是:

time      bs bprc  as aprc
--------------------------
10:00:00  10 0.05  10 0.06
10:00:02  10 0.05  10 0.07
10:00:03  10 0.06  10 0.07
10:00:04  10 0.06  20 0.07
10:00:06  20 0.06  20 0.07

有人在使用这个 tba 模式吗?它更加紧凑并且显着节省磁盘 space 但我发现它在查询方面并不是很灵活。如果我想获得每天的平均点差,我会怎么做?

谢谢!

您不需要 aj 来实现您想要的。 uj 结合 fills 将完成工作:

b: 1!`time`sym`bs`bprc xcol bid / convert bid to a keyed table
a: 1!`time`sym`as`aprc xcol ask / convert ask to a keyed table
ba: `time xasc b uj a / join them and sort by time
/ forward fill the missing prices
select time, sym, fills bs, fills bprc, fills as, fills aprc from ba

希望对您有所帮助。

更新。如果您需要支持多个符号,那么您应该按 time sym 分组,如@user2393012 所建议:

b: 2!`time`sym`bs`bprc xcol bid / convert bid to a keyed table
a: 2!`time`sym`as`aprc xcol ask / convert ask to a keyed table

然后:

ba: `time xasc b uj a / no change here
ungroup select time, sym, fills bs, fills bprc, fills as, fills aprc by sym from ba

我们的想法是,您仅将 fills 应用于每个组内的值,以防止价格泄漏到交易品种边界之外。

您可以使用联合联接来完成此操作。

q)bid
time                 sym  bsize bprice
--------------------------------------
0D10:00:00.000000000 AAPL 10    0.05
0D10:00:03.000000000 AAPL 10    0.06
0D10:00:06.000000000 AAPL 10    0.06

q)ask
time                 sym  asize aprice
--------------------------------------
0D10:00:00.000000000 AAPL 10    0.06
0D10:00:02.000000000 AAPL 10    0.07
0D10:00:04.000000000 AAPL 10    0.07

q)0!update bsize:fills bsize, bprice:fills bprice, asize:fills asize, aprice:fills aprice from `time xasc (2!bid) uj (2!ask)

time                 sym  bsize bprice asize aprice
---------------------------------------------------
0D10:00:00.000000000 AAPL 10    0.05   10    0.06
0D10:00:02.000000000 AAPL 10    0.05   10    0.07
0D10:00:03.000000000 AAPL 10    0.06   10    0.07
0D10:00:04.000000000 AAPL 10    0.06   10    0.07
0D10:00:06.000000000 AAPL 10    0.06   10    0.07 

更新

由于 uj 在以时间和 sym 为键的表上,因此联合连接也适用于 sym 列