-
Notifications
You must be signed in to change notification settings - Fork 23
/
Supertrend.js
98 lines (80 loc) · 3.16 KB
/
Supertrend.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
// Let's create our own strategy
var log = require('../core/log.js');
var config = require('../core/util.js').getConfig();
var settings = config.Supertrend;
var strat = {};
// Prepare everything our strat needs
strat.init = function() {
// your code!
this.requiredHistory = config.tradingAdvisor.historySize;
this.addTalibIndicator("myAtr", "atr", {optInTimePeriod: settings.atrEma});
this.bought = 0;
this.supertrend = {
upperBandBasic : 0,
lowerBandBasic : 0,
upperBand : 0,
lowerBand : 0,
supertrend : 0,
};
this.lastSupertrend = {
upperBandBasic : 0,
lowerBandBasic : 0,
upperBand : 0,
lowerBand : 0,
supertrend : 0,
};
this.lastCandleClose = 0;
}
// What happens on every new candle?
strat.update = function(candle) {
// your code!
}
// For debugging purposes.
strat.log = function() {
// your code!
}
// Based on the newly calculated
// information, check if we should
// update or not.
strat.check = function(candle) {
var atrResult = this.talibIndicators.myAtr.result.outReal;
this.supertrend.upperBandBasic = ((candle.high + candle.low) / 2) + (settings.bandFactor * atrResult);
this.supertrend.lowerBandBasic = ((candle.high + candle.low) / 2) - (settings.bandFactor * atrResult);
if(this.supertrend.upperBandBasic < this.lastSupertrend.upperBand || this.lastCandleClose > this.lastSupertrend.upperBand)
this.supertrend.upperBand = this.supertrend.upperBandBasic;
else
this.supertrend.upperBand = this.lastSupertrend.upperBand;
if(this.supertrend.lowerBandBasic > this.lastSupertrend.lowerBand || this.lastCandleClose < this.lastSupertrend.lowerBand)
this.supertrend.lowerBand = this.supertrend.lowerBandBasic;
else
this.supertrend.lowerBand = this.lastSupertrend.lowerBand;
if(this.lastSupertrend.supertrend == this.lastSupertrend.upperBand && candle.close <= this.supertrend.upperBand)
this.supertrend.supertrend = this.supertrend.upperBand;
else if(this.lastSupertrend.supertrend == this.lastSupertrend.upperBand && candle.close >= this.supertrend.upperBand)
this.supertrend.supertrend = this.supertrend.lowerBand;
else if(this.lastSupertrend.supertrend == this.lastSupertrend.lowerBand && candle.close >= this.supertrend.lowerBand)
this.supertrend.supertrend = this.supertrend.lowerBand;
else if(this.lastSupertrend.supertrend == this.lastSupertrend.lowerBand && candle.close <= this.supertrend.lowerBand)
this.supertrend.supertrend = this.supertrend.upperBand;
else
this.supertrend.supertrend = 0
if(candle.close > this.supertrend.supertrend && this.bought == 0){
this.advice("long");
this.bought = 1;
log.debug("Buy at: ", candle.close);
}
if(candle.close < this.supertrend.supertrend && this.bought == 1){
this.advice("short")
this.bought = 0;
log.debug("Sell at: ", candle.close);
}
this.lastCandleClose = candle.close;
this.lastSupertrend = {
upperBandBasic : this.supertrend.upperBandBasic,
lowerBandBasic : this.supertrend.lowerBandBasic,
upperBand : this.supertrend.upperBand,
lowerBand : this.supertrend.lowerBand,
supertrend : this.supertrend.supertrend,
};
}
module.exports = strat;