Skip to content

Commit

Permalink
feat(#186): add proxy & rate limits example. Misc doc updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagosiebler committed Dec 22, 2023
1 parent 923b1b0 commit 48eaa47
Show file tree
Hide file tree
Showing 17 changed files with 216 additions and 68 deletions.
115 changes: 63 additions & 52 deletions README.md

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions examples/deprecated/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Deprecated Examples

The examples in this folder use old/deprecated/obsolete APIs. They should all have a modern alternative.

As of December 2023, use the V5 APIs & WebSockets.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ContractClient } from '../src/index';
import { ContractClient } from '../../src/index';

// or
// import { ContractClient } from 'bybit-api';
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

// For testing with the repo directly, import from the src folder
import { ContractClient } from '../src';
import { ContractClient } from '../../src';

// or, use the version installed with npm
// import { ContractClient } from 'bybit-api';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CopyTradingClient } from '../src/index';
import { CopyTradingClient } from '../../src/index';

// or
// import { CopyTradingClient } from 'bybit-api';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LinearClient } from '../src/index';
import { LinearClient } from '../../src/index';

// or
// import { LinearClient } from 'bybit-api';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ContractClient } from '../src/index';
import { ContractClient } from '../../src/index';

// or
// import { ContractClient } from 'bybit-api';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SpotClientV3 } from '../src/index';
import { SpotClientV3 } from '../../src/index';

// or
// import { SpotClientV3 } from 'bybit-api';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SpotClientV3 } from '../src/index';
import { SpotClientV3 } from '../../src/index';

// or
// import { SpotClientV3 } from 'bybit-api';
Expand All @@ -25,15 +25,15 @@ const client = new SpotClientV3({
symbol,
orderId,
ordersPerPage,
0
0,
);
console.log('normal orders:', normalOrders);

const tpSlOrders = await client.getOpenOrders(
symbol,
orderId,
ordersPerPage,
1
1,
);
console.log('tpSlOrders:', tpSlOrders);
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UnifiedMarginClient } from '../src/index';
import { UnifiedMarginClient } from '../../src/index';

// or
// import { UnifiedMarginClient } from 'bybit-api';
Expand Down Expand Up @@ -36,7 +36,7 @@ const client = new UnifiedMarginClient({
});
console.log(
'both to compare:',
JSON.stringify(historicOrdersBoth, null, 2)
JSON.stringify(historicOrdersBoth, null, 2),
);
} catch (e) {
console.error('request failed: ', e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UnifiedMarginClient } from '../src/index';
import { UnifiedMarginClient } from '../../src/index';

// or
// import { UnifiedMarginClient } from 'bybit-api';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* 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';
import { DefaultLogger, WS_KEY_MAP, WebsocketClient } from '../../src';

// or
// import { DefaultLogger, WS_KEY_MAP, WebsocketClient } from 'bybit-api';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* 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';
import { DefaultLogger, WS_KEY_MAP, WebsocketClient } from '../../src';

// or
// import { DefaultLogger, WS_KEY_MAP, WebsocketClient } from 'bybit-api';
Expand Down
4 changes: 2 additions & 2 deletions examples/ws-public.ts → examples/deprecated/ws-public.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DefaultLogger, WS_KEY_MAP, WebsocketClient } from '../src';
import { DefaultLogger, WS_KEY_MAP, WebsocketClient } from '../../src';

// or
// import { DefaultLogger, WS_KEY_MAP, WebsocketClient } from 'bybit-api';
Expand All @@ -22,7 +22,7 @@ const wsClient = new WebsocketClient(
// market: 'unifiedOption',
market: 'contractUSDT',
},
logger
logger,
);

wsClient.on('update', (data) => {
Expand Down
66 changes: 66 additions & 0 deletions examples/rest-v5-proxies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { RestClientV5 } from '../src/index';

// or
// import { RestClientV5 } from 'bybit-api';

const key = process.env.API_KEY_COM;
const secret = process.env.API_SECRET_COM;

const client = new RestClientV5(
{
key: key,
secret: secret,
parseAPIRateLimits: true,
testnet: true,
// Sometimes using a proxy introduces recv timestamp errors (due to the extra latency)
// If that happens, you can try increasing the recv window (which is 5000ms by default)
// recv_window: 10000,
},
{
proxy: {
host: 'proxyhost',
port: Number('proxyport'),
auth: {
username: 'proxyuserifneeded',
password: 'proxypassifneeded',
},
},
},
);

(async () => {
try {
const res = await client.getWalletBalance({ accountType: 'UNIFIED' });

console.log('response: ', JSON.stringify(res, null, 2));

// const orders = await client.batchSubmitOrders('linear', [
// {
// symbol: 'ETHUSDT',
// orderType: 'Limit',
// side: 'Buy',
// qty: '1',
// orderIv: '6',
// timeInForce: 'GTC',
// orderLinkId: 'option-test-001',
// mmp: false,
// reduceOnly: false,
// },
// {
// symbol: 'ETHUSDT',
// orderType: 'Limit',
// side: 'Sell',
// qty: '2',
// price: '700',
// timeInForce: 'GTC',
// orderLinkId: 'option-test-001',
// mmp: false,
// reduceOnly: false,
// },
// ]);

// console.log('orders: ', JSON.stringify(orders, null, 2));
} catch (e) {
console.error('request failed: ', e);
}
})();
66 changes: 66 additions & 0 deletions examples/rest-v5-public.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import {
APIResponseV3WithTime,
CategoryListV5,
RestClientV5,
TickerLinearInverseV5,
} from '../src/index';

// or
// import { RestClientV5 } from 'bybit-api';

/**
* If you don't plan on making any private api calls,
* you can instance the REST client without any parameters
*/
const client = new RestClientV5();

(async () => {
try {
// const klineResult = await client.getKline({
// category: 'linear',
// interval: '15',
// symbol: 'BTCUSDT',
// });
// console.log('klineResult: ', klineResult);

// const markPriceKlineResult = await client.getMarkPriceKline({
// category: 'linear',
// interval: '15',
// symbol: 'BTCUSDT',
// });
// console.log('markPriceKlineResult: ', markPriceKlineResult);

// const indexPriceKline = await client.getIndexPriceKline({
// category: 'linear',
// interval: '15',
// symbol: 'BTCUSDT',
// });
// console.log('indexPriceKline: ', indexPriceKline);

// const openInterest = await client.getOpenInterest({
// category: 'linear',
// symbol: 'BTCUSDT',
// intervalTime: '5min',
// });

const tickers = await client.getTickers({ category: 'linear' });
// console.log(
// JSON.stringify(
// tickers.result.list.map((ticker) => ticker.symbol),
// null,
// 2,
// ),
// );

console.log('response', tickers);
// openInterest.result.list.forEach((row) => {
// console.log('int: ', {
// timestamp: row.timestamp,
// value: row.openInterest,
// });
// });
// console.log('openInterest: ', openInterest.result.list);
} catch (e) {
console.error('request failed: ', e);
}
})();

0 comments on commit 48eaa47

Please sign in to comment.