-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathmacd.js
103 lines (84 loc) · 2.31 KB
/
macd.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
100
101
102
103
const SignalResult = require('../dict/signal_result');
module.exports = class Macd {
getName() {
return 'macd';
}
buildIndicator(indicatorBuilder, options) {
if (!options.period) {
throw Error('Invalid period');
}
indicatorBuilder.add('macd', 'macd_ext', options.period, options);
indicatorBuilder.add('hma', 'hma', options.period, {
length: 9
});
indicatorBuilder.add('sma200', 'sma', options.period, {
length: 200
});
}
period(indicatorPeriod) {
const sma200Full = indicatorPeriod.getIndicator('sma200');
const macdFull = indicatorPeriod.getIndicator('macd');
const hmaFull = indicatorPeriod.getIndicator('hma');
if (!macdFull || !sma200Full || !hmaFull || macdFull.length < 2 || sma200Full.length < 2) {
return undefined;
}
const hma = hmaFull.slice(-1)[0];
const sma200 = sma200Full.slice(-1)[0];
const macd = macdFull.slice(-2);
// overall trend filter
const long = hma >= sma200;
const lastSignal = indicatorPeriod.getLastSignal();
const debug = {
sma200: sma200[0],
histogram: macd[0].histogram,
last_signal: lastSignal,
long: long
};
const current = macd[0].histogram;
const before = macd[1].histogram;
// trend change
if ((lastSignal === 'long' && before > 0 && current < 0) || (lastSignal === 'short' && before < 0 && current > 0)) {
return SignalResult.createSignal('close', debug);
}
if (long) {
// long
if (before < 0 && current > 0) {
return SignalResult.createSignal('long', debug);
}
} else {
// short
if (before > 0 && current < 0) {
return SignalResult.createSignal('short', debug);
}
}
return SignalResult.createEmptySignal(debug);
}
getBacktestColumns() {
return [
{
label: 'trend',
value: row => {
if (typeof row.long !== 'boolean') {
return undefined;
}
return row.long === true ? 'success' : 'danger';
},
type: 'icon'
},
{
label: 'histogram',
value: 'histogram',
type: 'histogram'
}
];
}
getOptions() {
return {
period: '15m',
default_ma_type: 'EMA',
fast_period: 12,
slow_period: 26,
signal_period: 9
};
}
};