简体   繁体   English

kdb 计算行之间的百分比变化

[英]kdb calculate percent change between rows

i calculate percent change in 24 hour by this way that you can see below.我通过这种方式计算 24 小时内的百分比变化,您可以在下面看到。

is there any way to get calculated percent directly from kdb by query?有没有办法通过查询直接从 kdb 获取计算百分比? df should return the percent as final value. df应该返回百分比作为最终值。

df = q.sendSync('{[x;y]value select by x xbar DateTime.minute from trades where DateTime > .z.p-0D01, symbol=y }',30,np.string_(symbol))

start  = df.iloc[0].price
end = df.iloc[-1].price

return ((end-start)/start) * 100 ##### return -0.01152

kdb query result: kdb查询结果:

                 DateTime    side       symbol  amount     price   
0 2022-04-15 14:59:59.975  b'buy'  b'ETH-USDT'  0.0033     3035.75  
1 2022-04-15 15:29:58.889  b'buy'  b'ETH-USDT'   0.079     3035.2100
2 2022-04-15 15:32:09.050  b'buy'  b'ETH-USDT'  0.0165     3034.1599
first exec 100*(last[price]-price[0])%price[0] from trades where DateTime > .z.p-0D01, symbol=y

update PctDiff:-[price%first price;1] from trades

This query creates additional column of "percent change from start to now", and of course you need to sort the table by time first.此查询创建了“从开始到现在的百分比变化”的附加列,当然您需要先按时间对表进行排序。

This query can also help if you have multiple symbols in the table and want to calculate for each sym:如果您在表中有多个交易品种并且想要为每个交易品种计算,此查询也可以提供帮助:

update PctDiff:-[price%first price;1] by sym from trades

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM