forked from cinar/indicatorts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chaikinOscillator.ts
66 lines (59 loc) · 1.62 KB
/
chaikinOscillator.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
// Copyright (c) 2022 Onur Cinar. All Rights Reserved.
// https://github.com/cinar/indicatorts
import { subtract } from '../../helper/numArray';
import { ema } from '../trend/exponentialMovingAverage';
import { ad } from '../volume/accumulationDistribution';
/**
* Chaikin oscillator result object.
*/
export interface CMOResult {
adResult: number[];
cmoResult: number[];
}
/**
* Optional configuration of Chaikin oscillator parameters.
*/
export interface CMOConfig {
fast?: number;
slow?: number;
}
/**
* The default configuration of Chaikin oscillator.
*/
export const CMODefaultConfig: Required<CMOConfig> = {
fast: 3,
slow: 10,
};
/**
* The ChaikinOscillator function measures the momentum of the
* Accumulation/Distribution (A/D) using the Moving Average
* Convergence Divergence (MACD) formula. It takes the
* difference between fast and slow periods EMA of the A/D.
* Cross above the A/D line indicates bullish.
*
* CO = Ema(fastPeriod, AD) - Ema(slowPeriod, AD)
*
* @param highs high values.
* @param lows low values.
* @param closings closing values.
* @param volumes volume values.
* @param config configuration.
* @return chaikin oscillator.
*/
export function cmo(
highs: number[],
lows: number[],
closings: number[],
volumes: number[],
config: CMOConfig = {}
): CMOResult {
const { fast, slow } = { ...CMODefaultConfig, ...config };
const adResult = ad(highs, lows, closings, volumes);
const cmoResult = subtract(
ema(adResult, { period: fast }),
ema(adResult, { period: slow })
);
return { adResult, cmoResult };
}
// Export full name
export { cmo as chaikinOscillator };