KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums kdb+ help with VWAP function

  • help with VWAP function

    Posted by fav_pa on June 23, 2024 at 6:27 pm

    Hi, as a small exercise, i am trying to compute the VWAP over the different depths of the order book.The simplest way is select timestamp, depth_vwap1: (bq0; bq1; aq0; aq1) wavg (bp0; bp1; ap0; ap1) from data now i am trying to abstract i in a function so that we can pass the max depth the vwap goes to as a parameter:

    [!data;
    ();
    0b;
    (enlist$ "vwap_d", string max_depth)!(enlist(wavg;(enlist;bq0;bq1;aq0; aq1 );
    (enlist;bp0;bp1;ap0; ap1 )))]

    now, i need to abstract the

    (enlist;bp0;bp1;ap0;ap1)

    part.I thought of doing :

    quantities: raze(("bq"; "aq"),/::string til maxDepth)
    prices : raze(("bp"; "ap"),/::string til maxDepth)

    but then i dont understand how to use them within the parse request.Would really appreciate is someone could help!Thank youPaul

    edit: sorry for the formatting i dont know what causes that

    • This discussion was modified 2 weeks, 4 days ago by  mwoods.
    • This discussion was modified 2 weeks, 3 days ago by  mwoods.
    • This discussion was modified 1 day, 20 hours ago by  supportelearningwp-com.
    fav_pa replied 4 months, 3 weeks ago 2 Members · 2 Replies
  • 2 Replies
  • rocuinneagain

    Member
    July 1, 2024 at 12:57 pm
    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
  • fav_pa

    Member
    July 1, 2024 at 1:55 pm

    thank you for such a complete answer! I was far from having the complete solution!
    Best,
    PF

Log in to reply.