steam lesen und orderbook kopieren funktioniert
This commit is contained in:
parent
d3ab345761
commit
022602002a
78
app.js
78
app.js
@ -2,10 +2,14 @@ var bs = require('binarysearch');
|
|||||||
var Gdax = require('gdax');
|
var Gdax = require('gdax');
|
||||||
|
|
||||||
|
|
||||||
var orderBook = {
|
var orderBook = [];
|
||||||
bids: {},
|
orderBook['bids'] = [];
|
||||||
asks: {}
|
orderBook['bids']['price'] = [];
|
||||||
};
|
orderBook['bids']['size'] = [];
|
||||||
|
orderBook['asks'] = [];
|
||||||
|
orderBook['asks']['price'] = [];
|
||||||
|
orderBook['asks']['size'] = [];
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var wsGdax = new Gdax.WebsocketClient(
|
var wsGdax = new Gdax.WebsocketClient(
|
||||||
@ -17,7 +21,7 @@ var wsGdax = new Gdax.WebsocketClient(
|
|||||||
|
|
||||||
|
|
||||||
function array_keys(input) {
|
function array_keys(input) {
|
||||||
var output = new Array();
|
var output = [];
|
||||||
var counter = 0;
|
var counter = 0;
|
||||||
for (i in input) {
|
for (i in input) {
|
||||||
output[counter++] = i;
|
output[counter++] = i;
|
||||||
@ -31,12 +35,21 @@ wsGdax.on('message', function(data) {
|
|||||||
// save snapshot on orderBook object
|
// save snapshot on orderBook object
|
||||||
for(var i=0; i<data.bids.length; i++) {
|
for(var i=0; i<data.bids.length; i++) {
|
||||||
var bid = data.bids[i];
|
var bid = data.bids[i];
|
||||||
orderBook.bids[bid[0]] = bid[1];
|
pos = bs.closest(orderBook['bids']['price'], bid[0]);
|
||||||
|
|
||||||
|
orderBook['bids']['price'].splice(pos, 0, bid[0]);
|
||||||
|
orderBook['bids']['size'].splice(pos, 0, bid[1]);
|
||||||
|
|
||||||
}
|
}
|
||||||
for(var i=0; i<data.asks.length; i++) {
|
for(var i=0; i<data.asks.length; i++) {
|
||||||
var ask = data.asks[i];
|
var ask = data.asks[i];
|
||||||
orderBook.asks[ask[0]] = ask[1];
|
pos = bs.closest(orderBook['asks']['price'], ask[0]);
|
||||||
|
|
||||||
|
orderBook['asks']['price'].splice(pos, 0, ask[0]);
|
||||||
|
orderBook['asks']['size'].splice(pos, 0, ask[1]);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,50 +60,31 @@ wsGdax.on('message', function(data) {
|
|||||||
var change = changes[i];
|
var change = changes[i];
|
||||||
if(change[0] === 'buy') {
|
if(change[0] === 'buy') {
|
||||||
if(change[2] === '0') {
|
if(change[2] === '0') {
|
||||||
delete orderBook.bids[change[1]];
|
pos = bs.closest(orderBook['bids']['price'], change[1]);
|
||||||
|
orderBook['bids']['price'].splice(pos, 1);
|
||||||
|
orderBook['bids']['size'].splice(pos, 1);
|
||||||
} else {
|
} else {
|
||||||
pos = bs.closest(array_keys(orderBook.bids), change[1]);
|
pos = bs.closest(orderBook['bids']['price'], change[1]);
|
||||||
tmp = new array();
|
|
||||||
tmp[change[1]] = change[2];
|
orderBook['bids']['price'].splice(pos, 0, change[1]);
|
||||||
|
orderBook['bids']['size'].splice(pos, 0, change[2]);
|
||||||
orderBook.bids = orderBook.bids.splice(pos, 0, tmp);
|
|
||||||
console.log(orderBook.bids);
|
|
||||||
|
|
||||||
delete tmp;
|
|
||||||
//orderBook.bids[change[1]] = change[2];
|
|
||||||
}
|
}
|
||||||
} else if (change[0] === 'sell') {
|
} else if (change[0] === 'sell') {
|
||||||
if(change[2] === '0') {
|
if(change[2] === '0') {
|
||||||
delete orderBook.asks[change[1]];
|
pos = bs.closest(orderBook['asks']['price'], change[1]);
|
||||||
|
orderBook['asks']['price'].splice(pos, 1);
|
||||||
|
orderBook['asks']['size'].splice(pos, 1);
|
||||||
} else {
|
} else {
|
||||||
orderBook.asks[change[1]] = change[2];
|
pos = bs.closest(orderBook['asks']['price'], change[1]);
|
||||||
|
|
||||||
|
orderBook['asks']['price'].splice(pos, 0, change[1]);
|
||||||
|
orderBook['asks']['size'].splice(pos, 0, change[2]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// crate arrays then sort
|
console.log("bids " + orderBook['bids']['size'].length + "/" + orderBook['bids']['price'].length + " = " + orderBook['bids']['price'].slice(-1) + " -------- " + "asks " + orderBook['asks']['size'].length + "/" + orderBook['asks']['price'].length + " = " + orderBook['asks']['price'].slice(-1));
|
||||||
var bids = [];
|
|
||||||
var asks = [];
|
|
||||||
for(var price in orderBook.bids) {
|
|
||||||
bids.push([parseFloat(price), parseFloat(orderBook.bids[price])]);
|
|
||||||
}
|
|
||||||
for(var price in orderBook.asks) {
|
|
||||||
asks.push([parseFloat(price), parseFloat(orderBook.asks[price])]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// sort asc
|
|
||||||
asks.sort(function (a, b) {
|
|
||||||
return a[0] - b[0];
|
|
||||||
});
|
|
||||||
|
|
||||||
// sort desc
|
|
||||||
bids.sort(function (a, b) {
|
|
||||||
return b[0] - a[0];
|
|
||||||
});
|
|
||||||
|
|
||||||
if( asks[0][0] < bids[0][0] ) {
|
|
||||||
console.log('order book out of sync', asks[0], bids[0]);
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user