-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathkeltnerChannel.ts
68 lines (60 loc) · 1.61 KB
/
keltnerChannel.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
// Copyright (c) 2022 Onur Cinar. All Rights Reserved.
// https://github.com/cinar/indicatorts
import { add, multiplyBy, subtract } from '../../helper/numArray';
import { ema } from '../trend/exponentialMovingAverage';
import { atr } from './averageTrueRange';
/**
* Keltner channel result object.
*/
export interface KCResult {
upper: number[];
middle: number[];
lower: number[];
}
/**
* Optional configuration of KC parameters.
*/
export interface KCConfig {
period?: number;
}
/**
* The default configuration of KC.
*/
export const KCDefaultConfig: Required<KCConfig> = {
period: 20,
};
/**
* The Keltner Channel (KC) provides volatility-based bands that are placed
* on either side of an asset's price and can aid in determining the
* direction of a trend.
*
* Middle Line = EMA(period, closings)
* Upper Band = EMA(period, closings) + 2 * ATR(period, highs, lows, closings)
* Lower Band = EMA(period, closings) - 2 * ATR(period, highs, lows, closings)
*
* @param highs high values.
* @param lows low values.
* @param closings closing values.
* @param config configuration.
* @returns kc result.
*/
export function kc(
highs: number[],
lows: number[],
closings: number[],
config: KCConfig = {}
): KCResult {
const { period } = { ...KCDefaultConfig, ...config };
const atrResult = atr(highs, lows, closings, { period });
const atr2 = multiplyBy(2, atrResult.atrLine);
const middle = ema(closings, { period });
const upper = add(middle, atr2);
const lower = subtract(middle, atr2);
return {
middle,
upper,
lower,
};
}
// Export full name
export { kc as keltnerChannel };