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 pathCCI.js
125 lines (110 loc) · 4 KB
/
CCI.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
// 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.currentTrend;
this.requiredHistory = this.tradingAdvisor.historySize;
this.age = 0;
this.trend = {
direction: 'undefined',
duration: 0,
persisted: false,
adviced: false
};
this.historySize = this.settings.history;
this.ppoadv = 'none';
this.uplevel = this.settings.thresholds.up;
this.downlevel = this.settings.thresholds.down;
this.persisted = this.settings.thresholds.persistence;
// log.debug("CCI started with:\nup:\t", this.uplevel, "\ndown:\t", this.downlevel, "\npersistence:\t", this.persisted);
// define the indicators we need
this.addIndicator('cci', 'CCI', this.settings);
}
// what happens on every new candle?
method.update = function(candle) {
}
// for debugging purposes: log the last calculated
// EMAs and diff.
method.log = function(candle) {
var cci = this.indicators.cci;
if (typeof(cci.result) == 'boolean') {
log.debug('Insufficient data available. Age: ', cci.size, ' of ', cci.maxSize);
return;
}
log.debug('calculated CCI properties for candle:');
log.debug('\t', 'Price:\t\t', candle.close.toFixed(8));
log.debug('\t', 'CCI tp:\t', cci.tp.toFixed(8));
log.debug('\t', 'CCI tp/n:\t', cci.avgtp.toFixed(8));
log.debug('\t', 'CCI md:\t', cci.mean.toFixed(8));
if (typeof(cci.result) == 'boolean' )
log.debug('\t In sufficient data available.');
else
log.debug('\t', 'CCI:\t\t', cci.result.toFixed(2));
}
/*
*
*/
method.check = function(candle) {
var lastPrice = candle.close;
this.age++;
var cci = this.indicators.cci;
if (typeof(cci.result) == 'number') {
// overbought?
if (cci.result >= this.uplevel && (this.trend.persisted || this.persisted == 0) && !this.trend.adviced && this.trend.direction == 'overbought' ) {
this.trend.adviced = true;
this.trend.duration++;
this.advice('short');
} else if (cci.result >= this.uplevel && this.trend.direction != 'overbought') {
this.trend.duration = 1;
this.trend.direction = 'overbought';
this.trend.persisted = false;
this.trend.adviced = false;
if (this.persisted == 0) {
this.trend.adviced = true;
this.advice('short');
}
} else if (cci.result >= this.uplevel) {
this.trend.duration++;
if (this.trend.duration >= this.persisted) {
this.trend.persisted = true;
}
} else if (cci.result <= this.downlevel && (this.trend.persisted || this.persisted == 0) && !this.trend.adviced && this.trend.direction == 'oversold') {
this.trend.adviced = true;
this.trend.duration++;
this.advice('long');
} else if (cci.result <= this.downlevel && this.trend.direction != 'oversold') {
this.trend.duration = 1;
this.trend.direction = 'oversold';
this.trend.persisted = false;
this.trend.adviced = false;
if (this.persisted == 0) {
this.trend.adviced = true;
this.advice('long');
}
} else if (cci.result <= this.downlevel) {
this.trend.duration++;
if (this.trend.duration >= this.persisted) {
this.trend.persisted = true;
}
} else {
if( this.trend.direction != 'nodirection') {
this.trend = {
direction: 'nodirection',
duration: 0,
persisted: false,
adviced: false
};
} else {
this.trend.duration++;
}
this.advice();
}
} else {
this.advice();
}
log.debug("Trend: ", this.trend.direction, " for ", this.trend.duration);
}
module.exports = method;