2017.12.25 09:36 AM
~/q? $ q
KDB+ 3.5 2017.11.30 Copyright (C) 1993-2017 Kx Systems
m32/ 4()core 16384MB Dovla dovla.homenet.telecomitalia.it 192.168.1.121
q)\l ob.q
q)msgs
time| BidAsk Rate Size Type
----| ----------------------
t1 | 1 10 1 order
t2 | 1 11 2 trade
t3 | 1 12 3 order
t4 | 1 13 4 order
t5 | 1 14 5 trade
q)ob
time| Rate Size
----| ---------
t2 | 10 1
t5 | 25 7
2017.12.25 11:31 PM
I am very new to kdb+/q and I am truly amazed with it. Got so many questions.. But among many reasons I want to use it is speed, and particularly I want to escape for loops.I understand that in functional language I need to twist my mind, and for some basic examples I do because I use apply family function in R, but there is a routine that I do over and over in many situations and still cannot wrap my head how to do it in R or kdb+/q..So I've got following simplified feed~/q? $ q
KDB+ 3.5 2017.11.30 Copyright (C) 1993-2017 Kx Systems
m32/ 4()core 16384MB Dovla dovla.homenet.
telecomitalia.it 192.168.1.121 q)\l ob.q
q)msgs
time| BidAsk Rate Size Type
----| ----------------------
t1 | 1 10 1 order
t2 | 1 11 2 trade
t3 | 1 12 3 order
t4 | 1 13 4 order
t5 | 1 14 5 trade
And I'd like to get sum of Rate and Size between trades in column Type. Notice that there can be any number of orders between trades.q)ob
time| Rate Size
----| ---------
t2 | 10 1
t5 | 25 7
usually I slice the table between current and last trade in a for loop, like following in RRate <- character(); Size <- character()index <- msg$Type == 'trade'indexP <- which(index %in% TRUE)knt <- sum(index)beg <- 1for(i in 1:knt) {cnt <- indexP[i] - 1print(beg)print(cnt)row1 <- msg[beg:cnt,]Rate[i] <- sum(row1$Rate)Size[i] <- sum(row1$Size)beg <- beg + 2 + (cnt-beg)}ob <- data.frame(Rate,Size)obRate Size1 10 12 25 7--------------------------------- So the whole idea is just to sum number of orders coming from a feed between trades. In real example, I build orderbook after each trade, and do couple of calculations between current and last trade, and store the trade with calculations as a row for later use. I apply additional filters for Ask/Bid and update OrderBook to real one from time to time.. but this is really essential part.Hope I clearly communicated idea..
2017.12.27 06:40 AM
EMEA
Tel: +44 (0)28 3025 2242
AMERICAS
Tel: +1 (212) 447 6700
APAC
Tel: +61 (0)2 9236 5700
KX. All Rights Reserved.
KX and kdb+ are registered trademarks of KX Systems, Inc., a subsidiary of FD Technologies plc.