-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaseTradingMethod.js
More file actions
334 lines (261 loc) · 7.97 KB
/
baseTradingMethod.js
File metadata and controls
334 lines (261 loc) · 7.97 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
const _ = require('lodash');
const fs = require('fs');
const util = require('../../core/util');
const config = util.getConfig();
const dirs = util.dirs();
const log = require(dirs.core + 'log');
const ENV = util.gekkoEnv();
const mode = util.gekkoMode();
const startTime = util.getStartTime();
const indicatorsPath = dirs.methods + 'indicators/';
const indicatorFiles = fs.readdirSync(indicatorsPath);
const Indicators = {};
const AsyncIndicatorRunner = require('./asyncIndicatorRunner');
_.each(indicatorFiles, function(indicator) {
const indicatorName = indicator.split(".")[0];
if (indicatorName[0] != "_")
try {
Indicators[indicatorName] = require(indicatorsPath + indicator);
} catch (e) {
log.error("Failed to load indicator", indicatorName);
}
});
const allowedIndicators = _.keys(Indicators);
var Base = function(settings) {
_.bindAll(this);
// properties
this.age = 0;
this.processedTicks = 0;
this.setup = false;
this.settings = settings;
this.tradingAdvisor = config.tradingAdvisor;
// defaults
this.priceValue = 'close';
this.indicators = {};
this.asyncTick = false;
this.deferredTicks = [];
this.propogatedAdvices = 0;
this.completedWarmup = false;
this.asyncIndicatorRunner = new AsyncIndicatorRunner();
this._currentDirection;
// make sure we have all methods
_.each(['init', 'check'], function(fn) {
if(!this[fn])
util.die('No ' + fn + ' function in this strategy found.')
}, this);
if(!this.update)
this.update = function() {};
if(!this.end)
this.end = function() {};
if(!this.onTrade)
this.onTrade = function() {};
// let's run the implemented starting point
this.init();
if(_.isNumber(this.requiredHistory)) {
log.debug('Ignoring strategy\'s required history, using the "config.tradingAdvisor.historySize" instead.');
}
this.requiredHistory = config.tradingAdvisor.historySize;
if(!config.debug || !this.log)
this.log = function() {};
this.setup = true;
if(_.size(this.asyncIndicatorRunner.talibIndicators) || _.size(this.asyncIndicatorRunner.tulipIndicators))
this.asyncTick = true;
else
delete this.asyncIndicatorRunner;
}
// teach our base trading method events
util.makeEventEmitter(Base);
Base.prototype.tick = function(candle, done) {
this.age++;
const afterAsync = () => {
this.calculateSyncIndicators(candle, done);
}
if(this.asyncTick) {
this.asyncIndicatorRunner.processCandle(candle, () => {
if(!this.talibIndicators) {
this.talibIndicators = this.asyncIndicatorRunner.talibIndicators;
this.tulipIndicators = this.asyncIndicatorRunner.tulipIndicators;
}
afterAsync();
});
} else {
afterAsync();
}
}
Base.prototype.isBusy = function() {
if(!this.asyncTick)
return false;
return this.asyncIndicatorRunner.inflight;
}
Base.prototype.calculateSyncIndicators = function(candle, done) {
// update all indicators
var price = candle[this.priceValue];
_.each(this.indicators, function(i) {
if(i.input === 'price')
i.update(price);
if(i.input === 'candle')
i.update(candle);
},this);
this.propogateTick(candle);
return done();
}
Base.prototype.propogateTick = function(candle) {
this.candle = candle;
this.update(candle);
this.processedTicks++;
var isAllowedToCheck = this.requiredHistory <= this.age;
if(!this.completedWarmup) {
// in live mode we might receive more candles
// than minimally needed. In that case check
// whether candle start time is > startTime
var isPremature = false;
if(mode === 'realtime') {
const startTimeMinusCandleSize = startTime
.clone()
.subtract(this.tradingAdvisor.candleSize, "minutes");
isPremature = candle.start < startTimeMinusCandleSize;
}
if(isAllowedToCheck && !isPremature) {
this.completedWarmup = true;
this.emit(
'stratWarmupCompleted',
{start: candle.start.clone()}
);
}
}
if(this.completedWarmup) {
this.log(candle);
this.check(candle);
if(
this.asyncTick &&
this.hasSyncIndicators &&
this.deferredTicks.length
) {
return this.tick(this.deferredTicks.shift())
}
}
const indicators = {};
_.each(this.indicators, (indicator, name) => {
indicators[name] = indicator.result;
});
_.each(this.tulipIndicators, (indicator, name) => {
indicators[name] = indicator.result.result
? indicator.result.result
: indicator.result;
});
_.each(this.talibIndicators, (indicator, name) => {
indicators[name] = indicator.result.outReal
? indicator.result.outReal
: indicator.result;
});
this.emit('stratUpdate', {
date: candle.start.clone(),
indicators
});
// are we totally finished?
const completed = this.age === this.processedTicks;
if(completed && this.finishCb)
this.finishCb();
}
Base.prototype.processTrade = function(trade) {
if(
this._pendingTriggerAdvice &&
trade.action === 'sell' &&
this._pendingTriggerAdvice === trade.adviceId
) {
// This trade came from a trigger of the previous advice,
// update stored direction
this._currentDirection = 'short';
this._pendingTriggerAdvice = null;
}
this.onTrade(trade);
}
Base.prototype.addTalibIndicator = function(name, type, parameters) {
this.asyncIndicatorRunner.addTalibIndicator(name, type, parameters);
}
Base.prototype.addTulipIndicator = function(name, type, parameters) {
this.asyncIndicatorRunner.addTulipIndicator(name, type, parameters);
}
Base.prototype.addIndicator = function(name, type, parameters) {
if(!_.contains(allowedIndicators, type))
util.die('I do not know the indicator ' + type);
if(this.setup)
util.die('Can only add indicators in the init method!');
return this.indicators[name] = new Indicators[type](parameters);
// some indicators need a price stream, others need full candles
}
Base.prototype.advice = function(newDirection) {
// ignore legacy soft advice
if(!newDirection) {
return;
}
let trigger;
if(_.isObject(newDirection)) {
if(!_.isString(newDirection.direction)) {
log.error('Strategy emitted unparsable advice:', newDirection);
return;
}
if(newDirection.direction === this._currentDirection) {
return;
}
if(_.isObject(newDirection.trigger)) {
if(newDirection.direction !== 'long') {
log.warn(
'Strategy adviced a stop on not long, this is not supported.',
'As such the stop is ignored'
);
} else {
// the trigger is implemented in a trader
trigger = newDirection.trigger;
if(trigger.trailPercentage && !trigger.trailValue) {
trigger.trailValue = trigger.trailPercentage / 100 * this.candle.close;
log.info('[StratRunner] Trailing stop trail value specified as percentage, setting to:', trigger.trailValue);
}
}
}
newDirection = newDirection.direction;
}
if(newDirection === this._currentDirection) {
return;
}
if(newDirection === 'short' && this._pendingTriggerAdvice) {
this._pendingTriggerAdvice = null;
}
this._currentDirection = newDirection;
this.propogatedAdvices++;
const advice = {
id: 'advice-' + this.propogatedAdvices,
recommendation: newDirection
};
if(trigger) {
advice.trigger = trigger;
this._pendingTriggerAdvice = 'advice-' + this.propogatedAdvices;
} else {
this._pendingTriggerAdvice = null;
}
this.emit('advice', advice);
return this.propogatedAdvices;
}
Base.prototype.notify = function(content) {
this.emit('stratNotification', {
content,
date: new Date(),
})
}
Base.prototype.finish = function(done) {
// Because the strategy might be async we need
// to be sure we only stop after all candles are
// processed.
if(!this.asyncTick) {
this.end();
return done();
}
if(this.age === this.processedTicks) {
this.end();
return done();
}
// we are not done, register cb
// and call after we are..
this.finishCb = done;
}
module.exports = Base;