forked from chrisleekr/binance-trading-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server-binance.js
147 lines (120 loc) · 3.87 KB
/
server-binance.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
const _ = require('lodash');
const moment = require('moment-timezone');
const config = require('config');
const { PubSub, binance, cache, slack } = require('./helpers');
const {
getGlobalConfiguration
} = require('./cronjob/trailingTradeHelper/configuration');
let websocketCandlesClean;
let lastReceivedAt = moment();
/**
* Setup web socket for retrieving candles
*
* @param {*} logger
*/
const setWebSocketCandles = async logger => {
logger.info('Set websocket for candles');
// Get configuration
const globalConfiguration = await getGlobalConfiguration(logger);
const { symbols } = globalConfiguration;
logger.info({ symbols }, 'Retrieved symbols');
if (websocketCandlesClean) {
logger.info('Existing opened socket for candles found, clean first');
websocketCandlesClean();
}
websocketCandlesClean = binance.client.ws.candles(symbols, '1m', candle => {
logger.info({ candle }, 'Received new candle');
// Record last received date/time
lastReceivedAt = moment();
// Save latest candle for the symbol
cache.hset(
'trailing-trade-symbols',
`${candle.symbol}-latest-candle`,
JSON.stringify(candle)
);
});
};
/**
* Setup retrieving latest candle from live server via Web Socket
*
* @param {*} logger
*/
const setupLive = async logger => {
PubSub.subscribe(
'trailing-trade-configuration-changed',
async (message, data) => {
logger.info(`Message: ${message}, Data: ${data}`);
await setWebSocketCandles(logger);
}
);
await setWebSocketCandles(logger);
};
const loopToCheckLastReceivedAt = async logger => {
const currentTime = moment();
// If last received candle time is more than a mintues, then it means something went wrong. Reconnect websocket.
if (lastReceivedAt.diff(currentTime) / 1000 < -60) {
logger.warn(
{ debug: true },
'Binance candle is not received in last mintues. Reconfigure websocket'
);
if (config.get('featureToggle.notifyDebug')) {
slack.sendMessage(
`Binance Websocket (${moment().format(
'HH:mm:ss.SSS'
)}): The bot didn't receive new candle from Binance Websocket since ${lastReceivedAt.fromNow()}.` +
` Reset Websocket connection.`
);
}
await setupLive(logger);
}
setTimeout(() => loopToCheckLastReceivedAt(logger), 1000);
};
/**
* Setup retrieving latest candle from test server via API
*
* @param {*} logger
*/
const setupTest = async logger => {
// Get configuration
const globalConfiguration = await getGlobalConfiguration(logger);
const { symbols } = globalConfiguration;
logger.info({ symbols }, 'Retrieved symbols');
const currentPrices = await binance.client.prices();
_.forEach(currentPrices, (currentPrice, currentSymbol) => {
if (symbols.includes(currentSymbol)) {
logger.info({ currentSymbol, currentPrice }, 'Received new price');
cache.hset(
'trailing-trade-symbols',
`${currentSymbol}-latest-candle`,
JSON.stringify({
eventType: 'kline',
symbol: currentSymbol,
close: currentPrice
})
);
}
});
// It's impossible to test async function in the setTimeout.
/* istanbul ignore next */
setTimeout(() => setupTest(logger), 1000);
};
/**
* Configure Binance Web Socket
*
* Note that Binance Test Server Web Socket is not providing test server's candles.
* To avoid the issue with the test server, when the mode is test, it will use API call to retrieve current prices.
*
* @param {*} serverLogger
*/
const runBinance = async serverLogger => {
const logger = serverLogger.child({ server: 'binance' });
const mode = config.get('mode');
logger.info({ config }, `Binance ${config.get('mode')} started on`);
if (mode === 'live') {
await setupLive(logger);
await loopToCheckLastReceivedAt(logger);
} else {
await setupTest(logger);
}
};
module.exports = { runBinance };