# Finance functions

## l2price​

l2price(target_quantity, quantity_1, price_1, quantity_2, price_2, ..., quantity_n, price_n)

Consider quantity_1, price_1, quantity_2, price_2, ..., quantity_n, price_n to be either side of an order book with n price levels. Then, the return value of the function is the average trade price of a market order executed with the size of target_quantity against the book.

Let's take the below order book as an example.

1014.1014.5014
1714.0014.6016
1913.9014.8023
2113.7015.1012
1813.40

A buy market order with the size of 50 would wipe out the first two price levels of the Ask side of the book, and would also trade on the third level.

The full price of the trade: 14 * $14.50 + 16 *$14.60 + (50 - 14 - 16) * $14.80 =$732.6

The average price of the instrument in this trade: $732.6 / 50 =$14.652

This average trade price is the output of the function when executed with the parameters taken from the above example:

select l2price(50, 14, 14.50, 16, 14.60, 23, 14.80, 12, 15.10);
l2price
14.652

### Parameters​

The function takes a target quantity, and a variable number of quantity/price pairs. Each represents a price level of the order book.

Each parameter is expected to be a double, or convertible to double (float, long, int, short, byte).

• target_quantity: The size of a hypothetical market order to be filled.
• quantity*: The number of instruments available at the corresponding price levels.
• price*: Price levels of the order book.

### Return value​

The function returns with a double, representing the average trade price.

Returns null if the price is not calculable. For example, if the target quantity cannot be filled, or there is incomplete data in the set (nulls).

### Examples​

Test data:

CREATE TABLE order_book (  ts TIMESTAMP,  bidSize1 DOUBLE, bid1 DOUBLE, bidSize2 DOUBLE, bid2 DOUBLE, bidSize3 DOUBLE, bid3 DOUBLE,  askSize1 DOUBLE, ask1 DOUBLE, askSize2 DOUBLE, ask2 DOUBLE, askSize3 DOUBLE, ask3 DOUBLE) TIMESTAMP(ts) PARTITION BY DAY;INSERT INTO order_book VALUES  ('2024-05-22T09:40:15.006000Z', 40, 14.10, 47, 14.00, 39, 13.90, 54, 14.50, 36, 14.60, 23, 14.80),  ('2024-05-22T09:40:15.175000Z', 42, 14.00, 45, 13.90, 35, 13.80, 16, 14.30, 57, 14.50, 30, 14.60),  ('2024-05-22T09:40:15.522000Z', 36, 14.10, 38, 14.00, 31, 13.90, 30, 14.40, 47, 14.50, 34, 14.60);

SELECT ts, L2PRICE(100, askSize1, ask1, askSize2, ask2, askSize3, ask3) AS buy FROM order_book;
2024-05-22T09:40:15.006000Z14.565999999999
2024-05-22T09:40:15.175000Z14.495
2024-05-22T09:40:15.522000Z14.493

Trading price of instrument when selling 100:

SELECT ts, L2PRICE(100, bidSize1, bid1, bidSize2, bid2, bidSize3, bid3) AS sell FROM order_book;
tssell
2024-05-22T09:40:15.006000Z14.027
2024-05-22T09:40:15.175000Z13.929
2024-05-22T09:40:15.522000Z14.01

The spread for target quantity 100:

SELECT ts, L2PRICE(100, askSize1, ask1, askSize2, ask2, askSize3, ask3)  - L2PRICE(100, bidSize1, bid1, bidSize2, bid2, bidSize3, bid3) AS spread FROM order_book;
2024-05-22T09:40:15.006000Z0.538999999999
2024-05-22T09:40:15.175000Z0.565999999999
2024-05-22T09:40:15.522000Z0.483

## vwap​

vwap(price, quantity) - Calculates the volume-weighted average price (VWAP) based on the given price and quantity columns. This is a handy replacement for the sum(price * quantity) / sum(quantity) expression.

### Parameters​

• price is any numeric price value.
• quantity is any numeric quantity value.

### Return value​

Return value type is double.

### Examples​

SELECT vwap(x, x)FROM (SELECT x FROM long_sequence(100));
vwap
67