-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
bollingerBandsWidth.ts
57 lines (49 loc) · 1.44 KB
/
bollingerBandsWidth.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 { divide, subtract } from '../../helper/numArray';
import { ema } from '../trend/exponentialMovingAverage';
import { BBResult } from './bollingerBands';
/**
* Bollinger bands width result.
*/
export interface BBWResult {
width: number[];
widthEma: number[];
}
/**
* Optional configuration of Bollinger bands width parameters.
*/
export interface BBWConfig {
period?: number;
}
/**
* The default configuration of Bollinger bands width.
*/
export const BBWDefaultConfig: Required<BBWConfig> = {
period: 90,
};
/**
* Bollinger Band Width. It measures the percentage difference between the
* upper band and the lower band. It decreases as Bollinger Bands narrows
* and increases as Bollinger Bands widens
*
* During a period of rising price volatity the band width widens, and
* during a period of low market volatity band width contracts.
*
* Band Width = (Upper Band - Lower Band) / Middle Band
*
* @param bb bollinger bands.
* @param config configuration.
* @return bollinger bands width result.
*/
export function bbw(bb: BBResult, config: BBWConfig = {}): BBWResult {
const { period } = { ...BBWDefaultConfig, ...config };
const width = divide(subtract(bb.upper, bb.lower), bb.middle);
const widthEma = ema(width, { period });
return {
width,
widthEma,
};
}
// Export full name
export { bbw as bollingerBandsWidth };