Merge pull request #265 from tiagosiebler/copytradingexample

feat(): add copy trading ws topic example
This commit is contained in:
Tiago
2023-06-01 14:45:16 +01:00
committed by GitHub
5 changed files with 64 additions and 4 deletions

View File

@@ -5,7 +5,7 @@
[![last commit](https://img.shields.io/github/last-commit/tiagosiebler/bybit-api)][1] [![last commit](https://img.shields.io/github/last-commit/tiagosiebler/bybit-api)][1]
[![CodeFactor](https://www.codefactor.io/repository/github/tiagosiebler/bybit-api/badge)](https://www.codefactor.io/repository/github/tiagosiebler/bybit-api) [![CodeFactor](https://www.codefactor.io/repository/github/tiagosiebler/bybit-api/badge)](https://www.codefactor.io/repository/github/tiagosiebler/bybit-api)
[![connector logo](https://cdn.builder.io/api/v1/image/assets%2Ffddee401a9284ab792b271538c28932d%2F5190c99403ac474699a1959fdee1ab6c?format=webp&width=2000)][1] [![connector logo](https://github.com/tiagosiebler/bybit-api/blob/master/docs/images/logo1.png?raw=true)][1]
[1]: https://www.npmjs.com/package/bybit-api [1]: https://www.npmjs.com/package/bybit-api

BIN
docs/images/logo1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View File

@@ -0,0 +1,61 @@
/* eslint-disable no-unused-vars */
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable @typescript-eslint/no-empty-function */
import { DefaultLogger, WS_KEY_MAP, WebsocketClient } from '../src';
// or
// import { DefaultLogger, WS_KEY_MAP, WebsocketClient } from 'bybit-api';
const logger = {
...DefaultLogger,
silly: (...params) => {
// console.log(params);
},
};
const key = process.env.API_KEY;
const secret = process.env.API_SECRET;
/**
* Copy trading api docs say that private topics should connect to: wss://stream.bybit.com/realtime_private
*
* Within this SDK, only the market `linear` uses this endpoint for private topics:
*/
const market = 'linear';
const wsClient = new WebsocketClient(
{
key: key,
secret: secret,
market: market,
},
logger,
);
wsClient.on('update', (data) => {
console.log('raw message received ', JSON.stringify(data, null, 2));
});
wsClient.on('open', (data) => {
console.log('connection opened open:', data.wsKey);
});
wsClient.on('response', (data) => {
console.log('ws response: ', JSON.stringify(data, null, 2));
});
wsClient.on('reconnect', ({ wsKey }) => {
console.log('ws automatically reconnecting.... ', wsKey);
});
wsClient.on('reconnected', (data) => {
console.log('ws has reconnected ', data?.wsKey);
});
wsClient.on('error', (data) => {
console.error('ws exception: ', data);
});
// copy trading topics from api docs: https://bybit-exchange.github.io/docs/copy-trade/ws-private/position
wsClient.subscribe([
'copyTradePosition',
'copyTradeOrder',
'copyTradeExecution',
'copyTradeWallet',
]);

View File

@@ -23,7 +23,6 @@ const secret = process.env.API_SECRET;
const market = 'contractUSDT'; const market = 'contractUSDT';
// const market = 'contractInverse'; // const market = 'contractInverse';
// Note: the WebsocketClient defaults to testnet. Set `livenet: true` to use live markets.
const wsClient = new WebsocketClient( const wsClient = new WebsocketClient(
{ {
key: key, key: key,
@@ -34,7 +33,7 @@ const wsClient = new WebsocketClient(
// enable_time_sync: true, // enable_time_sync: true,
}, },
}, },
logger logger,
); );
wsClient.on('update', (data) => { wsClient.on('update', (data) => {

View File

@@ -1,6 +1,6 @@
{ {
"name": "bybit-api", "name": "bybit-api",
"version": "3.6.0", "version": "3.6.1",
"description": "Complete & robust Node.js SDK for Bybit's REST APIs and WebSockets, with TypeScript & strong end to end tests.", "description": "Complete & robust Node.js SDK for Bybit's REST APIs and WebSockets, with TypeScript & strong end to end tests.",
"main": "lib/index.js", "main": "lib/index.js",
"types": "lib/index.d.ts", "types": "lib/index.d.ts",