-
Notifications
You must be signed in to change notification settings - Fork 1
/
Websockets.js
95 lines (81 loc) · 2.68 KB
/
Websockets.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
const binance = require('node-binance-api');
const calculate = require('./CalculateThroughput.js');
const normalize = require('./createDataObject.js');
let warmupTimer = 0;
let profit = 0;
let profitableTrades = 0;
let activeProfitableRoute = false;
binance.options({
// these are invalid, be sure to use your own key
APIKEY: 'ofVTc0iMdNO18rVgMk3KjdOhVdEVg7e2cj17A1DKCpWvNbf6Nnv2jjPxQLbSJ3JT',
APISECRET: 'BdOr83FEkgdPEP7oXA21MjdLI43v09NCWivgLxlsHBHhvsYgrnCrjM6MFDIsGzi7'
});
function subscribeTickerObject(
tickerSymbol,
tickersObject,
calculationFunction
) {
binance.websockets.depthCache([tickerSymbol], (symbol, depth) => {
let bids = binance.sortBids(depth.bids);
let asks = binance.sortAsks(depth.asks);
let previousBid = tickersObject[tickerSymbol].bid;
let previousAsk = tickersObject[tickerSymbol].ask;
let currentBid = binance.first(bids);
let currentAsk = binance.first(asks);
let currentBidVolume = bids[binance.first(bids)];
let currentAskVolume = asks[binance.first(asks)];
tickersObject[tickerSymbol].bid = currentBid;
tickersObject[tickerSymbol].bidQty = currentBidVolume;
tickersObject[tickerSymbol].ask = currentAsk;
tickersObject[tickerSymbol].askQty = currentAskVolume;
if (warmupTimer < 20) {
console.log('warming up:', warmupTimer);
warmupTimer++;
} else {
const dataObject = normalize.createDataObject(
'LTCBTC',
'LTCETH',
'ETHBTC',
'BUY',
'SELL',
'SELL',
tickersObject
);
let trade = calculate.calculateThroughput(dataObject, null);
if (trade.profitability > 0) {
if (activeProfitableRoute != true) {
activeProfitableRoute = true;
profit += trade.profitability;
profitableTrades++;
console.log(JSON.stringify(trade));
}
console.log(JSON.stringify(trade));
console.log('Profits:', profit);
console.log('trades', profitableTrades);
} else {
activeProfitableRoute = false;
}
}
});
}
function constructTickers(tickersObject, tickersNameArray) {
for (let i = 0; i < tickersNameArray.length; i++) {
tickersObject[tickersNameArray[i]] = {
bid: null,
bidQty: null,
ask: null,
askQty: null
};
}
}
function subscribeTickers(tickers, calculationFunction) {
let keys = Object.keys(tickers);
for (let i = 0; i < keys.length; i++) {
subscribeTickerObject(keys[i], tickers, calculationFunction);
}
}
function initializeData(tickers, tickerData, calculationFunction) {
constructTickers(tickers, tickerData);
subscribeTickers(tickers, calculationFunction);
}
module.exports.initialize = initializeData;