我正在尝试 Build 一个股票指数,我正在使用Q.你可能会说,我对此非常陌生 . 虽然我用SQL构建它,但是处理时间太长,因此我求助于Q.
到目前为止的设置如下:
- table ;将数据添加到表中,对其进行排序:
tickers:`ibm`bac`dis`gs`ibm`gs`dis`bac
pxs:100 50 30 250 110 240 45 48
dates:2013.05.01 2013.01.05 2013.02.03 2013.02.11 2013.06.17 2013.06.21 2013.04.24 2013.01.06
sectors:`auto`money`funny`money`auto`money`funny`money
trades:([sectors;tickers;dates];pxs)
`sectors`dates`tickers xasc `trades
-
到目前为止的处理(感谢其他友好的SO用户):
-
我计算了每个股票价格与上次记录价格相比变化的程度
trades: update delta:{0,1_deltas x}pxs by tickers from trades
- 我计算了部门指数成分的总市值
trades: update idxmv:sums[?[delta<>0;delta;pxs]] by sectors from trades
- 它看起来如何:
sectors tickers dates | pxs delta idxmv
--------------------------| ---------------
auto ibm 2013.05.01| 100 0 100
auto ibm 2013.06.17| 110 10 110
funny dis 2013.02.03| 30 0 30
funny dis 2013.04.24| 45 15 45
money bac 2013.01.05| 50 0 50
money bac 2013.01.06| 48 -2 48
money gs 2013.02.11| 250 0 298
money gs 2013.06.21| 240 -10 288
- 我想做什么:
我想要计算一个扇区索引,它在组件发生变化时会发生变化;为了做到这一点,我需要计算一个除数列和实际的索引列 . 我试图实现的逻辑如下:
-
if
delta != 0
,然后divisor = the last value of the divisor, in the same sector
-
if
delta != 0
,然后index = idxmv % the value of the divisor computed above
-
if
delta = 0
,然后index = the last value of the index, in the same sector
-
如果
delta = 0
,则divisor = idxmv % the value of the index computed above
,其中索引的初始值为100;所有部门的起始值均为100
基本上,我最终想要的是类似于下面的内容:
sectors tickers dates | pxs delta idxmv divisor index
--------------------------| ------------------------------
auto ibm 2013.05.01| 100 0 100 1 100
auto ibm 2013.06.17| 110 10 110 1 110
funny dis 2013.02.03| 30 0 30 0.30 100
funny dis 2013.04.24| 45 15 45 0.30 150
money bac 2013.01.05| 50 0 50 0.50 100
money bac 2013.01.06| 48 -2 48 0.50 96
money gs 2013.02.11| 250 0 298 3.10 96
money gs 2013.06.21| 240 -10 288 3.10 92.78
谢谢你的帮助,
担
1 回答
这应该可以解决您的问题 .
最后一行似乎不匹配 . 我已将扇区作为主键 . 您应该能够通过将主键更改为扇区/代码来匹配最后一行 .