feat(): extend demo trading sample to include more ws logic

This commit is contained in:
tiagosiebler
2024-10-08 09:11:33 +01:00
parent 10046f8714
commit 0c58670862

View File

@@ -1,4 +1,4 @@
import { RestClientV5, WebsocketClient } from '../src/index'; import { DefaultLogger, RestClientV5, WebsocketClient } from '../src/index';
// or // or
// import { RestClientV5 } from 'bybit-api'; // import { RestClientV5 } from 'bybit-api';
@@ -26,65 +26,97 @@ const restClient = new RestClientV5({
demoTrading: true, demoTrading: true,
}); });
const wsClient = new WebsocketClient({ // Optional, uncomment the "silly" override to log a lot more info about what the WS client is doing
market: 'v5', const customLogger = {
/** ...DefaultLogger,
* Set this to true to enable demo trading for the private account data WS // silly: (...params) => console.log('trace', ...params),
* Topics: order,execution,position,wallet,greeks };
*/
demoTrading: true, const wsClient = new WebsocketClient(
}); {
key: key,
secret: secret,
market: 'v5',
/**
* Set this to true to enable demo trading for the private account data WS
* Topics: order,execution,position,wallet,greeks
*/
demoTrading: true,
},
customLogger,
);
function setWsClientEventListeners( function setWsClientEventListeners(
websocketClient: WebsocketClient, websocketClient: WebsocketClient,
accountRef: string, accountRef: string,
): void { ): Promise<void> {
websocketClient.on('update', (data) => { return new Promise((resolve, reject) => {
console.log(new Date(), accountRef, 'data ', JSON.stringify(data)); websocketClient.on('update', (data) => {
// console.log('raw message received ', JSON.stringify(data, null, 2)); console.log(new Date(), accountRef, 'data ', JSON.stringify(data));
}); // console.log('raw message received ', JSON.stringify(data, null, 2));
});
websocketClient.on('open', (data) => { websocketClient.on('open', (data) => {
console.log(new Date(), accountRef, 'connection opened open:', data.wsKey); console.log(
}); new Date(),
websocketClient.on('response', (data) => { accountRef,
console.log( 'connection opened open:',
new Date(), data.wsKey,
accountRef, );
'log response: ', });
JSON.stringify(data, null, 2), websocketClient.on('response', (data) => {
); console.log(
}); new Date(),
websocketClient.on('reconnect', ({ wsKey }) => { accountRef,
console.log( 'log response: ',
new Date(), JSON.stringify(data, null, 2),
accountRef, );
'ws automatically reconnecting.... ',
wsKey, if (typeof data.req_id === 'string') {
); const topics = data.req_id.split(',');
}); if (topics.length) {
websocketClient.on('reconnected', (data) => { console.log(new Date(), accountRef, 'Subscribed to topics: ', topics);
console.log(new Date(), accountRef, 'ws has reconnected ', data?.wsKey); return resolve();
}); }
websocketClient.on('error', (data) => { }
console.error(new Date(), accountRef, 'ws exception: ', data); });
websocketClient.on('reconnect', ({ wsKey }) => {
console.log(
new Date(),
accountRef,
'ws automatically reconnecting.... ',
wsKey,
);
});
websocketClient.on('reconnected', (data) => {
console.log(new Date(), accountRef, 'ws has reconnected ', data?.wsKey);
});
websocketClient.on('error', (data) => {
console.error(new Date(), accountRef, 'ws exception: ', data);
});
}); });
} }
(async () => { (async () => {
try { try {
setWsClientEventListeners(wsClient, 'demoAcc'); const onSubscribed = setWsClientEventListeners(wsClient, 'demoAcc');
wsClient.subscribeV5(['position', 'execution', 'wallet'], 'linear');
// Simple promise to ensure we're subscribed before trying anything else
await onSubscribed;
// Start trading
const balResponse1 = await restClient.getWalletBalance({ const balResponse1 = await restClient.getWalletBalance({
accountType: 'CONTRACT', accountType: 'UNIFIED',
}); });
console.log('balResponse1: ', JSON.stringify(balResponse1, null, 2)); console.log('balResponse1: ', JSON.stringify(balResponse1, null, 2));
const demoFunds = await restClient.requestDemoTradingFunds(); const demoFunds = await restClient.requestDemoTradingFunds();
console.log(`requested demo funds: `, demoFunds); console.log('requested demo funds: ', demoFunds);
const balResponse2 = await restClient.getWalletBalance({ const balResponse2 = await restClient.getWalletBalance({
accountType: 'CONTRACT', accountType: 'UNIFIED',
}); });
console.log('balResponse2: ', JSON.stringify(balResponse2, null, 2)); console.log('balResponse2: ', JSON.stringify(balResponse2, null, 2));
@@ -116,7 +148,7 @@ function setWsClientEventListeners(
console.log('sellOrderResult:', sellOrderResult); console.log('sellOrderResult:', sellOrderResult);
const balResponse3 = await restClient.getWalletBalance({ const balResponse3 = await restClient.getWalletBalance({
accountType: 'CONTRACT', accountType: 'UNIFIED',
}); });
console.log('balResponse2: ', JSON.stringify(balResponse3, null, 2)); console.log('balResponse2: ', JSON.stringify(balResponse3, null, 2));
} catch (e) { } catch (e) {