forked from cinar/indicatorts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
movingMax.ts
45 lines (36 loc) · 985 Bytes
/
movingMax.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Copyright (c) 2022 Onur Cinar. All Rights Reserved.
// https://github.com/cinar/indicatorts
import { BinarySearchTree } from '../../helper/binarySearchTree';
/**
* Optional configuration of MMax parameters.
*/
export interface MMaxConfig {
period?: number;
}
/**
* The default configuration of MMax.
*/
export const MMaxDefaultConfig: Required<MMaxConfig> = {
period: 4,
};
/**
* Moving max for the given period.
* @param values values array.
* @param config configuration.
* @return moving max.
*/
export function mmax(values: number[], config: MMaxConfig = {}): number[] {
const { period } = { ...MMaxDefaultConfig, ...config };
const result = new Array<number>(values.length);
const bst = new BinarySearchTree();
for (let i = 0; i < values.length; i++) {
bst.insert(values[i]);
if (i >= period) {
bst.remove(values[i - period]);
}
result[i] = bst.max();
}
return result;
}
// Export full name
export { mmax as movingMax };