kdb

Apply var to the last n numbers of a column in a table

╄→尐↘猪︶ㄣ 提交于 2019-12-11 07:29:04
问题 In KDB I have a table with 4 columns, of which one is a returns column. I would like to create a 5th column that calculates the variance (var) of the past x elements of a specified column. I have managed this with two columns creating the returns of a price column, but am stuck when I need to reference more than one previous element. Example: t:([] td:2001.01.01 2001.01.02 2001.01.03 2001.01.04 2001.01.05 2001.01.06; px:121 125 127 126 129 130) t:update retLogPcnt:100*log px%prev px from t /t

Sum values from the previous N number of days in KDB?

◇◆丶佛笑我妖孽 提交于 2019-12-11 07:05:30
问题 I have a table with following two columns: Initial Table Date Value ------------------- 2019.01.01 | 150 2019.01.02 | 100 2019.01.04 | 200 2019.01.07 | 300 2019.01.08 | 100 2019.01.10 | 150 2019.01.14 | 200 2019.01.15 | 100 For each row, I would like to sum values from the previous N number of days. In this case, N = 5. Resultant Table Date Value Sum ------------------------ 2019.01.01 | 150 | 150 (01 -> ..) 2019.01.02 | 100 | 250 (02 -> 01) 2019.01.04 | 200 | 450 (04 -> 01) 2019.01.07 | 300

R -> kdb: Pass R data to kdb+ as binary objects

此生再无相见时 提交于 2019-12-11 04:39:10
问题 What's the most efficient way to insert R objects (more specifically, time series expressed as xts or data.table objects, i.e. time-based and numeric columns) into a kdb+ database? I was able to locate only solution involving string serialization via q expressions as described here and here. 回答1: My solution was inspired by this version of qserver.c from github Yang added two functions: convert_binary, convert_r that [de]serialized data, which is basically what you asked for. However, the

How to match date and string from 2 lists (KDB)?

拜拜、爱过 提交于 2019-12-11 04:19:58
问题 I have two lists: data: dt sym bid ask 2017.01.01D05:00:09.140745000 AAPL 101.20 101.30 2017.01.01D05:00:09.284281800 GOOG 801.00 802.00 2017.01.02D05:00:09.824847299 AAPL 101.30 101.40 info: date sym shares divisor 2017.01.01 AAPL 500 2 2017.01.01 GOOG 100 1 2017.01.02 AAPL 200 2 I need to append from "info" the shares and divisor values for each ticker based on the date. How can I achieve this? Below is an example: result: dt sym bid ask shares divisor 2017.01.01D05:00:09.140745000 AAPL 101

How to get range of elements in a list in KDB?

泪湿孤枕 提交于 2019-12-11 04:06:27
问题 For example, I have this list: test:(8;12;15;19;10) How may I select elements 2 to 4? When I try list[2;4] it doesn't work for me. 回答1: indexing a list is by far the fastest way. q)a 8 1 9 5 4 6 6 1 8 5 4 9 2 7 0 1 9 2 1 8 8 1 7 2 4 5 4 2 7 8 5 6 4 1 3 3 7 8 2.. q)\t do[100000;2 3 sublist a] 109 q)\t do[100000;a 2 3 4] 15 So just follow your list with a list of indexes. BTW you can create indexes with til q)til 2 0 1 q)2+til 2 2 3 回答2: You can use sublist for this test:(8;12;15;19;10); 2 3

Primary key generation for KDB table

爱⌒轻易说出口 提交于 2019-12-11 03:22:52
问题 Is there a way to specify a primary key column in KDB+ which will auto-increment, i.e. like in the traditional RMDS (say, Oracle's Sequence)? - don't need to insert in this column, it will be auto-populated with every new record. 回答1: Tables will automatically have a hidden (virtual) incrementing column called "i". q)tab:([]col1:`a`b`c;col2:1 2 3) q)select i,col1,col2 from tab x col1 col2 ----------- 0 a 1 1 b 2 2 c 3 q) q)exec i from tab where col1=`b ,1 If you needed to join on this you

Command to check partitioned directory in KDB

两盒软妹~` 提交于 2019-12-11 03:22:09
问题 I have one 'q' service running. I want to check whether current directory in the service is a partitioned directory or not. Is there any command to do that? 回答1: https://code.kx.com/q/ref/dotq/#qpf-partition-type or one of the other .Q's should do it for you (https://code.kx.com/q/ref/card/#q). Though KX don't necessarily recommend using .Q utilities. Another way would be to check for the existence of the variable "date" (if date-partitioned), which lists the available partitioned dates.

KDB: String comparison with a table

非 Y 不嫁゛ 提交于 2019-12-11 02:03:30
问题 I have a table bb: bb:([]key1: 0 1 2 1 7; col1: 1 2 3 4 5; col2: 5 4 3 2 1; col3:("11";"22" ;"33" ;"44"; "55")) How do I do a relational comparison of string? Say I want to get records with col3 less than or equal to "33" select from bb where col3 <= "33" Expected result: key1 col1 col2 col3 0 1 5 11 1 2 4 22 2 3 3 33 回答1: If you want col3 to remain of string type, then just cast temporarily within the qsql query? q)select from bb where ("J"$col3) <= 33 key1 col1 col2 col3 -------------------

How to produce a formatted date string in Q/KDB?

天大地大妈咪最大 提交于 2019-12-11 01:24:31
问题 How can one produce an ISO date string "yyyy-MM-dd" from a Q date type? I looked at concatenating the various parts but am not even able to get the day/month, e.g. d:2015.12.01;d.month prints 2015.12 , i.e. more than just the month. 回答1: q)"-" sv "." vs string[2015.12.01] "2015-12-01" vs vector from string, splits by "." above; sv string to vector, join by "-" above. Remember a string is just a char array, so you can grab each part as you require with indexing. But the above is useful as the

Aggregating timeseries from sensors

醉酒当歌 提交于 2019-12-10 23:19:16
问题 I have about 500 sensors which emit a value about once a minute each. It can be assumed that the value for a sensor remains constant until the next value is emitted, thus creating a time series. The sensors are not synchronized in terms of when they emit data (so the observation timestamps vary), but it's all collected centrally and stored per sensor (to allow filtering by subset of sensors). How can I produce an aggregate time series that gives the sum of the data from the sensors? n (need