-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathmacd.src.js
87 lines (77 loc) · 2.33 KB
/
macd.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
var signalExponent,
macdValues = [],
macdEMA,
label,
signalLabel,
histogramLabel;
function getRunUpCount (fastEMAPeriods, slowEMAPeriods, signalEMAPeriods) {
return (slowEMAPeriods * 2) + (signalEMAPeriods || 0);
}
function getBufferSize (fastEMAPeriods, slowEMAPeriods, signalEMAPeriods) {
return slowEMAPeriods;
}
function validate (fastEMAPeriods, slowEMAPeriods, signalEMAPeriods) {
validateField("fastEMAPeriods", fastEMAPeriods);
validateField("slowEMAPeriods", slowEMAPeriods);
if (fastEMAPeriods >= slowEMAPeriods) {
error("MACD slowEMAPeriods must be greater than fastEMAPeriods");
}
if (signalEMAPeriods) {
validateField("signalEMAPeriods", signalEMAPeriods);
}
}
function validateField (fieldName, value) {
if (typeof value !== "number") {
error("MACD " + fieldName + " must be a number");
}
if (value % 1 !== 0) {
error("MACD " + fieldName + " must be an integer");
}
if (value > 100) {
error("MACD " + fieldName + " maximum is 100");
}
if (value <= 0) {
error("MACD " + fieldName + " must be greater than 0");
}
}
function onStart (fastEMAPeriods, slowEMAPeriods, signalEMAPeriods) {
label = "MACD(" + fastEMAPeriods + "," + slowEMAPeriods + "," + signalEMAPeriods + ")";
histogramLabel = label + " Histogram";
signalLabel = label + " Signal";
if (signalEMAPeriods) {
signalExponent = 2 / (signalEMAPeriods + 1);
}
}
function onIntervalClose (fastEMAPeriods, slowEMAPeriods, signalEMAPeriods) {
var macd = (ema(fastEMAPeriods) - ema(slowEMAPeriods)) / INSTRUMENT.PIP_SIZE;
if (!signalEMAPeriods) {
return {
value: macd,
overlay: false
};
}
if (macdEMA !== undefined) {
macdEMA = (macd * signalExponent) + (macdEMA * (1 - signalExponent));
} else {
macdValues.push(macd);
if (macdValues.length === signalEMAPeriods) {
macdEMA = Math.average(macdValues);
} else {
return null;
}
}
return [{
name: label,
value: macd,
overlay: false
}, {
name: signalLabel,
value: macdEMA,
overlay: false
}, {
name: histogramLabel,
value: macd - macdEMA,
overlay: false,
type: "column"
}];
}