-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
movingAverageConvergenceDivergence.ts
58 lines (50 loc) · 1.25 KB
/
movingAverageConvergenceDivergence.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
// Copyright (c) 2022 Onur Cinar. All Rights Reserved.
// https://github.com/cinar/indicatorts
import { subtract } from '../../helper/numArray';
import { ema } from './exponentialMovingAverage';
export interface MACDResult {
macdLine: number[];
signalLine: number[];
}
/**
* Optional configuration of MACD parameters.
*/
export interface MACDConfig {
fast?: number;
slow?: number;
signal?: number;
}
/**
* The default configuration of MACD.
*/
export const MACDDefaultConfig: Required<MACDConfig> = {
fast: 12,
slow: 26,
signal: 9,
};
/**
* Moving Average Convergence Divergence (MACD).
*
* MACD = 12-Period EMA - 26-Period EMA.
* Signal = 9-Period EMA of MACD.
*
* @param closings closing values.
* @param config configuration.
* @return macd result.
*/
export function macd(closings: number[], config: MACDConfig = {}): MACDResult {
const { fast, slow, signal } = {
...MACDDefaultConfig,
...config,
};
const emaFast = ema(closings, { period: fast });
const emaSlow = ema(closings, { period: slow });
const macdLine = subtract(emaFast, emaSlow);
const signalLine = ema(macdLine, { period: signal });
return {
macdLine,
signalLine,
};
}
// Export full name
export { macd as movingAverageConvergenceDivergence };