msumTopN

New in version 2.00.4.

语法

msumTopN(X, S, window, top, [ascending=true], [tiesMethod=’oldest’])

参数说明和窗口计算规则请参考: mTopN 系列

详情

在给定长度(以元素个数衡量)的滑动窗口内,根据 ascending 指定的排序方式将 X 按照 S 进行稳定排序后,取前 top 个元素进行求和计算。

例子

$ X = 1..7
$ S = 0.3 0.5 0.1 0.1 0.5 0.2 0.4
$ msumTopN(X, S, 4, 2)
[1,3,4,7,7,7,10]

$ X = NULL 1 2 3 4 NULL 5
$ S = 3 5 1 1 5 2 4
$ msumTopN(X, S, 4, 2)
[,1,2,5,5,5,3]

$ X = matrix(1..5, 6..10)
$ S = 2022.01.01 2022.02.03 2022.01.23 2022.04.06 2021.12.29
$ msumTopN(X, S, 3, 2)

#0

#1

1

6

3

13

4

14

5

15

8

18

$ X = matrix(1..5, 6..10)
$ S = matrix(2022.01.01 2022.02.03 2022.01.23 NULL 2021.12.29,NULL 2022.02.03 2022.01.23 2022.04.06 NULL)
$ msumTopN(X, S, 3, 2)

#0

#1

1

3

7

4

15

5

15

8

17

给定的行情数据表包含四个列:股票代码(code)、交易日期(date)、收盘价(close)和交易量(volume)。

$ t = table(take(`IBM`APPL, 20) as code, 2020.01.01 + 1..20 as date, rand(100,20) + 20 as volume, rand(10,20) + 100.0 as close)

code

date

volume

close

IBM

2020.01.02

50

107

APPL

2020.01.03

55

100

IBM

2020.01.04

75

100

APPL

2020.01.05

84

108

IBM

2020.01.06

46

103

APPL

2020.01.07

100

101

IBM

2020.01.08

96

100

APPL

2020.01.09

84

102

IBM

2020.01.10

60

107

APPL

2020.01.11

40

103

IBM

2020.01.12

92

105

APPL

2020.01.13

61

106

IBM

2020.01.14

86

107

APPL

2020.01.15

41

102

IBM

2020.01.16

85

103

APPL

2020.01.17

72

105

IBM

2020.01.18

46

108

APPL

2020.01.19

25

100

IBM

2020.01.20

114

102

APPL

2020.01.21

50

104

对每支股票,在一个长度为5的窗口内,计算交易量最大的3条记录的收盘价之和。

$ select code, date, msumTopN(close, volume, 5, 3, false) from t context by code

code

date

msumTopN_close

APPL

2020.01.03

100

APPL

2020.01.05

208

APPL

2020.01.07

309

APPL

2020.01.09

311

APPL

2020.01.11

311

APPL

2020.01.13

311

APPL

2020.01.15

309

APPL

2020.01.17

313

APPL

2020.01.19

313

APPL

2020.01.21

315

IBM

2020.01.02

107

IBM

2020.01.04

207

IBM

2020.01.06

310

IBM

2020.01.08

307

IBM

2020.01.10

307

IBM

2020.01.12

305

IBM

2020.01.14

312

IBM

2020.01.16

312

IBM

2020.01.18

315

IBM

2020.01.20

314

相关函数:msum