forked from Haehnchen/crypto-trading-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
macd_t.js
93 lines (75 loc) · 2.4 KB
/
macd_t.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
'use strict';
let SignalResult = require('../dict/signal_result')
module.exports = class macd_t {
getName() {
return 'macd_t'
}
buildIndicator(indicatorBuilder, options) {
if (!options['period']) {
throw 'Invalid period'
}
indicatorBuilder.add('macd', 'macd', options['period'])
indicatorBuilder.add('sma720', 'sma', options['period'], {
'length': 720,
})
indicatorBuilder.add('ema550', 'ema', options['period'], {
'length': 550,
})
}
period(indicatorPeriod) {
return this.macd(
indicatorPeriod.getPrice(),
indicatorPeriod.getIndicator('sma720'),
indicatorPeriod.getIndicator('ema550'),
indicatorPeriod.getIndicator('macd'),
indicatorPeriod.getLastSignal(),
)
}
async macd(price, sma720Full, ema550Full, macdFull, lastSignal) {
if (!macdFull || !ema550Full || macdFull.length < 2 || sma720Full.length < 2 || ema550Full.length < 2) {
return
}
// remove incomplete candle
let sma720 = sma720Full.slice(0, -1)
let ema550 = ema550Full.slice(0, -1)
let macd = macdFull.slice(0, -1)
let debug = {
'sma720': sma720.slice(-1)[0],
'ema550': ema550.slice(-1)[0],
'histogram': macd.slice(-1)[0].histogram,
'last_signal': lastSignal,
}
let before = macd.slice(-2)[0].histogram
let last = macd.slice(-1)[0].histogram
// trend change
if (
(lastSignal === 'long' && before > 0 && last < 0)
|| (lastSignal === 'short' && before < 0 && last > 0)
) {
return SignalResult.createSignal('close', 'debug')
}
// sma long
let long = price >= sma720.slice(-1)[0]
// ema long
if (!long) {
long = price >= ema550.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': '1m',
}
}
}