diff --git a/reference/function/finance.md b/reference/function/finance.md index 07739faf..c62207f2 100644 --- a/reference/function/finance.md +++ b/reference/function/finance.md @@ -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: @@ -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 @@ -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 | + diff --git a/reference/function/numeric.md b/reference/function/numeric.md index 9e129e3e..00040edc 100644 --- a/reference/function/numeric.md +++ b/reference/function/numeric.md @@ -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.