-
Notifications
You must be signed in to change notification settings - Fork 41
/
pvo.src.js
92 lines (78 loc) · 2.42 KB
/
pvo.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
88
89
90
91
92
var signalExponent,
pvoValues = [],
pvoEMA,
label,
signalLabel,
histogramLabel;
function getRunUpCount (fastEMAPeriods, slowEMAPeriods, signalEMAPeriods) {
return (slowEMAPeriods * 2) + (signalEMAPeriods || 0);
}
function validate (fastEMAPeriods, slowEMAPeriods, signalEMAPeriods) {
validateField("fastEMAPeriods", fastEMAPeriods);
validateField("slowEMAPeriods", slowEMAPeriods);
if (fastEMAPeriods >= slowEMAPeriods) {
error("Price Volume Oscillator slowEMAPeriods must be greater than fastEMAPeriods");
}
if (signalEMAPeriods) {
validateField("signalEMAPeriods", signalEMAPeriods);
}
}
function validateField (fieldName, value) {
if (typeof value !== "number") {
error("Price Volume Oscillator " + fieldName + " must be a number");
}
if (value % 1 !== 0) {
error("Price Volume Oscillator " + fieldName + " must be an integer");
}
if (value > 100) {
error("Price Volume Oscillator " + fieldName + " maximum is 100");
}
if (value <= 0) {
error("Price Volume Oscillator " + fieldName + " must be greater than 0");
}
}
function onStart (fastEMAPeriods, slowEMAPeriods, signalEMAPeriods) {
if (signalEMAPeriods) {
label = "Price Volume Oscillator (" + fastEMAPeriods + "," + slowEMAPeriods + "," + signalEMAPeriods + ")";
signalLabel = label + " Signal";
histogramLabel = label + " Histogram";
signalExponent = 2 / (signalEMAPeriods + 1);
}
}
function onIntervalClose (fastEMAPeriods, slowEMAPeriods, signalEMAPeriods) {
var fastEMA = ema(fastEMAPeriods),
slowEMA = ema(slowEMAPeriods),
pvo;
if (!fastEMA || !slowEMA) {
return null;
}
pvo = ((fastEMA - slowEMA) / slowEMA) * 100;
if (!signalEMAPeriods) {
return {
value: pvo,
overlay: false
};
}
if (pvoEMA !== undefined) {
pvoEMA = ((pvo - pvoEMA) * signalExponent) + pvoEMA;
} else if (pvoValues.length === signalEMAPeriods) {
pvoEMA = Math.average(pvoValues);
} else {
pvoValues.push(pvo);
return null;
}
return [{
name: label,
value: pvo,
overlay: false
}, {
name: signalLabel,
value: pvoEMA,
overlay: false
}, {
name: histogramLabel,
value: pvo - pvoEMA,
overlay: false,
type: "column"
}];
}