-
Notifications
You must be signed in to change notification settings - Fork 41
/
vtx.src.js
72 lines (61 loc) · 1.76 KB
/
vtx.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
var positiveMovements = [],
negativeMovements = [],
trueRanges = [],
positiveLabel,
negativeLabel;
function getRunUpCount (periods) {
return periods;
}
function getBufferSize (periods) {
return 2;
}
function getStudyAxisConfig () {
return {
plotLines: [
{value: 0.9, width: 1, color: "#EEE"},
{value: 1.1, width: 1, color: "#EEE"}
]
};
}
function validate (periods) {
if (typeof periods !== "number") {
error("Vortex Indicator periods must be a number");
}
if (periods % 1 !== 0) {
error("Vortex Indicator periods must be an integer");
}
if (periods > 100) {
error("Vortex Indicator maximum period is 100");
}
if (periods <= 0) {
error("Vortex Indicator periods must be greater than 0");
}
}
function onStart (periods) {
positiveLabel = "vtx(" + periods + ") Positive";
negativeLabel = "vtx(" + periods + ") Negative";
}
function onIntervalClose (periods) {
var previousClose = price.close(1),
trueRangeSum;
positiveMovements.push(HIGH - price.low(1));
negativeMovements.push(LOW - price.high(1));
trueRanges.push(Math.max(HIGH - LOW, Math.abs(HIGH - previousClose), Math.abs(LOW - previousClose)));
if (positiveMovements.length < periods) {
return null;
} else if (positiveMovements.length > periods) {
positiveMovements.shift();
negativeMovements.shift();
trueRanges.shift();
}
trueRangeSum = Math.sum(trueRanges);
return [{
name: negativeLabel,
overlay: false,
value: -Math.sum(negativeMovements) / trueRangeSum
}, {
name: positiveLabel,
overlay: false,
value: Math.sum(positiveMovements) / trueRangeSum
}];
}