forked from Haehnchen/crypto-trading-bot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
awesome_oscillator_cross_zero.js
76 lines (61 loc) · 1.7 KB
/
awesome_oscillator_cross_zero.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
const SignalResult = require('../dict/signal_result');
module.exports = class AwesomeOscillatorCrossZero {
getName() {
return 'awesome_oscillator_cross_zero';
}
buildIndicator(indicatorBuilder, options) {
if (!options.period) {
throw 'Invalid period';
}
indicatorBuilder.add('ao', 'ao', options.period, options);
indicatorBuilder.add('sma200', 'sma', options.period, {
length: 200
});
}
period(indicatorPeriod) {
return this.macd(
indicatorPeriod.getPrice(),
indicatorPeriod.getIndicator('sma200'),
indicatorPeriod.getIndicator('ao'),
indicatorPeriod.getLastSignal()
);
}
macd(price, sma200Full, aoFull, lastSignal) {
if (aoFull.length <= 2 || sma200Full.length < 2) {
return;
}
// remove incomplete candle
const sma200 = sma200Full.slice(0, -1);
const ao = aoFull.slice(0, -1);
const debug = {
sma200: sma200.slice(-1)[0],
ao: ao.slice(-1)[0],
last_signal: lastSignal
};
const before = ao.slice(-2)[0];
const last = ao.slice(-1)[0];
// trend change
if ((lastSignal === 'long' && before > 0 && last < 0) || (lastSignal === 'short' && before < 0 && last > 0)) {
return SignalResult.createSignal('close', debug);
}
// sma long
const long = price >= sma200.slice(-1)[0];
if (long) {
// long
if (before < 0 && last > 0) {
return SignalResult.createSignal('long', debug);
}
} else {
// short
if (before > 0 && last < 0) {
return SignalResult.createSignal('short', debug);
}
}
return SignalResult.createEmptySignal(debug);
}
getOptions() {
return {
period: '15m'
};
}
};