-
Notifications
You must be signed in to change notification settings - Fork 41
/
forceIndex.src.js
52 lines (44 loc) · 1.15 KB
/
forceIndex.src.js
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
var emaExponent,
forceIndices = [],
forceIndexEMA;
function getBufferSize (periods) {
return 2;
}
function getRunUpCount (periods) {
return periods;
}
function validate (periods) {
if (typeof periods !== "number") {
error("Force Index periods must be a number");
}
if (periods % 1 !== 0) {
error("Force Index periods must be an integer");
}
if (periods > 100) {
error("Force Index maximum periods is 100");
}
if (periods <= 0) {
error("Force Index periods must be greater than zero");
}
}
function onStart (periods) {
emaExponent = 2 / (periods + 1);
}
function onIntervalClose (periods) {
var forceIndex = (CLOSE - price(1)) * VOLUME;
if (forceIndexEMA === undefined) {
forceIndices.push(forceIndex);
if (forceIndices.length === periods) {
forceIndexEMA = Math.average(forceIndices);
} else {
return null;
}
} else {
forceIndexEMA = ((forceIndex - forceIndexEMA) * emaExponent) + forceIndexEMA;
}
return {
overlay: false,
value: forceIndexEMA,
type: "areaspline"
};
}