-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathbollingerBands.ts
57 lines (50 loc) · 1.28 KB
/
bollingerBands.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
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright (c) 2022 Onur Cinar. All Rights Reserved.
// https://github.com/cinar/indicatorts
import { add, multiplyBy, subtract } from '../../helper/numArray';
import { sma } from '../trend/simpleMovingAverage';
import { mstd } from './movingStandardDeviation';
/**
* Bollinger bands result object.
*/
export interface BBResult {
upper: number[];
middle: number[];
lower: number[];
}
/**
* Optional configuration of Bollinger bands parameters.
*/
export interface BBConfig {
period?: number;
}
/**
* The default configuration of Bollinger bands.
*/
export const BBDefaultConfig: Required<BBConfig> = {
period: 20,
};
/**
* Bollinger Bands.
*
* Middle Band = 20-Period SMA.
* Upper Band = 20-Period SMA + 2 (20-Period Std)
* Lower Band = 20-Period SMA - 2 (20-Period Std)
*
* @param closings closing values.
* @param config configuration.
* @return bollinger bands.
*/
export function bb(closings: number[], config: BBConfig = {}): BBResult {
const { period } = { ...BBDefaultConfig, ...config };
const std2 = multiplyBy(2, mstd(closings, { period }));
const middle = sma(closings, { period });
const upper = add(middle, std2);
const lower = subtract(middle, std2);
return {
upper,
middle,
lower,
};
}
// Export full name
export { bb as bollingerBands };