-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetectcrossover.js
More file actions
36 lines (29 loc) · 805 Bytes
/
detectcrossover.js
File metadata and controls
36 lines (29 loc) · 805 Bytes
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
_ = require('underscore');
var fastAboveSlow; //Initialize to the right value!
function xOverArray(fastarr, slowarr, buy, sell) {
if(fastarr.length != slowarr.length) {
throw "length of the fast and slow datasets are different";
}
if(typeof fastAboveSlow === 'undefined') {
fastAboveSlow = fastarr[0] > slowarr[0];
}
_.map(_.zip(fastarr, slowarr), function(arr) {
xOverHelper(arr[0], arr[1], buy, sell);
});
}
function xOver(fast, slow, buy, sell) {
if(typeof fastAboveSlow === 'undefined') {
fastAboveSlow = fastarr[0] > slowarr[0];
}
xOverHelper(fast, slow, buy, sell);
}
function xOverHelper(fast, slow, buy, sell) {
if(fastAboveSlow != fast > slow) {
fastAboveSlow = !fastAboveSlow;
if(fastAboveSlow) { //If fast just overtook slow
buy();
} else {
sell();
}
}
}