forked from askmike/gekko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtalib-macd.js
59 lines (46 loc) · 1.51 KB
/
talib-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
// If you want to use your own trading methods you can
// write them here. For more information on everything you
// can use please refer to this document:
//
// https://github.com/askmike/gekko/blob/stable/docs/trading_methods.md
// Let's create our own method
var method = {};
// Prepare everything our method needs
method.init = function() {
this.name = 'talib-macd'
this.input = 'candle';
// 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 = 'none';
// how many candles do we need as a base
// before we can start giving advice?
this.requiredHistory = this.tradingAdvisor.historySize;
var customMACDSettings = this.settings.parameters;
// define the indicators we need
this.addTalibIndicator('mymacd', 'macd', customMACDSettings);
}
// What happens on every new candle?
method.update = function(candle) {
// nothing!
}
method.log = function() {
// nothing!
}
// Based on the newly calculated
// information, check if we should
// update or not.
method.check = function(candle) {
var price = candle.close;
var result = this.talibIndicators.mymacd.result;
var macddiff = result['outMACD'] - result['outMACDSignal'];
if(this.settings.thresholds.down > macddiff && this.trend !== 'short') {
this.trend = 'short';
this.advice('short');
} else if(this.settings.thresholds.up < macddiff && this.trend !== 'long'){
this.trend = 'long';
this.advice('long');
}
}
module.exports = method;