get recent trade orders info in web socket streams binance api(node js)

Viewed 2199

i need to get recent orders vie websocets currenty saw this, this is dont using node-binance-api

// The only time the user data (account balances) and order execution websockets will fire, is if you create or cancel an order, or an order gets filled or partially filled
function balance_update(data) {
    console.log("Balance Update");
    for ( let obj of data.B ) {
        let { a:asset, f:available, l:onOrder } = obj;
        if ( available == "0.00000000" ) continue;
        console.log(asset+"\tavailable: "+available+" ("+onOrder+" on order)");
    }
}
function execution_update(data) {
    let { x:executionType, s:symbol, p:price, q:quantity, S:side, o:orderType, i:orderId, X:orderStatus } = data;
    if ( executionType == "NEW" ) {
        if ( orderStatus == "REJECTED" ) {
            console.log("Order Failed! Reason: "+data.r);
        }
        console.log(symbol+" "+side+" "+orderType+" ORDER #"+orderId+" ("+orderStatus+")");
        console.log("..price: "+price+", quantity: "+quantity);
        return;
    }
    //NEW, CANCELED, REPLACED, REJECTED, TRADE, EXPIRED
    console.log(symbol+"\t"+side+" "+executionType+" "+orderType+" ORDER #"+orderId);
}
binance.websockets.userData(balance_update, execution_update);

but i need to get only the recent order data alone with order status like open, partially filled or fully filled. and by any means is this possible- like getting this data on a ws stream

i currently use

const WS = require('ws');
const ws = new WS('wss://stream.binance.com:9443/ws/shibusdt@bookTicker');
ws.on('message', function incoming(sdata) {}

like this, so for order info i can create other ws variable and get incomming data form that, or something like that. id that possible.

1 Answers

your sample code requires node-binance-api. See binance.websockets.userData(balance_update, execution_update)); it needs API KEY and SECRET to access. To get real time orders via this secure request, try...

binance.websockets.trades(['SHIBUSDT','ETHBTC'], (trades) => {
  let {e:eventType, E:eventTime, s:symbol, p:price, q:quantity, m:maker, a:tradeId} = trades;
  console.info(symbol+" trade update. price: "+price+", quantity: "+quantity+", maker: "+maker);
});

To get real time orders via websockets, try using @trade instead of @bookTicker

const ws = new WS('wss://stream.binance.com:9443/ws/shibusdt@trade');
Related