Cumulative (Aggregation) Expressions

View as Markdown

Summary

  • AdapTableQL performs cumulative aggregations with a CUMULATIVE clause on an Aggregation Function (SUM{[PnL], CUMULATIVE([TradeDate])})

Cumulative Expressions, as the name suggests, perform cumulative Aggregations.

Hint

  • This is particularly useful when calculating Running Totals
  • AG Grid has no running totals: its formulas can only reach another row by its fixed row number

Cumulative Expressions are actually a special case of Aggregation Expression but where the Aggregation Operation is applied to each row in the set cumulatively.

Why use Cumulative Expressions?

  • Run values in one line: running PnL, running Notional, highest PnL so far - SUM{[PnL], CUMULATIVE([TradeDate])}
  • Per group too: add GROUP_BY and each Book, Desk or Currency gets its own running total
  • Order comes from data: running value follows TradeDate (or any date / number Column), whatever the Grid sort
  • Always live: edit or add a trade and every running value after it updates
  • Filter what counts: add WHERE to keep a running total of just Buy trades, or just Open positions

Aggregation Operations

There are 5 Aggregation Operations available when using Cumulative Aggregations:

  • SUM
  • MIN
  • MAX
  • AVG
  • COUNT

Using CUMULATIVE to set Order

The Aggregation Operation is applied in a specific order.

That order is specified by the CUMULATIVE clause, which takes the name of an Adaptable Column as its first parameter.

Note

The CUMULATIVE clause only works with Columns of type date or number

// Derive cumulative sum of all PnL columns according to TradeDate order
 SUM{[PnL], CUMULATIVE([TradeDate])};
 // Calculate cumulative maximum value of all PnL columns according to TradeDate order
 MAX{[PnL], CUMULATIVE([TradeDate])};

Hint

  • There is no need physically to sort the Grid using the column specified by CUMULATIVE
  • It ignores the current Grid Sort and uses the order in the given Column

Sort Direction

By default the Aggregation accumulates in ascending order (smallest or earliest first).

To accumulate in the opposite direction, pass 'DESC' as an optional second parameter to CUMULATIVE ('ASC' is the default and can be omitted).

// Running total of PnL from the most recent trade backwards
 SUM{[PnL], CUMULATIVE([TradeDate], 'DESC')};

Note

Rows with a blank value in the CUMULATIVE Column are always placed last - in both ascending and descending order

Filtering Rows

A WHERE clause restricts which rows are accumulated - see WHERE Clause for full details:

// Running total of PnL for Buy trades only
 SUM{[PnL] WHERE [Side] = 'Buy', CUMULATIVE([TradeDate])};

Note

Rows excluded by WHERE return a blank value

Running Weighted Average

AVG with a WEIGHT clause can also be cumulative - e.g. a running VWAP (Volume-Weighted Average Price):

// Running VWAP - average Price weighted by Quantity, in TradeTime order
 AVG{[Price], WEIGHT([Quantity]), CUMULATIVE([TradeTime])};
AdapTableQL: Cumulative Aggregation
Fork
  • This demo contains 2 Calculated Columns that use Cumulative Aggregation Scalar Expressions:
    • Cumul Stars - displays the cumulative sum of stars count of all repos in the grid, aggregated over the Created date
    • Max Technical Debt - shows the cumulative maxima of open issues of all repos in the grid, aggregated over the GitHub Stars count

Expand to see the Aggregated Cumulative Scalar Expressions

Loading demo…

Grouped Cumulative Aggregation

Like with Standard Scalar Aggregations, Cumulative Aggregations can be grouped.

Again this is accomplished by using the GROUP_BY clause.

Caution

GROUP_BY is a clause on the Aggregation Function (e.g. SUM), together with CUMULATIVE

// Cumulative sum of all PnL columns ordering by TradeDate, grouping by Currency
 SUM{[PnL], GROUP_BY([currency]), CUMULATIVE([TradeDate])};

Similar to Standard Scalar Aggregations, there is no limit on the number of Columns which can be grouped.

// Cumulative sum of all PnL columns ordering by TradeDate, grouping by Currency and Country
 SUM{[PnL], GROUP_BY([currency], [country]), CUMULATIVE([TradeDate])};
AdapTableQL: Cumulative Aggregations Grouped
Fork
  • This demo contains 2 Calculated Columns that use Cumulative Aggregation Scalar Expressions, one with grouping:
    • Cumul Stars - displays the cumulative sum of stars count of all repos in the grid, aggregated over the Created date (same as in demo above)
    • Cumul Lang Stars - displays the cumulative sum of stars count in the grid, but grouped by Language, and aggregated over the Created date

Expand to see the Aggregated Cumulative Scalar Expressions

Loading demo…