From feb49353bde23ed79f9579eab939406fce92386f Mon Sep 17 00:00:00 2001 From: talrasha007 Date: Fri, 10 Nov 2023 15:16:19 +0800 Subject: [PATCH] update examples. --- examples/v5/ws.js | 2 +- examples/v5/ws_order.js | 47 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 examples/v5/ws_order.js diff --git a/examples/v5/ws.js b/examples/v5/ws.js index e69b4d0..5f5963e 100644 --- a/examples/v5/ws.js +++ b/examples/v5/ws.js @@ -10,7 +10,7 @@ const { wsApi } = require('./api'); console.log(await wsApi.subscribePublic([{ "channel" : "tickers", - "instId": "ETH-USD-210625" + "instId": "ETH-USD-240329" }, { "channel" : "tickers", "instId": "ETH-USD-SWAP" diff --git a/examples/v5/ws_order.js b/examples/v5/ws_order.js new file mode 100644 index 0000000..5abd71b --- /dev/null +++ b/examples/v5/ws_order.js @@ -0,0 +1,47 @@ +const { wsApi } = require('./api'); + +async function main() { + if (!wsApi.loggedIn) { + await new Promise(resolve => wsApi.once('login', resolve)); + } + + wsApi.subscribePrivate([ + { + channel: 'account', + ccy: 'BTC' + }, { + channel: 'account', + ccy: 'ETH' + }, { + channel: 'account', + ccy: 'USDT' + }, { + channel: 'positions', + instType: 'ANY' + }, { + channel: 'orders', + instType: 'ANY' + } +], true); + + const orders = await wsApi.batchOrder([ + wsApi.toOrder('ETH-USDT-SWAP', 'buy', 'long', 'market', 1), + wsApi.toOrder('ETH-USDT-240329', 'sell', 'short', 'market', 1) + ]).catch(console.error); + + const [longOrder, shortOrder] = await wsApi.waitForOrders(orders); + if (longOrder.sCode != 0 || shortOrder.sCode != 0) { + console.log(longOrder) + console.log(shortOrder) + console.error(longOrder.sMsg || shortOrder.sMsg); + this.disabled = true; + diff = -1; + } else { + console.log('longOrder:', longOrder); + console.log('shortOrder:', shortOrder); + diff = shortOrder.avgPx / longOrder.avgPx - 1; + console.log('diff:', diff) + } +} + +main().catch(e => console.error(e.stack)); \ No newline at end of file