This repository has been archived by the owner on Feb 16, 2020. It is now read-only.
/
trader.js
412 lines (338 loc) · 10.4 KB
/
trader.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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
const _ = require('lodash');
const util = require('../../core/util.js');
const config = util.getConfig();
const dirs = util.dirs();
const moment = require('moment');
const log = require(dirs.core + 'log');
const Broker = require(dirs.broker + '/gekkoBroker');
require(dirs.gekko + '/exchange/dependencyCheck');
const Trader = function(next) {
_.bindAll(this);
this.brokerConfig = {
...config.trader,
...config.watch,
private: true
}
this.propogatedTrades = 0;
this.propogatedTriggers = 0;
try {
this.broker = new Broker(this.brokerConfig);
} catch(e) {
util.die(e.message);
}
if(!this.broker.capabilities.gekkoBroker) {
util.die('This exchange is not yet supported');
}
this.sync(() => {
log.info('\t', 'Portfolio:');
log.info('\t\t', this.portfolio.currency, this.brokerConfig.currency);
log.info('\t\t', this.portfolio.asset, this.brokerConfig.asset);
log.info('\t', 'Balance:');
log.info('\t\t', this.balance, this.brokerConfig.currency);
log.info('\t', 'Exposed:');
log.info('\t\t',
this.exposed ? 'yes' : 'no',
`(${(this.exposure * 100).toFixed(2)}%)`
);
next();
});
this.cancellingOrder = false;
this.sendInitialPortfolio = false;
setInterval(this.sync, 1000 * 60 * 10);
}
// teach our trader events
util.makeEventEmitter(Trader);
Trader.prototype.sync = function(next) {
log.debug('syncing private data');
this.broker.syncPrivateData(() => {
if(!this.price) {
this.price = this.broker.ticker.bid;
}
const oldPortfolio = this.portfolio;
this.setPortfolio();
this.setBalance();
if(this.sendInitialPortfolio && !_.isEqual(oldPortfolio, this.portfolio)) {
this.relayPortfolioChange();
}
// balance is relayed every minute
// no need to do it here.
if(next) {
next();
}
});
}
Trader.prototype.relayPortfolioChange = function() {
this.deferredEmit('portfolioChange', {
asset: this.portfolio.asset,
currency: this.portfolio.currency
});
}
Trader.prototype.relayPortfolioValueChange = function() {
this.deferredEmit('portfolioValueChange', {
balance: this.balance
});
}
Trader.prototype.setPortfolio = function() {
this.portfolio = {
currency: _.find(
this.broker.portfolio.balances,
b => b.name === this.brokerConfig.currency
).amount,
asset: _.find(
this.broker.portfolio.balances,
b => b.name === this.brokerConfig.asset
).amount
}
}
Trader.prototype.setBalance = function() {
this.balance = this.portfolio.currency + this.portfolio.asset * this.price;
this.exposure = (this.portfolio.asset * this.price) / this.balance;
// if more than 10% of balance is in asset we are exposed
this.exposed = this.exposure > 0.1;
}
Trader.prototype.processCandle = function(candle, done) {
this.price = candle.close;
const previousBalance = this.balance;
this.setPortfolio();
this.setBalance();
if(!this.sendInitialPortfolio) {
this.sendInitialPortfolio = true;
this.deferredEmit('portfolioChange', {
asset: this.portfolio.asset,
currency: this.portfolio.currency
});
}
if(this.balance !== previousBalance) {
// this can happen because:
// A) the price moved and we have > 0 asset
// B) portfolio got changed
this.relayPortfolioValueChange();
}
done();
}
Trader.prototype.processAdvice = function(advice) {
let direction;
if(advice.recommendation === 'long') {
direction = 'buy';
} else if(advice.recommendation === 'short') {
direction = 'sell';
} else {
log.error('ignoring advice in unknown direction');
return;
}
const id = 'trade-' + (++this.propogatedTrades);
if(this.order) {
if(this.order.side === direction) {
return log.info('ignoring advice: already in the process to', direction);
}
if(this.cancellingOrder) {
return log.info('ignoring advice: already cancelling previous', this.order.side, 'order');
}
log.info('Received advice to', direction, 'however Gekko is already in the process to', this.order.side);
log.info('Canceling', this.order.side, 'order first');
return this.cancelOrder(id, advice, () => this.processAdvice(advice));
}
let amount;
if(direction === 'buy') {
if(this.exposed) {
log.info('NOT buying, already exposed');
return this.deferredEmit('tradeAborted', {
id,
adviceId: advice.id,
action: direction,
portfolio: this.portfolio,
balance: this.balance,
reason: "Portfolio already in position."
});
}
amount = this.portfolio.currency / this.price * 0.95;
log.info(
'Trader',
'Received advice to go long.',
'Buying ', this.brokerConfig.asset
);
} else if(direction === 'sell') {
if(!this.exposed) {
log.info('NOT selling, already no exposure');
return this.deferredEmit('tradeAborted', {
id,
adviceId: advice.id,
action: direction,
portfolio: this.portfolio,
balance: this.balance,
reason: "Portfolio already in position."
});
}
// clean up potential old stop trigger
if(this.activeStopTrigger) {
this.deferredEmit('triggerAborted', {
id: this.activeStopTrigger.id,
date: advice.date
});
this.activeStopTrigger.instance.cancel();
delete this.activeStopTrigger;
}
amount = this.portfolio.asset;
log.info(
'Trader',
'Received advice to go short.',
'Selling ', this.brokerConfig.asset
);
}
this.createOrder(direction, amount, advice, id);
}
Trader.prototype.createOrder = function(side, amount, advice, id) {
const type = 'sticky';
// NOTE: this is the best check we can do at this point
// with the best price we have. The order won't be actually
// created with this.price, but it should be close enough to
// catch non standard errors (lot size, price filter) on
// exchanges that have them.
const check = this.broker.isValidOrder(amount, this.price);
if(!check.valid) {
log.warn('NOT creating order! Reason:', check.reason);
return this.deferredEmit('tradeAborted', {
id,
adviceId: advice.id,
action: side,
portfolio: this.portfolio,
balance: this.balance,
reason: check.reason
});
}
log.debug('Creating order to', side, amount, this.brokerConfig.asset);
this.deferredEmit('tradeInitiated', {
id,
adviceId: advice.id,
action: side,
portfolio: this.portfolio,
balance: this.balance
});
this.order = this.broker.createOrder(type, side, amount);
this.order.on('fill', f => log.info('[ORDER] partial', side, 'fill, total filled:', f));
this.order.on('statusChange', s => log.debug('[ORDER] statusChange:', s));
this.order.on('error', e => {
log.error('[ORDER] Gekko received error from GB:', e.message);
log.debug(e);
this.order = null;
this.cancellingOrder = false;
this.deferredEmit('tradeErrored', {
id,
adviceId: advice.id,
date: moment(),
reason: e.message
});
});
this.order.on('completed', () => {
this.order.createSummary((err, summary) => {
if(!err && !summary) {
err = new Error('GB returned an empty summary.')
}
if(err) {
log.error('Error while creating summary:', err);
return this.deferredEmit('tradeErrored', {
id,
adviceId: advice.id,
date: moment(),
reason: err.message
});
}
log.info('[ORDER] summary:', summary);
this.order = null;
this.sync(() => {
let cost;
if(_.isNumber(summary.feePercent)) {
cost = summary.feePercent / 100 * summary.amount * summary.price;
}
let effectivePrice;
if(_.isNumber(summary.feePercent)) {
if(side === 'buy') {
effectivePrice = summary.price * (1 + summary.feePercent / 100);
} else {
effectivePrice = summary.price * (1 - summary.feePercent / 100);
}
} else {
log.warn('WARNING: exchange did not provide fee information, assuming no fees..');
effectivePrice = summary.price;
}
this.deferredEmit('tradeCompleted', {
id,
adviceId: advice.id,
action: summary.side,
cost,
amount: summary.amount,
price: summary.price,
portfolio: this.portfolio,
balance: this.balance,
date: summary.date,
feePercent: summary.feePercent,
effectivePrice
});
if(
side === 'buy' &&
advice.trigger &&
advice.trigger.type === 'trailingStop'
) {
const trigger = advice.trigger;
const triggerId = 'trigger-' + (++this.propogatedTriggers);
this.deferredEmit('triggerCreated', {
id: triggerId,
at: advice.date,
type: 'trailingStop',
properties: {
trail: trigger.trailValue,
initialPrice: summary.price,
}
});
log.info(`Creating trailingStop trigger "${triggerId}"! Properties:`);
log.info(`\tInitial price: ${summary.price}`);
log.info(`\tTrail of: ${trigger.trailValue}`);
this.activeStopTrigger = {
id: triggerId,
adviceId: advice.id,
instance: this.broker.createTrigger({
type: 'trailingStop',
onTrigger: this.onStopTrigger,
props: {
trail: trigger.trailValue,
initialPrice: summary.price,
}
})
}
}
});
})
});
}
Trader.prototype.onStopTrigger = function(price) {
log.info(`TrailingStop trigger "${this.activeStopTrigger.id}" fired! Observed price was ${price}`);
this.deferredEmit('triggerFired', {
id: this.activeStopTrigger.id,
date: moment()
});
const adviceMock = {
recommendation: 'short',
id: this.activeStopTrigger.adviceId
}
delete this.activeStopTrigger;
this.processAdvice(adviceMock);
}
Trader.prototype.cancelOrder = function(id, advice, next) {
if(!this.order) {
return next();
}
this.cancellingOrder = true;
this.order.removeAllListeners();
this.order.cancel();
this.order.once('completed', () => {
this.order = null;
this.cancellingOrder = false;
this.deferredEmit('tradeCancelled', {
id,
adviceId: advice.id,
date: moment()
});
this.sync(next);
});
}
module.exports = Trader;