-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
vortex.ts
95 lines (83 loc) · 2.24 KB
/
vortex.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (c) 2022 Onur Cinar. All Rights Reserved.
// https://github.com/cinar/indicatorts
import {
abs,
checkSameLength,
divide,
max,
shiftRightBy,
subtract,
} from '../../helper/numArray';
import { msum } from './movingSum';
/**
* Vortex result.
*/
export interface VortexResult {
plus: number[];
minus: number[];
}
/**
* Optional configuration of vortex parameters.
*/
export interface VortexConfig {
period?: number;
}
/**
* The default configuration of vortex.
*/
export const VortexDefaultConfig: Required<VortexConfig> = {
period: 14,
};
/**
* Vortex Indicator. It provides two oscillators that capture positive and
* negative trend movement. A bullish signal triggers when the positive
* trend indicator crosses above the negative trend indicator or a key
* level. A bearish signal triggers when the negative trend indicator
* crosses above the positive trend indicator or a key level.
*
* +VM = Abs(Current High - Prior Low)
* -VM = Abs(Current Low - Prior High)
*
* +VM14 = 14-Period Sum of +VM
* -VM14 = 14-Period Sum of -VM
*
* TR = Max((High[i]-Low[i]), Abs(High[i]-Closing[i-1]), Abs(Low[i]-Closing[i-1]))
* TR14 = 14-Period Sum of TR
*
* +VI14 = +VM14 / TR14
* -VI14 = -VM14 / TR14
*
* Based on https://school.stockcharts.com/doku.php?id=technical_indicators:vortex_indicator
*
* @param highs high values.
* @param lows low values.
* @param closings closing values.
* @param config configuration.
* @return vortex result.
*/
export function vortex(
highs: number[],
lows: number[],
closings: number[],
config: VortexConfig = {}
): VortexResult {
checkSameLength(highs, lows, closings);
const { period } = { ...VortexDefaultConfig, ...config };
const prevClosings = shiftRightBy(1, closings);
const plusVm = abs(subtract(highs, shiftRightBy(1, lows)));
const minusVm = abs(subtract(lows, shiftRightBy(1, highs)));
const plusVmSum = msum(plusVm, { period });
const minusVmSum = msum(minusVm, { period });
const tr = max(
subtract(highs, lows),
abs(subtract(highs, prevClosings)),
abs(subtract(lows, prevClosings))
);
const trSum = msum(tr, { period });
const plus = divide(plusVmSum, trSum);
const minus = divide(minusVmSum, trSum);
return {
plus,
minus,
};
}