4

我正在学习 KDB+ 并将 tic 数据加载到表 W 中,如下所示。我的问题是,如何将数据传输到 5(或 n)分钟 OHLCVA?

"Stk_ID","Date","Time","Price","Chg","Vol","Amt","Ty"
300032,2011-03-03,09:51:40,20.40,0.00,10.0,20400.0,S
300032,2011-03-03,09:51:30,20.40,-0.01,9.0,18360.0,S
300032,2011-03-03,09:51:00,20.41,0.01,2.0,4082.0,B
300032,2011-03-03,09:51:00,20.40,-0.01,115.0,234599.0,S
300032,2011-03-03,09:50:45,20.41,0.00,10.0,20410.0,S
300032,2011-03-03,09:50:45,20.41,-0.02,7.0,14287.0,S
300032,2011-03-03,09:50:20,20.43,-0.01,4.0,8172.0,S
300032,2011-03-03,09:50:05,20.44,0.01,25.0,51100.0,B
300032,2011-03-03,09:50:00,20.43,-0.01,28.0,57204.0,S

我用这样的 Q 码获取 1 分钟的数据,但不知道如何获取 5 分钟。:

select Open: first price,High: max price, Low: min price,Close: last price,Vol: sum vol, Amt: sum amt,Avg_Price: ((sum amt)%(sum vol))%100 by stk_id,time.hh,time.mm from asc W

结果:

stk_id hh mm| Open  High  Low   Close Vol  Amt           Avg_Price
------------| ----------------------------------------------------
000001 9  30| 16.24 16.24 16.22 16.24 3253 5282086       16.23758
000001 9  31| 16.22 16.24 16.21 16.21 1974 3204276       16.2324
000001 9  32| 16.23 16.23 16.2  16.2  3764 6102207       16.21203
000001 9  33| 16.21 16.21 16.19 16.2  4407 7143120       16.20858
000001 9  34| 16.2  16.2  16.19 16.19 1701 2756614       16.20584
000001 9  35| 16.19 16.21 16.19 16.21 2756 4466988       16.20823
000001 9  36| 16.22 16.25 16.22 16.24 3123 5076089       16.25389
000001 9  37| 16.25 16.27 16.25 16.27 1782 2897340       16.25892
4

2 回答 2

5

我建议不要按 time.hh 和 time.mm 分别分组,而是做一个单独的组:

by stk_id,time.minute

从那里开始,你需要做的 5 分钟存储桶就是使用 xbar:

by stk_id,5 xbar time.minute
于 2011-12-05T15:58:24.933 回答
0

聚合值的稍微动态的版本:

q)b:`Date`Time`stk!(`Date;(xbar;1;`Time.minute);`Stk_ID)
q)a:`op`cp`hp`lp!((first;`Price);(last;`Price);(max;`Price);(min;`Price))

q)?[w;();b;a]  
Date Time  stk   | op    cp    hp    lp
-----------------| -----------------------
2011 09:50 300032| 20.41 20.43 20.44 20.41
2011 09:51 300032| 20.4  20.4  20.41 20.4
于 2018-06-11T22:39:46.503 回答