Hi guys,
I am using ‘ORDER_TRADE_UPDATE’. I want to get data if what happen with open orders, such as: create new, or delete, or open orders => position.
But i test, don’t see anything in console if “create new, or delete, or open orders => position.”. listenKey, apiKey is correct !
const startUserDataStream = async () => {
try {
// Create a listenKey
listenKey = await createListenKey();
//console.log('listenKey:', listenKey);
// Set interval to send a ping about every 50 minutes
setInterval(keepListenKeyAlive, 50 * 60 * 1000);
// Establish a WebSocket connection
const ws = new WebSocket(`${STREAM_URL}/${listenKey}`);
// Handle incoming WebSocket messages
ws.on('message', message => {
console.log('Received WebSocket message:', message);
const data = JSON.parse(message);
if (data && data.e === 'ACCOUNT_UPDATE') {
const balance = data.a.find(asset => asset.a === 'USDT');
const positions = data.p.filter(position => position.s === 'USDTM');
if (balance) {
console.log('Account Balance:', balance.w);
}
if (positions.length > 0) {
positions.forEach(position => {
console.log('Position:');
console.log('Symbol:', position.s);
console.log('Entry Price:', position.ep);
console.log('Mark Price:', position.mr);
console.log('Liquidation Price:', position.l);
// Calculate PNL (ROE%)
const pnl = (position.up * 100) / position.op;
console.log('PNL (ROE%):', pnl);
});
}
} else if (data && data.e === 'ORDER_TRADE_UPDATE') {
console.log('Order Update:', data);
}
});
// Handle WebSocket connection open
ws.on('open', () => {
console.log('WebSocket connection opened');
});
// Handle WebSocket connection close
ws.on('close', () => {
console.log('WebSocket connection closed');
});
} catch (error) {
console.error('Error starting User Data Stream:', error.message);
}
};
// Start the User Data Stream
startUserDataStream();