-
Notifications
You must be signed in to change notification settings - Fork 941
Expand file tree
/
Copy pathbitfinex.ts
More file actions
551 lines (461 loc) · 18.8 KB
/
bitfinex.ts
File metadata and controls
551 lines (461 loc) · 18.8 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
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/// <reference path="../utils.ts" />
/// <reference path="../../common/models.ts" />
/// <reference path="nullgw.ts" />
/// <reference path="../config.ts"/>
/// <reference path="../utils.ts"/>
/// <reference path="../interfaces.ts"/>
import ws = require('ws');
import Q = require("q");
import crypto = require("crypto");
import request = require("request");
import url = require("url");
import querystring = require("querystring");
import Config = require("../config");
import NullGateway = require("./nullgw");
import Models = require("../../common/models");
import Utils = require("../utils");
import util = require("util");
import Interfaces = require("../interfaces");
import moment = require("moment");
import _ = require("lodash");
var shortId = require("shortid");
var Deque = require("collections/deque");
interface BitfinexMarketTrade {
tid: number;
timestamp: number;
price: string;
amount: string;
exchange: string;
type: string;
}
interface BitfinexMarketLevel {
price: string;
amount: string;
timestamp: string;
}
interface BitfinexOrderBook {
bids: BitfinexMarketLevel[];
asks: BitfinexMarketLevel[];
}
function decodeSide(side: string) {
switch (side) {
case "buy": return Models.Side.Bid;
case "sell": return Models.Side.Ask;
default: return Models.Side.Unknown;
}
}
function encodeSide(side: Models.Side) {
switch (side) {
case Models.Side.Bid: return "buy";
case Models.Side.Ask: return "sell";
default: return "";
}
}
function encodeTimeInForce(tif: Models.TimeInForce, type: Models.OrderType) {
if (type === Models.OrderType.Market) {
return "exchange market";
}
else if (type === Models.OrderType.Limit) {
if (tif === Models.TimeInForce.FOK) return "exchange fill-or-kill";
if (tif === Models.TimeInForce.GTC) return "exchange limit";
}
throw new Error("unsupported tif " + Models.TimeInForce[tif] + " and order type " + Models.OrderType[type]);
}
class BitfinexMarketDataGateway implements Interfaces.IMarketDataGateway {
ConnectChanged = new Utils.Evt<Models.ConnectivityStatus>();
private _since: number = null;
MarketTrade = new Utils.Evt<Models.GatewayMarketTrade>();
private onTrades = (trades: Models.Timestamped<BitfinexMarketTrade[]>) => {
_.forEach(trades.data, trade => {
var px = parseFloat(trade.price);
var sz = parseFloat(trade.amount);
var time = moment.unix(trade.timestamp);
var side = decodeSide(trade.type);
var mt = new Models.GatewayMarketTrade(px, sz, time, this._since === null, side);
this.MarketTrade.trigger(mt);
});
this._since = moment().unix();
};
private downloadMarketTrades = () => {
var qs = { timestamp: this._since === null ? moment.utc().subtract(60, "seconds").unix() : this._since };
this._http
.get<BitfinexMarketTrade[]>("trades/" + this._symbolProvider.symbol, qs)
.then(this.onTrades)
.done();
};
private static ConvertToMarketSide(level: BitfinexMarketLevel): Models.MarketSide {
return new Models.MarketSide(parseFloat(level.price), parseFloat(level.amount));
}
private static ConvertToMarketSides(level: BitfinexMarketLevel[]): Models.MarketSide[] {
return _.map(level, BitfinexMarketDataGateway.ConvertToMarketSide);
}
MarketData = new Utils.Evt<Models.Market>();
private onMarketData = (book: Models.Timestamped<BitfinexOrderBook>) => {
var bids = BitfinexMarketDataGateway.ConvertToMarketSides(book.data.bids);
var asks = BitfinexMarketDataGateway.ConvertToMarketSides(book.data.asks);
this.MarketData.trigger(new Models.Market(bids, asks, book.time));
};
private downloadMarketData = () => {
this._http
.get<BitfinexOrderBook>("book/" + this._symbolProvider.symbol, { limit_bids: 5, limit_asks: 5 })
.then(this.onMarketData)
.done();
};
constructor(
timeProvider: Utils.ITimeProvider,
private _http: BitfinexHttp,
private _symbolProvider: BitfinexSymbolProvider) {
timeProvider.setInterval(this.downloadMarketData, moment.duration(5, "seconds"));
timeProvider.setInterval(this.downloadMarketTrades, moment.duration(15, "seconds"));
this.downloadMarketData();
this.downloadMarketTrades();
_http.ConnectChanged.on(s => this.ConnectChanged.trigger(s));
}
}
interface RejectableResponse {
message: string;
}
interface BitfinexNewOrderRequest {
symbol: string;
amount: string;
price: string; //Price to buy or sell at. Must be positive. Use random number for market orders.
exchange: string; //always "bitfinex"
side: string; // buy or sell
type: string; // "market" / "limit" / "stop" / "trailing-stop" / "fill-or-kill" / "exchange market" / "exchange limit" / "exchange stop" / "exchange trailing-stop" / "exchange fill-or-kill". (type starting by "exchange " are exchange orders, others are margin trading orders)
is_hidden?: boolean;
}
interface BitfinexNewOrderResponse extends RejectableResponse {
order_id: string;
}
interface BitfinexCancelOrderRequest {
order_id: string;
}
interface BitfinexCancelReplaceOrderRequest extends BitfinexNewOrderRequest {
order_id: string;
}
interface BitfinexCancelReplaceOrderResponse extends BitfinexCancelOrderRequest, RejectableResponse { }
interface BitfinexOrderStatusRequest {
order_id: string;
}
interface BitfinexMyTradesRequest {
symbol: string;
timestamp: number;
}
interface BitfinexMyTradesResponse extends RejectableResponse {
price: string;
amount: string;
timestamp: number;
exchange: string;
type: string;
fee_currency: string;
fee_amount: string;
tid: number;
order_id: string;
}
interface BitfinexOrderStatusResponse extends RejectableResponse {
symbol: string;
exchange: string; // bitstamp or bitfinex
price: number;
avg_execution_price: string;
side: string;
type: string; // "market" / "limit" / "stop" / "trailing-stop".
timestamp: number;
is_live: boolean;
is_cancelled: boolean;
is_hidden: boolean;
was_forced: boolean;
executed_amount: string;
remaining_amount: string;
original_amount: string;
}
class BitfinexOrderEntryGateway implements Interfaces.IOrderEntryGateway {
OrderUpdate = new Utils.Evt<Models.OrderStatusReport>();
ConnectChanged = new Utils.Evt<Models.ConnectivityStatus>();
supportsCancelAllOpenOrders = () : boolean => { return false; };
cancelAllOpenOrders = () : Q.Promise<number> => { return Q(0); };
generateClientOrderId = () => shortId.generate();
public cancelsByClientOrderId = false;
private convertToOrderRequest = (order: Models.Order): BitfinexNewOrderRequest => {
return {
amount: Utils.roundFloat(order.quantity).toString(),
exchange: "bitfinex",
price: Utils.roundFloat(order.price).toString(),
side: encodeSide(order.side),
symbol: this._symbolProvider.symbol,
type: encodeTimeInForce(order.timeInForce, order.type)
};
}
sendOrder = (order: Models.BrokeredOrder): Models.OrderGatewayActionReport => {
var req = this.convertToOrderRequest(order);
this._http
.post<BitfinexNewOrderRequest, BitfinexNewOrderResponse>("order/new", req)
.then(resp => {
if (typeof resp.data.message !== "undefined") {
this.OrderUpdate.trigger({
orderStatus: Models.OrderStatus.Rejected,
orderId: order.orderId,
rejectMessage: resp.data.message,
time: resp.time
});
return;
}
this.OrderUpdate.trigger({
orderId: order.orderId,
exchangeId: resp.data.order_id,
time: resp.time,
orderStatus: Models.OrderStatus.Working
});
}).done();
return new Models.OrderGatewayActionReport(Utils.date());
};
cancelOrder = (cancel: Models.BrokeredCancel): Models.OrderGatewayActionReport => {
var req = { order_id: cancel.exchangeId };
this._http
.post<BitfinexCancelOrderRequest, any>("order/cancel", req)
.then(resp => {
if (typeof resp.data.message !== "undefined") {
this.OrderUpdate.trigger({
orderStatus: Models.OrderStatus.Rejected,
cancelRejected: true,
orderId: cancel.clientOrderId,
rejectMessage: resp.data.message,
time: resp.time
});
return;
}
this.OrderUpdate.trigger({
orderId: cancel.clientOrderId,
time: resp.time,
orderStatus: Models.OrderStatus.Cancelled
});
})
.done();
return new Models.OrderGatewayActionReport(Utils.date());
};
replaceOrder = (replace: Models.BrokeredReplace): Models.OrderGatewayActionReport => {
this.cancelOrder(new Models.BrokeredCancel(replace.origOrderId, replace.orderId, replace.side, replace.exchangeId));
return this.sendOrder(replace);
};
private downloadOrderStatuses = () => {
var tradesReq = { timestamp: this._since.unix(), symbol: this._symbolProvider.symbol };
this._http
.post<BitfinexMyTradesRequest, BitfinexMyTradesResponse[]>("mytrades", tradesReq)
.then(resps => {
_.forEach(resps.data, t => {
this._http
.post<BitfinexOrderStatusRequest, BitfinexOrderStatusResponse>("order/status", { order_id: t.order_id })
.then(r => {
this.OrderUpdate.trigger({
exchangeId: t.order_id,
lastPrice: parseFloat(t.price),
lastQuantity: parseFloat(t.amount),
orderStatus: BitfinexOrderEntryGateway.GetOrderStatus(r.data),
averagePrice: parseFloat(r.data.avg_execution_price),
leavesQuantity: parseFloat(r.data.remaining_amount),
cumQuantity: parseFloat(r.data.executed_amount),
quantity: parseFloat(r.data.original_amount)
});
})
.done();
});
}).done();
this._since = moment.utc();
};
private static GetOrderStatus(r: BitfinexOrderStatusResponse) {
if (r.is_cancelled) return Models.OrderStatus.Cancelled;
if (r.is_live) return Models.OrderStatus.Working;
if (r.executed_amount === r.original_amount) return Models.OrderStatus.Complete;
return Models.OrderStatus.Other;
}
private _since = moment.utc();
private _log = Utils.log("tribeca:gateway:BitfinexOE");
constructor(timeProvider: Utils.ITimeProvider,
private _http: BitfinexHttp,
private _symbolProvider: BitfinexSymbolProvider) {
_http.ConnectChanged.on(s => this.ConnectChanged.trigger(s));
timeProvider.setInterval(this.downloadOrderStatuses, moment.duration(8, "seconds"));
}
}
class RateLimitMonitor {
private _log = Utils.log("tribeca:gateway:rlm");
private _queue = Deque();
private _durationMs: number;
public add = () => {
var now = moment.utc();
while (now.diff(this._queue.peek()) > this._durationMs) {
this._queue.shift();
}
this._queue.push(now);
if (this._queue.length > this._number) {
this._log.error("Exceeded rate limit", { nRequests: this._queue.length, max: this._number, durationMs: this._durationMs });
}
}
constructor(private _number: number, duration: moment.Duration) {
this._durationMs = duration.asMilliseconds();
}
}
class BitfinexHttp {
ConnectChanged = new Utils.Evt<Models.ConnectivityStatus>();
private _timeout = 15000;
get = <T>(actionUrl: string, qs?: any): Q.Promise<Models.Timestamped<T>> => {
const url = this._baseUrl + "/" + actionUrl;
var opts = {
timeout: this._timeout,
url: url,
qs: qs || undefined,
method: "GET"
};
return this.doRequest<T>(opts, url);
};
// Bitfinex seems to have a race condition where nonces are processed out of order when rapidly placing orders
// Retry here - look to mitigate in the future by batching orders?
post = <TRequest, TResponse>(actionUrl: string, msg: TRequest): Q.Promise<Models.Timestamped<TResponse>> => {
return this.postOnce<TRequest, TResponse>(actionUrl, _.clone(msg)).then(resp => {
var rejectMsg: string = (<any>(resp.data)).message;
if (typeof rejectMsg !== "undefined" && rejectMsg.indexOf("Nonce is too small") > -1)
return this.post<TRequest, TResponse>(actionUrl, _.clone(msg));
else
return resp;
});
}
private postOnce = <TRequest, TResponse>(actionUrl: string, msg: TRequest): Q.Promise<Models.Timestamped<TResponse>> => {
msg["request"] = "/v1/" + actionUrl;
msg["nonce"] = this._nonce.toString();
this._nonce += 1;
var payload = new Buffer(JSON.stringify(msg)).toString("base64");
var signature = crypto.createHmac("sha384", this._secret).update(payload).digest('hex');
const url = this._baseUrl + "/" + actionUrl;
var opts: request.Options = {
timeout: this._timeout,
url: url,
headers: {
"X-BFX-APIKEY": this._apiKey,
"X-BFX-PAYLOAD": payload,
"X-BFX-SIGNATURE": signature
},
method: "POST"
};
return this.doRequest<TResponse>(opts, url);
};
private doRequest = <TResponse>(msg: request.Options, url: string): Q.Promise<Models.Timestamped<TResponse>> => {
var d = Q.defer<Models.Timestamped<TResponse>>();
this._monitor.add();
request(msg, (err, resp, body) => {
if (err) {
this._log.error(err, "Error returned: url=", url, "err=", err);
d.reject(err);
}
else {
try {
var t = Utils.date();
var data = JSON.parse(body);
d.resolve(new Models.Timestamped(data, t));
}
catch (err) {
this._log.error(err, "Error parsing JSON url=", url, "err=", err, ", body=", body);
d.reject(err);
}
}
});
return d.promise;
};
private _log = Utils.log("tribeca:gateway:BitfinexHTTP");
private _baseUrl: string;
private _apiKey: string;
private _secret: string;
private _nonce: number;
constructor(config: Config.IConfigProvider, private _monitor: RateLimitMonitor) {
this._baseUrl = config.GetString("BitfinexHttpUrl")
this._apiKey = config.GetString("BitfinexKey");
this._secret = config.GetString("BitfinexSecret");
this._nonce = new Date().valueOf();
this._log.info("Starting nonce: ", this._nonce);
setTimeout(() => this.ConnectChanged.trigger(Models.ConnectivityStatus.Connected), 10);
}
}
interface BitfinexPositionResponseItem {
type: string;
currency: string;
amount: string;
available: string;
}
class BitfinexPositionGateway implements Interfaces.IPositionGateway {
PositionUpdate = new Utils.Evt<Models.CurrencyPosition>();
private onRefreshPositions = () => {
this._http.post<{}, BitfinexPositionResponseItem[]>("balances", {}).then(res => {
_.forEach(_.filter(res.data, x => x.type === "exchange"), p => {
var amt = parseFloat(p.amount);
var cur = GetCurrencyEnum(p.currency);
var held = amt - parseFloat(p.available);
var rpt = new Models.CurrencyPosition(amt, held, cur);
this.PositionUpdate.trigger(rpt);
});
}).done();
}
private _log = Utils.log("tribeca:gateway:BitfinexPG");
constructor(timeProvider: Utils.ITimeProvider, private _http: BitfinexHttp) {
timeProvider.setInterval(this.onRefreshPositions, moment.duration(15, "seconds"));
this.onRefreshPositions();
}
}
class BitfinexBaseGateway implements Interfaces.IExchangeDetailsGateway {
public get hasSelfTradePrevention() {
return false;
}
name(): string {
return "Bitfinex";
}
makeFee(): number {
return 0.001;
}
takeFee(): number {
return 0.002;
}
exchange(): Models.Exchange {
return Models.Exchange.Bitfinex;
}
private static AllPairs = [
new Models.CurrencyPair(Models.Currency.BTC, Models.Currency.USD),
//new Models.CurrencyPair(Models.Currency.LTC, Models.Currency.USD),
];
public get supportedCurrencyPairs() {
return BitfinexBaseGateway.AllPairs;
}
}
function GetCurrencyEnum(c: string): Models.Currency {
switch (c.toLowerCase()) {
case "usd": return Models.Currency.USD;
case "ltc": return Models.Currency.LTC;
case "btc": return Models.Currency.BTC;
default: throw new Error("Unsupported currency " + c);
}
}
function GetCurrencySymbol(c: Models.Currency): string {
switch (c) {
case Models.Currency.USD: return "usd";
case Models.Currency.LTC: return "ltc";
case Models.Currency.BTC: return "btc";
default: throw new Error("Unsupported currency " + Models.Currency[c]);
}
}
class BitfinexSymbolProvider {
public symbol: string;
constructor(pair: Models.CurrencyPair) {
this.symbol = GetCurrencySymbol(pair.base) + GetCurrencySymbol(pair.quote);
}
}
export class Bitfinex extends Interfaces.CombinedGateway {
constructor(timeProvider: Utils.ITimeProvider, config: Config.IConfigProvider, pair: Models.CurrencyPair) {
var symbol = new BitfinexSymbolProvider(pair);
var monitor = new RateLimitMonitor(60, moment.duration(1, "minutes"));
var http = new BitfinexHttp(config, monitor);
var orderGateway = config.GetString("BitfinexOrderDestination") == "Bitfinex"
? <Interfaces.IOrderEntryGateway>new BitfinexOrderEntryGateway(timeProvider, http, symbol)
: new NullGateway.NullOrderGateway();
super(
new BitfinexMarketDataGateway(timeProvider, http, symbol),
orderGateway,
new BitfinexPositionGateway(timeProvider, http),
new BitfinexBaseGateway());
}
}