-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathsma.orig.js
71 lines (53 loc) · 1.79 KB
/
sma.orig.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
var values = [];
var length = null;
var cacheSum = null;
module.exports = {
getRunUpCount: function (PARAMETER) {
return PARAMETER;
},
// BID and ASK are close prices
onIntervalClose: function (PARAMETER, BID, ASK) {
var mid = (BID + ASK) / 2,
sum = 0;
// Push closing mid price to end of values array
values.push(mid);
// Store length (slightly faster than accessing repeatedly)
length = values.length;
// We haven't got enough run up data to produce a value, so return null
if (length < PARAMETER) {
return null;
}
// Re-indexing array is slow, so rather than splicing dropped value from the beginning each time we just splice out old data every so often
if (length > PARAMETER * 10) {
values.splice(0, length - PARAMETER);
length = values.length;
}
// Clear the onTick cacheSum optimisation
cacheSum = null;
// Sum the last n period values
for (var i = length - PARAMETER; i < length; i++) {
sum += values[i];
}
// Return the mean average
return sum / PARAMETER;
},
onTick: function (PARAMETER, BID, ASK) {
var mid = (BID + ASK) / 2,
sum = 0;
if (cacheSum === null) {
// Sum the last n period - 1 values
for (var i = length - PARAMETER + 1; i < length; i++) {
sum += values[i];
}
// Cache the summed value to save us iterating every tick
cacheSum = sum;
} else {
// Use the cached value
sum = cacheSum;
}
// Add the current value
sum += mid;
// Return the mean average
return sum / PARAMETER;
}
};