Initial commit

This commit is contained in:
Stefan Aebischer
2019-09-14 16:02:11 +02:00
commit ed28d14171
13 changed files with 1184 additions and 0 deletions

85
doc/rest-client.md Normal file
View File

@@ -0,0 +1,85 @@
# Rest API
## Class: RestClient
### new RestClient([key][, secret])
- `key` {String} Bybit API Key
- `secret` {String} Bybit private key
If you only use the [public endpoints](#public-endpoints) you can ommit key and secret.
### Private enpoints
#### async placeActiveOrder(params)
[See bybit documentation](https://github.com/bybit-exchange/bybit-official-api-docs/blob/master/en/rest_api.md#place-active-order)
#### async getActiveOrder(params)
[See bybit documentation](https://github.com/bybit-exchange/bybit-official-api-docs/blob/master/en/rest_api.md#get-active-order)
#### async cancelActiveOrder(params)
[See bybit documentation](https://github.com/bybit-exchange/bybit-official-api-docs/blob/master/en/rest_api.md#cancel-active-order)
#### async placeConditionalOrder(params)
[See bybit documentation](https://github.com/bybit-exchange/bybit-official-api-docs/blob/master/en/rest_api.md#place-conditional-order)
#### async getConditioanlOrder(params)
[See bybit documentation](https://github.com/bybit-exchange/bybit-official-api-docs/blob/master/en/rest_api.md#get-conditional-order)
#### async cancelConditionalOrder(params)
[See bybit documentation](https://github.com/bybit-exchange/bybit-official-api-docs/blob/master/en/rest_api.md#cancel-conditional-order-)
#### async getUserLeverage()
[See bybit documentation](https://github.com/bybit-exchange/bybit-official-api-docs/blob/master/en/rest_api.md#user-leverage)
#### async changeUserLeverage(params)
[See bybit documentation](https://github.com/bybit-exchange/bybit-official-api-docs/blob/master/en/rest_api.md#-change-user-leverage)
#### async getPosition()
[See bybit documentation](https://github.com/bybit-exchange/bybit-official-api-docs/blob/master/en/rest_api.md#-my-position)
#### async changePositionMargin(params)
[See bybit documentation](https://github.com/bybit-exchange/bybit-official-api-docs/blob/master/en/rest_api.md#-change-position-margin)
#### async getLastFundingRate(params)
[See bybit documentation](https://github.com/bybit-exchange/bybit-official-api-docs/blob/master/en/rest_api.md#-get-the-last-funding-rate)
#### async getMyLastFundingFee(params)
[See bybit documentation](https://github.com/bybit-exchange/bybit-official-api-docs/blob/master/en/rest_api.md#-get-my-last-funding-fee)
#### async getPredictedFunding(params)
[See bybit documentation](https://github.com/bybit-exchange/bybit-official-api-docs/blob/master/en/rest_api.md#get-predicted-funding-rate-and-funding-fee)
#### async getOrderTradeRecords(params)
[See bybit documentation](https://github.com/bybit-exchange/bybit-official-api-docs/blob/master/en/rest_api.md#get-the-trade-records-of-a-order)
### Public enpoints
#### async getOrderBook(params)
[See bybit documentation](https://github.com/bybit-exchange/bybit-official-api-docs/blob/master/en/rest_api.md#get-orderbook)
#### async getLatestInformation()
[See bybit documentation](https://github.com/bybit-exchange/bybit-official-api-docs/blob/master/en/rest_api.md#latest-information-for-symbol)
## Example
```js
const {RestClient} = require('@pxtrn/bybit-api');
const API_KEY = 'xxx';
const PRIVATE_KEY = 'yyy';
const client = new RestClient(API_KEY, PRIVATE_KEY);
client.changeUserLeverage({leverage: 4, symbol: 'ETHUSD'})
.then(result => {
console.log(result);
})
.catch(err => {
console.error(error);
});
```

183
doc/websocket-client.md Normal file
View File

@@ -0,0 +1,183 @@
# Websocket API
## Class: WebsocketClient
The `WebsocketClient` inherits from `EventEmitter`. After establishing a
connection, the client sends heartbeats in regular intervalls, and reconnects
to the server once connection has been lost.
### new WebsocketClient([options][, logger])
- `options` {Object} Configuration options
- `key` {String} Bybit API Key. Only needed if private topics are subscribed
- `secret` {String} Bybit private Key. Only needed if private topics are
subscribed
- `livenet` {Bool} Weather to connect to livenet (`true`). Default `false`.
- `pingInterval` {Integer} Interval in ms for heartbeat ping. Default: `10000`,
- `pongTimeout` {Integer} Timeout in ms waiting for heartbeat pong response
from server. Default: `1000`,
- `reconnectTimeout` {Integer} Timeout in ms the client waits before trying
to reconnect after a lost connection. Default: 500
- `logger` {Object} Optional custom logger
Custom logger must contain the following methods:
```js
const logger = {
debug: function(message, data) {}
notice: function(message, data) {}
info: function(message, data) {}
warning: function(message, data) {}
error: function(message, data) {},
}
```
### ws.subscribe(topics)
- `topics` {String|Array} Single topic as string or multiple topics as array of strings.
Subscribe to one or multiple topics. See [available topics](#available-topics)
### ws.close()
Close the connection to the server.
### Event: 'open'
Emmited when the connection has been opened.
### Event: 'update'
- `message` {Object}
- `topic` {String} the topic for which the update occured
- `data` {Array|Object} updated data (see docs for each [topic](#available-topics)).
- `type` {String} Some topics might have different update types (see docs for each [topic](#available-topics)).
Emmited whenever an update to a subscribed topic occurs.
### Event: 'response'
- `response` {Object}
- `success` {Bool}
- `ret_msg` {String} empty if operation was successfull, otherwise error message.
- `conn_id` {String} connection id
- `request` {Object} Original request, to which the response belongs
- `op` {String} operation
- `args` {Array} Request Arguments
Emited when the server responds to an operation sent by the client (usually after subscribing to a topic).
### Event: 'close'
Emitted when the connection has been closed.
### Event: 'error'
- `error` {Error}
Emitted when an error occurs.
## Available Topics
Generaly all topics as described in the
[official bybit api documentation](https://github.com/bybit-exchange/bybit-official-api-docs/blob/master/en/websocket.md)
are available.
### Private topics
#### Positions of your account
All positions of your account.
Topic: `position`
[See bybit documentation](https://github.com/bybit-exchange/bybit-official-api-docs/blob/master/en/websocket.md#positions-of-your-account)
#### Execution message
Execution message, whenever an order has been (partially) filled.
Topic: `execution`
[See bybit documentation](https://github.com/bybit-exchange/bybit-official-api-docs/blob/master/en/websocket.md#execution-message)
#### Update for your orders
Updates for your active orders
Topic: `order`
[See bybit documentation](https://github.com/bybit-exchange/bybit-official-api-docs/blob/master/en/websocket.md#update-for-your-orders)
### Public topics
#### Candlestick chart
Candlestick OHLC "candles" for selected symbol and interval.
Example topic: `kline.BTCUSD.1m`
[See bybit documentation](https://github.com/bybit-exchange/bybit-official-api-docs/blob/master/en/websocket.md#kline)
#### Real-time trading information
All trades as they occur.
Topic: `trade`
[See bybit documentation](https://github.com/bybit-exchange/bybit-official-api-docs/blob/master/en/websocket.md#trade)
#### Daily insurance fund update
Topic: `insurance`
[See bybit documentation](https://github.com/bybit-exchange/bybit-official-api-docs/blob/master/en/websocket.md#daily-insurance-fund-update)
#### OrderBook of 25 depth per side
OrderBook for selected symbol
Example topic: `orderBookL2_25.BTCUSD`
[See bybit documentation](https://github.com/bybit-exchange/bybit-official-api-docs/blob/master/en/websocket.md#orderBook25_v2)
#### Latest information for symbol
LAtest information for selected symbol
Example topic: `instrument_info.100ms.BTCUSD`
[See bybit documentation](https://github.com/bybit-exchange/bybit-official-api-docs/blob/master/en/websocket.md#instrument_info)
## Example
```js
const {WebsocketClient} = require('@pxtrn/bybit-api');
const API_KEY = 'xxx';
const PRIVATE_KEY = 'yyy';
const ws = new WebsocketClient({key: API_KEY, secret: PRIVATE_KEY});
ws.on('open', function() {
ws.subscribe(['position', 'execution', 'trade']);
ws.subscribe('kline.BTCUSD.1m');
});
ws.on('update', function(message) {
console.log('update', message);
});
ws.on('response', function(response) {
console.log('response', response);
});
ws.on('close', function() {
console.log('connection closed');
});
ws.on('error', function(err) {
console.error('ERR', err);
});
```