From 2745e092099daa65ecc362a97d0114fa3ca0b022 Mon Sep 17 00:00:00 2001 From: Chris Sellers Date: Sun, 7 Apr 2024 19:40:22 +1000 Subject: [PATCH] Add Bybit initial integration guide --- README.md | 2 +- docs/integrations/binance.md | 1 + docs/integrations/bybit.md | 173 +++++++++++++++++++++++++++++++++++ docs/integrations/index.md | 3 +- 4 files changed, 177 insertions(+), 2 deletions(-) create mode 100644 docs/integrations/bybit.md diff --git a/README.md b/README.md index 404fdedab080..776b68ad14c5 100644 --- a/README.md +++ b/README.md @@ -145,7 +145,7 @@ into a unified interface. The following integrations are currently supported: | [Binance](https://binance.com) | `BINANCE` | Crypto Exchange (CEX) | ![status](https://img.shields.io/badge/stable-green) | [Guide](https://docs.nautilustrader.io/integrations/binance.html) | | [Binance US](https://binance.us) | `BINANCE` | Crypto Exchange (CEX) | ![status](https://img.shields.io/badge/stable-green) | [Guide](https://docs.nautilustrader.io/integrations/binance.html) | | [Binance Futures](https://www.binance.com/en/futures) | `BINANCE` | Crypto Exchange (CEX) | ![status](https://img.shields.io/badge/stable-green) | [Guide](https://docs.nautilustrader.io/integrations/binance.html) | -| [Bybit](https://www.bybit.com) | `BYBIT` | Crypto Exchange (CEX) | ![status](https://img.shields.io/badge/building-orange) | | +| [Bybit](https://www.bybit.com) | `BYBIT` | Crypto Exchange (CEX) | ![status](https://img.shields.io/badge/beta-yellow) | [Guide](https://docs.nautilustrader.io/integrations/bybit.html) | | [Databento](https://databento.com) | `DATABENTO` | Data Provider | ![status](https://img.shields.io/badge/beta-yellow) | [Guide](https://docs.nautilustrader.io/integrations/databento.html) | | [Interactive Brokers](https://www.interactivebrokers.com) | `INTERACTIVE_BROKERS` | Brokerage (multi-venue) | ![status](https://img.shields.io/badge/stable-green) | [Guide](https://docs.nautilustrader.io/integrations/ib.html) | diff --git a/docs/integrations/binance.md b/docs/integrations/binance.md index e66c1115b161..d4696c53b4a2 100644 --- a/docs/integrations/binance.md +++ b/docs/integrations/binance.md @@ -43,6 +43,7 @@ pair, and the `BTCUSDT` perpetual futures contract (this symbol is used for _bot E.g. for Binance Futures, the said instruments symbol is `BTCUSDT-PERP` within the Nautilus system boundary. ## Order types + | | Spot | Margin | Futures | |------------------------|---------------------------------|---------------------------------|-------------------| | `MARKET` | ✓ | ✓ | ✓ | diff --git a/docs/integrations/bybit.md b/docs/integrations/bybit.md new file mode 100644 index 000000000000..4e3ce0813a19 --- /dev/null +++ b/docs/integrations/bybit.md @@ -0,0 +1,173 @@ +# Bybit + +```{warning} +We are currently working on this integration guide. +``` + +Founded in 2018, Bybit is one of the largest cryptocurrency exchanges in terms +of daily trading volume, and open interest of crypto assets and crypto +derivative products. This integration supports live market data ingest and order +execution with Bybit. + +## Overview + +The following documentation assumes a trader is setting up for both live market +data feeds, and trade execution. The full Bybit integration consists of an assortment of components, +which can be used together or separately depending on the users needs. + +- `BybitHttpClient` - Low-level HTTP API connectivity +- `BybitWebSocketClient` - Low-level WebSocket API connectivity +- `BybitInstrumentProvider` - Instrument parsing and loading functionality +- `BybitDataClient` - A market data feed manager +- `BybitExecutionClient` - An account management and trade execution gateway +- `BybitLiveDataClientFactory` - Factory for Bybit data clients (used by the trading node builder) +- `BybitLiveExecClientFactory` - Factory for Bybit execution clients (used by the trading node builder) + +```{note} +Most users will simply define a configuration for a live trading node (as below), +and won't need to necessarily work with these lower level components directly. +``` + +## Bybit documentation + +Bybit provides extensive documentation for users which can be found in the [Bybit help center](https://www.bybit.com/en/help-center). +It's recommended you also refer to the Bybit documentation in conjunction with this NautilusTrader integration guide. + +## Products + +A product is an umberalla term for a group of related instrument types. + +```{note} +Product is also referred to as `category` in the Bybit v5 API. +``` + +The following product types are supported on Bybit: + +- Spot cryptocurrencies +- Perpetual contracts +- Perpetual inverse contracts +- Futures contracts +- Futures inverse contracts + +Options contracts are not currently supported (will be implemented in a future version) + +## Symbology + +To distinguish between different product types on Bybit, the following instrument ID suffix's are used: + +- `-SPOT`: spot cryptocurrencies +- `-LINEAR`: perpeutal and futures contracts +- `-INVERSE`: inverse perpetual and inverse futures contracts +- `-OPTION`: options contracts (not currently supported) + +These must be appended to the Bybit raw symbol string to be able to identify the specific +product type for the instrument ID, e.g. the Ether/Tether spot currency pair is identified with: + +`ETHUSDT-SPOT` + +The BTCUSDT perpetual futures contract is identified with: + +`BTCUSDT-LINEAR` + +The BTCUSD inverse perpetual futures contract is identified with: + +`BTCUSD-INVERSE` + +## Order types + +```{warning} +Only Market and Limit orders have been tested and are available. +The remaining order types will be added on a best effort basis going forward. +``` + +| | Spot | Derivatives (Linear, Inverse, Options) | +|------------------------|----------------------|-----------------------------------------| +| `MARKET` | ✓ | ✓ | +| `LIMIT` | ✓ | ✓ | +| `STOP_MARKET` | | | +| `STOP_LIMIT` | | | +| `TRAILING_STOP_MARKET` | | | + +## Configuration + +The product types for each client must be specified in the configurations. + +### Data clients + +For data clients, if no product types are specified then all product types will +be loaded and available. + +### Execution clients + +For execution clients, there is a limitation that +you cannot specify `SPOT` with any of the other derivative product types. + +- `CASH` account type will be used for `SPOT` products +- `MARGIN` account type will be used for all other derivative products + +The most common use case is to configure a live `TradingNode` to include Bybit +data and execution clients. To achieve this, add a `BYBIT` section to your client +configuration(s): + +```python +from nautilus_trader.adapters.bybit.common.enums import BybitProductType +from nautilus_trader.live.node import TradingNode + +config = TradingNodeConfig( + ..., # Omitted + data_clients={ + "BYBIT": { + "api_key": "YOUR_BYBIT_API_KEY", + "api_secret": "YOUR_BYBIT_API_SECRET", + "base_url_http": None, # Override with custom endpoint + "product_types": [BybitProductType.LINEAR] + "testnet": False, + }, + }, + exec_clients={ + "BYBIT": { + "api_key": "YOUR_BYBIT_API_KEY", + "api_secret": "YOUR_BYBIT_API_SECRET", + "base_url_http": None, # Override with custom endpoint + "product_types": [BybitProductType.LINEAR] + "testnet": False, + }, + }, +) +``` + +Then, create a `TradingNode` and add the client factories: + +```python +from nautilus_trader.adapters.bybit.factories import BybitLiveDataClientFactory +from nautilus_trader.adapters.bybit.factories import BybitLiveExecClientFactory +from nautilus_trader.live.node import TradingNode + +# Instantiate the live trading node with a configuration +node = TradingNode(config=config) + +# Register the client factories with the node +node.add_data_client_factory("BYBIT", BybitLiveDataClientFactory) +node.add_exec_client_factory("BYBIT", BybitLiveExecClientFactory) + +# Finally build the node +node.build() +``` + +### API credentials + +There are two options for supplying your credentials to the Bybit clients. +Either pass the corresponding `api_key` and `api_secret` values to the configuration objects, or +set the following environment variables: + +For Bybit live clients, you can set: +- `BYBIT_API_KEY` +- `BYBIT_API_SECRET` + +For Bybit testnet clients, you can set: +- `BYBIT_TESTNET_API_KEY` +- `BYBIT_TESTNET_API_SECRET` + +When starting the trading node, you'll receive immediate confirmation of whether your +credentials are valid and have trading permissions. + diff --git a/docs/integrations/index.md b/docs/integrations/index.md index 123fdc6c13b7..e92037bfe50e 100644 --- a/docs/integrations/index.md +++ b/docs/integrations/index.md @@ -9,6 +9,7 @@ betfair.md binance.md + bybit.md databento.md ib.md ``` @@ -23,7 +24,7 @@ into a unified interface. The following integrations are currently supported: | [Binance](https://binance.com) | `BINANCE` | Crypto Exchange (CEX) | ![status](https://img.shields.io/badge/stable-green) | [Guide](https://docs.nautilustrader.io/integrations/binance.html) | | [Binance US](https://binance.us) | `BINANCE` | Crypto Exchange (CEX) | ![status](https://img.shields.io/badge/stable-green) | [Guide](https://docs.nautilustrader.io/integrations/binance.html) | | [Binance Futures](https://www.binance.com/en/futures) | `BINANCE` | Crypto Exchange (CEX) | ![status](https://img.shields.io/badge/stable-green) | [Guide](https://docs.nautilustrader.io/integrations/binance.html) | -| [Bybit](https://www.bybit.com) | `BYBIT` | Crypto Exchange (CEX) | ![status](https://img.shields.io/badge/building-orange) | | +| [Bybit](https://www.bybit.com) | `BYBIT` | Crypto Exchange (CEX) | ![status](https://img.shields.io/badge/beta-yellow) | [Guide](https://docs.nautilustrader.io/integrations/bybit.html) | | [Databento](https://databento.com) | `DATABENTO` | Data Provider | ![status](https://img.shields.io/badge/beta-yellow) | [Guide](https://docs.nautilustrader.io/integrations/databento.html) | | [Interactive Brokers](https://www.interactivebrokers.com) | `INTERACTIVE_BROKERS` | Brokerage (multi-venue) | ![status](https://img.shields.io/badge/stable-green) | [Guide](https://docs.nautilustrader.io/integrations/ib.html) |