forked from askmike/gekko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMACD.js
134 lines (103 loc) · 3.09 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
MACD - DJM 31/12/2013
(updated a couple of times since, check git history)
*/
// helpers
var _ = require('lodash');
var log = require('../core/log.js');
// let's create our own method
var method = {};
// prepare everything our method needs
method.init = function() {
// keep state about the current trend
// here, on every new candle we use this
// state object to check if we need to
// report it.
this.trend = {
direction: 'none',
duration: 0,
persisted: false,
adviced: false
};
// how many candles do we need as a base
// before we can start giving advice?
this.requiredHistory = this.tradingAdvisor.historySize;
// define the indicators we need
this.addIndicator('macd', 'MACD', this.settings);
}
// what happens on every new candle?
method.update = function(candle) {
// nothing!
}
// for debugging purposes: log the last calculated
// EMAs and diff.
method.log = function() {
var digits = 8;
var macd = this.indicators.macd;
var diff = macd.diff;
var signal = macd.signal.result;
log.debug('calculated MACD properties for candle:');
log.debug('\t', 'short:', macd.short.result.toFixed(digits));
log.debug('\t', 'long:', macd.long.result.toFixed(digits));
log.debug('\t', 'macd:', diff.toFixed(digits));
log.debug('\t', 'signal:', signal.toFixed(digits));
log.debug('\t', 'macdiff:', macd.result.toFixed(digits));
}
method.check = function() {
var macddiff = this.indicators.macd.result;
if(macddiff > this.settings.thresholds.up) {
// new trend detected
if(this.trend.direction !== 'up')
// reset the state for the new trend
this.trend = {
duration: 0,
persisted: false,
direction: 'up',
adviced: false
};
this.trend.duration++;
log.debug('In uptrend since', this.trend.duration, 'candle(s)');
if(this.trend.duration >= this.settings.thresholds.persistence)
this.trend.persisted = true;
if(this.trend.persisted && !this.trend.adviced) {
this.trend.adviced = true;
this.advice('long');
} else
this.advice();
} else if(macddiff < this.settings.thresholds.down) {
// new trend detected
if(this.trend.direction !== 'down')
// reset the state for the new trend
this.trend = {
duration: 0,
persisted: false,
direction: 'down',
adviced: false
};
this.trend.duration++;
log.debug('In downtrend since', this.trend.duration, 'candle(s)');
if(this.trend.duration >= this.settings.thresholds.persistence)
this.trend.persisted = true;
if(this.trend.persisted && !this.trend.adviced) {
this.trend.adviced = true;
this.advice('short');
} else
this.advice();
} else {
log.debug('In no trend');
// we're not in an up nor in a downtrend
// but for now we ignore sideways trends
//
// read more @link:
//
// https://github.com/askmike/gekko/issues/171
// this.trend = {
// direction: 'none',
// duration: 0,
// persisted: false,
// adviced: false
// };
this.advice();
}
}
module.exports = method;