-
Notifications
You must be signed in to change notification settings - Fork 24
/
websocket-orderbook.js
208 lines (186 loc) · 7.53 KB
/
websocket-orderbook.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
'use strict';
const ccxt = require('../../ccxt.js')
const extend = ccxt.extend;
var fs = require('fs');
const baseConfig = {
exchangeDefaults: {
verbose: false
},
symbolDefaults: {
limit: 5
},
marketTable: {
marketsByRow: 3,
maxLimit: 10,
marketColumnWidth: 50,
},
exchanges : {
bitfinex2 : {
symbols: {
"BTC/USDT": {},
"XRP/USDT": {}
},
options: {
}
},
binance: {
symbols: {
'BTC/USDT':{},
'NANO/BTC':{},
'ETH/BTC':{}
},
options: {
}
}
}
};
var marketTable;
function main() {
var config;
if (process.argv.length > 2) {
config = extend ({}, baseConfig, JSON.parse(fs.readFileSync(process.argv[2], 'utf8')));
} else {
config = extend ({} , baseConfig);
}
marketTable = new MarketTable(config.marketTable);
for (var id in config.exchanges) {
var exchange = config.exchanges[id];
const exConf = extend ({}, config.exchangeDefaults, exchange.options | {});
const ex = new ccxt[id]({
apiKey: exConf.apiKey | '',
secret: exConf.apiSecret | '',
enableRateLimit: true,
verbose: exConf.verbose | false,
});
for (var symbol in exchange.symbols) {
marketTable.addMarket (ex.id, symbol);
}
subscribe (ex, exchange.symbols, config.symbolDefaults);
}
}
function subscribe(exchange, symbols, defaultConfig) {
exchange.on('err', (err, conxid) => {
try {
exchange.websocketClose(conxid);
} catch (ex) {
}
for (var symbol in symbols) {
marketTable.marketError(exchange.id, symbol, err);
}
marketTable.print();
});
exchange.on('ob', (market, ob) => {
marketTable.updateMarket (exchange.id, market, ob);
marketTable.print();
});
exchange.loadMarkets().
then(async () => {
for (var symbol in symbols) {
const config = extend ({}, defaultConfig, symbols[symbol]);
try {
await exchange.websocketSubscribe('ob', symbol, config);
} catch (ex) {
marketTable.marketError(exchange.id, symbol, ex);
marketTable.print();
}
}
}).
catch((error) => {
for (var symbol in symbols) {
marketTable.marketError(exchange.id, symbol, error);
}
marketTable.print();
});
}
class MarketTable {
constructor (options){
this.options = extend ({}, {
marketsByRow: 2,
maxLimit: 10,
marketColumnWidth: 50,
}, options);
this.markets = {};
this.grid = [];
this.newEmptyLine = Array(this.options.marketColumnWidth * this.options.marketsByRow).join(" ");
this.hr = Array(this.options.marketColumnWidth).join("-");
this.bidsAdksSeparator = Array(this.options.marketColumnWidth).join(".");
this.emptyCell = Array(this.options.marketColumnWidth).join(" ");
this.amountColumn = Math.floor(this.options.marketColumnWidth / 2);
this.height = 2+this.options.maxLimit + 1 + this.options.maxLimit + 1;
}
replaceAt (str, index, replacement) {
return str.substr(0, index) + replacement + str.substr(index + replacement.length);
}
addMarket (marketId, marketSymbol) {
const gridPosition = Object.keys(this.markets).length;
const gridRow = Math.floor(gridPosition / this.options.marketsByRow) * this.height;
const gridColumn = (gridPosition % this.options.marketsByRow) * this.options.marketColumnWidth;
const lastUpdate = new Date();
this.markets[marketId + '@' + marketSymbol] = {
gridPosition,
gridRow,
gridColumn,
lastUpdate,
marketId,
marketSymbol
};
// add new row
if (this.grid.length <= gridRow) {
const newHeight = gridRow + (this.options.maxLimit * 2) + 4;
for (var i=this.grid.length ; i < newHeight; i++) {
this.grid.push(this.newEmptyLine);
}
}
this.grid[gridRow] = this.replaceAt (this.grid[gridRow], gridColumn, marketId + ":" + marketSymbol+ ":" + lastUpdate.toISOString());
this.grid[gridRow+1] = this.replaceAt (this.grid[gridRow+1], gridColumn, this.hr);
const separatorRow = gridRow+2+this.options.maxLimit;
this.grid[separatorRow] = this.replaceAt (this.grid[separatorRow], gridColumn, this.bidsAdksSeparator);
}
updateMarket (marketId, marketSymbol, ob) {
const lastUpdate = new Date();
const gridRow = this.markets[marketId + '@' + marketSymbol].gridRow;
const gridColumn = this.markets[marketId + '@' + marketSymbol].gridColumn;
this.markets[marketId + '@' + marketSymbol].lastUpdate = lastUpdate;
// update title
this.grid[gridRow] = this.replaceAt (this.grid[gridRow], gridColumn, marketId + ":" + marketSymbol+ ":" + lastUpdate.toISOString());
for (var index=0,i=gridRow + 2 + this.options.maxLimit - 1; (index < this.options.maxLimit) && (index < ob.bids.length); i--, index++) {
const price = ob.bids[index][0];
const ammount = ob.bids[index][1];
this.grid[i] = this.replaceAt (this.grid[i], gridColumn, this.emptyCell);
this.grid[i] = this.replaceAt (this.grid[i], gridColumn, price.toString());
this.grid[i] = this.replaceAt (this.grid[i], gridColumn + this.amountColumn, ":"+ ammount);
}
for (; index < this.options.maxLimit; index++,i--) {
this.grid[i] = this.replaceAt (this.grid[i], gridColumn, this.emptyCell);
}
for (var index=0,i = gridRow + 2 + this.options.maxLimit + 1; (index < this.options.maxLimit) && (index < ob.asks.length); i++, index++) {
const price = ob.asks[index][0];
const ammount = ob.asks[index][1];
this.grid[i] = this.replaceAt (this.grid[i], gridColumn, this.emptyCell);
this.grid[i] = this.replaceAt (this.grid[i], gridColumn, price.toString());
this.grid[i] = this.replaceAt (this.grid[i], gridColumn + this.amountColumn, ":"+ ammount);
}
for (; index < this.options.maxLimit; index++,i++) {
this.grid[i] = this.replaceAt (this.grid[i], gridColumn, this.emptyCell);
}
}
marketError (marketId, marketSymbol, error) {
const gridRow = this.markets[marketId + '@' + marketSymbol].gridRow;
const gridColumn = this.markets[marketId + '@' + marketSymbol].gridColumn;
const errLines = error.toString().split(this.options.marketColumnWidth - 1);
// this.grid[gridRow+1] = this.replaceAt (this.grid[gridRow+1], gridColumn, this.bidsAdksSeparator);
for (var index=0,i=gridRow + 2; (index < this.options.maxLimit * 2 + 1) && (index < errLines.length); i++, index++) {
this.grid[i] = this.replaceAt (this.grid[i], gridColumn, this.emptyCell);
this.grid[i] = this.replaceAt (this.grid[i], gridColumn, errLines[index]);
}
}
print () {
//process.stdout.write('\\033c');
// process.stdout.write("\u001b[2J\u001b[0;0H");
process.stdout.write("\u001b[0;0H");
for (var i = 0; i<this.grid.length; i++) {
console.log(this.grid[i]);
}
}
}
main();