This repository was archived by the owner on Feb 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathDEMA.js
73 lines (58 loc) · 1.91 KB
/
DEMA.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
// 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() {
this.name = 'DEMA';
this.currentTrend;
this.requiredHistory = this.tradingAdvisor.historySize;
// define the indicators we need
this.addIndicator('dema', 'DEMA', this.settings);
this.addIndicator('sma', 'SMA', this.settings.weight);
}
// what happens on every new candle?
method.update = function(candle) {
// nothing!
}
// for debugging purposes: log the last calculated
// EMAs and diff.
method.log = function() {
let dema = this.indicators.dema;
let sma = this.indicators.sma;
log.debug('Calculated DEMA and SMA properties for candle:');
log.debug('\t Inner EMA:', dema.inner.result.toFixed(8));
log.debug('\t Outer EMA:', dema.outer.result.toFixed(8));
log.debug('\t DEMA:', dema.result.toFixed(5));
log.debug('\t SMA:', sma.result.toFixed(5));
log.debug('\t DEMA age:', dema.inner.age, 'candles');
}
method.check = function(candle) {
let dema = this.indicators.dema;
let sma = this.indicators.sma;
let resDEMA = dema.result;
let resSMA = sma.result;
let price = candle.close;
let diff = resSMA - resDEMA;
let message = '@ ' + price.toFixed(8) + ' (' + resDEMA.toFixed(5) + '/' + diff.toFixed(5) + ')';
if(diff > this.settings.thresholds.up) {
log.debug('we are currently in uptrend', message);
if(this.currentTrend !== 'up') {
this.currentTrend = 'up';
this.advice('long');
} else
this.advice();
} else if(diff < this.settings.thresholds.down) {
log.debug('we are currently in a downtrend', message);
if(this.currentTrend !== 'down') {
this.currentTrend = 'down';
this.advice('short');
} else
this.advice();
} else {
log.debug('we are currently not in an up or down trend', message);
this.advice();
}
}
module.exports = method;