Skip to content

Commit

Permalink
Implement custom client IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed Apr 8, 2024
1 parent 9410846 commit 6f96865
Show file tree
Hide file tree
Showing 19 changed files with 99 additions and 54 deletions.
4 changes: 2 additions & 2 deletions nautilus_trader/adapters/betfair/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def create( # type: ignore
loop : asyncio.AbstractEventLoop
The event loop for the client.
name : str
The client name.
The custom client ID.
config : dict[str, Any]
The configuration dictionary.
msgbus : MessageBus
Expand Down Expand Up @@ -201,7 +201,7 @@ def create( # type: ignore
loop : asyncio.AbstractEventLoop
The event loop for the client.
name : str
The client name.
The custom client ID.
config : dict[str, Any]
The configuration for the client.
msgbus : MessageBus
Expand Down
5 changes: 4 additions & 1 deletion nautilus_trader/adapters/binance/common/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ class BinanceCommonDataClient(LiveMarketDataClient):
The account type for the client.
base_url_ws : str
The base url for the WebSocket client.
name : str, optional
The custom client ID.
config : BinanceDataClientConfig
The configuration for the client.
Expand All @@ -122,11 +124,12 @@ def __init__(
instrument_provider: InstrumentProvider,
account_type: BinanceAccountType,
base_url_ws: str,
name: str | None,
config: BinanceDataClientConfig,
) -> None:
super().__init__(
loop=loop,
client_id=ClientId(BINANCE_VENUE.value),
client_id=ClientId(name or BINANCE_VENUE.value),
venue=BINANCE_VENUE,
msgbus=msgbus,
cache=cache,
Expand Down
7 changes: 5 additions & 2 deletions nautilus_trader/adapters/binance/common/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ class BinanceCommonExecutionClient(LiveExecutionClient):
The account type for the client.
base_url_ws : str
The base URL for the WebSocket client.
name : str, optional
The custom client ID.
config : BinanceExecClientConfig
The configuration for the client.
Expand All @@ -131,11 +133,12 @@ def __init__(
instrument_provider: InstrumentProvider,
account_type: BinanceAccountType,
base_url_ws: str,
name: str | None,
config: BinanceExecClientConfig,
) -> None:
super().__init__(
loop=loop,
client_id=ClientId(BINANCE_VENUE.value),
client_id=ClientId(name or BINANCE_VENUE.value),
venue=BINANCE_VENUE,
oms_type=OmsType.HEDGING if account_type.is_futures else OmsType.NETTING,
instrument_provider=instrument_provider,
Expand All @@ -162,7 +165,7 @@ def __init__(
self._log.info(f"{config.max_retries=}", LogColor.BLUE)
self._log.info(f"{config.retry_delay=}", LogColor.BLUE)

self._set_account_id(AccountId(f"{BINANCE_VENUE.value}-spot-master"))
self._set_account_id(AccountId(f"{name or BINANCE_VENUE.value}-spot-master"))

# Enum parser
self._enum_parser = enum_parser
Expand Down
10 changes: 7 additions & 3 deletions nautilus_trader/adapters/binance/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def get_cached_binance_http_client(
("allOrders", Quota.rate_per_minute(int(1200 / 20))),
]

client_key: str = "|".join((key, secret))
client_key: str = "|".join((account_type.value, key, secret))
if client_key not in BINANCE_HTTP_CLIENTS:
client = BinanceHttpClient(
clock=clock,
Expand Down Expand Up @@ -214,7 +214,7 @@ def create( # type: ignore
loop : asyncio.AbstractEventLoop
The event loop for the client.
name : str
The client name.
The custom client ID.
config : BinanceDataClientConfig
The client configuration.
msgbus : MessageBus
Expand Down Expand Up @@ -271,6 +271,7 @@ def create( # type: ignore
instrument_provider=provider,
account_type=config.account_type,
base_url_ws=config.base_url_ws or default_base_url_ws,
name=name,
config=config,
)
else:
Expand All @@ -291,6 +292,7 @@ def create( # type: ignore
instrument_provider=provider,
account_type=config.account_type,
base_url_ws=config.base_url_ws or default_base_url_ws,
name=name,
config=config,
)

Expand All @@ -317,7 +319,7 @@ def create( # type: ignore
loop : asyncio.AbstractEventLoop
The event loop for the client.
name : str
The client name.
The custom client ID.
config : BinanceExecClientConfig
The configuration for the client.
msgbus : MessageBus
Expand Down Expand Up @@ -374,6 +376,7 @@ def create( # type: ignore
instrument_provider=provider,
base_url_ws=config.base_url_ws or default_base_url_ws,
account_type=config.account_type,
name=name,
config=config,
)
else:
Expand All @@ -394,5 +397,6 @@ def create( # type: ignore
instrument_provider=provider,
base_url_ws=config.base_url_ws or default_base_url_ws,
account_type=config.account_type,
name=name,
config=config,
)
8 changes: 6 additions & 2 deletions nautilus_trader/adapters/binance/futures/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ class BinanceFuturesDataClient(BinanceCommonDataClient):
The instrument provider.
base_url_ws : str
The base URL for the WebSocket client.
account_type : BinanceAccountType
The account type for the client.
config : BinanceDataClientConfig
The configuration for the client.
account_type : BinanceAccountType, default 'USDT_FUTURE'
The account type for the client.
name : str, optional
The custom client ID.
"""

Expand All @@ -77,6 +79,7 @@ def __init__(
base_url_ws: str,
config: BinanceDataClientConfig,
account_type: BinanceAccountType = BinanceAccountType.USDT_FUTURE,
name: str | None = None,
):
PyCondition.true(
account_type.is_futures,
Expand All @@ -101,6 +104,7 @@ def __init__(
instrument_provider=instrument_provider,
account_type=account_type,
base_url_ws=base_url_ws,
name=name,
config=config,
)

Expand Down
8 changes: 6 additions & 2 deletions nautilus_trader/adapters/binance/futures/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,12 @@ class BinanceFuturesExecutionClient(BinanceCommonExecutionClient):
The instrument provider.
base_url_ws : str
The base URL for the WebSocket client.
account_type : BinanceAccountType
The account type for the client.
config : BinanceExecClientConfig
The configuration for the client.
account_type : BinanceAccountType, default 'USDT_FUTURE'
The account type for the client.
name : str, optional
The custom client ID.
"""

Expand All @@ -90,6 +92,7 @@ def __init__(
base_url_ws: str,
config: BinanceExecClientConfig,
account_type: BinanceAccountType = BinanceAccountType.USDT_FUTURE,
name: str | None = None,
):
PyCondition.true(
account_type.is_futures,
Expand Down Expand Up @@ -118,6 +121,7 @@ def __init__(
instrument_provider=instrument_provider,
account_type=account_type,
base_url_ws=base_url_ws,
name=name,
config=config,
)

Expand Down
1 change: 1 addition & 0 deletions nautilus_trader/adapters/binance/futures/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ def _parse_instrument(
underlying=base_currency,
quote_currency=quote_currency,
settlement_currency=settlement_currency,
is_inverse=False, # No inverse instruments trade on Binance
activation_ns=activation.value,
expiration_ns=expiration.value,
price_precision=price_precision,
Expand Down
8 changes: 6 additions & 2 deletions nautilus_trader/adapters/binance/spot/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@ class BinanceSpotDataClient(BinanceCommonDataClient):
The instrument provider.
base_url_ws : str
The base URL for the WebSocket client.
account_type : BinanceAccountType
The account type for the client.
config : BinanceDataClientConfig
The configuration for the client.
account_type : BinanceAccountType, default 'SPOT'
The account type for the client.
name : str, optional
The custom client ID.
"""

Expand All @@ -74,6 +76,7 @@ def __init__(
base_url_ws: str,
config: BinanceDataClientConfig,
account_type: BinanceAccountType = BinanceAccountType.SPOT,
name: str | None = None,
):
PyCondition.true(
account_type.is_spot_or_margin,
Expand All @@ -97,6 +100,7 @@ def __init__(
instrument_provider=instrument_provider,
account_type=account_type,
base_url_ws=base_url_ws,
name=name,
config=config,
)

Expand Down
8 changes: 6 additions & 2 deletions nautilus_trader/adapters/binance/spot/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,12 @@ class BinanceSpotExecutionClient(BinanceCommonExecutionClient):
The instrument provider.
base_url_ws : str
The base URL for the WebSocket client.
account_type : BinanceAccountType
The account type for the client.
config : BinanceExecClientConfig
The configuration for the client.
account_type : BinanceAccountType, default 'SPOT'
The account type for the client.
name : str, optional
The custom client ID.
"""

Expand All @@ -83,6 +85,7 @@ def __init__(
base_url_ws: str,
config: BinanceExecClientConfig,
account_type: BinanceAccountType = BinanceAccountType.SPOT,
name: str | None = None,
):
PyCondition.true(
account_type.is_spot_or_margin,
Expand Down Expand Up @@ -111,6 +114,7 @@ def __init__(
instrument_provider=instrument_provider,
account_type=account_type,
base_url_ws=base_url_ws,
name=name,
config=config,
)

Expand Down
5 changes: 4 additions & 1 deletion nautilus_trader/adapters/bybit/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ class BybitDataClient(LiveMarketDataClient):
The product base urls for the WebSocket clients.
config : BybitDataClientConfig
The configuration for the client.
name : str, optional
The custom client ID.
"""

Expand All @@ -108,11 +110,12 @@ def __init__(
product_types: list[BybitProductType],
ws_base_urls: dict[BybitProductType, str],
config: BybitDataClientConfig,
name: str | None,
) -> None:
self._enum_parser = BybitEnumParser()
super().__init__(
loop=loop,
client_id=ClientId(BYBIT_VENUE.value),
client_id=ClientId(name or BYBIT_VENUE.value),
venue=BYBIT_VENUE,
msgbus=msgbus,
cache=cache,
Expand Down
7 changes: 5 additions & 2 deletions nautilus_trader/adapters/bybit/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ class BybitExecutionClient(LiveExecutionClient):
The base URL for the WebSocket client.
config : BybitExecClientConfig
The configuration for the client.
name : str, optional
The custom client ID.
"""

Expand All @@ -115,6 +117,7 @@ def __init__(
product_types: list[BybitProductType],
base_url_ws: str,
config: BybitExecClientConfig,
name: str | None,
) -> None:
if BybitProductType.SPOT in product_types:
if len(set(product_types)) > 1:
Expand All @@ -125,7 +128,7 @@ def __init__(

super().__init__(
loop=loop,
client_id=ClientId(BYBIT_VENUE.value),
client_id=ClientId(name or BYBIT_VENUE.value),
venue=BYBIT_VENUE,
oms_type=OmsType.NETTING,
instrument_provider=instrument_provider,
Expand Down Expand Up @@ -153,7 +156,7 @@ def __init__(

self._enum_parser = BybitEnumParser()

account_id = AccountId(f"{BYBIT_VENUE.value}-UNIFIED")
account_id = AccountId(f"{name or BYBIT_VENUE.value}-UNIFIED")
self._set_account_id(account_id)

# WebSocket API
Expand Down
6 changes: 4 additions & 2 deletions nautilus_trader/adapters/bybit/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def create( # type: ignore
loop : asyncio.AbstractEventLoop
The event loop for the client.
name : str
The client name.
The custom client ID.
config : BybitDataClientConfig
The client configuration.
msgbus : MessageBus
Expand Down Expand Up @@ -202,6 +202,7 @@ def create( # type: ignore
product_types=product_types,
ws_base_urls=ws_base_urls,
config=config,
name=name,
)


Expand All @@ -227,7 +228,7 @@ def create( # type: ignore
loop : asyncio.AbstractEventLoop
The event loop for the client.
name : str
The client name.
The custom client ID.
config : BybitExecClientConfig
The client configuration.
msgbus : MessageBus
Expand Down Expand Up @@ -266,4 +267,5 @@ def create( # type: ignore
product_types=config.product_types or [BybitProductType.SPOT],
base_url_ws=config.base_url_ws or base_url_ws,
config=config,
name=name,
)
8 changes: 6 additions & 2 deletions nautilus_trader/adapters/databento/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from nautilus_trader.adapters.databento.common import databento_schema_from_nautilus_bar_type
from nautilus_trader.adapters.databento.config import DatabentoDataClientConfig
from nautilus_trader.adapters.databento.constants import ALL_SYMBOLS
from nautilus_trader.adapters.databento.constants import DATABENTO_CLIENT_ID
from nautilus_trader.adapters.databento.constants import DATABENTO
from nautilus_trader.adapters.databento.constants import PUBLISHERS_PATH
from nautilus_trader.adapters.databento.enums import DatabentoSchema
from nautilus_trader.adapters.databento.loaders import DatabentoDataLoader
Expand All @@ -48,6 +48,7 @@
from nautilus_trader.model.data import capsule_to_data
from nautilus_trader.model.enums import BookType
from nautilus_trader.model.enums import bar_aggregation_to_str
from nautilus_trader.model.identifiers import ClientId
from nautilus_trader.model.identifiers import InstrumentId
from nautilus_trader.model.identifiers import Venue
from nautilus_trader.model.instruments import instruments_from_pyo3
Expand Down Expand Up @@ -78,6 +79,8 @@ class DatabentoDataClient(LiveMarketDataClient):
The loader for the client.
config : DatabentoDataClientConfig, optional
The configuration for the client.
name : str, optional
The custom client ID.
"""

Expand All @@ -91,14 +94,15 @@ def __init__(
instrument_provider: DatabentoInstrumentProvider,
loader: DatabentoDataLoader | None = None,
config: DatabentoDataClientConfig | None = None,
name: str | None = None,
) -> None:
if config is None:
config = DatabentoDataClientConfig()
PyCondition.type(config, DatabentoDataClientConfig, "config")

super().__init__(
loop=loop,
client_id=DATABENTO_CLIENT_ID,
client_id=ClientId(name or DATABENTO),
venue=None, # Not applicable
msgbus=msgbus,
cache=cache,
Expand Down
Loading

0 comments on commit 6f96865

Please sign in to comment.