forked from askmike/gekko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCCI.js
140 lines (119 loc) · 4.36 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// helpers
var _ = require('lodash');
var log = require('../core/log.js');
// configuration
var config = require('../core/util.js').getConfig();
var settings = config.CCI;
var pposettings = config.PPO;
// let's create our own method
var method = {};
// teach our trading method events
var Util = require('util');
//var EventEmitter = require('events').EventEmitter;
//Util.inherits(TradingMethod, EventEmitter);
// prepare everything our method needs
method.init = function() {
this.currentTrend;
this.requiredHistory = config.tradingAdvisor.historySize;
this.age = 0;
this.trend = {
direction: 'undefined',
duration: 0,
persisted: false,
adviced: false
};
this.historySize = config.tradingAdvisor.historySize;
this.ppoadv = 'none';
this.uplevel = config.CCI.thresholds.up;
this.downlevel = config.CCI.thresholds.down;
this.persisted = config.CCI.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', config.CCI);
}
// what happens on every new candle?
method.update = function(candle) {
}
// for debugging purposes: log the last calculated
// EMAs and diff.
method.log = function() {
var cci = this.indicators.cci;
if (typeof(cci.result) == 'boolean') {
log.debug('Insufficient data available. Age: ', cci.size, ' of ', cci.maxSize);
log.debug('ind: ', cci.TP.result, ' ', cci.TP.age, ' ', cci.TP.depth);
return;
}
log.debug('calculated CCI properties for candle:');
log.debug('\t', 'Price:\t\t\t', this.lastPrice);
log.debug('\t', 'CCI tp:\t', cci.tp.toFixed(8));
log.debug('\t', 'CCI tp/n:\t', cci.TP.result.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', cci.result.toFixed(2));
}
/*
*
*/
method.check = function() {
var price = this.lastPrice;
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;