-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathaccelerationBands.ts
81 lines (72 loc) · 1.71 KB
/
accelerationBands.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright (c) 2022 Onur Cinar. All Rights Reserved.
// https://github.com/cinar/indicatorts
import {
add,
addBy,
checkSameLength,
divide,
multiply,
multiplyBy,
subtract,
} from '../../helper/numArray';
import { sma } from '../trend/simpleMovingAverage';
/**
* Acceleration bands result object.
*/
export interface ABResult {
upper: number[];
middle: number[];
lower: number[];
}
/**
* Optional configuration of acceleration bands parameters.
*/
export interface ABConfig {
period?: number;
multiplier?: number;
}
/**
* The default configuration of acceleration bands.
*/
export const ABDefaultConfig: Required<ABConfig> = {
period: 20,
multiplier: 4,
};
/**
* Acceleration Bands. Plots upper and lower envelope bands
* around a simple moving average.
*
* Upper Band = SMA(High * (1 + 4 * (High - Low) / (High + Low)))
* Middle Band = SMA(Closing)
* Lower Band = SMA(Low * (1 - 4 * (High - Low) / (High + Low)))
*
* @param highs high values.
* @param lows low values.
* @param closings closing values.
* @param config configuration.
* @return acceleration band.
*/
export function ab(
highs: number[],
lows: number[],
closings: number[],
config: ABConfig = {}
): ABResult {
checkSameLength(highs, lows, closings);
const { period, multiplier } = { ...ABDefaultConfig, ...config };
const k = divide(subtract(highs, lows), add(highs, lows));
const upper = sma(multiply(highs, addBy(1, multiplyBy(multiplier, k))), {
period,
});
const middle = sma(closings, { period });
const lower = sma(multiply(lows, addBy(1, multiplyBy(-1 * multiplier, k))), {
period,
});
return {
upper,
middle,
lower,
};
}
// Export full name
export { ab as accelerationBands };