Skip to content

Commit

Permalink
add new functions from recent releases (#60)
Browse files Browse the repository at this point in the history
Adds functions from 8.1.2 and some that were added earlier.
  • Loading branch information
nwoolmer authored Oct 7, 2024
1 parent d0e219a commit 21c36f8
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 4 deletions.
130 changes: 126 additions & 4 deletions reference/function/finance.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,19 @@ Let's take the below order book as an example.
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`
The full price of the trade:
$$
14 \cdot \$14.50 + 16 \cdot \$14.60 + (50 - 14 - 16) \cdot \$14.80 = \$732.60
$$



The average price of the instrument in this trade:

$$
\$732.60 / 50 = \$14.652
$$

This average trade price is the output of the function when executed with the parameters taken from
the above example:
Expand Down Expand Up @@ -115,11 +125,80 @@ SELECT ts, L2PRICE(100, askSize1, ask1, askSize2, ask2, askSize3, ask3)
| 2024-05-22T09:40:15.175000Z | 0.565999999999 |
| 2024-05-22T09:40:15.522000Z | 0.483 |


## mid

`mid(bid, ask)` - calculates the midpoint of a bidding price and asking price.

Returns null if either argument is NaN or null.

### Parameters

- `bid` is any numeric bidding price value.
- `ask` is any numeric asking price value.

### Return value

Return value type is `double`.

### Examples

```questdb-sql
SELECT mid(1.5760, 1.5763)
```

| mid |
|:-------------|
| 1.57615 |



## spread_bps

`spread_bps(bid, ask)` - calculates the quoted bid-ask spread, based on the highest bidding price,
and the lowest asking price.

The result is provided in basis points, and the calculation is:

$$
\frac
{\text{spread}\left(\text{bid}, \text{ask}\right)}
{\text{mid}\left(\text{bid}, \text{ask}\right)}
\cdot
10\,000
$$


### Parameters

- `bid` is any numeric bidding price value.
- `ask` is any numeric asking price value.

### Return value

Return value type is `double`.

### Examples

```questdb-sql
SELECT spread_bps(1.5760, 1.5763)
```

| spread_bps |
|:---------------|
| 1.903372140976 |

## 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.
based on the given price and quantity columns. This is defined by the following formula:

$$
\text{vwap} =
\frac
{\text{sum}\left(\text{price} \cdot \text{quantity}\right)}
{\text{sum}\left(\text{quantity}\right)}
$$

### Parameters

Expand All @@ -140,3 +219,46 @@ FROM (SELECT x FROM long_sequence(100));
| vwap |
| :--- |
| 67 |


## wmid

`wmid(bidSize, bidPrice, askPrice, askSize)` - calculates the weighted mid-price
for a sized bid/ask pair.

It is calculated with these formulae:

$$
\text{imbalance} =
\frac
{ \text{bidSize} }
{ \left( \text{bidSize} + \text{askSize} \right) }
$$

$$
\text{wmid} = \text{askPrice} \cdot \text{imbalance}
+ \text{bidPrice}
\cdot \left( 1 - \text{imbalance} \right)
$$

### Parameters

- `bidSize` is any numeric value representing the size of the bid offer.
- `bidPrice` is any numeric value representing the bidding price.
- `askPrice` is any numeric value representing the asking price.
- `askSize` is any numeric value representing the size of the ask offer.

### Return value

Return value type is `double`.

### Examples

```questdb-sql
SELECT wmid(100, 5, 6, 100)
```

| wmid |
|:-----|
| 5.5 |

50 changes: 50 additions & 0 deletions reference/function/numeric.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,56 @@ SELECT floor(15.75) as RoundedDown;
| --------- |
| 15 |


## greatest

`greatest(args...)` returns the largest entry in a series of numbers.

**Arguments:**

- `args...` is a variable-size list of `long` or `double` values.

**Return value:**

Return value type is `double` or `long`.

**Examples:**

```questdb-sql
SELECT greatest(11, 3, 8, 15)
```

| greatest |
|----------|
| 15 |



## least

`least(args...)` returns the smallest entry in a series of numbers.

**Arguments:**

- `args...` is a variable-size list of `long` or `double` values.

**Return value:**

Return value type is `double` or `long`.

**Examples:**

```questdb-sql
SELECT least(11, 3, 8, 15)
```

| least |
|-------|
| 3 |




## ln

`ln(value)` return the natural logarithm (**log*e***) of a given number.
Expand Down

0 comments on commit 21c36f8

Please sign in to comment.