depth
Problems / q / kdb+ / Medium

VWAP per sym

Given this table, compute the volume-weighted average price per symbol.

trade:([] sym:`A`B`A`B`A; price:10 20 11 19 12f; size:100 200 150 50 100)

Expected: A has about 11, B has 19.8.

Hint

There is a weighted-average operator, and qSQL has by.

Solution

select vwap:size wavg price by sym from trade

wavg takes the weights on the left and the values on the right.

Answer

select vwap:size wavg price by sym from trade