我有以下斐波那契函数,它返回预期的输出
q)fib:{{x,sum -2#x}/[{last[x] < 100};x]}
q)fib 1 1
1 1 2 3 5 8 13 21 34 55 89 144
{last[x] < 100}
我们如何用外部函数的参数替换内部函数中的值 100 ?
预期函数调用结构 -
q)fib[1 1;100]
1 1 2 3 5 8 13 21 34 55 89 144 /- expected output
我有以下斐波那契函数,它返回预期的输出
q)fib:{{x,sum -2#x}/[{last[x] < 100};x]}
q)fib 1 1
1 1 2 3 5 8 13 21 34 55 89 144
{last[x] < 100}
我们如何用外部函数的参数替换内部函数中的值 100 ?
预期函数调用结构 -
q)fib[1 1;100]
1 1 2 3 5 8 13 21 34 55 89 144 /- expected output
列:
date of transaction
uid is the trader id
sym is the trade id
表格子集:
q) select date, uid, sym from tb
date uid sym
------------------------
2011.08.12 171196 537876
2012.09.08 171196 562161
2012.12.28 171196 570391
2014.04.29 171196 599420
2014.04.29 171196 601520
2014.05.11 171196 602286
2014.06.24 171196 605785
2014.07.19 171196 605686
2011.03.15 160872 524982
2011.07.11 168153 536311
2011.07.25 168153 535616
2011.08.25 30746 537340
2011.01.27 122083 523350
2011.03.05 122083 525676
2011.05.06 122083 531523
2011.01.07 181372 521088
2011.02.07 181372 522780
2011.03.02 181372 523984
2011.03.15 181372 524980
2011.03.21 181372 525448
2011.04.09 181372 529164
2011.04.19 181372 527627
2011.04.28 181372 528302
2011.05.16 181372 530337
2011.06.14 181372 532987
目的
我正在尝试获取每个交易的前 6 次uid
交易sym
。
uid
每个 都有多个sym
;对于每个 ,我需要它们之前的 6sym
个uid
。需要返回所有列,而不仅仅是上面看到的子集。
例如,对于此行:
2011.04.19 181372 527627
它将逐行返回+附加列 - 因此它将返回每个先前日期的每个先前信息,sym
并uid
在新表中的同一行中匹配 - 因此它是原始表,其中包含包含每行先前数据的附加列。
date uid sym prevdate1 prevuid1 prevsym1 prevdate2 prevuid2 etc.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2011.04.19 181372 527627 2011.01.07 181372 521088 2011.03.02 181372 523984 2011.03.15 181372 524980 2011.03.21 181372 525448
如果没有先前的日期或只有 3 个而不是 6 个等等,那么它仍将返回列,但将为空。
我的尝试:
getHHData:{[tb;syms;colnames;numtrades]
dict:exec i by uid from select uid from tb where sym=syms;
tradedate:first exec date from tb where sym=syms;
histdates:?[?[tb;enlist (in;`uid;(raze;(inv;`dict)));(enlist`uid)!enlist`uid;colnames!colnames];();`uid;`date];
histdatesro:raze each value (asc dict),histdates;
ind:til count histdatesro;
preind:{[prevtrades;ind;tradedate;histdatesro]
{[prevtrades;ind;tradedate;histdatesro]
prevtrades+{where y[z] in x}[tradedate;histdatesro;]each ind
}[;ind;tradedate;histdatesro]
}[;ind;tradedate;histdatesro]each neg 1_til numtrades;
uids:exec uid from tb where sym=syms;
histdata:reverse each key asc(value ?[tb;enlist (in;`uid;`uids);`uid;last colnames])!(value dict);
histCols:{[x;data]
{$[x<count y;y[x];0N]}[x] each data}[;histdata] each til numtrades-1;
flip(`sym`uid,`$string[last colnames],/:string 1+til numtrades-1)!(enlist[count[histdata]#syms],enlist[uids],histCols)
}
包装函数:
getHHDataMerged:{[tb;syms;colnames;numtrades]
raze{[tb;sym;colnames;numtrades](lj/){`sym`uid xkey x
}each {[tb;sym;c;numtrades]getHHData[tb;sym;c;numtrades]
}[tb;sym;;numtrades]each colnames
}[tb;;colnames;numtrades]each syms
}
我正在寻找一种更简洁的方法来解决这个问题。
编辑:
q)updCols:{`$string[x],\:string[y]}
q)prevCols:{if[x=0;:`date`uid`sym]; updCols[;x] `prevdate`prevuid`prevsym}
q)7#f/[tb1;1+til 6]
'type
[0] 7#f/[tb1;1+til 6]
^
q)tb1
date uid sym
------------------------
2011.08.12 171196 537876
2012.09.08 171196 562161
2012.12.28 171196 570391
2014.04.29 171196 599420
2014.04.29 171196 601520
2014.05.11 171196 602286
2014.06.24 171196 605785
2014.07.19 171196 605686
2011.03.15 160872 524982
我有一个像这样的字符串列表
q)select id from temp
id
---------------------------------
,"_"
"01coin"
"0chain"
"0dog"
"0-knowledge-network"
"0-mee"
"0vix-protocol"
"0x"
"0x0-ai-ai-smart-contract"
"0x678-landwolf-1933"
"0xanon"
"0xcoco"
"0xdao"
"0xgasless-2"
"0xgen"
...
我需要它看起来像这样
"_,01coin,0chain,0dog,0-knowledge-network,0-mee,0vix-protocol,...etc..."
在 kdb 中将这样的列表连接成单个字符串的最佳方法是什么?我应该将我的“字符串”转换为“符号”,然后 vs :/ 它们吗?我已经走到这一步了,但我不知道如何将这个“字符串”列表转换为单个“字符串,......”
("_,";"01coin,";"0chain,";"0dog,";"0-knowledge-network,";"0-mee,";"0vix-protocol,"; ...)
我尝试创建一个函数,让输出成为下一次迭代的输入。我传入名为 tab 的表,并希望传递每个阈值。
功能如下:
test:{[data;t]
data:update check:t`t3 from data where id within (t`t1;t`t2)
}/[tab;]threshold
数据:
tab:([]id:12 130 44 46 456;side:`S`S`B`S`B;entityVal:123 123 456 456 456;eventType:`New`Filled`New`Partial`Partial)
threshold:([]t1:100 10 39 60 80 100 ;t2:200 20 49 70 90 150;t3:`test1`test2`test3`tes4`tes5`test6);
有人能告诉我这个函数出了什么问题吗?它目前给出的是等级:无效等级或价数(参数计数)错误。
我在将解析输出转换为功能选择时遇到了问题。我认为你需要将每个输出转换,
为enlist
完整问题:
q)dict:exec i by uid from select uid from tb where sym=syms
一:
q)hd:exec date by uid from select date,ht by uid from tb where uid in raze key dict
q)parse"exec date by uid from select date,ht by uid from tb where uid in raze key dict"
?
(?;`tb;,,(in;`uid;(,/;(!:;`dict)));(,`uid)!,`uid;`date`ht!`date`ht)
()
,`uid
,`date
二:
q)ht:key asc(value exec ht by uid from tb where uid in uids)!(value dict)
q)parse"exec ht by uid from tb where uid in uids"
?
`tb
,,(in;`uid;`uids)
,`uid
,`ht
q)
我的尝试:
一(不起作用):
q)?[(?;`tb;enlist enlist (in;`uid;(,/;(!:;`dict)));(enlist `uid)!enlist `uid;`date`ht!`date`ht);();enlist `uid;enlist `date]
'type
[0] ?[(?;`tb;enlist enlist (in;`uid;(,/;(!:;`dict)));(enlist `uid)!enlist `uid;`date`ht!`date`ht);();(enlist`date)!enlist`date]
二(工作):
q) ?[`tb;enlist(in;`uid;`uids);`uid;(enlist`ht)!enlist`ht]
我有一张如下表:
q)tab:([]time:.z.t;sym:`ax`bx`cx`ds;col1:`a`a`b`b;price:1.4,1.2,1.3,1.4)
q)tab
time sym col1 price
---------------------------
20:29:37.309 ax a 1.4
20:29:37.309 bx a 1.2
20:29:37.309 cx b 1.3
20:29:37.309 ds b 1.4
q)
我的目标是将 col1 中的值从 a->b(其中 a 和 b->a(其中 b 和列名相同))更改为 a->b(其中 b 和列名相同)。下面是我的代码,可以完成这项工作,但是有没有更好的方法可以用更少的代码行来实现这一点?
testTab:update col2:count[i]#enlist `b from tab where col1=`a
testTab:update col2:count[i]#enlist `a from testTab where col1=`b
q)testTab
time sym col1 price col2
--------------------------------
20:29:37.309 ax a 1.4 b
20:29:37.309 bx a 1.2 b
20:29:37.309 cx b 1.3 a
20:29:37.309 ds b 1.4 a
q)testTab:`time`sym`col2 xcols testTab //Bring forward col2
q)testTab
time sym col2 col1 price
--------------------------------
20:29:37.309 ax b a 1.4
20:29:37.309 bx b a 1.2
20:29:37.309 cx a b 1.3
20:29:37.309 ds a b 1.4
q)testTab: delete col1 from testTab //Delete original col1 from the table
q)testTab: `time`sym`col1 xcol testTab // Rename col2 to col1
q)testTab
time sym col1 price
---------------------------
20:29:37.309 ax b 1.4
20:29:37.309 bx b 1.2
20:29:37.309 cx a 1.3
20:29:37.309 ds a 1.4
q)
谢谢!
我正在尝试获取时间戳的 30 秒倍数:
q)ts:2025.02.14D11:09:31.033
q)ts.date + 30 xbar `second$ts
2025.02.14D11:09:30.000000000
当我在函数中执行此操作时,它的行为有所不同:
q)f:{[t]: t.date + 30 xbar `second$t}
q)f ts
2000.01.02D11:09:30.000000000
请注意,这里的日期已丢失并变成 2000.01.02
有人能解释一下这里发生了什么吗?
我想翻转函数内部的列表列表,但行为发生了变化:
l:(1 5; 7 9; 12 15)
{[x]: x[1] - x[0]} flip l
4 2 3
{[x]: xx:flip x; xx[1] - xx[0]} l
(1 7 12;5 9 15)
我有一张带有时间戳/符号的表,其中每一行都有一个增量时间戳和一个随机符号。
q)sym:`aapl`tsla`aapl`msft`tsla`qcom`msft`tsla`aapl`qcom
::
q)t:([] ts:.z.p+til count sym;sym)
::
q)t
ts sym
----------------------------------
2025.02.09D14:43:43.054056807 aapl
2025.02.09D14:43:43.054056808 tsla
2025.02.09D14:43:43.054056809 aapl
2025.02.09D14:43:43.054056810 msft
2025.02.09D14:43:43.054056811 tsla
2025.02.09D14:43:43.054056812 qcom
2025.02.09D14:43:43.054056813 msft
2025.02.09D14:43:43.054056814 tsla
2025.02.09D14:43:43.054056815 aapl
2025.02.09D14:43:43.054056816 qcom
我想添加一个包含随机价格的列,其中每行都位于该符号的最后价格的 +/-(N*delta_unit) 范围内。例如,假设的起始价格为aapl
200.00 美元,delta_unit 为 0.01。如果 N 为 2,则 aapl 的每个后续价格应在最后价格的 0.02 美元范围内:
ts sym
----------------------------------
2025.02.09D14:43:43.054056807 aapl $200.00
2025.02.09D14:43:43.054056808 tsla
2025.02.09D14:43:43.054056809 aapl $199.98
2025.02.09D14:43:43.054056810 msft
2025.02.09D14:43:43.054056811 tsla
2025.02.09D14:43:43.054056812 qcom
2025.02.09D14:43:43.054056813 msft
2025.02.09D14:43:43.054056814 tsla
2025.02.09D14:43:43.054056815 aapl $199.99
我怎样才能生成这样的随机价格(也许给出从符号到起始价格的映射)?
我不明白当将 where 应用于整数列表(或具有 int 值的字典)时会做什么:
where til 4
/ 1 2 2 3 3 3
where `a`b`c`d!til 4
/ `b `c `c `d `d `d
返回值的模式相同,但我不确定应用的操作是什么。我终于明白了《银河系漫游指南》中关于 42 的全部内容……在这个用例中,“哪里”回答了什么问题?