Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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;
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);
}
terminateWebsocket: function(websocketName) {
var endpoints = Object.keys(binance.websockets.subscriptions())
if(endpoints.indexOf(websocketName) != -1) {
binance.websockets.terminate(websocketName);
}
}
}
removeSocket = endpoint => binance.websockets.terminate(endpoint);
function initUserDataUpdates() {
if(userDataWebsocket.connecting) return
userDataWebsocket.connectionTimestamp = Date.now()
userDataWebsocket.connecting = true
binance.websockets.userData(balance_update, execution_update, function(listenKey) {
userDataWebsocket.listenKey = listenKey
userDataWebsocket.connecting = false
})
}
bindSocket = (symbol) => {
binance.websockets.candlesticks([symbol], '1m', (candlesticks) => {
const { k: ticks } = candlesticks;
const { c: close } = ticks;
const currentPrice = parseFloat(close);
this.setState({ currentPrice });
this.checkPrice(currentPrice);
});
};
startChartUpdate: function(pairName, interval, next) {
binance.websockets.chart(pairName, interval, (pairName, interval, chart) => {
next(pairName, interval, chart)
});
},
websocketActive: function(websocketName) {