-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
donchianChannel.ts
61 lines (54 loc) · 1.56 KB
/
donchianChannel.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
// Copyright (c) 2022 Onur Cinar. All Rights Reserved.
// https://github.com/cinar/indicatorts
import { add, divideBy } from '../../helper/numArray';
import { mmax } from '../trend/movingMax';
import { mmin } from '../trend/movingMin';
/**
* Donchian channel result object.
*/
export interface DCResult {
upper: number[];
middle: number[];
lower: number[];
}
/**
* Optional configuration of DC parameters.
*/
export interface DCConfig {
period?: number;
}
/**
* The default configuration of DC.
*/
export const DCDefaultConfig: Required<DCConfig> = {
period: 4,
};
/**
* The Donchian Channel (DC) calculates three lines generated by moving average
* calculations that comprise an indicator formed by upper and lower bands
* around a midrange or median band. The upper band marks the highest
* price of an asset while the lower band marks the lowest price of
* an asset, and the area between the upper and lower bands
* represents the Donchian Channel.
*
* Upper Channel = Mmax(closings, { period })
* Lower Channel = Mmin(closings, { period })
* Middle Channel = (Upper Channel + Lower Channel) / 2
*
* @param closings closing values.
* @param config configuration.
* @returns dc result.
*/
export function dc(closings: number[], config: DCConfig = {}): DCResult {
const { period } = { ...DCDefaultConfig, ...config };
const upper = mmax(closings, { period });
const lower = mmin(closings, { period });
const middle = divideBy(2, add(upper, lower));
return {
upper,
middle,
lower,
};
}
// Export full name
export { dc as donchianChannel };