tick.q 中的 Kdb+ 键控表语法

问题描述 投票:0回答:2

我正在关注这个文档: https://code.kx.com/q/wp/capi/#publishing-to-a-kdb-tickerplant

在 sym.q 中: 交易:([]时间:

timespan$();[sym:
交易品种$()];价格:
float$();size:
多头$())

现在我想要的是使用键控表,这就是为什么上面的模式遵循键控表语法。 但问题是所有的tickerplant模式都应该以time:timespan列开始,它引起的问题是当我输入

type trade
时,它返回98h而不是99h(这是键控表的类型号)。

所以在 r.q 中:

/q tick/r.q [host]:port[:usr:pwd] [host]:port[:usr:pwd]
/2008.09.09 .k ->.q

if[not "w"=first string .z.o;system "sleep 1"];

upd:upsert

/ get the ticker plant and history ports, defaults are 5010,5012
.u.x:.z.x,(count .z.x)_(":5010";":5012");

/ end of day: save, clear, hdb reload
.u.end:{t:tables`.;t@:where `g=attr each t@\:`sym;.Q.hdpf[`$":",.u.x 1;`:.;x;`sym];@[;`sym;`g#] each t;};

/ init schema and sync up from log file;cd to hdb(so client save can run)
.u.rep:{(.[;();:;].)each x;if[null first y;:()];-11!y;system "cd ",1_-10_string first reverse y};
/ HARDCODE \cd if other than logdir/db

/ connect to ticker plant for (schema;(logcount;log))
.u.rep .(hopen `$":",.u.x 0)"(.u.sub[`;`];`.u `i`L)";

我有upsert,但由于它不是真正的键控表,它导致一次又一次插入重复的条目,而不是更新同一行的唯一键。

c database kdb q
2个回答
2
投票

您的表定义无效。你好像有

trade:([]time:`timespan$();[sym:`symbol$()];price:`float$();size:`long$())

我认为这意味着您希望表格由符号键入。在这种情况下,表定义应该是:

trade:([sym:`symbol$()]time:`timespan$();price:`float$();size:`long$())

但是您可能不希望在tickerplant模式中对表进行键控,只有rdb应该对它进行键控。这样,您就可以在tickerplant中维护可重播的日志,并且不必更改tickerplant逻辑(需要删除“timesym”检查,并且upd函数必须对数据添加时间戳,而不是作为第一列)。

所以你应该保持交易模式不加密

trade:([]time:`timespan$();sym:`symbol$();price:`float$();size:`long$())

您可以将rdb中的重放功能更改为:

.u.rep:{(.[;();:;].)each x;`sym xkey `trade;if[null first y;:()];-11!y;system "cd ",1_-10_string first reverse y};

最后要注意的是,您的

upd:upsert
会比默认的
upd:insert


0
投票

@works 也许不是您正在寻找的答案,但如果您很难理解tick.q,我创建了一个博客逐行解释内容。您可以在这里找到它https://www.defconq.tech/docs/tutorials/tick

© www.soinside.com 2019 - 2024. All rights reserved.