创建基于唯一条目的滚动计算 (Q/KDB+)

Create rolling calculation based unique entries (Q/KDB+)

我有一个 table:

q)data:([]dt:2017.01.05D19:45:00.238248239 2017.01.05D20:46:00.282382392 2017.01.05D21:47:00.232842342 2017.01.05D22:48:00.835838442 2017.01.05D20:49:00.282382392;sym:`AAPL`GOOG`AAPL`BBRY`GOOG;price:101.20 800.20 102.30 2.20 800.50;shares:500 100 500 900 100)
q)data
dt                            sym    price   shares
2017.01.05D19:45:00.238248239 AAPL   101.20  500
2017.01.05D20:46:00.282382392 GOOG   800.20  100
2017.01.05D21:47:00.232842342 AAPL   102.30  500
2017.01.05D22:48:00.835838442 BBRY     2.20  900
2017.01.05D20:49:00.282382392 GOOG   800.50  100

我需要创建一个列,其中包含价格*份额的总和,用于每个股票代码的最新观察。

为了演示如何使用上述数据,我们正在寻找:

data:
dt                            sym    price   shares   index
2017.01.05D19:45:00.238248239 AAPL   101.20  500      50,600
2017.01.05D20:46:00.282382392 GOOG   800.20  100      130,620
2017.01.05D21:47:00.232842342 AAPL   102.30  500      131,170
2017.01.05D22:48:00.835838442 BBRY     2.20  900      133,150
2017.01.05D20:49:00.282382392 GOOG   800.50  100      133,180

为了进一步说明,在第 1 行,仅包含 1 个符号,在第 2 行,2 个符号,然后是 2 个,然后是 3 个,然后在第 5 行又是 3 个。

在另一个线程中回答:

q)delete ind from update index:sum@'ind from (update ind:@\[()!();sym;:;shares*price%divisor] from data) where i>=max(first;i)fby sym
dt                            sym  price shares divisor index
--------------------------------------------------------------
2017.01.05D19:45:00.238248239 AAPL 101.2 500    2
2017.01.05D20:45:00.282382392 GOOG 800.2 500    1
2017.01.05D21:45:00.232842342 AAPL 102.3 500    2
2017.01.05D22:45:00.835838442 BBRY 2.2   500    1       426775

您得到的答案略有不同,这是求和(份额*价格%除数)而不是单独求和。

一个稍微凌乱和复杂的版本,得到的答案与您似乎期望的相同:

q)delete ind from update index:sum'[ind[;;0]]*sum'[ind[;;1]]%sum'[ind[;;2]] from (update ind:@\[()!();sym;:;shares,'price,'divisor] from data) where i>=max(first;i)fby sym
dt                            sym  price shares divisor index
----------------------------------------------------------------
2017.01.05D19:45:00.238248239 AAPL 101.2 500    2
2017.01.05D20:45:00.282382392 GOOG 800.2 500    1
2017.01.05D21:45:00.232842342 AAPL 102.3 500    2
2017.01.05D22:45:00.835838442 BBRY 2.2   500    1       339262.5

Jonathon 的解决方案略有变化,使用条件向量:

q)delete dict from update index:?[all flip distinct[sym]in/: key'[dict]; {sum[x]*sum[y]%sum z} ./: flip each value each dict;0N] from update dict:@[;;:;]\[()!();sym;flip (price;shares;divisor)] from data
dt                            sym  price shares divisor index
----------------------------------------------------------------
2018.02.05D22:47:22.175914000 AAPL 101.2 500    2
2018.02.05D22:21:10.175914000 GOOG 800.2 500    1
2018.02.05D22:58:00.175914000 AAPL 102.3 500    2
2018.02.05D22:19:27.175914000 BBRY 2.2   500    1       339262.5

鉴于问题自最初发布和我之前的回答以来发生了很大变化,这里是更新的解决方案:

q)delete ind from update index:sum@'ind from (update ind:@\[()!();sym;:;shares*price] from data) where i>=max(first;i)fby sym
dt                            sym  price shares index
------------------------------------------------------
2017.01.05D19:45:00.238248239 AAPL 101.2 500
2017.01.05D20:46:00.282382392 GOOG 800.2 100
2017.01.05D21:47:00.232842342 AAPL 102.3 500
2017.01.05D22:48:00.835838442 BBRY 2.2   900    133150
2017.01.05D20:49:00.282382392 GOOG 800.5 100    133180

或者没有其他初始条件,即只应在所有代码都已勾选后填充它:

q)delete ind from update index:sum@'ind from update ind:@\[()!();sym;:;shares*price] from data
dt                            sym  price shares index
------------------------------------------------------
2017.01.05D19:45:00.238248239 AAPL 101.2 500    50600
2017.01.05D20:46:00.282382392 GOOG 800.2 100    130620
2017.01.05D21:47:00.232842342 AAPL 102.3 500    131170
2017.01.05D22:48:00.835838442 BBRY 2.2   900    133150
2017.01.05D20:49:00.282382392 GOOG 800.5 100    133180

(注意这些只是对我昨天发布的解决方案的小修改,针对问题中更改的要求进行了更新)