-
Notifications
You must be signed in to change notification settings - Fork 41
/
dailyPivot.src.js
99 lines (78 loc) · 2.21 KB
/
dailyPivot.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
93
94
95
96
97
98
99
var msDay = 86400000,
today,
dailyPivot,
R1,
S1,
R2,
S2,
R3,
S3;
function getBufferSize () {
return 200;
}
function onIntervalClose () {
var yesterdayHLC;
if (!dailyPivot || today !== TIME.getUTCDay()) {
today = TIME.getUTCDay();
yesterdayHLC = getYesterdayHLC();
dailyPivot = Math.mean(yesterdayHLC);
R1 = (dailyPivot * 2) - yesterdayHLC[1];
S1 = (dailyPivot * 2) - yesterdayHLC[0];
R2 = dailyPivot + (yesterdayHLC[0] - yesterdayHLC[1]);
S2 = dailyPivot - (yesterdayHLC[0] - yesterdayHLC[1]);
R3 = R1 + (yesterdayHLC[0] - yesterdayHLC[1]);
S3 = S1 - (yesterdayHLC[0] - yesterdayHLC[1]);
}
return [{
name: 'R3',
value: R3,
color: "#90ed7d"
}, {
name: "R2",
value: R2,
color: "#90ed7d"
}, {
name: "R1",
value: R1,
color: "#90ed7d"
}, {
name: "dailyPivot",
value: dailyPivot,
color: "#7cb5ec"
}, {
name: "S1",
value: S1,
color: "#f15c80"
}, {
name: "S2",
value: S2,
color: "#f15c80"
}, {
name: "S3",
value: S3,
color: "#f15c80"
}];
}
function getYesterdayHLC () {
var lastMidnight = TIME.getTime() - (TIME.getTime() % msDay),
start = new Date(lastMidnight - msDay),
end = new Date(lastMidnight),
closes;
// If Sunday
if (TIME.getUTCDay() === 0) {
// Pivot calculated from Thursday's bar
start = new Date(lastMidnight - (3 * msDay));
end = new Date(lastMidnight - (2 * msDay));
// If Monday
} else if (TIME.getUTCDay() === 1) {
// Pivot calculated from Friday/Sunday bar
start = new Date(lastMidnight - (3 * msDay));
end = new Date(lastMidnight);
}
closes = prices.close(start, end); // Improve to retrieve only single bar (once added to API)
return [
Math.highest(prices.high(start, end)),
Math.lowest(prices.low(start, end)),
closes[closes.length - 1]
];
}