Home › Forums › kdb+ › help with VWAP function › Reply To: help with VWAP function
-
Sample table for testing:
q)data:([] timestamp:til 10;bq0:til 10;bq1:til 10;bq2:til 10;bp0:til 10;bp1:til 10;bp2:til 10;aq0:til 10;aq1:til 10;aq2:til 10;ap0:til 10;ap1:til 10;ap2:til 10) q)data timestamp bq0 bq1 bq2 bp0 bp1 bp2 aq0 aq1 aq2 ap0 ap1 ap2 --------------------------------------------------------- 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9 9 9 9
Test provided query:q)select timestamp, depth_vwap1: (bq0; bq1; aq0; aq1) wavg (bp0; bp1; ap0; ap1) from data timestamp depth_vwap1 --------------------- 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9
parse query to functional form:q)parse"select timestamp, depth_vwap1: (bq0; bq1; aq0; aq1) wavg (bp0; bp1; ap0; ap1) from data" ? `data () 0b `timestamp`depth_vwap1!(`timestamp;(wavg; (enlist;`bq0;`bq1;`aq0;`aq1); (enlist;`bp0;`bp1;`ap0;`ap1)))
Test functional query:q)?[data;();0b;`timestamp`depth_vwap1!(`timestamp;(wavg; (enlist;`bq0;`bq1;`aq0;`aq1); (enlist;`bp0;`bp1;`ap0;`ap1)))] timestamp depth_vwap1 --------------------- 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9
Set up variables:q)maxDepth:2 q)quantities: `$raze(("bq"; "aq"),/:\:string til maxDepth) // Note `$ to convert strings to symbols q)prices : `$raze(("bp"; "ap"),/:\:string til maxDepth) // Note `$ to convert strings to symbols
Create needed list to pass to functional query:q)enlist,quantities enlist `bq0 `bq1 `aq0 `aq1 q)-1 .Q.s1 enlist,quantities; //.Q.s1 very useful to show full structure of object (enlist;`bq0;`bq1;`aq0;`aq1)
Test new dynamic functional select:q)?[data;();0b;`timestamp`depth_vwap1!(`timestamp; (wavg;enlist,quantities;enlist,prices))] timestamp depth_vwap1 --------------------- 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9