1
我正在嘗試構建股票指數,並使用Q作爲目的。正如你可能知道的那樣,我對此很新穎。而我在SQL建造它,它需要太長時間來處理,因此,我訴諸Q.KDB/Q構建股票市場指數
的設置到目前爲止是這樣的:
表;數據添加到表中,對其進行排序:
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
我想要做什麼:
我想計算板塊指數,當其成分改變而改變;爲了做到這一點,我需要計算除數列和實際索引列。我想實現的邏輯如下:
- 如果
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
感謝您的幫助,
丹
感謝您的幫助akash。這遠遠超過我迄今爲止所獲得的。我正在研究最後一行正確計算,再次感謝指出一些真正有用的功能。 – user1158959