-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
431 lines (400 loc) · 12.8 KB
/
app.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
const bookkeeper = {
init: async function(pair) {
getBook(pair).then((books) =>
sessionStorage.setItem('book', JSON.stringify(parseBook(books))),
//$('#storageElem').text(JSON.stringify(parseBook(books)))
)
},
read: function() {
//return JSON.parse($('#storageElem').text());
return JSON.parse(sessionStorage.getItem('book'))
},
update: function(data) {
// $('#storageElem').text(JSON.stringify(data));
sessionStorage.setItem('book', JSON.stringify(data))
},
listen: function(pair) {
let socket = new WebSocket(
'wss://stream.binance.com:9443/ws/' +
pair.toString().toLowerCase() +
'@depth',
)
socket.onmessage = function(event) {
let data = JSON.parse(event.data)
let book = bookkeeper.read()
const newBook = updateTheBooks(book, parseUpdate(data))
const updatedBook = {
bids: newBook.bids.filter((bid) => bid.amount > 0),
asks: newBook.asks.filter((ask) => ask.amount > 0),
}
bookkeeper.update(updatedBook)
render(updatedBook)
}
},
}
function getMarkPrice(book){
const bids = book.bids;
const asks = book.asks;
const midPrice = (bids[0].price + asks[0].price)/2;
return midPrice;
}
function updateTheBooks(oldBook, update) {
let newBook = {
bids: [],
asks: [],
}
let sumBidAsset = 0
let sumBidQuote = 0
let sumAskAsset = 0
let sumAskQuote = 0
let asksD = newLevels(oldBook.asks, update.asks)
let bidsD = newLevels(oldBook.bids, update.bids)
if (asksD.length > 0) {
asksD.forEach((ask) => {
oldBook.asks.push(ask)
})
}
if (bidsD.length > 0) {
bidsD.forEach((bid) => {
oldBook.bids.push(bid)
})
}
for (let i = 0; i < oldBook.bids.length; i++) {
let bid = oldBook.bids[i]
for (let j = 0; j < update.bids.length; j++) {
let updateBid = update.bids[j]
if (bid.price == updateBid.price) {
bid.amount = updateBid.amount
bid.total = updateBid.total
}
}
sumBidAsset += bid.amount
sumBidQuote += bid.total
bid.sumAsset = sumBidAsset
bid.sumQuote = sumBidQuote
newBook.bids.push(bid)
}
for (let i = 0; i < oldBook.asks.length; i++) {
let ask = oldBook.asks[i]
for (let j = 0; j < update.asks.length; j++) {
let updateAsk = update.asks[j]
if (ask.price == updateAsk.price) {
ask.amount = updateAsk.amount
ask.total = updateAsk.total
}
}
sumAskAsset += ask.amount
sumAskQuote += ask.total
ask.sumAsset = sumAskAsset
ask.sumQuote = sumAskQuote
newBook.asks.push(ask)
}
const book= {
bids: newBook.bids.sort(function(a, b) {
return parseFloat(b.price) - parseFloat(a.price)
}),
asks: newBook.asks.sort(function(a, b) {
return parseFloat(a.price) - parseFloat(b.price)
})
}
return book
}
function parseBook(data) {
let buyRaw = data.bids
let sellRaw = data.asks
let buy = buyRaw.map((order) => {
return {
price: parseFloat(order[0]),
amount: parseFloat(order[1]),
total: order[0] * order[1],
}
})
let sell = sellRaw.map((order) => {
return {
price: parseFloat(order[0]),
amount: parseFloat(order[1]),
total: order[0] * order[1],
}
})
return {
bids: buy,
asks: sell,
}
}
function parseUpdate(data) {
let adjData = {
bids: data.b,
asks: data.a,
}
return parseBook(adjData)
}
async function getBook(pair) {
const limit = getParameter("limit")
try {
let res = await axios({
url: 'https://api.binance.com/api/v1/depth?symbol=' +
pair.toString().toUpperCase() +
'&limit='+limit,
method: 'get',
timeout: 8000,
headers: {
'Content-Type': 'application/json',
},
})
if (res.status == 200) {
// test for status you want, etc
console.log(res.status)
}
// Don't forget to return something
return res.data
} catch (err) {
console.error(err)
}
}
function render(book) {
const htmlBids = htmlBid(book)
const htmlAsks = htmlAsk(book)
const rangeData = getRangeData(book)
const markPrice = getMarkPrice(book)
$("#marketPrice").text(markPrice.toFixed(2));
const htmlRange = {
bid:htmlBidRange(rangeData),
ask:htmlAskRange(rangeData),
}
var precision = parseInt(getPrecision());
var total = parseFloat(rangeData.bid.total).toFixed(precision)-parseFloat(rangeData.ask.total).toFixed(precision);
if(total>0){
$("#loadOrderBook").removeClass("btn-primary").removeClass("btn-danger").addClass("btn-success")
$("#volumeDiff").text(formatMoney(total));
}else{
total = total;
$("#loadOrderBook").removeClass("btn-primary").removeClass("btn-success").addClass("btn-danger")
$("#volumeDiff").text(formatMoney(total));
}
$('#bidTotalRange').html(htmlRange.bid)
$('#askTotalRange').html(htmlRange.ask)
$('#orderBookDataBuy').html(htmlBids)
$('#orderBookDataSell').html(htmlAsks)
}
/*
buy <tr> <td>price</td> <td>asset</td> <td>quote</td> <td>total asset</td> <td>total quote</td> </tr>
sell <tr> <td>total quote</td> <td>total asset</td> <td>quote</td> <td>asset</td> <td>price</td> </tr>
*/
function htmlBid(book) {
let html = ''
for (let i = 0; i < book.bids.length; i++) {
html += '<tr>'
html += '<td>' + formatMoney(book.bids[i].price) + '</td>'
html += '<td>' + formatMoney(book.bids[i].amount) + '</td>'
html += '<td>' + formatMoney(parseFloat(book.bids[i].sumAsset).toFixed(2)) + '</td>'
html += '<td>' + formatMoney(parseFloat(book.bids[i].total).toFixed(2)) + '</td>'
html += '<td>' + formatMoney(parseFloat(book.bids[i].sumQuote).toFixed(2)) + '</td>'
html += '</tr>'
}
return html
}
function htmlAsk(book) {
let html = ''
for (let i = 0; i < book.asks.length; i++) {
html += '<tr>'
html += '<td>' + formatMoney(parseFloat(book.asks[i].sumQuote).toFixed(2)) + '</td>'
html += '<td>' + formatMoney(parseFloat(book.asks[i].total).toFixed(2)) + '</td>'
html += '<td>' + formatMoney(parseFloat(book.asks[i].sumAsset).toFixed(2)) + '</td>'
html += '<td>' + formatMoney(book.asks[i].amount) + '</td>'
html += '<td>' + formatMoney(book.asks[i].price) + '</td>'
html += '</tr>'
}
return html
}
function htmlBidRange(rangeData) {
var precision = parseInt(getPrecision());
var total = parseFloat(rangeData.bid.total).toFixed(precision)-parseFloat(rangeData.ask.total).toFixed(precision);
var bidcolor='';
var askcolor='';
if(total<0) {
bidcolor='red';
askcolor='green';
} else {
bidcolor='green';
askcolor='red';
}
// return '<table><td>'+formatMoney(rangeData.bid.price[0])+'</td><td>'+formatMoney(rangeData.bid.price[1])+'</td><td style="color:'+bidcolor+'">'+formatMoney(rangeData.bid.total.toFixed(precision))+'</td></table>';
return formatMoney(rangeData.bid.price[0])+' < '+formatMoney(rangeData.bid.total.toFixed(precision))+' > '+formatMoney(rangeData.bid.price[1]);
}
function htmlAskRange(rangeData) {
var precision = parseInt(getPrecision());
var total = parseFloat(rangeData.bid.total).toFixed(precision)-parseFloat(rangeData.ask.total).toFixed(precision);
var bidcolor='';
var askcolor='';
if(total<0) {
bidcolor='red';
askcolor='green';
} else {
bidcolor='green';
askcolor='red';
}
// return '<table><td style="color:'+askcolor+'">'+formatMoney(rangeData.ask.total.toFixed(precision))+'</td><td>'+formatMoney(rangeData.ask.price[0])+'</td><td>'+formatMoney(rangeData.ask.price[1])+'</td></table>';
return formatMoney(rangeData.ask.price[0])+' < '+formatMoney(rangeData.ask.total.toFixed(precision))+' > '+formatMoney(rangeData.ask.price[1]);
}
function getPrecision(){
return getParameter("precision");
}
function getRangeData(book) {
var rangeData = {
bid:{
price: [],
amount: 0,
total: 0,
},
ask:{
price: [],
amount: 0,
total: 0,
}
}
let bidLimits = getBidLimit();
let askLimits = getAskLimit();
book.bids.forEach((bid) => {
if (bid.price >= bidLimits.min && bid.price <= bidLimits.max) {
rangeData.bid.price.push(bid.price)
rangeData.bid.amount += bid.amount
rangeData.bid.total += bid.total
}
}
)
book.asks.forEach((ask) => {
if (ask.price >= askLimits.min && ask.price <= askLimits.max) {
rangeData.ask.price.push(ask.price)
rangeData.ask.amount += ask.amount
rangeData.ask.total += ask.total
}
}
)
bidPrices = rangeData.bid.price.sort(function(a, b) {
return parseFloat(a) - parseFloat(b)
})
askPrices = rangeData.ask.price.sort(function(a, b) {
return parseFloat(a) - parseFloat(b)
})
let res ={
bid: {
price: [bidPrices[0], bidPrices[bidPrices.length - 1]],
amount: rangeData.bid.amount,
total: rangeData.bid.total,
},
ask: {
price: [askPrices[0], askPrices[askPrices.length - 1]],
amount: rangeData.ask.amount,
total: rangeData.ask.total,
},
};
return res;
}
function getBidLimit() {
var uplim = $("#upLimitBidInput").val();
var lowlim = $("#dnLimitBidInput").val();
if(uplim=='' || uplim==null || lowlim=='' || lowlim==null){
up=getParameter("bidUp");
down=getParameter("bidDown");
$("#upLimitBidInput").val(up);
$("#dnLimitBidInput").val(down);
return {
max:parseFloat(up),
min:parseFloat(down)
}
}else{
up = parseFloat($("#upLimitBidInput").val());
down = parseFloat($("#dnLimitBidInput").val());
return {
max:parseFloat(up),
min:parseFloat(down)
}
}
}
function getAskLimit() {
var uplim = $("#upLimitAskInput").val();
var lowlim = $("#dnLimitAskInput").val();
if(uplim=='' || uplim==null || lowlim=='' || lowlim==null){
up=getParameter("askUp");
down=getParameter("askDown");
$("#upLimitAskInput").val(up);
$("#dnLimitAskInput").val(down);
return {
max:parseFloat(up),
min:parseFloat(down)
}
}else{
up = parseFloat($("#upLimitAskInput").val());
down = parseFloat($("#dnLimitAskInput").val());
return {
max:parseFloat(up),
min:parseFloat(down)
}
}
}
function newLevels(a, b) {
const isSamePrice = (a, b) => a.price === b.price
const onlyInLeft = (left, right, compareFunction) =>
left.filter(
(leftValue) =>
!right.some((rightValue) => compareFunction(leftValue, rightValue)),
)
const onlyInA = onlyInLeft(a, b, isSamePrice)
const onlyInB = onlyInLeft(b, a, isSamePrice)
const result = [...onlyInB]
return result
}
function formatMoney(n) {
return (Math.round(n * 100) / 100).toLocaleString();
}
/*
$(document).ready(function() {
const asset = 'BTC'
const quote = 'USDT'
$('.asset').text(asset.toUpperCase())
$('.quote').text(quote.toUpperCase())
const pair = asset + quote
bookkeeper.init(pair)
bookkeeper.listen(pair)
});
*/
function getParameter(p)
{
var url = window.location.search.substring(1);
var varUrl = url.split('&');
for (var i = 0; i < varUrl.length; i++)
{
var parameter = varUrl[i].split('=');
if (parameter[0] == p)
{
return parameter[1];
}
}
}
$(document).ready(function() {
const asset = getParameter("asset")
const quote = getParameter("quote")
ub=getParameter("bidUp");
db=getParameter("bidDown");
$("#upLimitBidInput").val(ub);
$("#dnLimitBidInput").val(db);
ua=getParameter("askUp");
da=getParameter("askDown");
$("#upLimitAskInput").val(ua);
$("#dnLimitAskInput").val(da);
$('.asset').text(asset.toUpperCase())
$('.quote').text(quote.toUpperCase())
const pair = asset + quote
bookkeeper.init(pair)
bookkeeper.listen(pair)
$(".btn").click(function() {
$("#settingsModal").modal("show");
});
});
/*
todo
bid
order = price, amount, totalAmount, currency, totalCurrency
ask
order = totalCurrency, totalAmount, currency, amount, price
*/