Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// The connection was closed
stream.on('close', function() {
clients.splice(clients.indexOf(stream), 1);
console.log('Closed connection ');
});
});
});
// ===============================================================================
// Binance API / webSocket
const binance = require('node-binance-api');
binance.options({
APIKEY: '',
APISECRET: '',
useServerTime: true, // If you get timestamp errors, synchronize to server time at startup
test: false // If you want to use sandbox mode where orders are simulated
});
// --------- BTCUSDT ---------
var gameTimeCounter = 0;
var startBtcPrice = 0;
var endBtcPrice = 0;
// For a specific symbol:
binance.websockets.prevDay('BTCUSDT', (error, response) => {
// var t = new Date( response.eventTime );
// var formatted = t.format("dd.mm.yyyy hh:MM:ss");
const binance = require('node-binance-api');
binance.options({
APIKEY: '',
APISECRET: '',
useServerTime: true, // If you get timestamp errors, synchronize to server time at startup
test: false // If you want to use sandbox mode where orders are simulated
});
// --------- BTCUSDT ---------
var gameTimeCounter = 0;
var startBtcPrice = 0;
var endBtcPrice = 0;
// For a specific symbol:
binance.websockets.prevDay('BTCUSDT', (error, response) => {
// var t = new Date( response.eventTime );
// var formatted = t.format("dd.mm.yyyy hh:MM:ss");
// save to database
if(response.symbol && response.close && response.closeTime && response.closeQty){
let closeTime = moment(response.closeTime).format("HH:mm:ss");
let closePrice = response.close;
// game loop counter
if(gameTimeCounter == 0) {
startBtcPrice = response.close;
gameTimeCounter++;
} else if(gameTimeCounter == 9){
gameTimeCounter = 0;
endBtcPrice = closePrice;
module.exports = function(models,clients){
const binance = require('node-binance-api')().options({
APIKEY: '',
APISECRET: '',
useServerTime: true, // If you get timestamp errors, synchronize to server time at startup
test: false // If you want to use sandbox mode where orders are simulated
})
// --------- BTCUSDT ---------
const BTCUSDT_20 = 10;
var Btcusdt = models.btcusdt_table;
var initTimer = new Date().getTime() + BTCUSDT_20*1000;
var thisGameId = Math.round(initTimer/1000);
var nextGameId = thisGameId+BTCUSDT_20;
var gameStartBtcPrice = 0;
var gameEndBtcPrice = 0;
normalizeAmount: function(pair, amount, price) {
// Set minimum order amount with minQty
if ( amount < vars.pairsInfo[pair].filters.minQty ) amount = vars.pairsInfo[pair].filters.minQty;
// Set minimum order amount with minNotional
if (price && price * amount < vars.pairsInfo[pair].filters.minNotional ) {
amount = vars.pairsInfo[pair].filters.minNotional / price;
}
// Round to stepSize
return binance.roundStep(amount, vars.pairsInfo[pair].filters.stepSize);
},
fixPrice: function(pair, price) {
if (!isPresent(index)) {
UNFILLED.push(symbol);
console.log('Added ' + symbol + ' (' + askDepth + '/' + bidDepth + ') - ' + UNFILLED.length);
}
} else if (isPresent(index)) {
UNFILLED.splice(index, 1);
console.log('Removed ' + symbol + ' (' + askDepth + '/' + bidDepth + ') - ' + UNFILLED.length);
}
sockets.emit('depth:new', {
ticker: symbol,
unfilled: UNFILLED,
time: new Date().getTime(),
bid: bids,
ask: asks,
market: (parseFloat(binance.first(bids)) + parseFloat(binance.first(asks))) / 2
});
}, LIMIT);
binance.websockets.depthCache(tickers, function(symbol, depth) {
var bids = binance.sortBids(depth.bids);
var asks = binance.sortAsks(depth.asks);
var askDepth = Object.keys(asks).length;
var bidDepth = Object.keys(bids).length;
var index = UNFILLED.indexOf(symbol);
if (askDepth < MIN_THRESHOLD && bidDepth < MIN_THRESHOLD) {
if (!isPresent(index)) {
UNFILLED.push(symbol);
console.log('Added ' + symbol + ' (' + askDepth + '/' + bidDepth + ') - ' + UNFILLED.length);
}
} else if (isPresent(index)) {
UNFILLED.splice(index, 1);
console.log('Removed ' + symbol + ' (' + askDepth + '/' + bidDepth + ') - ' + UNFILLED.length);
}
sockets.emit('depth:new', {
ticker: symbol,
binance.websockets.depthCache(tickers, function(symbol, depth) {
var bids = binance.sortBids(depth.bids);
var asks = binance.sortAsks(depth.asks);
var askDepth = Object.keys(asks).length;
var bidDepth = Object.keys(bids).length;
var index = UNFILLED.indexOf(symbol);
if (askDepth < MIN_THRESHOLD && bidDepth < MIN_THRESHOLD) {
if (!isPresent(index)) {
UNFILLED.push(symbol);
console.log('Added ' + symbol + ' (' + askDepth + '/' + bidDepth + ') - ' + UNFILLED.length);
}
} else if (isPresent(index)) {
UNFILLED.splice(index, 1);
console.log('Removed ' + symbol + ' (' + askDepth + '/' + bidDepth + ') - ' + UNFILLED.length);
}
sockets.emit('depth:new', {
componentDidMount() {
var self = this;
binance.websockets.candlesticks(['BTCUSDT'], "1m", (candlesticks) => {
let { e:eventType, E:eventTime, s:symbol, k:ticks } = candlesticks;
let { o:open, h:high, l:low, c:close, v:volume, n:trades, i:interval, x:isFinal, q:quoteVolume, V:buyVolume, Q:quoteBuyVolume } = ticks;
//console.log(symbol+" "+interval+" candlestick update");
self.setState({
currentPrice: close
})
});
}
function watch(tickers, interval='1m') {
if (!tickers) throw 'No tickers provided';
if (typeof tickers === 'string') tickers = [tickers];
tickers.forEach(clearCandles);
console.log(`Opening websocket connection for ${tickers} ...`);
binance.websockets.candlesticks(tickers, interval, processTick);
}
function depthCache(tickers, sockets) {
console.log('Opening depth websockets for ' + tickers.length + ' tickers');
binance.websockets.depthCache(tickers, function(symbol, depth) {
var bids = binance.sortBids(depth.bids);
var asks = binance.sortAsks(depth.asks);
var askDepth = Object.keys(asks).length;
var bidDepth = Object.keys(bids).length;
var index = UNFILLED.indexOf(symbol);
if (askDepth < MIN_THRESHOLD && bidDepth < MIN_THRESHOLD) {
if (!isPresent(index)) {
UNFILLED.push(symbol);
console.log('Added ' + symbol + ' (' + askDepth + '/' + bidDepth + ') - ' + UNFILLED.length);
}
} else if (isPresent(index)) {
UNFILLED.splice(index, 1);
console.log('Removed ' + symbol + ' (' + askDepth + '/' + bidDepth + ') - ' + UNFILLED.length);
}