# Count/Sum orders between trades

**URL:** <https://forum.kx.com/t/count-sum-orders-between-trades/11668>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [December 25, 2017, 5:36pm UTC](https://forum.kx.com/t/count-sum-orders-between-trades/11668 "2017-12-25T17:36:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mwmsigma](https://avatars.discourse-cdn.com/v4/letter/m/c57346/32.png) [@mwmsigma](https://forum.kx.com/u/mwmsigma)\
**Post date:** [December 25, 2017, 5:36pm UTC](https://forum.kx.com/t/count-sum-orders-between-trades/11668/1 "2017-12-25T17:36:00Z")

</div>

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&nbsp; **for** &nbsp; **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? $&nbsp;q

KDB+ 3.5 2017.11.30 Copyright (C) 1993-2017 Kx Systems

m32/ 4()core 16384MB Dovla&nbsp;dovla.homenet.telecomitalia.it&nbsp;192.168.1.121 &nbsp;

q)\l ob.q

q)msgs

time| BidAsk Rate Size Type&nbsp;

----| ----------------------

t1&nbsp; | 1&nbsp; &nbsp; &nbsp; 10 &nbsp; 1&nbsp; &nbsp; order

t2&nbsp; | 1&nbsp; &nbsp; &nbsp; 11 &nbsp; 2&nbsp; &nbsp; trade

t3&nbsp; | 1&nbsp; &nbsp; &nbsp; 12 &nbsp; 3&nbsp; &nbsp; order

t4&nbsp; | 1&nbsp; &nbsp; &nbsp; 13 &nbsp; 4&nbsp; &nbsp; order

t5&nbsp; | 1&nbsp; &nbsp; &nbsp; 14 &nbsp; 5&nbsp; &nbsp; 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&nbsp; | 10 &nbsp; 1&nbsp; &nbsp;

t5&nbsp; | 25 &nbsp; 7 &nbsp;&nbsp;

usually I slice the table between current and last trade in a for loop, like following in R

Rate \<- character(); Size \<- character()&nbsp;

index \<- msg$Type == ‘trade’

indexP \<- which(index %in% TRUE)

knt \<- sum(index)

beg \<- 1

for(i in 1:knt) {

&nbsp; cnt \<- indexP[i] - 1

&nbsp; print(beg)

&nbsp; print(cnt)

&nbsp; row1 \<- msg[beg:cnt,]

&nbsp; Rate[i] \<- sum(row1$Rate)

&nbsp; Size[i] \<- sum(row1$Size)

&nbsp; beg \<- beg + 2 + (cnt-beg)

}

ob \<- data.frame(Rate,Size)

ob

&nbsp; Rate Size

1 &nbsp; 10 &nbsp; &nbsp;1

2 &nbsp; 25 &nbsp; &nbsp;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..

---

<div class="post-metadata">

**Author:** ![quintanar4011](https://avatars.discourse-cdn.com/v4/letter/q/6f9a4e/32.png) [@quintanar4011](https://forum.kx.com/u/quintanar4011)\
**Post date:** [December 26, 2017, 7:31am UTC](https://forum.kx.com/t/count-sum-orders-between-trades/11668/2 "2017-12-26T07:31:00Z")

</div>

In Q SQL you can use the fact that rows are ordered. Also you should always try to use vector operations. For example:

select last time, sum[rate]-last rate, sum[size]-last size by sums prev Type=`trade from msgs

Here the “sums prev Type=`trade” assigns to each “until the next trade” block a unique number. Sums, prev, =, last&nbsp;are all vector ops eliminating the need for explicit cycles.

???, 25 ??? 2017 ?., 21:18:27 UTC+3 ??? Victor Velletti ???:

> 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&nbsp; **for** &nbsp; **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? $&nbsp;q
> 
> KDB+ 3.5 2017.11.30 Copyright (C) 1993-2017 Kx Systems
> 
> m32/ 4()core 16384MB Dovla&nbsp;dovla.homenet.telecomitalia.it&nbsp;192.168.1.121 &nbsp;
> 
> q)\l ob.q
> 
> q)msgs
> 
> time| BidAsk Rate Size Type&nbsp;
> 
> ----| ----------------------
> 
> t1&nbsp; | 1&nbsp; &nbsp; &nbsp; 10 &nbsp; 1&nbsp; &nbsp; order
> 
> t2&nbsp; | 1&nbsp; &nbsp; &nbsp; 11 &nbsp; 2&nbsp; &nbsp; trade
> 
> t3&nbsp; | 1&nbsp; &nbsp; &nbsp; 12 &nbsp; 3&nbsp; &nbsp; order
> 
> t4&nbsp; | 1&nbsp; &nbsp; &nbsp; 13 &nbsp; 4&nbsp; &nbsp; order
> 
> t5&nbsp; | 1&nbsp; &nbsp; &nbsp; 14 &nbsp; 5&nbsp; &nbsp; 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&nbsp; | 10 &nbsp; 1&nbsp; &nbsp;
> 
> t5&nbsp; | 25 &nbsp; 7 &nbsp;&nbsp;
> 
> usually I slice the table between current and last trade in a for loop, like following in R
> 
> Rate \<- character(); Size \<- character()&nbsp;
> 
> index \<- msg$Type == ‘trade’
> 
> indexP \<- which(index %in% TRUE)
> 
> knt \<- sum(index)
> 
> beg \<- 1
> 
> for(i in 1:knt) {
> 
> &nbsp; cnt \<- indexP[i] - 1
> 
> &nbsp; print(beg)
> 
> &nbsp; print(cnt)
> 
> &nbsp; row1 \<- msg[beg:cnt,]
> 
> &nbsp; Rate[i] \<- sum(row1$Rate)
> 
> &nbsp; Size[i] \<- sum(row1$Size)
> 
> &nbsp; beg \<- beg + 2 + (cnt-beg)
> 
> }
> 
> ob \<- data.frame(Rate,Size)
> 
> ob
> 
> &nbsp; Rate Size
> 
> 1 &nbsp; 10 &nbsp; &nbsp;1
> 
> 2 &nbsp; 25 &nbsp; &nbsp;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..

---

<div class="post-metadata">

**Author:** ![mwmsigma](https://avatars.discourse-cdn.com/v4/letter/m/c57346/32.png) [@mwmsigma](https://forum.kx.com/u/mwmsigma)\
**Post date:** [December 27, 2017, 2:40pm UTC](https://forum.kx.com/t/count-sum-orders-between-trades/11668/3 "2017-12-27T14:40:00Z")

</div>

Spasibo Andrei.

That sounds exactly perfect. Ill try to implement now.

Thank You so much.
