N:300<o:p></o:p>
trades:(price:100+ 0.01*N?100;size:1+N?1000)<o:p></o:p>
select vwap:size wavg price by bs: (init,1_price{(01b!
buy`sell) x > y max where x <> y}'price til@/:til count price) from trades<o:p></o:p>
<o:p> </o:p>
To understand this query I suggest you to look at:<o:p></o:p>
<o:p> </o:p>
update bs: (init,1_price{(01b!
buy`sell) x > y max where x <> y}'price til@/:til count price) from trades<o:p></o:p>
<o:p> </o:p>
HTH,<o:p></o:p>
<o:p> </o:p>
Kim<o:p></o:p>
<o:p> </o:p>
Von: personal-kdbplus@googlegroups.com [mailto:personal-kdbplus@googlegroups.com] Im Auftrag von Manish Patel
Gesendet: Donnerstag, 20. Februar 2014 14:07
An: personal-kdbplus@googlegroups.com
Betreff: Re: [personal kdb+] looping in Q KDB<o:p></o:p>
<o:p> </o:p>
try using prev with ? … simple below but you’ll need to handle the == condition too (whatever that should be)<o:p></o:p>
<o:p> </o:p>
update flag:?[price<prev price;buy;
sell] from trade<o:p></o:p>
<o:p> </o:p>
On Thu, Feb 20, 2014 at 12:59 PM, Siddhartha Nanda <siddnanda@gmail.com> wrote:<o:p></o:p>
Hi People- I am new to the forum and to KDB and Q, and need your help..
I am trying to solve this question:
Randomly generate a table of 300 trades with price (two decimals) and size (integer). Compare each trade price (Pn) against its previous one (Pn-1), if Pn-1 > Pn, it is a buy initiated trade, if Pn-1 < Pn it is a sell initiated trade, if Pn-1 = Pn, compare against Pn-2, or Pn-3 until you find the last price movement. Then calculate the size weighted average price of all trades (VWAP), all the buy initiated trades (VWAPB) and all the sell initiated trades (VWAPS),
I could compare each price to its previous, but dont know the code to compare the ones prior to it based on a condition.
The following is my solution for VWAPB: ( but only for previous price)
VWAPB
q)select TradeCount:count i,VWAPB:(sum(size*price)%sum(size)), BuyVol:sum(size), slippage:10000*(first price - (sum(size*price)%sum(size)) % first price) from trades where trades[i; price] \< trades[-1+i;
price]
Can you please help me solve this..<o:p></o:p>
–
Submitted via Google Groups