forked from askmike/gekko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StochRSI.js
121 lines (90 loc) · 2.71 KB
/
StochRSI.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
/*
StochRSI - SamThomp 11/06/2014
(updated by askmike) @ 30/07/2016
*/
// helpers
var _ = require('lodash');
var log = require('../core/log.js');
var RSI = require('./indicators/RSI.js');
// let's create our own method
var method = {};
// prepare everything our method needs
method.init = function() {
this.interval = this.settings.interval;
this.trend = {
direction: 'none',
duration: 0,
persisted: false,
adviced: false
};
this.requiredHistory = this.tradingAdvisor.historySize;
// define the indicators we need
this.addIndicator('rsi', 'RSI', { interval: this.interval });
this.RSIhistory = [];
}
// what happens on every new candle?
method.update = function(candle) {
this.rsi = this.indicators.rsi.result;
this.RSIhistory.push(this.rsi);
if(_.size(this.RSIhistory) > this.interval)
// remove oldest RSI value
this.RSIhistory.shift();
this.lowestRSI = _.min(this.RSIhistory);
this.highestRSI = _.max(this.RSIhistory);
this.stochRSI = ((this.rsi - this.lowestRSI) / (this.highestRSI - this.lowestRSI)) * 100;
}
// for debugging purposes log the last
// calculated parameters.
method.log = function() {
var digits = 8;
log.debug('calculated StochRSI properties for candle:');
log.debug('\t', 'rsi:', this.rsi.toFixed(digits));
log.debug("StochRSI min:\t\t" + this.lowestRSI.toFixed(digits));
log.debug("StochRSI max:\t\t" + this.highestRSI.toFixed(digits));
log.debug("StochRSI Value:\t\t" + this.stochRSI.toFixed(2));
}
method.check = function() {
if(this.stochRSI > this.settings.thresholds.high) {
// new trend detected
if(this.trend.direction !== 'high')
this.trend = {
duration: 0,
persisted: false,
direction: 'high',
adviced: false
};
this.trend.duration++;
log.debug('In high 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 if(this.stochRSI < this.settings.thresholds.low) {
// new trend detected
if(this.trend.direction !== 'low')
this.trend = {
duration: 0,
persisted: false,
direction: 'low',
adviced: false
};
this.trend.duration++;
log.debug('In low 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 {
// trends must be on consecutive candles
this.trend.duration = 0;
log.debug('In no trend');
this.advice();
}
}
module.exports = method;