2014-01-11 87 views
1

我正在尝试构建股票指数,并使用Q作为目的。正如你可能知道的那样,我对此很新颖。而我在SQL建造它,它需要太长时间来处理,因此,我诉诸Q.KDB/Q构建股票市场指数

的设置到目前为止是这样的:

  1. 表;数据添加到表中,对其进行排序:

    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 
    
  2. 至此的处理(感谢其他友好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 
      
  3. 如何看起来:

    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 
    
  4. 我想要做什么:

    我想计算板块指数,当其成分改变而改变;为了做到这一点,我需要计算除数列和实际索引列。我想实现的逻辑如下:

    • 如果delta != 0,然后divisor = the last value of the divisor, in the same sector
    • 如果delta != 0,然后index = idxmv % the value of the divisor computed above
    • 如果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 

感谢您的帮助,

回答

2
trades1:update index:{100f,1_count[x]#0n}[delta] by sectors from trades;/starting value of index as 100 
trades1:update divisor:?[delta=0;idxmv%index;0n] by sectors from trades1;/divisor=y%z when delta=0 
trades1:update divisor:?[delta<>0;fills divisor;divisor] by sectors from trades1;/divisor=last divisor when delta <> 0 
trades1:update index:?[delta<>0;idxmv%divisor;index] by sectors from trades1;/index=i%d when delta <>0 
trades1:update index:?[delta=0;fills index;index] by sectors from trades1/index=last index when delta=0 
trades1:update divisor:?[delta=0;idxmv%index;divisor] by sectors from trades1; /divisor=y%z when delta=0 

这应该解决您的问题。
最后一行似乎没有匹配。我将行业作为主键。您应该能够通过将主键更改为扇区/行情来匹配最后一行。

+0

感谢您的帮助akash。这远远超过我迄今为止所获得的。我正在研究最后一行正确计算,再次感谢指出一些真正有用的功能。 – user1158959

相关问题