Skip to content

Commit

Permalink
Build out DatabentoInstrumentProvider
Browse files Browse the repository at this point in the history
cjdsellers committed Dec 9, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 1c4f707 commit 47a83f6
Showing 7 changed files with 180 additions and 8 deletions.
41 changes: 41 additions & 0 deletions nautilus_trader/adapters/databento/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# -------------------------------------------------------------------------------------------------
# Copyright (C) 2015-2023 Nautech Systems Pty Ltd. All rights reserved.
# https://nautechsystems.io
#
# Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -------------------------------------------------------------------------------------------------


from nautilus_trader.config import LiveDataClientConfig


class DatabentoDataClientConfig(LiveDataClientConfig, frozen=True):
"""
Configuration for ``DatabentoDataClient`` instances.
Parameters
----------
api_key : str, optional
The Binance API public key.
If ``None`` then will source the `BINANCE_API_KEY` or
`BINANCE_TESTNET_API_KEY` environment variables.
api_secret : str, optional
The Binance API public key.
If ``None`` then will source the `BINANCE_API_KEY` or
`BINANCE_TESTNET_API_KEY` environment variables.
http_gateway : str, optional
The HTTP historical client gateway override.
"""

api_key: str | None = None
api_secret: str | None = None
http_gateway: str | None = None
57 changes: 57 additions & 0 deletions nautilus_trader/adapters/databento/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# -------------------------------------------------------------------------------------------------
# Copyright (C) 2015-2023 Nautech Systems Pty Ltd. All rights reserved.
# https://nautechsystems.io
#
# Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -------------------------------------------------------------------------------------------------

from functools import lru_cache

import databento

from nautilus_trader.adapters.env import get_env_key


DATABENTO_HTTP_CLIENTS: dict[str, databento.Historical] = {}


@lru_cache(1)
def get_cached_databento_http_client(
key: str | None = None,
gateway: str | None = None,
) -> databento.Historical:
"""
Cache and return a Databento historical HTTP client with the given key and gateway.
If a cached client with matching key and gateway already exists, then that
cached client will be returned.
Parameters
----------
key : str, optional
The API key for the client.
gateway : str, optional
The HTTP historical client gateway override.
Returns
-------
databento.Historical
"""
global BINANCE_HTTP_CLIENTS

key = key or get_env_key("DATABENTO_API_KEY")

client_key: str = "|".join((key, gateway or ""))
if client_key not in DATABENTO_HTTP_CLIENTS:
client = databento.Historical(key=key, gateway=gateway or databento.HistoricalGateway.BO1)
DATABENTO_HTTP_CLIENTS[client_key] = client
return DATABENTO_HTTP_CLIENTS[client_key]
5 changes: 3 additions & 2 deletions nautilus_trader/adapters/databento/loaders.py
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@

import databento
import msgspec
from databento import InstrumentMap

from nautilus_trader.adapters.databento.common import check_file_path
from nautilus_trader.adapters.databento.parsing import parse_record
@@ -34,7 +35,7 @@ class DatabentoDataLoader:
Supported schemas:
- MBO
- MBP_1
- MBP_10 (top-level only)
- MBP_10 (decodes top-level only)
- TBBO
- TRADES
- OHLCV_1S
@@ -169,7 +170,7 @@ def from_dbn(self, path: PathLike[str] | str) -> list[Data]:
"""
store = databento.from_dbn(path)
instrument_map = databento.common.symbology.InstrumentMap()
instrument_map = InstrumentMap()
instrument_map.insert_metadata(metadata=store.metadata)

output: list[Data] = []
14 changes: 8 additions & 6 deletions nautilus_trader/adapters/databento/providers.py
Original file line number Diff line number Diff line change
@@ -29,8 +29,10 @@ class DatabentoInstrumentProvider(InstrumentProvider):
Parameters
----------
client : databento.Historical
The Databento historical data client for the provider.
http_client : databento.Historical
The historical Databento data client for the provider.
live_client : databento.Live
The live Databento data client for the provider.
logger : Logger
The logger for the provider.
clock : LiveClock
@@ -42,7 +44,8 @@ class DatabentoInstrumentProvider(InstrumentProvider):

def __init__(
self,
client: databento.Historical,
http_client: databento.Historical,
live_client: databento.Live,
logger: Logger,
clock: LiveClock,
config: InstrumentProviderConfig | None = None,
@@ -55,11 +58,10 @@ def __init__(
self._clock = clock
self._config = config

self._http_client = http_client
self._live_client = live_client
self._loader = DatabentoDataLoader()

# HTTP API
self._http_client = client

async def load_all_async(self, filters: dict | None = None) -> None:
raise RuntimeError(
"requesting all instrument definitions is not currently supported, "
2 changes: 2 additions & 0 deletions nautilus_trader/common/providers.py
Original file line number Diff line number Diff line change
@@ -64,6 +64,8 @@ def __init__(
self._loaded = False
self._loading = False

self._log.info("READY.")

@property
def count(self) -> int:
"""
14 changes: 14 additions & 0 deletions tests/integration_tests/adapters/databento/sandbox/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# -------------------------------------------------------------------------------------------------
# Copyright (C) 2015-2023 Nautech Systems Pty Ltd. All rights reserved.
# https://nautechsystems.io
#
# Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -------------------------------------------------------------------------------------------------
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# -------------------------------------------------------------------------------------------------
# Copyright (C) 2015-2023 Nautech Systems Pty Ltd. All rights reserved.
# https://nautechsystems.io
#
# Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -------------------------------------------------------------------------------------------------

import os

import databento
import pytest

from nautilus_trader.adapters.databento.factories import get_cached_databento_http_client
from nautilus_trader.adapters.databento.providers import DatabentoInstrumentProvider
from nautilus_trader.common.clock import LiveClock
from nautilus_trader.common.logging import Logger
from nautilus_trader.model.identifiers import InstrumentId


@pytest.mark.asyncio()
async def test_binance_futures_testnet_market_http_client():
clock = LiveClock()

key = os.getenv("DATABENTO_API_KEY")

http_client = get_cached_databento_http_client(
key=key,
# gateway=gateway,
)

live_client = databento.Live(
key=key,
# gateway=gateway,
)

provider = DatabentoInstrumentProvider(
http_client=http_client,
live_client=live_client,
clock=clock,
logger=Logger(clock=clock),
)

instrument_ids = [
InstrumentId.from_str("ESM2.GLBX"),
]

provider.load_ids(instrument_ids)

0 comments on commit 47a83f6

Please sign in to comment.