Skip to content

Document N-dimensional arrays #160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 45 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
8fdb8c3
Improve docs for other types
mtopolnik Mar 28, 2025
1f68487
Add docs for ARRAY
mtopolnik Mar 28, 2025
915c077
Merge branch 'main' into mt_array
mtopolnik Mar 28, 2025
7f03a57
Don't use footnotes
mtopolnik Mar 28, 2025
abf268a
Add IPv4 limitation to ILP Limitations
mtopolnik Mar 31, 2025
0741ba6
Generally improve docs
mtopolnik Mar 31, 2025
3dcf965
Move array docs to Concepts
mtopolnik Mar 31, 2025
91f1a53
Remove outdated note
mtopolnik Apr 9, 2025
1aad93c
Merge branch 'main' into mt_array
mtopolnik May 13, 2025
3b68314
Document dim_length() and out-of-bounds access
mtopolnik May 13, 2025
a9f2ed5
Merge branch 'main' into mt_array
mtopolnik May 20, 2025
3f7f7f7
New page for array functions
mtopolnik May 20, 2025
77ad0e9
Update sidebars
mtopolnik May 20, 2025
1e4ab1e
broken link fixed
jerrinot May 21, 2025
cf1d39d
Proper SQL examples with results in Array Concept
mtopolnik May 21, 2025
3d5600c
execute many
jerrinot May 21, 2025
589c2ac
inserting arrays with asyncpg
jerrinot May 21, 2025
edeef25
Fix parsing errors, improve
mtopolnik May 21, 2025
bea799d
Improve examples in arry functions
mtopolnik May 21, 2025
13acbbe
Document protocol version config in ILP
mtopolnik May 21, 2025
bf01db4
Improve rendering of examples
mtopolnik May 21, 2025
20ae7ba
Demote subheadings in Aggregate Functions
mtopolnik May 21, 2025
98950c2
Touch up Finance page
mtopolnik May 21, 2025
5eb4f66
better wording
jerrinot May 23, 2025
263542b
better wording
jerrinot May 23, 2025
6f4abb2
links to anchors
jerrinot May 23, 2025
9445872
Merge branch 'main' into mt_array
mtopolnik May 23, 2025
00d8d6f
Add ARRAY literal section in Concepts
mtopolnik May 23, 2025
2041417
inserting arrays with psycopg3
jerrinot May 23, 2025
16431e2
explain binary array transfers with psycopg3
jerrinot May 23, 2025
ee55757
binary for all psycopg3 transfers
jerrinot May 26, 2025
24183bb
asyncpg and timezones explained
jerrinot May 26, 2025
774ff8e
inserting arrays via pgwire
jerrinot May 26, 2025
a1d7584
Add note on limited Beta support
mtopolnik May 26, 2025
5cc187d
Auto-style on java_ilp.md
mtopolnik May 26, 2025
97a3fa4
Document array ingestion for Java ILP client
mtopolnik May 26, 2025
5b6bcdc
Touch up explanation of "transpose"
mtopolnik May 27, 2025
0f0bd91
Touch up performance explanation
mtopolnik May 27, 2025
f6f8cdc
Rephrase explanation of storing the array
mtopolnik May 27, 2025
454b3a4
Move sample array to higher level
mtopolnik May 27, 2025
df4a9a4
document `ndarray` and `protocol_version` in java and rust client.
kafka1991 May 28, 2025
4a02f24
add c/c++/rust array example.
kafka1991 May 28, 2025
f3d0402
Improve ILP config page
mtopolnik May 28, 2025
c2e6b79
Improve Rust ILP page
mtopolnik May 28, 2025
7a4659f
fix c/java protocol_version part.
kafka1991 May 29, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 88 additions & 15 deletions documentation/reference/function/finance.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ This page describes functions specific to the financial services domain.

Trade price calculation.

`l2price(target_quantity, quantity_1, price_1, quantity_2, price_2, ..., quantity_n, price_n)`
`l2price(target_size, size_array, price_array)`

Consider `quantity_1`, `price_1`, `quantity_2`, `price_2`, ..., `quantity_n`,
`l2price(target_size, size_1, price_1, size_2, price_2, ..., size_n, price_n)`

Consider `size_1`, `price_1`, `size_2`, `price_2`, ..., `size_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.
executed with the size of `target_size` against the book.

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

Expand Down Expand Up @@ -45,6 +47,12 @@ $$
This average trade price is the output of the function when executed with the
parameters taken from the above example:

```questdb-sql
select l2price(50, ARRAY[14.0, 16.0, 23.0, 12.0], ARRAY[14.50, 14.60, 14.80, 15.10]);
```

or

```questdb-sql
select l2price(50, 14, 14.50, 16, 14.60, 23, 14.80, 12, 15.10);
```
Expand All @@ -55,25 +63,89 @@ select l2price(50, 14, 14.50, 16, 14.60, 23, 14.80, 12, 15.10);

### Parameters

The function takes a `target quantity`, and a variable number of
`quantity`/`price` pairs. Each represents a price level of the order book.
There are two variants of the function, one accepting arrays of numbers,
and the other accepting individual numbers:

The variant with arrays takes a `target size`, and a pair of arrays of type
`DOUBLE[]`: `size` and `price`. The arrays must match in length. Each
element of the array 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).
The variant with individual numbers takes a `target size`, and a variable
number of `size`/`price` pairs of type `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.
- `target_size`: The size of a hypothetical market order to be filled.
- `size*`: The sizes of offers available at the corresponding price levels (can
be fractional).
- `price*`: Price levels of the order book.

### Return value

The function returns with a `double`, representing the average trade price.
The function returns 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).
It returns `NULL` if the price is not calculable. For example, if the target
size cannot be filled, or there is incomplete data in the set (nulls).

### Examples
### Examples - ARRAY

Test data:

```questdb-sql
CREATE TABLE order_book (
ts TIMESTAMP,
bidSize DOUBLE[], bid DOUBLE[],
askSize DOUBLE[], ask DOUBLE[]
) TIMESTAMP(ts) PARTITION BY DAY;

INSERT INTO order_book VALUES
('2024-05-22T09:40:15.006000Z',
ARRAY[40.0, 47.0, 39.0], ARRAY[14.10, 14.00, 13.90],
ARRAY[54.0, 36.0, 23.0], ARRAY[14.50, 14.60, 14.80]),
('2024-05-22T09:40:15.175000Z',
ARRAY[42.0, 45.0, 35.0], ARRAY[14.00, 13.90, 13.80],
ARRAY[16.0, 57.0, 30.0], ARRAY[14.30, 14.50, 14.60]),
('2024-05-22T09:40:15.522000Z',
ARRAY[36.0, 38.0, 31.0], ARRAY[14.10, 14.00, 13.90],
ARRAY[30.0, 47.0, 34.0], ARRAY[14.40, 14.50, 14.60]);
```

Trading price of instrument when buying 100:

```questdb-sql
SELECT ts, L2PRICE(100, askSize, ask) AS buy FROM order_book;
```

| ts | buy |
| --------------------------- | --------------- |
| 2024-05-22T09:40:15.006000Z | 14.565999999999 |
| 2024-05-22T09:40:15.175000Z | 14.495 |
| 2024-05-22T09:40:15.522000Z | 14.493 |

Trading price of instrument when selling 100:

```questdb-sql
SELECT ts, L2PRICE(100, bidSize, bid) AS sell FROM order_book;
```

| ts | sell |
| --------------------------- | ------ |
| 2024-05-22T09:40:15.006000Z | 14.027 |
| 2024-05-22T09:40:15.175000Z | 13.929 |
| 2024-05-22T09:40:15.522000Z | 14.01 |

The spread for target quantity 100:

```questdb-sql
SELECT ts, L2PRICE(100, askSize, ask) - L2PRICE(100, bidSize, bid) AS spread FROM order_book;
```

| ts | spread |
| --------------------------- | -------------- |
| 2024-05-22T09:40:15.006000Z | 0.538999999999 |
| 2024-05-22T09:40:15.175000Z | 0.565999999999 |
| 2024-05-22T09:40:15.522000Z | 0.483 |

### Examples - scalar columns

Test data:

Expand Down Expand Up @@ -114,7 +186,7 @@ SELECT ts, L2PRICE(100, bidSize1, bid1, bidSize2, bid2, bidSize3, bid3) AS sell
| 2024-05-22T09:40:15.175000Z | 13.929 |
| 2024-05-22T09:40:15.522000Z | 14.01 |

The spread for target quantity 100:
The spread for target size of 100:

```questdb-sql
SELECT ts, L2PRICE(100, askSize1, ask1, askSize2, ask2, askSize3, ask3)
Expand Down Expand Up @@ -172,6 +244,7 @@ b_0 = \bar{y} - b_1 \bar{x}
$$

Where:

- $\bar{y}$ is the mean of y values
- $\bar{x}$ is the mean of x values
- $b_1$ is the slope calculated by `regr_slope(y, x)`
Expand Down
5 changes: 5 additions & 0 deletions documentation/reference/operators/numeric.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ sidebar_label: Numeric
description: Numeric operators
---

These operations work for any numeric types. Also, addition and multiplication
work for N-dimensional arrays. The result will be an array where each element is
the result of applying the operation to the elements at the same coordinates in
the operand arrays.

## `*` Multiply

`*` is a binary operation to multiply two numbers together.
Expand Down
Loading