Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WebSocket API trading support for Bybit #2129

Merged
merged 6 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/live/bybit/bybit_ema_cross.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
api_key=None, # 'BYBIT_API_KEY' env var
api_secret=None, # 'BYBIT_API_SECRET' env var
base_url_http=None, # Override with custom endpoint
base_url_ws=None, # Override with custom endpoint
base_url_ws_private=None, # Override with custom endpoint
instrument_provider=InstrumentProviderConfig(load_all=True),
product_types=[product_type],
testnet=False, # If client uses the testnet
Expand Down
2 changes: 1 addition & 1 deletion examples/live/bybit/bybit_ema_cross_bracket_algo.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
api_key=None, # 'BYBIT_API_KEY' env var
api_secret=None, # 'BYBIT_API_SECRET' env var
base_url_http=None, # Override with custom endpoint
base_url_ws=None, # Override with custom endpoint
base_url_ws_private=None, # Override with custom endpoint
instrument_provider=InstrumentProviderConfig(load_all=True),
product_types=[product_type],
testnet=False, # If client uses the testnet
Expand Down
2 changes: 1 addition & 1 deletion examples/live/bybit/bybit_ema_cross_stop_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
api_key=None, # 'BYBIT_API_KEY' env var
api_secret=None, # 'BYBIT_API_SECRET' env var
base_url_http=None, # Override with custom endpoint
base_url_ws=None, # Override with custom endpoint
base_url_ws_private=None, # Override with custom endpoint
instrument_provider=InstrumentProviderConfig(load_all=True),
product_types=[product_type],
testnet=False, # If client uses the testnet
Expand Down
2 changes: 1 addition & 1 deletion examples/live/bybit/bybit_ema_cross_with_trailing_stop.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
api_key=None, # 'BYBIT_API_KEY' env var
api_secret=None, # 'BYBIT_API_SECRET' env var
base_url_http=None, # Override with custom endpoint
base_url_ws=None, # Override with custom endpoint
base_url_ws_private=None, # Override with custom endpoint
instrument_provider=InstrumentProviderConfig(load_all=True),
product_types=[product_type],
testnet=False, # If client uses the testnet
Expand Down
2 changes: 1 addition & 1 deletion examples/live/bybit/bybit_market_maker.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
api_key=None, # 'BYBIT_API_KEY' env var
api_secret=None, # 'BYBIT_API_SECRET' env var
base_url_http=None, # Override with custom endpoint
base_url_ws=None, # Override with custom endpoint
base_url_ws_private=None, # Override with custom endpoint
instrument_provider=InstrumentProviderConfig(load_all=True),
product_types=[product_type],
demo=False, # If client uses the demo API
Expand Down
6 changes: 6 additions & 0 deletions nautilus_trader/adapters/bybit/common/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
# Set of Bybit error codes for which Nautilus will attempt retries,
# potentially temporary conditions where a retry might make sense.
BYBIT_RETRY_ERRORS_UTA: Final[set[int]] = {
# > ------------------------------------------------------------
# > Self defined error codes
-10_408, # Client request timed out
# > ------------------------------------------------------------
# > Bybit defined error codes
# > https://bybit-exchange.github.io/docs/v5/error
10_000, # Server Timeout
10_002, # The request time exceeds the time window range
10_006, # Too many visits. Exceeded the API Rate Limit
Expand Down
8 changes: 8 additions & 0 deletions nautilus_trader/adapters/bybit/common/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ def parse_to_position_side(self) -> PositionSide:
raise RuntimeError(f"invalid position side, was {self}")


@unique
class BybitWsOrderRequestMsgOP(Enum):
CREATE = "order.create"
AMEND = "order.amend"
CANCEL = "order.cancel"


@unique
class BybitKlineInterval(Enum):
MINUTE_1 = "1"
Expand Down Expand Up @@ -149,6 +156,7 @@ class BybitOrderStatus(Enum):

@unique
class BybitOrderSide(Enum):
UNKNOWN = "" # It will be an empty string in some cases
BUY = "Buy"
SELL = "Sell"

Expand Down
7 changes: 7 additions & 0 deletions nautilus_trader/adapters/bybit/common/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,10 @@ def get_ws_base_url_private(is_testnet: bool) -> str:
return "wss://stream-testnet.bybit.com/v5/private"
else:
return "wss://stream.bybit.com/v5/private"


def get_ws_base_url_trade(is_testnet: bool) -> str:
if is_testnet:
return "wss://stream-testnet.bybit.com/v5/trade"
else:
return "wss://stream.bybit.com/v5/trade"
17 changes: 16 additions & 1 deletion nautilus_trader/adapters/bybit/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,31 @@ class BybitExecClientConfig(LiveExecClientConfig, frozen=True):
If None then will default to 'SPOT', you also cannot mix 'SPOT' with
any other product type for execution, and it will use a `CASH` account
type, vs `MARGIN` for the other derivative products.
base_url_ws_private : str, optional
The base URL for the `private` WebSocket client.
base_url_ws_trade : str, optional
The base URL for the `trade` WebSocket client.
demo : bool, default False
If the client is connecting to the Bybit demo API.
testnet : bool, default False
If the client is connecting to the Bybit testnet API.
use_gtd : bool, default False
If False, then GTD time in force will be remapped to GTC
(this is useful if managing GTD orders locally).
use_ws_trade_api : bool, default False
If the client is using websocket to send order requests.
use_http_batch_api : bool, default False
If the client is using http api to send batch order requests.
Effective only when `use_ws_trade_api` is set to `True`.
max_retries : PositiveInt, optional
The maximum number of times a submit, cancel or modify order request will be retried.
retry_delay : PositiveFloat, optional
The delay (seconds) between retries. Short delays with frequent retries may result in account bans.
max_ws_reconnection_tries: int, default 3
The number of retries to reconnect the websocket connection if the
connection is broken.
ws_trade_timeout_secs : float, default 5.0
The timeout for trade websocket messages.

Warnings
--------
Expand All @@ -103,10 +114,14 @@ class BybitExecClientConfig(LiveExecClientConfig, frozen=True):
api_secret: str | None = None
product_types: list[BybitProductType] | None = None
base_url_http: str | None = None
base_url_ws: str | None = None
base_url_ws_private: str | None = None
base_url_ws_trade: str | None = None
demo: bool = False
testnet: bool = False
use_gtd: bool = False # Not supported on Bybit
use_ws_trade_api: bool = False
use_http_batch_api: bool = False
max_retries: PositiveInt | None = None
retry_delay: PositiveFloat | None = None
max_ws_reconnection_tries: int | None = 3
ws_trade_timeout_secs: float | None = 5.0
Loading
Loading