-
Notifications
You must be signed in to change notification settings - Fork 41
/
atr.src.js
69 lines (59 loc) · 1.84 KB
/
atr.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
var previousClose,
trueRangeValues = [],
averageTrueRange;
function validate (periods) {
if (typeof periods !== "number") {
error("Average True Range periods must be a number");
}
if (periods % 1 !== 0) {
error("Average True Range periods must be an integer");
}
if (periods > 100) {
error("Average True Range maximum periods is 100");
}
if (periods <= 0) {
error("Average True Range periods must be greater than zero");
}
}
function getRunUpCount (periods) {
return periods * 2;
}
function getBufferSize (periods) {
return 0;
}
function onIntervalClose (periods) {
var trueRange;
if (!previousClose) {
previousClose = CLOSE;
return null;
}
trueRange = Math.max(
HIGH - LOW,
Math.abs(HIGH - previousClose),
Math.abs(LOW - previousClose)
);
previousClose = CLOSE;
if (trueRangeValues.length < periods) {
trueRangeValues.push(trueRange);
if (trueRangeValues.length < periods) {
return null;
} else {
averageTrueRange = Math.average(trueRangeValues);
return {
overlay: false,
value: averageTrueRange
};
}
} else {
// NOTE
// This smoothing formula is given here: http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:average_true_range_a
// and http://en.wikipedia.org/wiki/Average_true_range
// though some sources e.g. http://user42.tuxfamily.org/chart/manual/Average-True-Range.html#Average-True-Range
// smooth with EMA. These produce different results
averageTrueRange = ((averageTrueRange * (periods - 1)) + trueRange) / periods;
return {
overlay: false,
value: averageTrueRange
};
}
}