mwsumTopN
Syntax
mwsumTopN(X, Y, S, window, top, [ascending=true],
[tiesMethod='oldest'])
Please see mTopN for the parameters and windowing logic.
Details
Within a sliding window of given length (measured by the number of elements), the function stably sorts X and Y by S in the order specified by ascending, then calculates the moving sums of X with Y as weights.
Returns
-
Returns a DOUBLE vector of the same length as the input when the input is a vector.
-
Returns a matrix with the same dimensions as the input when the input is a matrix.
-
Returns a table with the same schema as the input when the input is a table.
-
Returns a tuple with the corresponding structure when the input is a tuple.
Examples
Using IBM stock as an example, simulate trading prices, trading volumes, and turnover rates for six consecutive trading days:
symbol = take(`IBM, 6)
tradeDate = 2024.01.02 2024.01.03 2024.01.04 2024.01.05 2024.01.08 2024.01.09
tradePrice = [182.5, 183.8, 181.2, 184.6, 183.1, 185.0]
tradeVolume = [520, 860, 610, 940, 650, 880]
turnoverRate = [1.8, 2.6, 1.9, 3.1, 2.1, 2.8]
stockDaily = table(symbol, tradeDate, tradePrice, tradeVolume, turnoverRate)
stockDaily;
Output:
| symbol | tradeDate | tradePrice | tradeVolume | turnoverRate |
|---|---|---|---|---|
| IBM | 2024.01.02 | 182.5 | 520 | 1.8 |
| IBM | 2024.01.03 | 183.8 | 860 | 2.6 |
| IBM | 2024.01.04 | 181.2 | 610 | 1.9 |
| IBM | 2024.01.05 | 184.6 | 940 | 3.1 |
| IBM | 2024.01.08 | 183.1 | 650 | 2.1 |
| IBM | 2024.01.09 | 185 | 880 | 2.8 |
Over the most recent four trading days, select the top two trading days by turnover rate and calculate the inner product of tradePrice and tradeVolume:
mwsumTopN(X=tradePrice, Y=tradeVolume, S=turnoverRate, window=4, top=2, ascending=false)
// Output: [94,900, 252,968, 268,600, 331,592, 331,592, 336,324]
- turnoverRate is used to select the trading days with the highest turnover rates;
- For the data on 2024.01.09, within the most recent 4-day window, the two trading days with the highest turnover rates correspond to the samples (184.6, 940) and (185.0, 880). Their inner product is 184.6*940 + 185.0*880 = 336,324.
Related function: mwsum
