-
Notifications
You must be signed in to change notification settings - Fork 41
/
cci.src.js
64 lines (55 loc) · 1.53 KB
/
cci.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
var typicalPrices = [];
function getBufferSize () {
return 0;
}
function getRunUpCount (periods) {
return periods;
}
function getStudyAxisConfig () {
return {
gridLineWidth: 0,
plotLines: [
{value: 100, color: "#EEE", width: 1},
{value: 0, color: "#EEE", width: 1, dashStyle: "Dash"},
{value: -100, color: "#EEE", width: 1}
]
};
}
function validate (periods, constant) {
if (typeof periods !== "number") {
error("CCI periods must be a number");
}
if (periods % 1 !== 0) {
error("CCI periods must be an integer");
}
if (periods > 100) {
error("CCI maximum periods is 100");
}
if (periods <= 0) {
error("CCI periods must be greater than 0");
}
if (typeof constant !== "number") {
error("CCI constant must be a number");
}
if (constant <= 0) {
error("CCI constant must be greater than 0");
}
}
function onIntervalClose (periods, constant) {
var typicalPrice = (HIGH + LOW + CLOSE) / 3,
typicalPriceSMA,
meanDeviation;
typicalPrices.push(typicalPrice);
if (typicalPrices.length < periods) {
return null;
} else if (typicalPrices.length > periods) {
typicalPrices.shift();
}
typicalPriceSMA = Math.average(typicalPrices);
meanDeviation = Math.meanDeviation(typicalPrices);
return {
overlay: false,
value: (typicalPrice - typicalPriceSMA) / (constant * meanDeviation),
precision: 1
};
}