From ecbda362ce2116a5a4accaac8c56f21f34845e8b Mon Sep 17 00:00:00 2001 From: Chris Sellers Date: Tue, 23 Jan 2024 19:25:49 +1100 Subject: [PATCH] Consolidate clock module into components --- RELEASES.md | 3 +- docs/api_reference/common.md | 10 - docs/concepts/adapters.md | 2 +- .../crypto_ema_cross_with_binance_provider.py | 2 +- .../bybit/bybit_request_custom_endpoint.py | 2 +- nautilus_trader/accounting/manager.pxd | 2 +- nautilus_trader/accounting/manager.pyx | 2 +- nautilus_trader/adapters/betfair/data.py | 2 +- nautilus_trader/adapters/betfair/execution.py | 2 +- nautilus_trader/adapters/betfair/factories.py | 2 +- .../adapters/binance/common/data.py | 2 +- .../adapters/binance/common/execution.py | 2 +- nautilus_trader/adapters/binance/factories.py | 2 +- .../adapters/binance/futures/data.py | 2 +- .../adapters/binance/futures/execution.py | 2 +- .../adapters/binance/futures/http/account.py | 2 +- .../adapters/binance/futures/http/wallet.py | 2 +- .../adapters/binance/futures/providers.py | 2 +- .../adapters/binance/http/account.py | 2 +- .../adapters/binance/http/client.py | 2 +- nautilus_trader/adapters/binance/spot/data.py | 2 +- .../adapters/binance/spot/execution.py | 2 +- .../adapters/binance/spot/http/account.py | 2 +- .../adapters/binance/spot/http/wallet.py | 2 +- .../adapters/binance/spot/providers.py | 2 +- .../adapters/binance/websocket/client.py | 2 +- nautilus_trader/adapters/bybit/data.py | 2 +- nautilus_trader/adapters/bybit/execution.py | 2 +- nautilus_trader/adapters/bybit/factories.py | 2 +- .../adapters/bybit/http/account.py | 2 +- nautilus_trader/adapters/bybit/http/client.py | 2 +- nautilus_trader/adapters/bybit/http/market.py | 2 +- nautilus_trader/adapters/bybit/provider.py | 2 +- .../adapters/bybit/websocket/client.py | 2 +- nautilus_trader/adapters/databento/data.py | 2 +- .../adapters/databento/factories.py | 2 +- .../adapters/databento/providers.py | 2 +- .../interactive_brokers/client/client.py | 2 +- .../interactive_brokers/client/common.py | 2 +- .../adapters/interactive_brokers/data.py | 2 +- .../adapters/interactive_brokers/execution.py | 2 +- .../adapters/interactive_brokers/factories.py | 2 +- .../interactive_brokers/historic/client.py | 2 +- nautilus_trader/adapters/sandbox/execution.py | 4 +- nautilus_trader/adapters/sandbox/factory.py | 2 +- nautilus_trader/backtest/data_client.pyx | 2 +- nautilus_trader/backtest/engine.pxd | 2 +- nautilus_trader/backtest/engine.pyx | 8 +- nautilus_trader/backtest/exchange.pxd | 2 +- nautilus_trader/backtest/exchange.pyx | 2 +- nautilus_trader/backtest/execution_client.pyx | 2 +- nautilus_trader/backtest/matching_engine.pxd | 2 +- nautilus_trader/backtest/matching_engine.pyx | 2 +- nautilus_trader/common/actor.pxd | 2 +- nautilus_trader/common/actor.pyx | 4 +- nautilus_trader/common/clock.pxd | 156 --- nautilus_trader/common/clock.pyx | 1228 ----------------- nautilus_trader/common/component.pxd | 138 +- nautilus_trader/common/component.pyx | 1206 +++++++++++++++- nautilus_trader/common/factories.pxd | 2 +- nautilus_trader/common/factories.pyx | 2 +- nautilus_trader/common/generators.pxd | 2 +- nautilus_trader/common/generators.pyx | 2 +- nautilus_trader/data/aggregation.pxd | 4 +- nautilus_trader/data/aggregation.pyx | 4 +- nautilus_trader/data/client.pyx | 2 +- nautilus_trader/data/engine.pxd | 2 +- nautilus_trader/data/engine.pyx | 2 +- nautilus_trader/examples/algorithms/twap.py | 2 +- nautilus_trader/execution/algorithm.pxd | 2 +- nautilus_trader/execution/algorithm.pyx | 2 +- nautilus_trader/execution/client.pyx | 2 +- nautilus_trader/execution/emulator.pyx | 2 +- nautilus_trader/execution/engine.pyx | 2 +- nautilus_trader/execution/manager.pxd | 2 +- nautilus_trader/execution/manager.pyx | 2 +- nautilus_trader/live/data_client.py | 2 +- nautilus_trader/live/data_engine.py | 2 +- nautilus_trader/live/execution_client.py | 2 +- nautilus_trader/live/execution_engine.py | 2 +- nautilus_trader/live/factories.py | 2 +- nautilus_trader/live/node_builder.py | 2 +- nautilus_trader/live/risk_engine.py | 2 +- nautilus_trader/portfolio/portfolio.pxd | 2 +- nautilus_trader/risk/engine.pyx | 2 +- nautilus_trader/system/kernel.py | 6 +- nautilus_trader/test_kit/stubs/component.py | 2 +- nautilus_trader/trading/strategy.pxd | 4 +- nautilus_trader/trading/strategy.pyx | 4 +- nautilus_trader/trading/trader.py | 4 +- .../adapters/betfair/test_betfair_factory.py | 2 +- .../adapters/betfair/test_betfair_parsing.py | 2 +- .../betfair/test_betfair_providers.py | 2 +- .../adapters/betfair/test_betting_account.py | 2 +- .../adapters/binance/conftest.py | 2 +- .../sandbox/sandbox_http_futures_account.py | 2 +- .../sandbox_http_futures_testnet_account.py | 2 +- ...ttp_futures_testnet_instrument_provider.py | 2 +- .../sandbox_http_futures_testnet_market.py | 2 +- .../sandbox_http_futures_testnet_wallet.py | 2 +- .../sandbox/sandbox_http_spot_account.py | 2 +- .../sandbox_http_spot_instrument_provider.py | 2 +- .../binance/sandbox/sandbox_http_spot_user.py | 2 +- .../binance/sandbox/sandbox_http_wallet.py | 2 +- .../sandbox/sandbox_ws_futures_market.py | 2 +- .../binance/sandbox/sandbox_ws_spot_user.py | 2 +- .../adapters/binance/test_data_spot.py | 2 +- .../binance/test_execution_futures.py | 2 +- .../adapters/binance/test_execution_spot.py | 2 +- .../adapters/binance/test_factories.py | 2 +- .../adapters/binance/test_http_account.py | 2 +- .../adapters/binance/test_http_market.py | 2 +- .../adapters/binance/test_http_user.py | 2 +- .../adapters/binance/test_http_wallet.py | 2 +- .../adapters/binance/test_providers.py | 2 +- .../adapters/bybit/conftest.py | 2 +- .../bybit/sandbox/sandbox_http_account.py | 2 +- .../bybit/sandbox/sandbox_http_market.py | 2 +- .../adapters/bybit/test_factories.py | 2 +- .../adapters/bybit/test_http_account.py | 2 +- .../adapters/bybit/test_http_market.py | 2 +- .../adapters/bybit/test_providers.py | 2 +- tests/integration_tests/adapters/conftest.py | 4 +- .../sandbox/sandbox_instrument_provider.py | 2 +- .../infrastructure/test_cache_database.py | 2 +- tests/performance_tests/conftest.py | 2 +- tests/performance_tests/test_perf_clock.py | 6 +- .../test_perf_live_execution.py | 2 +- tests/performance_tests/test_perf_order.py | 4 +- .../test_perf_serialization.py | 2 +- tests/unit_tests/accounting/test_cash.py | 2 +- tests/unit_tests/accounting/test_margin.py | 2 +- tests/unit_tests/analysis/test_analyzer.py | 2 +- tests/unit_tests/analysis/test_reports.py | 2 +- .../analysis/test_statistics_long_ratio.py | 2 +- .../backtest/test_data_wranglers.py | 2 +- tests/unit_tests/backtest/test_exchange.py | 2 +- .../backtest/test_exchange_bitmex.py | 2 +- ...est_exchange_bracket_if_touched_entries.py | 2 +- .../backtest/test_exchange_contingencies.py | 2 +- .../backtest/test_exchange_l2_mbp.py | 2 +- .../backtest/test_exchange_stop_limits.py | 2 +- .../backtest/test_exchange_trailing_stops.py | 2 +- .../backtest/test_matching_engine.py | 2 +- tests/unit_tests/cache/test_execution.py | 2 +- tests/unit_tests/common/test_actor.py | 2 +- tests/unit_tests/common/test_clock.py | 8 +- tests/unit_tests/common/test_events.py | 2 +- tests/unit_tests/common/test_factories.py | 2 +- tests/unit_tests/common/test_generators.py | 2 +- tests/unit_tests/common/test_throttler.py | 2 +- tests/unit_tests/data/test_aggregation.py | 2 +- tests/unit_tests/data/test_client.py | 2 +- tests/unit_tests/data/test_engine.py | 2 +- tests/unit_tests/data/test_messages.py | 2 +- tests/unit_tests/execution/test_algorithm.py | 4 +- tests/unit_tests/execution/test_client.py | 2 +- tests/unit_tests/execution/test_emulator.py | 2 +- .../execution/test_emulator_list.py | 2 +- tests/unit_tests/execution/test_engine.py | 2 +- tests/unit_tests/execution/test_messages.py | 2 +- tests/unit_tests/live/test_data_client.py | 2 +- tests/unit_tests/live/test_data_engine.py | 2 +- .../unit_tests/live/test_execution_engine.py | 2 +- tests/unit_tests/live/test_execution_recon.py | 2 +- tests/unit_tests/live/test_risk_engine.py | 2 +- tests/unit_tests/model/test_events.py | 2 +- tests/unit_tests/model/test_orders.py | 2 +- tests/unit_tests/model/test_position.py | 2 +- tests/unit_tests/msgbus/test_bus.py | 2 +- tests/unit_tests/portfolio/test_portfolio.py | 2 +- tests/unit_tests/risk/test_engine.py | 2 +- tests/unit_tests/serialization/test_arrow.py | 2 +- .../unit_tests/serialization/test_msgpack.py | 2 +- tests/unit_tests/trading/test_strategy.py | 2 +- tests/unit_tests/trading/test_trader.py | 2 +- 176 files changed, 1529 insertions(+), 1592 deletions(-) delete mode 100644 nautilus_trader/common/clock.pxd delete mode 100644 nautilus_trader/common/clock.pyx diff --git a/RELEASES.md b/RELEASES.md index d9e04856c593..773bfd8359b9 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -6,7 +6,8 @@ Released on TBD (UTC). None ### Breaking Changes -- Consolidated `logging` module into one `components` module (reduce binary wheel size) +- Consolidated `clock` module into `components` module (reduce binary wheel size) +- Consolidated `logging` module into `components` module (reduce binary wheel size) ### Fixes None diff --git a/docs/api_reference/common.md b/docs/api_reference/common.md index cf445a65d652..64cce70b87a8 100644 --- a/docs/api_reference/common.md +++ b/docs/api_reference/common.md @@ -12,16 +12,6 @@ :member-order: bysource ``` -## Clock - -```{eval-rst} -.. automodule:: nautilus_trader.common.clock - :show-inheritance: - :inherited-members: - :members: - :member-order: bysource -``` - ## Component ```{eval-rst} diff --git a/docs/concepts/adapters.md b/docs/concepts/adapters.md index f0f2eff50c44..b9fa99276735 100644 --- a/docs/concepts/adapters.md +++ b/docs/concepts/adapters.md @@ -27,7 +27,7 @@ Here is an example of discovering the current instruments for the Binance Future from nautilus_trader.adapters.binance.common.enums import BinanceAccountType from nautilus_trader.adapters.binance.factories import get_cached_binance_http_client from nautilus_trader.adapters.binance.futures.providers import BinanceFuturesInstrumentProvider -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock clock = LiveClock() diff --git a/examples/backtest/crypto_ema_cross_with_binance_provider.py b/examples/backtest/crypto_ema_cross_with_binance_provider.py index 6332e9215b32..37139aef068c 100644 --- a/examples/backtest/crypto_ema_cross_with_binance_provider.py +++ b/examples/backtest/crypto_ema_cross_with_binance_provider.py @@ -25,7 +25,7 @@ from nautilus_trader.adapters.binance.futures.providers import BinanceFuturesInstrumentProvider from nautilus_trader.backtest.engine import BacktestEngine from nautilus_trader.backtest.engine import BacktestEngineConfig -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.config import InstrumentProviderConfig from nautilus_trader.config import LoggingConfig from nautilus_trader.examples.strategies.ema_cross_trailing_stop import EMACrossTrailingStop diff --git a/examples/live/bybit/bybit_request_custom_endpoint.py b/examples/live/bybit/bybit_request_custom_endpoint.py index b50d22041de2..4349d01e07a6 100644 --- a/examples/live/bybit/bybit_request_custom_endpoint.py +++ b/examples/live/bybit/bybit_request_custom_endpoint.py @@ -24,7 +24,7 @@ from nautilus_trader.adapters.bybit.factories import BybitLiveExecClientFactory from nautilus_trader.adapters.bybit.schemas.market.ticker import BybitTickerData from nautilus_trader.common import Environment -from nautilus_trader.common.clock import TimeEvent +from nautilus_trader.common.component import TimeEvent from nautilus_trader.config import InstrumentProviderConfig from nautilus_trader.config import LiveExecEngineConfig from nautilus_trader.config import LoggingConfig diff --git a/nautilus_trader/accounting/manager.pxd b/nautilus_trader/accounting/manager.pxd index a516cab20215..7505da5f5f3e 100644 --- a/nautilus_trader/accounting/manager.pxd +++ b/nautilus_trader/accounting/manager.pxd @@ -19,7 +19,7 @@ from nautilus_trader.accounting.accounts.base cimport Account from nautilus_trader.accounting.accounts.cash cimport CashAccount from nautilus_trader.accounting.accounts.margin cimport MarginAccount from nautilus_trader.cache.base cimport CacheFacade -from nautilus_trader.common.clock cimport Clock +from nautilus_trader.common.component cimport Clock from nautilus_trader.common.component cimport Logger from nautilus_trader.core.rust.model cimport OrderSide from nautilus_trader.model.events.account cimport AccountState diff --git a/nautilus_trader/accounting/manager.pyx b/nautilus_trader/accounting/manager.pyx index 672504f942b0..3402de31d547 100644 --- a/nautilus_trader/accounting/manager.pyx +++ b/nautilus_trader/accounting/manager.pyx @@ -22,7 +22,7 @@ from nautilus_trader.accounting.accounts.base cimport Account from nautilus_trader.accounting.accounts.cash cimport CashAccount from nautilus_trader.accounting.accounts.margin cimport MarginAccount from nautilus_trader.cache.base cimport CacheFacade -from nautilus_trader.common.clock cimport Clock +from nautilus_trader.common.component cimport Clock from nautilus_trader.common.component cimport Logger from nautilus_trader.core.correctness cimport Condition from nautilus_trader.core.rust.common cimport logging_is_initialized diff --git a/nautilus_trader/adapters/betfair/data.py b/nautilus_trader/adapters/betfair/data.py index 91b5feb40947..667fc7ac8c88 100644 --- a/nautilus_trader/adapters/betfair/data.py +++ b/nautilus_trader/adapters/betfair/data.py @@ -31,7 +31,7 @@ from nautilus_trader.adapters.betfair.providers import BetfairInstrumentProvider from nautilus_trader.adapters.betfair.sockets import BetfairMarketStreamClient from nautilus_trader.cache.cache import Cache -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.common.enums import LogColor from nautilus_trader.core.correctness import PyCondition diff --git a/nautilus_trader/adapters/betfair/execution.py b/nautilus_trader/adapters/betfair/execution.py index ae29bfbae305..22adc95970f2 100644 --- a/nautilus_trader/adapters/betfair/execution.py +++ b/nautilus_trader/adapters/betfair/execution.py @@ -54,7 +54,7 @@ from nautilus_trader.adapters.betfair.providers import BetfairInstrumentProvider from nautilus_trader.adapters.betfair.sockets import BetfairOrderStreamClient from nautilus_trader.cache.cache import Cache -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.common.enums import LogColor from nautilus_trader.core.correctness import PyCondition diff --git a/nautilus_trader/adapters/betfair/factories.py b/nautilus_trader/adapters/betfair/factories.py index e92780a74e93..726aba7737d3 100644 --- a/nautilus_trader/adapters/betfair/factories.py +++ b/nautilus_trader/adapters/betfair/factories.py @@ -25,7 +25,7 @@ from nautilus_trader.adapters.betfair.providers import BetfairInstrumentProvider from nautilus_trader.adapters.betfair.providers import BetfairInstrumentProviderConfig from nautilus_trader.cache.cache import Cache -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import Logger from nautilus_trader.common.component import MessageBus from nautilus_trader.live.factories import LiveDataClientFactory diff --git a/nautilus_trader/adapters/binance/common/data.py b/nautilus_trader/adapters/binance/common/data.py index 873fc5a27863..6652911c2b5b 100644 --- a/nautilus_trader/adapters/binance/common/data.py +++ b/nautilus_trader/adapters/binance/common/data.py @@ -41,7 +41,7 @@ from nautilus_trader.adapters.binance.http.market import BinanceMarketHttpAPI from nautilus_trader.adapters.binance.websocket.client import BinanceWebSocketClient from nautilus_trader.cache.cache import Cache -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.common.enums import LogColor from nautilus_trader.common.providers import InstrumentProvider diff --git a/nautilus_trader/adapters/binance/common/execution.py b/nautilus_trader/adapters/binance/common/execution.py index 28733ff5f45c..99b541057cf3 100644 --- a/nautilus_trader/adapters/binance/common/execution.py +++ b/nautilus_trader/adapters/binance/common/execution.py @@ -35,7 +35,7 @@ from nautilus_trader.adapters.binance.http.user import BinanceUserDataHttpAPI from nautilus_trader.adapters.binance.websocket.client import BinanceWebSocketClient from nautilus_trader.cache.cache import Cache -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.common.enums import LogColor from nautilus_trader.common.providers import InstrumentProvider diff --git a/nautilus_trader/adapters/binance/factories.py b/nautilus_trader/adapters/binance/factories.py index 35b2428e20f8..54157f617f51 100644 --- a/nautilus_trader/adapters/binance/factories.py +++ b/nautilus_trader/adapters/binance/factories.py @@ -28,7 +28,7 @@ from nautilus_trader.adapters.binance.spot.providers import BinanceSpotInstrumentProvider from nautilus_trader.adapters.env import get_env_key from nautilus_trader.cache.cache import Cache -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.config import InstrumentProviderConfig from nautilus_trader.core.nautilus_pyo3 import Quota diff --git a/nautilus_trader/adapters/binance/futures/data.py b/nautilus_trader/adapters/binance/futures/data.py index 9c467a34c7d9..815a51e9de02 100644 --- a/nautilus_trader/adapters/binance/futures/data.py +++ b/nautilus_trader/adapters/binance/futures/data.py @@ -27,7 +27,7 @@ from nautilus_trader.adapters.binance.futures.types import BinanceFuturesMarkPriceUpdate from nautilus_trader.adapters.binance.http.client import BinanceHttpClient from nautilus_trader.cache.cache import Cache -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.common.providers import InstrumentProvider from nautilus_trader.core.correctness import PyCondition diff --git a/nautilus_trader/adapters/binance/futures/execution.py b/nautilus_trader/adapters/binance/futures/execution.py index 87add1b79c6a..db363d739b2f 100644 --- a/nautilus_trader/adapters/binance/futures/execution.py +++ b/nautilus_trader/adapters/binance/futures/execution.py @@ -37,7 +37,7 @@ from nautilus_trader.adapters.binance.http.client import BinanceHttpClient from nautilus_trader.adapters.binance.http.error import BinanceError from nautilus_trader.cache.cache import Cache -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.common.enums import LogColor from nautilus_trader.core.correctness import PyCondition diff --git a/nautilus_trader/adapters/binance/futures/http/account.py b/nautilus_trader/adapters/binance/futures/http/account.py index f2f45467f97b..c01ec0563c03 100644 --- a/nautilus_trader/adapters/binance/futures/http/account.py +++ b/nautilus_trader/adapters/binance/futures/http/account.py @@ -28,7 +28,7 @@ from nautilus_trader.adapters.binance.http.account import BinanceAccountHttpAPI from nautilus_trader.adapters.binance.http.client import BinanceHttpClient from nautilus_trader.adapters.binance.http.endpoint import BinanceHttpEndpoint -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.core.nautilus_pyo3 import HttpMethod diff --git a/nautilus_trader/adapters/binance/futures/http/wallet.py b/nautilus_trader/adapters/binance/futures/http/wallet.py index 736a8e851684..bac079d89804 100644 --- a/nautilus_trader/adapters/binance/futures/http/wallet.py +++ b/nautilus_trader/adapters/binance/futures/http/wallet.py @@ -22,7 +22,7 @@ from nautilus_trader.adapters.binance.futures.schemas.wallet import BinanceFuturesCommissionRate from nautilus_trader.adapters.binance.http.client import BinanceHttpClient from nautilus_trader.adapters.binance.http.endpoint import BinanceHttpEndpoint -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.core.nautilus_pyo3 import HttpMethod diff --git a/nautilus_trader/adapters/binance/futures/providers.py b/nautilus_trader/adapters/binance/futures/providers.py index 67f41d915323..52a9c5705742 100644 --- a/nautilus_trader/adapters/binance/futures/providers.py +++ b/nautilus_trader/adapters/binance/futures/providers.py @@ -33,7 +33,7 @@ from nautilus_trader.adapters.binance.futures.schemas.market import BinanceFuturesSymbolInfo from nautilus_trader.adapters.binance.futures.schemas.wallet import BinanceFuturesCommissionRate from nautilus_trader.adapters.binance.http.client import BinanceHttpClient -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.providers import InstrumentProvider from nautilus_trader.config import InstrumentProviderConfig from nautilus_trader.core.correctness import PyCondition diff --git a/nautilus_trader/adapters/binance/http/account.py b/nautilus_trader/adapters/binance/http/account.py index 1489df4d05ba..3b8ef219359d 100644 --- a/nautilus_trader/adapters/binance/http/account.py +++ b/nautilus_trader/adapters/binance/http/account.py @@ -27,7 +27,7 @@ from nautilus_trader.adapters.binance.common.schemas.symbol import BinanceSymbol from nautilus_trader.adapters.binance.http.client import BinanceHttpClient from nautilus_trader.adapters.binance.http.endpoint import BinanceHttpEndpoint -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.core.correctness import PyCondition from nautilus_trader.core.nautilus_pyo3 import HttpMethod diff --git a/nautilus_trader/adapters/binance/http/client.py b/nautilus_trader/adapters/binance/http/client.py index 3d3460482dae..e44ddb4102a9 100644 --- a/nautilus_trader/adapters/binance/http/client.py +++ b/nautilus_trader/adapters/binance/http/client.py @@ -23,7 +23,7 @@ import nautilus_trader from nautilus_trader.adapters.binance.http.error import BinanceClientError from nautilus_trader.adapters.binance.http.error import BinanceServerError -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import Logger from nautilus_trader.core.nautilus_pyo3 import HttpClient from nautilus_trader.core.nautilus_pyo3 import HttpMethod diff --git a/nautilus_trader/adapters/binance/spot/data.py b/nautilus_trader/adapters/binance/spot/data.py index 8b7fa03c69a5..5b88aee29f64 100644 --- a/nautilus_trader/adapters/binance/spot/data.py +++ b/nautilus_trader/adapters/binance/spot/data.py @@ -26,7 +26,7 @@ from nautilus_trader.adapters.binance.spot.schemas.market import BinanceSpotOrderBookPartialDepthMsg from nautilus_trader.adapters.binance.spot.schemas.market import BinanceSpotTradeMsg from nautilus_trader.cache.cache import Cache -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.common.providers import InstrumentProvider from nautilus_trader.core.correctness import PyCondition diff --git a/nautilus_trader/adapters/binance/spot/execution.py b/nautilus_trader/adapters/binance/spot/execution.py index 29acccde60c2..9d4b676022de 100644 --- a/nautilus_trader/adapters/binance/spot/execution.py +++ b/nautilus_trader/adapters/binance/spot/execution.py @@ -32,7 +32,7 @@ from nautilus_trader.adapters.binance.spot.schemas.user import BinanceSpotOrderUpdateWrapper from nautilus_trader.adapters.binance.spot.schemas.user import BinanceSpotUserMsgWrapper from nautilus_trader.cache.cache import Cache -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.common.enums import LogColor from nautilus_trader.core.correctness import PyCondition diff --git a/nautilus_trader/adapters/binance/spot/http/account.py b/nautilus_trader/adapters/binance/spot/http/account.py index 2dda262eda02..6867d2547eea 100644 --- a/nautilus_trader/adapters/binance/spot/http/account.py +++ b/nautilus_trader/adapters/binance/spot/http/account.py @@ -30,7 +30,7 @@ from nautilus_trader.adapters.binance.http.endpoint import BinanceHttpEndpoint from nautilus_trader.adapters.binance.spot.schemas.account import BinanceSpotAccountInfo from nautilus_trader.adapters.binance.spot.schemas.account import BinanceSpotOrderOco -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.core.nautilus_pyo3 import HttpMethod diff --git a/nautilus_trader/adapters/binance/spot/http/wallet.py b/nautilus_trader/adapters/binance/spot/http/wallet.py index 0c41edb8a20a..461f88e69ed7 100644 --- a/nautilus_trader/adapters/binance/spot/http/wallet.py +++ b/nautilus_trader/adapters/binance/spot/http/wallet.py @@ -22,7 +22,7 @@ from nautilus_trader.adapters.binance.http.client import BinanceHttpClient from nautilus_trader.adapters.binance.http.endpoint import BinanceHttpEndpoint from nautilus_trader.adapters.binance.spot.schemas.wallet import BinanceSpotTradeFee -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.core.nautilus_pyo3 import HttpMethod diff --git a/nautilus_trader/adapters/binance/spot/providers.py b/nautilus_trader/adapters/binance/spot/providers.py index d9498c5b36cc..51da0966c9fb 100644 --- a/nautilus_trader/adapters/binance/spot/providers.py +++ b/nautilus_trader/adapters/binance/spot/providers.py @@ -28,7 +28,7 @@ from nautilus_trader.adapters.binance.spot.http.wallet import BinanceSpotWalletHttpAPI from nautilus_trader.adapters.binance.spot.schemas.market import BinanceSpotSymbolInfo from nautilus_trader.adapters.binance.spot.schemas.wallet import BinanceSpotTradeFee -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.providers import InstrumentProvider from nautilus_trader.config import InstrumentProviderConfig from nautilus_trader.core.correctness import PyCondition diff --git a/nautilus_trader/adapters/binance/websocket/client.py b/nautilus_trader/adapters/binance/websocket/client.py index f69bc392403a..abaee63d0c1f 100644 --- a/nautilus_trader/adapters/binance/websocket/client.py +++ b/nautilus_trader/adapters/binance/websocket/client.py @@ -19,7 +19,7 @@ from typing import Any from nautilus_trader.adapters.binance.common.schemas.symbol import BinanceSymbol -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import Logger from nautilus_trader.common.enums import LogColor from nautilus_trader.core.nautilus_pyo3 import WebSocketClient diff --git a/nautilus_trader/adapters/bybit/data.py b/nautilus_trader/adapters/bybit/data.py index 8fd6fe5eb452..6fb891b57d54 100644 --- a/nautilus_trader/adapters/bybit/data.py +++ b/nautilus_trader/adapters/bybit/data.py @@ -33,7 +33,7 @@ from nautilus_trader.adapters.bybit.utils import get_api_secret from nautilus_trader.adapters.bybit.websocket.client import BybitWebsocketClient from nautilus_trader.cache.cache import Cache -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.common.providers import InstrumentProvider from nautilus_trader.core.datetime import secs_to_millis diff --git a/nautilus_trader/adapters/bybit/execution.py b/nautilus_trader/adapters/bybit/execution.py index 90e2c3ddd441..6b9f19fad325 100644 --- a/nautilus_trader/adapters/bybit/execution.py +++ b/nautilus_trader/adapters/bybit/execution.py @@ -36,7 +36,7 @@ from nautilus_trader.adapters.bybit.utils import get_api_secret from nautilus_trader.adapters.bybit.websocket.client import BybitWebsocketClient from nautilus_trader.cache.cache import Cache -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.common.providers import InstrumentProvider from nautilus_trader.core.correctness import PyCondition diff --git a/nautilus_trader/adapters/bybit/factories.py b/nautilus_trader/adapters/bybit/factories.py index 6bf9a7f7b183..594100ad01ba 100644 --- a/nautilus_trader/adapters/bybit/factories.py +++ b/nautilus_trader/adapters/bybit/factories.py @@ -24,7 +24,7 @@ from nautilus_trader.adapters.bybit.provider import BybitInstrumentProvider from nautilus_trader.adapters.env import get_env_key from nautilus_trader.cache.cache import Cache -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.config import InstrumentProviderConfig from nautilus_trader.core.nautilus_pyo3 import Quota diff --git a/nautilus_trader/adapters/bybit/http/account.py b/nautilus_trader/adapters/bybit/http/account.py index 2d2d58ac8a9c..79b8dfceb13d 100644 --- a/nautilus_trader/adapters/bybit/http/account.py +++ b/nautilus_trader/adapters/bybit/http/account.py @@ -41,7 +41,7 @@ from nautilus_trader.adapters.bybit.schemas.position import BybitPositionStruct from nautilus_trader.adapters.bybit.schemas.symbol import BybitSymbol from nautilus_trader.adapters.bybit.utils import get_category_from_instrument_type -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.core.correctness import PyCondition diff --git a/nautilus_trader/adapters/bybit/http/client.py b/nautilus_trader/adapters/bybit/http/client.py index 482a6147f04d..21ef614741be 100644 --- a/nautilus_trader/adapters/bybit/http/client.py +++ b/nautilus_trader/adapters/bybit/http/client.py @@ -24,7 +24,7 @@ import nautilus_trader from nautilus_trader.adapters.bybit.common.error import raise_bybit_error from nautilus_trader.adapters.bybit.http.errors import BybitError -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import Logger from nautilus_trader.core.nautilus_pyo3 import HttpClient from nautilus_trader.core.nautilus_pyo3 import HttpMethod diff --git a/nautilus_trader/adapters/bybit/http/market.py b/nautilus_trader/adapters/bybit/http/market.py index 512a9ea7cc97..5bc412f21be6 100644 --- a/nautilus_trader/adapters/bybit/http/market.py +++ b/nautilus_trader/adapters/bybit/http/market.py @@ -34,7 +34,7 @@ from nautilus_trader.adapters.bybit.schemas.market.ticker import BybitTickerList from nautilus_trader.adapters.bybit.schemas.symbol import BybitSymbol from nautilus_trader.adapters.bybit.utils import get_category_from_instrument_type -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.core.correctness import PyCondition from nautilus_trader.model.data import Bar from nautilus_trader.model.data import BarType diff --git a/nautilus_trader/adapters/bybit/provider.py b/nautilus_trader/adapters/bybit/provider.py index 5e1c2be1197f..d50af8f98907 100644 --- a/nautilus_trader/adapters/bybit/provider.py +++ b/nautilus_trader/adapters/bybit/provider.py @@ -26,7 +26,7 @@ from nautilus_trader.adapters.bybit.schemas.instrument import BybitInstrumentList from nautilus_trader.adapters.bybit.schemas.instrument import BybitInstrumentOption from nautilus_trader.adapters.bybit.schemas.instrument import BybitInstrumentSpot -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.providers import InstrumentProvider from nautilus_trader.config import InstrumentProviderConfig from nautilus_trader.core.correctness import PyCondition diff --git a/nautilus_trader/adapters/bybit/websocket/client.py b/nautilus_trader/adapters/bybit/websocket/client.py index b75a33584f0e..8cec3a5c76db 100644 --- a/nautilus_trader/adapters/bybit/websocket/client.py +++ b/nautilus_trader/adapters/bybit/websocket/client.py @@ -18,7 +18,7 @@ import json from collections.abc import Callable -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import Logger from nautilus_trader.common.enums import LogColor from nautilus_trader.core.nautilus_pyo3 import WebSocketClient diff --git a/nautilus_trader/adapters/databento/data.py b/nautilus_trader/adapters/databento/data.py index 6f00b8036a7b..bd74cc033be5 100644 --- a/nautilus_trader/adapters/databento/data.py +++ b/nautilus_trader/adapters/databento/data.py @@ -36,7 +36,7 @@ from nautilus_trader.adapters.databento.types import DatabentoPublisher from nautilus_trader.adapters.databento.types import Dataset from nautilus_trader.cache.cache import Cache -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.common.enums import LogColor from nautilus_trader.core import nautilus_pyo3 diff --git a/nautilus_trader/adapters/databento/factories.py b/nautilus_trader/adapters/databento/factories.py index d5aa00fefba9..479ecca6ccf3 100644 --- a/nautilus_trader/adapters/databento/factories.py +++ b/nautilus_trader/adapters/databento/factories.py @@ -24,7 +24,7 @@ from nautilus_trader.adapters.databento.providers import DatabentoInstrumentProvider from nautilus_trader.adapters.env import get_env_key from nautilus_trader.cache.cache import Cache -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.config.common import InstrumentProviderConfig from nautilus_trader.live.factories import LiveDataClientFactory diff --git a/nautilus_trader/adapters/databento/providers.py b/nautilus_trader/adapters/databento/providers.py index 4fd6a1747ab3..a4028e40a0cf 100644 --- a/nautilus_trader/adapters/databento/providers.py +++ b/nautilus_trader/adapters/databento/providers.py @@ -20,7 +20,7 @@ from nautilus_trader.adapters.databento.loaders import DatabentoDataLoader from nautilus_trader.adapters.databento.parsing import parse_record_with_metadata -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.providers import InstrumentProvider from nautilus_trader.config import InstrumentProviderConfig from nautilus_trader.core.correctness import PyCondition diff --git a/nautilus_trader/adapters/interactive_brokers/client/client.py b/nautilus_trader/adapters/interactive_brokers/client/client.py index 5b3ba3651af8..f5ad74479558 100644 --- a/nautilus_trader/adapters/interactive_brokers/client/client.py +++ b/nautilus_trader/adapters/interactive_brokers/client/client.py @@ -46,8 +46,8 @@ from nautilus_trader.adapters.interactive_brokers.common import IBContract from nautilus_trader.adapters.interactive_brokers.parsing.instruments import instrument_id_to_ib_contract from nautilus_trader.cache.cache import Cache -from nautilus_trader.common.clock import LiveClock from nautilus_trader.common.component import Component +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.common.enums import LogColor from nautilus_trader.model.identifiers import ClientId diff --git a/nautilus_trader/adapters/interactive_brokers/client/common.py b/nautilus_trader/adapters/interactive_brokers/client/common.py index 0202ddf787ba..caa22d39f3eb 100644 --- a/nautilus_trader/adapters/interactive_brokers/client/common.py +++ b/nautilus_trader/adapters/interactive_brokers/client/common.py @@ -28,7 +28,7 @@ from nautilus_trader.adapters.interactive_brokers.common import IBContract from nautilus_trader.cache.cache import Cache -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import Logger from nautilus_trader.common.component import MessageBus from nautilus_trader.model.data import BarType diff --git a/nautilus_trader/adapters/interactive_brokers/data.py b/nautilus_trader/adapters/interactive_brokers/data.py index 70519f447d9a..3280ff737169 100644 --- a/nautilus_trader/adapters/interactive_brokers/data.py +++ b/nautilus_trader/adapters/interactive_brokers/data.py @@ -27,7 +27,7 @@ from nautilus_trader.adapters.interactive_brokers.parsing.data import timedelta_to_duration_str from nautilus_trader.adapters.interactive_brokers.providers import InteractiveBrokersInstrumentProvider from nautilus_trader.cache.cache import Cache -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.core.uuid import UUID4 from nautilus_trader.live.data_client import LiveMarketDataClient diff --git a/nautilus_trader/adapters/interactive_brokers/execution.py b/nautilus_trader/adapters/interactive_brokers/execution.py index b0df8fec2aa6..eb9b8b48762b 100644 --- a/nautilus_trader/adapters/interactive_brokers/execution.py +++ b/nautilus_trader/adapters/interactive_brokers/execution.py @@ -41,7 +41,7 @@ from nautilus_trader.adapters.interactive_brokers.parsing.execution import timestring_to_timestamp from nautilus_trader.adapters.interactive_brokers.providers import InteractiveBrokersInstrumentProvider from nautilus_trader.cache.cache import Cache -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.core.correctness import PyCondition from nautilus_trader.core.rust.common import LogColor diff --git a/nautilus_trader/adapters/interactive_brokers/factories.py b/nautilus_trader/adapters/interactive_brokers/factories.py index 60a607dbdd03..66098b03fa71 100644 --- a/nautilus_trader/adapters/interactive_brokers/factories.py +++ b/nautilus_trader/adapters/interactive_brokers/factories.py @@ -29,7 +29,7 @@ from nautilus_trader.adapters.interactive_brokers.gateway import InteractiveBrokersGateway from nautilus_trader.adapters.interactive_brokers.providers import InteractiveBrokersInstrumentProvider from nautilus_trader.cache.cache import Cache -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.live.factories import LiveDataClientFactory from nautilus_trader.live.factories import LiveExecClientFactory diff --git a/nautilus_trader/adapters/interactive_brokers/historic/client.py b/nautilus_trader/adapters/interactive_brokers/historic/client.py index 97b0af6e1e1d..2770b1ff56d9 100644 --- a/nautilus_trader/adapters/interactive_brokers/historic/client.py +++ b/nautilus_trader/adapters/interactive_brokers/historic/client.py @@ -31,7 +31,7 @@ # fmt: on from nautilus_trader.cache.cache import Cache -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import Logger from nautilus_trader.common.component import MessageBus from nautilus_trader.common.component import init_logging diff --git a/nautilus_trader/adapters/sandbox/execution.py b/nautilus_trader/adapters/sandbox/execution.py index 2add4da090eb..76a5639285ce 100644 --- a/nautilus_trader/adapters/sandbox/execution.py +++ b/nautilus_trader/adapters/sandbox/execution.py @@ -24,9 +24,9 @@ from nautilus_trader.backtest.models import FillModel from nautilus_trader.backtest.models import LatencyModel from nautilus_trader.cache.cache import Cache -from nautilus_trader.common.clock import LiveClock -from nautilus_trader.common.clock import TestClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus +from nautilus_trader.common.component import TestClock from nautilus_trader.common.providers import InstrumentProvider from nautilus_trader.core.data import Data from nautilus_trader.execution.reports import FillReport diff --git a/nautilus_trader/adapters/sandbox/factory.py b/nautilus_trader/adapters/sandbox/factory.py index 11a7ea4e19fc..83bcdd6a2a2c 100644 --- a/nautilus_trader/adapters/sandbox/factory.py +++ b/nautilus_trader/adapters/sandbox/factory.py @@ -18,7 +18,7 @@ from nautilus_trader.adapters.sandbox.config import SandboxExecutionClientConfig from nautilus_trader.adapters.sandbox.execution import SandboxExecutionClient from nautilus_trader.cache.cache import Cache -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.live.factories import LiveExecClientFactory from nautilus_trader.portfolio import PortfolioFacade diff --git a/nautilus_trader/backtest/data_client.pyx b/nautilus_trader/backtest/data_client.pyx index d698fd10f7fb..5a412366d948 100644 --- a/nautilus_trader/backtest/data_client.pyx +++ b/nautilus_trader/backtest/data_client.pyx @@ -22,7 +22,7 @@ from nautilus_trader.config.common import NautilusConfig from cpython.datetime cimport datetime from nautilus_trader.cache.cache cimport Cache -from nautilus_trader.common.clock cimport Clock +from nautilus_trader.common.component cimport Clock from nautilus_trader.common.component cimport MessageBus from nautilus_trader.core.correctness cimport Condition from nautilus_trader.core.rust.model cimport BookType diff --git a/nautilus_trader/backtest/engine.pxd b/nautilus_trader/backtest/engine.pxd index 9d921b139073..0aebade786e5 100644 --- a/nautilus_trader/backtest/engine.pxd +++ b/nautilus_trader/backtest/engine.pxd @@ -16,7 +16,7 @@ from cpython.datetime cimport datetime from libc.stdint cimport uint64_t -from nautilus_trader.common.clock cimport Clock +from nautilus_trader.common.component cimport Clock from nautilus_trader.common.component cimport Logger from nautilus_trader.core.data cimport Data from nautilus_trader.core.rust.backtest cimport TimeEventAccumulatorAPI diff --git a/nautilus_trader/backtest/engine.pyx b/nautilus_trader/backtest/engine.pyx index b8eaee7d34d2..98496f084f8b 100644 --- a/nautilus_trader/backtest/engine.pyx +++ b/nautilus_trader/backtest/engine.pyx @@ -43,11 +43,11 @@ from nautilus_trader.backtest.models cimport LatencyModel from nautilus_trader.backtest.modules cimport SimulationModule from nautilus_trader.cache.base cimport CacheFacade from nautilus_trader.common.actor cimport Actor -from nautilus_trader.common.clock cimport LiveClock -from nautilus_trader.common.clock cimport TestClock -from nautilus_trader.common.clock cimport TimeEvent -from nautilus_trader.common.clock cimport TimeEventHandler +from nautilus_trader.common.component cimport LiveClock from nautilus_trader.common.component cimport Logger +from nautilus_trader.common.component cimport TestClock +from nautilus_trader.common.component cimport TimeEvent +from nautilus_trader.common.component cimport TimeEventHandler from nautilus_trader.common.component cimport log_level_from_str from nautilus_trader.common.component cimport log_sysinfo from nautilus_trader.common.component cimport set_logging_clock_realtime_mode diff --git a/nautilus_trader/backtest/exchange.pxd b/nautilus_trader/backtest/exchange.pxd index 68e0f1bd4aa7..561376f7397e 100644 --- a/nautilus_trader/backtest/exchange.pxd +++ b/nautilus_trader/backtest/exchange.pxd @@ -21,7 +21,7 @@ from nautilus_trader.backtest.matching_engine cimport OrderMatchingEngine from nautilus_trader.backtest.models cimport FillModel from nautilus_trader.backtest.models cimport LatencyModel from nautilus_trader.cache.cache cimport Cache -from nautilus_trader.common.clock cimport Clock +from nautilus_trader.common.component cimport Clock from nautilus_trader.common.component cimport Logger from nautilus_trader.common.component cimport MessageBus from nautilus_trader.core.data cimport Data diff --git a/nautilus_trader/backtest/exchange.pyx b/nautilus_trader/backtest/exchange.pyx index f5591a817093..b76765126429 100644 --- a/nautilus_trader/backtest/exchange.pyx +++ b/nautilus_trader/backtest/exchange.pyx @@ -28,8 +28,8 @@ from nautilus_trader.backtest.models cimport FillModel from nautilus_trader.backtest.models cimport LatencyModel from nautilus_trader.backtest.modules cimport SimulationModule from nautilus_trader.cache.base cimport CacheFacade -from nautilus_trader.common.clock cimport TestClock from nautilus_trader.common.component cimport Logger +from nautilus_trader.common.component cimport TestClock from nautilus_trader.core.correctness cimport Condition from nautilus_trader.core.rust.model cimport AccountType from nautilus_trader.core.rust.model cimport BookType diff --git a/nautilus_trader/backtest/execution_client.pyx b/nautilus_trader/backtest/execution_client.pyx index 9aa3b7de9ac3..8098a69340cd 100644 --- a/nautilus_trader/backtest/execution_client.pyx +++ b/nautilus_trader/backtest/execution_client.pyx @@ -18,8 +18,8 @@ from nautilus_trader.config.common import NautilusConfig from nautilus_trader.backtest.exchange cimport SimulatedExchange from nautilus_trader.cache.cache cimport Cache -from nautilus_trader.common.clock cimport TestClock from nautilus_trader.common.component cimport MessageBus +from nautilus_trader.common.component cimport TestClock from nautilus_trader.core.correctness cimport Condition from nautilus_trader.execution.client cimport ExecutionClient from nautilus_trader.execution.messages cimport BatchCancelOrders diff --git a/nautilus_trader/backtest/matching_engine.pxd b/nautilus_trader/backtest/matching_engine.pxd index 922d6acfb4aa..c341b01afb22 100644 --- a/nautilus_trader/backtest/matching_engine.pxd +++ b/nautilus_trader/backtest/matching_engine.pxd @@ -19,7 +19,7 @@ from libc.stdint cimport uint64_t from nautilus_trader.backtest.models cimport FillModel from nautilus_trader.cache.base cimport CacheFacade -from nautilus_trader.common.clock cimport Clock +from nautilus_trader.common.component cimport Clock from nautilus_trader.common.component cimport Logger from nautilus_trader.common.component cimport MessageBus from nautilus_trader.core.data cimport Data diff --git a/nautilus_trader/backtest/matching_engine.pyx b/nautilus_trader/backtest/matching_engine.pyx index 028654599555..12b535c49681 100644 --- a/nautilus_trader/backtest/matching_engine.pyx +++ b/nautilus_trader/backtest/matching_engine.pyx @@ -22,10 +22,10 @@ from libc.stdint cimport uint64_t from nautilus_trader.backtest.models cimport FillModel from nautilus_trader.cache.base cimport CacheFacade -from nautilus_trader.common.clock cimport TestClock from nautilus_trader.common.component cimport LogColor from nautilus_trader.common.component cimport Logger from nautilus_trader.common.component cimport MessageBus +from nautilus_trader.common.component cimport TestClock from nautilus_trader.core.correctness cimport Condition from nautilus_trader.core.data cimport Data from nautilus_trader.core.rust.common cimport logging_is_initialized diff --git a/nautilus_trader/common/actor.pxd b/nautilus_trader/common/actor.pxd index 49ce6edfbbb8..d66b39bb0ff3 100644 --- a/nautilus_trader/common/actor.pxd +++ b/nautilus_trader/common/actor.pxd @@ -19,7 +19,7 @@ from cpython.datetime cimport datetime from libc.stdint cimport uint64_t from nautilus_trader.cache.base cimport CacheFacade -from nautilus_trader.common.clock cimport Clock +from nautilus_trader.common.component cimport Clock from nautilus_trader.common.component cimport Component from nautilus_trader.common.component cimport Logger from nautilus_trader.common.component cimport MessageBus diff --git a/nautilus_trader/common/actor.pyx b/nautilus_trader/common/actor.pyx index ddc7e1698e37..940112e7985f 100644 --- a/nautilus_trader/common/actor.pyx +++ b/nautilus_trader/common/actor.pyx @@ -39,12 +39,12 @@ from cpython.datetime cimport datetime from libc.stdint cimport uint64_t from nautilus_trader.cache.base cimport CacheFacade -from nautilus_trader.common.clock cimport Clock -from nautilus_trader.common.clock cimport LiveClock from nautilus_trader.common.component cimport CMD from nautilus_trader.common.component cimport REQ from nautilus_trader.common.component cimport SENT +from nautilus_trader.common.component cimport Clock from nautilus_trader.common.component cimport Component +from nautilus_trader.common.component cimport LiveClock from nautilus_trader.common.component cimport Logger from nautilus_trader.common.component cimport MessageBus from nautilus_trader.core.correctness cimport Condition diff --git a/nautilus_trader/common/clock.pxd b/nautilus_trader/common/clock.pxd deleted file mode 100644 index a6a2ec60adc7..000000000000 --- a/nautilus_trader/common/clock.pxd +++ /dev/null @@ -1,156 +0,0 @@ -# ------------------------------------------------------------------------------------------------- -# Copyright (C) 2015-2024 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 typing import Callable - -from cpython.datetime cimport datetime -from cpython.datetime cimport timedelta -from cpython.datetime cimport tzinfo -from libc.stdint cimport int64_t -from libc.stdint cimport uint64_t - -from nautilus_trader.core.message cimport Event -from nautilus_trader.core.rust.common cimport LiveClock_API -from nautilus_trader.core.rust.common cimport TestClock_API -from nautilus_trader.core.rust.common cimport TimeEvent_t -from nautilus_trader.core.rust.core cimport CVec -from nautilus_trader.core.uuid cimport UUID4 - - -cdef class Clock: - cpdef double timestamp(self) - cpdef uint64_t timestamp_ms(self) - cpdef uint64_t timestamp_ns(self) - cpdef datetime utc_now(self) - cpdef datetime local_now(self, tzinfo tz=*) - cpdef uint64_t next_time_ns(self, str name) - cpdef void register_default_handler(self, handler: Callable[[TimeEvent], None]) - cpdef void set_time_alert( - self, - str name, - datetime alert_time, - callback: Callable[[TimeEvent], None]=*, - ) - cpdef void set_time_alert_ns( - self, - str name, - uint64_t alert_time_ns, - callback: Callable[[TimeEvent], None]=*, - ) - cpdef void set_timer( - self, - str name, - timedelta interval, - datetime start_time=*, - datetime stop_time=*, - callback: Callable[[TimeEvent], None]=*, - ) - cpdef void set_timer_ns( - self, - str name, - uint64_t interval_ns, - uint64_t start_time_ns, - uint64_t stop_time_ns, - callback: Callable[[TimeEvent], None]=*, - ) - cpdef void cancel_timer(self, str name) - cpdef void cancel_timers(self) - - -cdef class TestClock(Clock): - cdef TestClock_API _mem - - cpdef void set_time(self, uint64_t to_time_ns) - cdef CVec advance_time_c(self, uint64_t to_time_ns, bint set_time=*) - cpdef list advance_time(self, uint64_t to_time_ns, bint set_time=*) - - -cdef class LiveClock(Clock): - cdef LiveClock_API _mem - cdef object _default_handler - cdef dict _handlers - - cdef object _loop - cdef int _timer_count - cdef dict _timers - cdef LiveTimer[:] _stack - cdef tzinfo _utc - cdef uint64_t _next_event_time_ns - - cpdef void _raise_time_event(self, LiveTimer timer) - - cdef void _handle_time_event(self, TimeEvent event) - cdef void _add_timer(self, LiveTimer timer, handler: Callable[[TimeEvent], None]) - cdef void _remove_timer(self, LiveTimer timer) - cdef void _update_stack(self) - cdef void _update_timing(self) - cdef LiveTimer _create_timer( - self, - str name, - callback: Callable[[TimeEvent], None], - uint64_t interval_ns, - uint64_t start_time_ns, - uint64_t stop_time_ns, - ) - - -cdef class TimeEvent(Event): - cdef TimeEvent_t _mem - - cdef str to_str(self) - - @staticmethod - cdef TimeEvent from_mem_c(TimeEvent_t raw) - - -cdef class TimeEventHandler: - cdef object _handler - cdef readonly TimeEvent event - """The handlers event.\n\n:returns: `TimeEvent`""" - - cpdef void handle(self) - - -cdef class LiveTimer: - cdef object _internal - - cdef readonly str name - """The timers name using for hashing.\n\n:returns: `str`""" - cdef readonly object callback - """The timers callback function.\n\n:returns: `object`""" - cdef readonly uint64_t interval_ns - """The timers set interval.\n\n:returns: `uint64_t`""" - cdef readonly uint64_t start_time_ns - """The timers set start time.\n\n:returns: `uint64_t`""" - cdef readonly uint64_t next_time_ns - """The timers next alert timestamp.\n\n:returns: `uint64_t`""" - cdef readonly uint64_t stop_time_ns - """The timers set stop time (if set).\n\n:returns: `uint64_t`""" - cdef readonly bint is_expired - """If the timer is expired.\n\n:returns: `bool`""" - - cpdef TimeEvent pop_event(self, UUID4 event_id, uint64_t ts_init) - cpdef void iterate_next_time(self, uint64_t to_time_ns) - cpdef void cancel(self) - cpdef void repeat(self, uint64_t ts_now) - cdef object _start_timer(self, uint64_t ts_now) - - -cdef class ThreadTimer(LiveTimer): - pass - - -cdef class LoopTimer(LiveTimer): - cdef object _loop diff --git a/nautilus_trader/common/clock.pyx b/nautilus_trader/common/clock.pyx deleted file mode 100644 index e03a35a10da1..000000000000 --- a/nautilus_trader/common/clock.pyx +++ /dev/null @@ -1,1228 +0,0 @@ -# ------------------------------------------------------------------------------------------------- -# Copyright (C) 2015-2024 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 asyncio -from threading import Timer as TimerThread -from typing import Callable - -import cython -import numpy as np -import pandas as pd -import pytz - -from cpython.datetime cimport datetime -from cpython.datetime cimport timedelta -from cpython.datetime cimport tzinfo -from cpython.object cimport PyCallable_Check -from cpython.object cimport PyObject -from libc.stdint cimport uint64_t -from libc.stdio cimport printf - -from nautilus_trader.core.correctness cimport Condition -from nautilus_trader.core.datetime cimport dt_to_unix_nanos -from nautilus_trader.core.datetime cimport maybe_dt_to_unix_nanos -from nautilus_trader.core.message cimport Event -from nautilus_trader.core.rust.common cimport TimeEventHandler_t -from nautilus_trader.core.rust.common cimport live_clock_drop -from nautilus_trader.core.rust.common cimport live_clock_new -from nautilus_trader.core.rust.common cimport live_clock_timestamp -from nautilus_trader.core.rust.common cimport live_clock_timestamp_ms -from nautilus_trader.core.rust.common cimport live_clock_timestamp_ns -from nautilus_trader.core.rust.common cimport live_clock_timestamp_us -from nautilus_trader.core.rust.common cimport test_clock_advance_time -from nautilus_trader.core.rust.common cimport test_clock_cancel_timer -from nautilus_trader.core.rust.common cimport test_clock_cancel_timers -from nautilus_trader.core.rust.common cimport test_clock_drop -from nautilus_trader.core.rust.common cimport test_clock_new -from nautilus_trader.core.rust.common cimport test_clock_next_time_ns -from nautilus_trader.core.rust.common cimport test_clock_register_default_handler -from nautilus_trader.core.rust.common cimport test_clock_set_time -from nautilus_trader.core.rust.common cimport test_clock_set_time_alert_ns -from nautilus_trader.core.rust.common cimport test_clock_set_timer_ns -from nautilus_trader.core.rust.common cimport test_clock_timer_count -from nautilus_trader.core.rust.common cimport test_clock_timer_names -from nautilus_trader.core.rust.common cimport test_clock_timestamp -from nautilus_trader.core.rust.common cimport test_clock_timestamp_ms -from nautilus_trader.core.rust.common cimport test_clock_timestamp_ns -from nautilus_trader.core.rust.common cimport test_clock_timestamp_us -from nautilus_trader.core.rust.common cimport time_event_new -from nautilus_trader.core.rust.common cimport time_event_to_cstr -from nautilus_trader.core.rust.common cimport vec_time_event_handlers_drop -from nautilus_trader.core.rust.core cimport CVec -from nautilus_trader.core.rust.core cimport nanos_to_millis -from nautilus_trader.core.rust.core cimport nanos_to_secs -from nautilus_trader.core.rust.core cimport uuid4_from_cstr -from nautilus_trader.core.string cimport cstr_to_pystr -from nautilus_trader.core.string cimport pystr_to_cstr -from nautilus_trader.core.string cimport ustr_to_pystr -from nautilus_trader.core.uuid cimport UUID4 - - -cdef class Clock: - """ - The base class for all clocks. - - Notes - ----- - An *active* timer is one which has not expired. - - Warnings - -------- - This class should not be used directly, but through a concrete subclass. - """ - - @property - def timer_names(self) -> list[str]: - """ - Return the names of *active* timers running in the clock. - - Returns - ------- - list[str] - - """ - raise NotImplementedError("method `timer_names` must be implemented in the subclass") # pragma: no cover - - @property - def timer_count(self) -> int: - """ - Return the count of *active* timers running in the clock. - - Returns - ------- - int - - """ - raise NotImplementedError("method `timer_count` must be implemented in the subclass") # pragma: no cover - - cpdef double timestamp(self): - """ - Return the current UNIX time in seconds. - - Returns - ------- - double - - References - ---------- - https://en.wikipedia.org/wiki/Unix_time - - """ - raise NotImplementedError("method `timestamp` must be implemented in the subclass") # pragma: no cover - - cpdef uint64_t timestamp_ms(self): - """ - Return the current UNIX time in milliseconds (ms). - - Returns - ------- - uint64_t - - References - ---------- - https://en.wikipedia.org/wiki/Unix_time - - """ - raise NotImplementedError("method `timestamp_ms` must be implemented in the subclass") # pragma: no cover - - cpdef uint64_t timestamp_ns(self): - """ - Return the current UNIX time in nanoseconds (ns). - - Returns - ------- - uint64_t - - References - ---------- - https://en.wikipedia.org/wiki/Unix_time - - """ - raise NotImplementedError("method `timestamp_ns` must be implemented in the subclass") # pragma: no cover - - cpdef datetime utc_now(self): - """ - Return the current time (UTC). - - Returns - ------- - datetime - The current tz-aware UTC time of the clock. - - """ - return pd.Timestamp(self.timestamp_ns(), tz=pytz.utc) - - cpdef datetime local_now(self, tzinfo tz = None): - """ - Return the current datetime of the clock in the given local timezone. - - Parameters - ---------- - tz : tzinfo, optional - The local timezone (if None the system local timezone is assumed for - the target timezone). - - Returns - ------- - datetime - tz-aware in local timezone. - - """ - return self.utc_now().astimezone(tz) - - cpdef void register_default_handler(self, handler: Callable[[TimeEvent], None]): - """ - Register the given handler as the clocks default handler. - - handler : Callable[[TimeEvent], None] - The handler to register. - - Raises - ------ - TypeError - If `handler` is not of type `Callable`. - - """ - raise NotImplementedError("method `register_default_handler` must be implemented in the subclass") # pragma: no cover - - cpdef uint64_t next_time_ns(self, str name): - """ - Find a particular timer. - - Parameters - ---------- - name : str - The name of the timer. - - Returns - ------- - uint64_t - - Raises - ------ - ValueError - If `name` is not a valid string. - - """ - raise NotImplementedError("method `next_time_ns` must be implemented in the subclass") # pragma: no cover - - cpdef void set_time_alert( - self, - str name, - datetime alert_time, - callback: Callable[[TimeEvent], None] = None, - ): - """ - Set a time alert for the given time. - - When the time is reached the handler will be passed the `TimeEvent` - containing the timers unique name. If no handler is passed then the - default handler (if registered) will receive the `TimeEvent`. - - Parameters - ---------- - name : str - The name for the alert (must be unique for this clock). - alert_time : datetime - The time for the alert. - callback : Callable[[TimeEvent], None], optional - The callback to receive time events. - - Raises - ------ - ValueError - If `name` is not a valid string. - KeyError - If `name` is not unique for this clock. - TypeError - If `handler` is not of type `Callable` or ``None``. - ValueError - If `handler` is ``None`` and no default handler is registered. - - Warnings - -------- - If `alert_time` is in the past or at current time, then an immediate - time event will be generated (rather than being invalid and failing a condition check). - - """ - self.set_time_alert_ns( - name=name, - alert_time_ns=dt_to_unix_nanos(alert_time), - callback=callback, - ) - - cpdef void set_time_alert_ns( - self, - str name, - uint64_t alert_time_ns, - callback: Callable[[TimeEvent], None] = None, - ): - """ - Set a time alert for the given time. - - When the time is reached the handler will be passed the `TimeEvent` - containing the timers unique name. If no callback is passed then the - default handler (if registered) will receive the `TimeEvent`. - - Parameters - ---------- - name : str - The name for the alert (must be unique for this clock). - alert_time_ns : uint64_t - The UNIX time (nanoseconds) for the alert. - callback : Callable[[TimeEvent], None], optional - The callback to receive time events. - - Raises - ------ - ValueError - If `name` is not a valid string. - ValueError - If `name` is not unique for this clock. - TypeError - If `callback` is not of type `Callable` or ``None``. - ValueError - If `callback` is ``None`` and no default handler is registered. - - Warnings - -------- - If `alert_time_ns` is in the past or at current time, then an immediate - time event will be generated (rather than being invalid and failing a condition check). - - """ - raise NotImplementedError("method `set_time_alert_ns` must be implemented in the subclass") # pragma: no cover - - cpdef void set_timer( - self, - str name, - timedelta interval, - datetime start_time = None, - datetime stop_time = None, - callback: Callable[[TimeEvent], None] | None = None, - ): - """ - Set a timer to run. - - The timer will run from the start time (optionally until the stop time). - When the intervals are reached the handlers will be passed the - `TimeEvent` containing the timers unique name. If no handler is passed - then the default handler (if registered) will receive the `TimeEvent`. - - Parameters - ---------- - name : str - The name for the timer (must be unique for this clock). - interval : timedelta - The time interval for the timer. - start_time : datetime, optional - The start time for the timer (if None then starts immediately). - stop_time : datetime, optional - The stop time for the timer (if None then repeats indefinitely). - callback : Callable[[TimeEvent], None], optional - The callback to receive time events. - - Raises - ------ - ValueError - If `name` is not a valid string. - KeyError - If `name` is not unique for this clock. - ValueError - If `interval` is not positive (> 0). - ValueError - If `stop_time` is not ``None`` and `stop_time` < time now. - ValueError - If `stop_time` is not ``None`` and `start_time` + `interval` > `stop_time`. - TypeError - If `handler` is not of type `Callable` or ``None``. - ValueError - If `handler` is ``None`` and no default handler is registered. - - """ - self.set_timer_ns( - name=name, - interval_ns=pd.Timedelta(interval).value, - start_time_ns=maybe_dt_to_unix_nanos(start_time) or 0, - stop_time_ns=maybe_dt_to_unix_nanos(stop_time) or 0, - callback=callback, - ) - - cpdef void set_timer_ns( - self, - str name, - uint64_t interval_ns, - uint64_t start_time_ns, - uint64_t stop_time_ns, - callback: Callable[[TimeEvent], None] | None = None, - ): - """ - Set a timer to run. - - The timer will run from the start time until the stop time. - When the intervals are reached the handlers will be passed the - `TimeEvent` containing the timers unique name. If no handler is passed - then the default handler (if registered) will receive the `TimeEvent`. - - Parameters - ---------- - name : str - The name for the timer (must be unique for this clock). - interval_ns : uint64_t - The time interval (nanoseconds) for the timer. - start_time_ns : uint64_t - The start UNIX time (nanoseconds) for the timer. - stop_time_ns : uint64_t - The stop UNIX time (nanoseconds) for the timer. - callback : Callable[[TimeEvent], None], optional - The callback to receive time events. - - Raises - ------ - ValueError - If `name` is not a valid string. - KeyError - If `name` is not unique for this clock. - ValueError - If `interval` is not positive (> 0). - ValueError - If `stop_time` is not ``None`` and `stop_time` < time now. - ValueError - If `stop_time` is not ``None`` and `start_time` + interval > `stop_time`. - TypeError - If `callback` is not of type `Callable` or ``None``. - ValueError - If `callback` is ``None`` and no default handler is registered. - - """ - raise NotImplementedError("method `set_timer_ns` must be implemented in the subclass") # pragma: no cover - - cpdef void cancel_timer(self, str name): - """ - Cancel the timer corresponding to the given label. - - Parameters - ---------- - name : str - The name for the timer to cancel. - - Raises - ------ - ValueError - If `name` is not a valid string. - KeyError - If `name` is not an active timer name for this clock. - - """ - raise NotImplementedError("method `cancel_timer` must be implemented in the subclass") # pragma: no cover - - cpdef void cancel_timers(self): - """ - Cancel all timers. - """ - raise NotImplementedError("method `cancel_timers` must be implemented in the subclass") # pragma: no cover - - -cdef class TestClock(Clock): - """ - Provides a monotonic clock for backtesting and unit testing. - - """ - - __test__ = False # Required so pytest does not consider this a test class - - def __init__(self): - self._mem = test_clock_new() - - def __del__(self) -> None: - if self._mem._0 != NULL: - test_clock_drop(self._mem) - - @property - def timer_names(self) -> list[str]: - return sorted(test_clock_timer_names(&self._mem)) - - @property - def timer_count(self) -> int: - return test_clock_timer_count(&self._mem) - - cpdef double timestamp(self): - return test_clock_timestamp(&self._mem) - - cpdef uint64_t timestamp_ms(self): - return test_clock_timestamp_ms(&self._mem) - - cpdef uint64_t timestamp_ns(self): - return test_clock_timestamp_ns(&self._mem) - - cpdef void register_default_handler(self, callback: Callable[[TimeEvent], None]): - Condition.callable(callback, "callback") - - test_clock_register_default_handler(&self._mem, callback) - - cpdef void set_time_alert_ns( - self, - str name, - uint64_t alert_time_ns, - callback: Callable[[TimeEvent], None] | None = None, - ): - Condition.valid_string(name, "name") - Condition.not_in(name, self.timer_names, "name", "self.timer_names") - - test_clock_set_time_alert_ns( - &self._mem, - pystr_to_cstr(name), - alert_time_ns, - callback, - ) - - cpdef void set_timer_ns( - self, - str name, - uint64_t interval_ns, - uint64_t start_time_ns, - uint64_t stop_time_ns, - callback: Callable[[TimeEvent], None] | None = None, - ): - Condition.valid_string(name, "name") - Condition.not_in(name, self.timer_names, "name", "self.timer_names") - Condition.positive_int(interval_ns, "interval_ns") - - cdef uint64_t ts_now = self.timestamp_ns() - - if start_time_ns == 0: - start_time_ns = ts_now - if stop_time_ns: - Condition.true(stop_time_ns > ts_now, "`stop_time_ns` was < `ts_now`") - Condition.true(start_time_ns + interval_ns <= stop_time_ns, "`start_time_ns` + `interval_ns` was > `stop_time_ns`") - - test_clock_set_timer_ns( - &self._mem, - pystr_to_cstr(name), - interval_ns, - start_time_ns, - stop_time_ns, - callback, - ) - - cpdef uint64_t next_time_ns(self, str name): - Condition.valid_string(name, "name") - return test_clock_next_time_ns(&self._mem, pystr_to_cstr(name)) - - cpdef void cancel_timer(self, str name): - Condition.valid_string(name, "name") - Condition.is_in(name, self.timer_names, "name", "self.timer_names") - - test_clock_cancel_timer(&self._mem, pystr_to_cstr(name)) - - cpdef void cancel_timers(self): - test_clock_cancel_timers(&self._mem) - - cpdef void set_time(self, uint64_t to_time_ns): - """ - Set the clocks datetime to the given time (UTC). - - Parameters - ---------- - to_time_ns : uint64_t - The UNIX time (nanoseconds) to set. - - """ - test_clock_set_time(&self._mem, to_time_ns) - - cdef CVec advance_time_c(self, uint64_t to_time_ns, bint set_time=True): - Condition.true(to_time_ns >= test_clock_timestamp_ns(&self._mem), "to_time_ns was < time_ns (not monotonic)") - - return test_clock_advance_time(&self._mem, to_time_ns, set_time) - - cpdef list advance_time(self, uint64_t to_time_ns, bint set_time=True): - """ - Advance the clocks time to the given `to_time_ns`. - - Parameters - ---------- - to_time_ns : uint64_t - The UNIX time (nanoseconds) to advance the clock to. - set_time : bool - If the clock should also be set to the given `to_time_ns`. - - Returns - ------- - list[TimeEventHandler] - Sorted chronologically. - - Raises - ------ - ValueError - If `to_time_ns` is < the clocks current time. - - """ - cdef CVec raw_handler_vec = self.advance_time_c(to_time_ns, set_time) - cdef TimeEventHandler_t* raw_handlers = raw_handler_vec.ptr - cdef list event_handlers = [] - - cdef: - uint64_t i - object callback - TimeEvent event - TimeEventHandler_t raw_handler - TimeEventHandler event_handler - for i in range(raw_handler_vec.len): - raw_handler = raw_handlers[i] - event = TimeEvent.from_mem_c(raw_handler.event) - - # Cast raw `PyObject *` to a `PyObject` - callback = raw_handler.callback_ptr - - event_handler = TimeEventHandler(event, callback) - event_handlers.append(event_handler) - - vec_time_event_handlers_drop(raw_handler_vec) - - return event_handlers - - -cdef class LiveClock(Clock): - """ - Provides a monotonic clock for live trading. - - All times are tz-aware UTC. - - Parameters - ---------- - loop : asyncio.AbstractEventLoop - The event loop for the clocks timers. - """ - - def __init__(self, loop: asyncio.AbstractEventLoop | None = None): - self._mem = live_clock_new() - self._default_handler = None - self._handlers: dict[str, Callable[[TimeEvent], None]] = {} - - self._loop = loop - self._timers: dict[str, LiveTimer] = {} - self._stack = np.ascontiguousarray([], dtype=LiveTimer) - - self._timer_count = 0 - self._next_event_time_ns = 0 - - def __del__(self) -> None: - if self._mem._0 != NULL: - live_clock_drop(self._mem) - - @property - def timer_names(self) -> list[str]: - return list(self._timers.keys()) - - @property - def timer_count(self) -> int: - return self._timer_count - - cpdef double timestamp(self): - return live_clock_timestamp(&self._mem) - - cpdef uint64_t timestamp_ms(self): - return live_clock_timestamp_ms(&self._mem) - - cpdef uint64_t timestamp_ns(self): - return live_clock_timestamp_ns(&self._mem) - - cpdef void register_default_handler(self, callback: Callable[[TimeEvent], None]): - Condition.callable(callback, "callback") - - self._default_handler = callback - - cpdef void set_time_alert_ns( - self, - str name, - uint64_t alert_time_ns, - callback: Callable[[TimeEvent], None] | None = None, - ): - Condition.valid_string(name, "name") - Condition.not_in(name, self.timer_names, "name", "self.timer_names") - if callback is None: - callback = self._default_handler - - cdef uint64_t ts_now = self.timestamp_ns() - - cdef LiveTimer timer = self._create_timer( - name=name, - callback=callback, - interval_ns=alert_time_ns - ts_now, - start_time_ns=ts_now, - stop_time_ns=alert_time_ns, - ) - self._add_timer(timer, callback) - - cpdef void set_timer_ns( - self, - str name, - uint64_t interval_ns, - uint64_t start_time_ns, - uint64_t stop_time_ns, - callback: Callable[[TimeEvent], None] | None = None, - ): - Condition.not_in(name, self.timer_names, "name", "self.timer_names") - - cdef uint64_t ts_now = self.timestamp_ns() # Call here for greater accuracy - - Condition.valid_string(name, "name") - if callback is None: - callback = self._default_handler - - Condition.not_in(name, self._timers, "name", "_timers") - Condition.not_in(name, self._handlers, "name", "_handlers") - Condition.true(interval_ns > 0, f"interval was {interval_ns}") - Condition.callable(callback, "callback") - - if start_time_ns == 0: - start_time_ns = ts_now - if stop_time_ns: - Condition.true(stop_time_ns > ts_now, "stop_time was < ts_now") - Condition.true(start_time_ns + interval_ns <= stop_time_ns, "start_time + interval was > stop_time") - - cdef LiveTimer timer = self._create_timer( - name=name, - callback=callback, - interval_ns=interval_ns, - start_time_ns=start_time_ns, - stop_time_ns=stop_time_ns, - ) - self._add_timer(timer, callback) - - cdef void _add_timer(self, LiveTimer timer, handler: Callable[[TimeEvent], None]): - self._timers[timer.name] = timer - self._handlers[timer.name] = handler - self._update_stack() - self._update_timing() - - cdef void _remove_timer(self, LiveTimer timer): - self._timers.pop(timer.name, None) - self._handlers.pop(timer.name, None) - self._update_stack() - self._update_timing() - - cdef void _update_stack(self): - self._timer_count = len(self._timers) - - if self._timer_count > 0: - # The call to `np.ascontiguousarray` here looks inefficient, its - # only called when a timer is added or removed. This then allows the - # construction of an efficient Timer[:] memoryview. - timers = list(self._timers.values()) - self._stack = np.ascontiguousarray(timers, dtype=LiveTimer) - else: - self._stack = None - - cpdef uint64_t next_time_ns(self, str name): - return self._timers[name].next_time_ns - - cpdef void cancel_timer(self, str name): - Condition.valid_string(name, "name") - Condition.is_in(name, self.timer_names, "name", "self.timer_names") - - cdef LiveTimer timer = self._timers.pop(name, None) - if not timer: - # No timer with given name - return - - timer.cancel() - self._handlers.pop(name, None) - self._remove_timer(timer) - - cpdef void cancel_timers(self): - cdef str name - for name in self.timer_names: - # Using a list of timer names from the property and passing this - # to cancel_timer() handles the clean removal of both the handler - # and timer. - self.cancel_timer(name) - - @cython.boundscheck(False) - @cython.wraparound(False) - cdef void _update_timing(self): - if self._timer_count == 0: - self._next_event_time_ns = 0 - return - - cdef LiveTimer first_timer = self._stack[0] - if self._timer_count == 1: - self._next_event_time_ns = first_timer.next_time_ns - return - - cdef uint64_t next_time_ns = first_timer.next_time_ns - cdef: - int i - LiveTimer timer - uint64_t observed_ns - for i in range(self._timer_count - 1): - timer = self._stack[i + 1] - observed_ns = timer.next_time_ns - if observed_ns < next_time_ns: - next_time_ns = observed_ns - - self._next_event_time_ns = next_time_ns - - cdef LiveTimer _create_timer( - self, - str name, - callback: Callable[[TimeEvent], None], - uint64_t interval_ns, - uint64_t start_time_ns, - uint64_t stop_time_ns, - ): - if self._loop is not None: - return LoopTimer( - loop=self._loop, - name=name, - callback=self._raise_time_event, - interval_ns=interval_ns, - ts_now=self.timestamp_ns(), # Timestamp here for accuracy - start_time_ns=start_time_ns, - stop_time_ns=stop_time_ns, - ) - else: - return ThreadTimer( - name=name, - callback=self._raise_time_event, - interval_ns=interval_ns, - ts_now=self.timestamp_ns(), # Timestamp here for accuracy - start_time_ns=start_time_ns, - stop_time_ns=stop_time_ns, - ) - - cpdef void _raise_time_event(self, LiveTimer timer): - cdef uint64_t now = self.timestamp_ns() - cdef TimeEvent event = timer.pop_event( - event_id=UUID4(), - ts_init=now, - ) - - if now < timer.next_time_ns: - timer.iterate_next_time(timer.next_time_ns) - else: - timer.iterate_next_time(now) - - self._handle_time_event(event) - - if timer.is_expired: - self._remove_timer(timer) - else: # Continue timing - timer.repeat(ts_now=self.timestamp_ns()) - self._update_timing() - - cdef void _handle_time_event(self, TimeEvent event): - handler = self._handlers.get(event.name) - if handler is not None: - handler(event) - - -cdef class TimeEvent(Event): - """ - Represents a time event occurring at the event timestamp. - - Parameters - ---------- - name : str - The event name. - event_id : UUID4 - The event ID. - ts_event : uint64_t - The UNIX timestamp (nanoseconds) when the time event occurred. - ts_init : uint64_t - The UNIX timestamp (nanoseconds) when the object was initialized. - """ - - def __init__( - self, - str name not None, - UUID4 event_id not None, - uint64_t ts_event, - uint64_t ts_init, - ): - # Precondition: `name` validated in Rust - self._mem = time_event_new( - pystr_to_cstr(name), - event_id._mem, - ts_event, - ts_init, - ) - - def __getstate__(self): - return ( - self.to_str(), - self.id.value, - self.ts_event, - self.ts_init, - ) - - def __setstate__(self, state): - self._mem = time_event_new( - pystr_to_cstr(state[0]), - uuid4_from_cstr(pystr_to_cstr(state[1])), - self.ts_event, - self.ts_init, - ) - - cdef str to_str(self): - return ustr_to_pystr(self._mem.name) - - def __eq__(self, TimeEvent other) -> bool: - return self.to_str() == other.to_str() - - def __hash__(self) -> int: - return hash(self.to_str()) - - def __str__(self) -> str: - return self.to_str() - - def __repr__(self) -> str: - return cstr_to_pystr(time_event_to_cstr(&self._mem)) - - @property - def name(self) -> str: - """ - Return the name of the time event. - - Returns - ------- - str - - """ - return ustr_to_pystr(self._mem.name) - - @property - def id(self) -> UUID4: - """ - The event message identifier. - - Returns - ------- - UUID4 - - """ - cdef UUID4 uuid4 = UUID4.__new__(UUID4) - uuid4._mem = self._mem.event_id - return uuid4 - - @property - def ts_event(self) -> int: - """ - The UNIX timestamp (nanoseconds) when the event occurred. - - Returns - ------- - int - - """ - return self._mem.ts_event - - @property - def ts_init(self) -> int: - """ - The UNIX timestamp (nanoseconds) when the object was initialized. - - Returns - ------- - int - - """ - return self._mem.ts_init - - @staticmethod - cdef TimeEvent from_mem_c(TimeEvent_t mem): - cdef TimeEvent event = TimeEvent.__new__(TimeEvent) - event._mem = mem - return event - - -cdef class TimeEventHandler: - """ - Represents a time event with its associated handler. - """ - - def __init__( - self, - TimeEvent event not None, - handler not None: Callable[[TimeEvent], None], - ): - self.event = event - self._handler = handler - - cpdef void handle(self): - """Call the handler with the contained time event.""" - self._handler(self.event) - - def __eq__(self, TimeEventHandler other) -> bool: - return self.event.ts_event == other.event.ts_event - - def __lt__(self, TimeEventHandler other) -> bool: - return self.event.ts_event < other.event.ts_event - - def __le__(self, TimeEventHandler other) -> bool: - return self.event.ts_event <= other.event.ts_event - - def __gt__(self, TimeEventHandler other) -> bool: - return self.event.ts_event > other.event.ts_event - - def __ge__(self, TimeEventHandler other) -> bool: - return self.event.ts_event >= other.event.ts_event - - def __repr__(self) -> str: - return ( - f"{type(self).__name__}(" - f"event={repr(self.event)})" - ) - - -cdef class LiveTimer: - """ - The base class for all live timers. - - Parameters - ---------- - name : str - The name for the timer. - callback : Callable[[TimeEvent], None] - The delegate to call at the next time. - interval_ns : uint64_t - The time interval for the timer. - ts_now : uint64_t - The current UNIX time (nanoseconds). - start_time_ns : uint64_t - The start datetime for the timer (UTC). - stop_time_ns : uint64_t, optional - The stop datetime for the timer (UTC) (if None then timer repeats). - - Raises - ------ - TypeError - If `callback` is not of type `Callable`. - - Warnings - -------- - This class should not be used directly, but through a concrete subclass. - """ - - def __init__( - self, - str name not None, - callback not None: Callable[[TimeEvent], None], - uint64_t interval_ns, - uint64_t ts_now, - uint64_t start_time_ns, - uint64_t stop_time_ns=0, - ): - Condition.valid_string(name, "name") - Condition.callable(callback, "callback") - - self.name = name - self.callback = callback - self.interval_ns = interval_ns - self.start_time_ns = start_time_ns - self.next_time_ns = start_time_ns + interval_ns - self.stop_time_ns = stop_time_ns - self.is_expired = False - - self._internal = self._start_timer(ts_now) - - def __eq__(self, LiveTimer other) -> bool: - return self.name == other.name - - def __hash__(self) -> int: - return hash(self.name) - - def __repr__(self) -> str: - return ( - f"{type(self).__name__}(" - f"name={self.name}, " - f"interval_ns={self.interval_ns}, " - f"start_time_ns={self.start_time_ns}, " - f"next_time_ns={self.next_time_ns}, " - f"stop_time_ns={self.stop_time_ns}, " - f"is_expired={self.is_expired})" - ) - - cpdef TimeEvent pop_event(self, UUID4 event_id, uint64_t ts_init): - """ - Return a generated time event with the given ID. - - Parameters - ---------- - event_id : UUID4 - The ID for the time event. - ts_init : uint64_t - The UNIX timestamp (nanoseconds) when the object was initialized. - - Returns - ------- - TimeEvent - - """ - # Precondition: `event_id` validated in `TimeEvent` - - return TimeEvent( - name=self.name, - event_id=event_id, - ts_event=self.next_time_ns, - ts_init=ts_init, - ) - - cpdef void iterate_next_time(self, uint64_t ts_now): - """ - Iterates the timers next time and checks if the timer is now expired. - - Parameters - ---------- - ts_now : uint64_t - The current UNIX time (nanoseconds). - - """ - self.next_time_ns += self.interval_ns - if self.stop_time_ns and ts_now >= self.stop_time_ns: - self.is_expired = True - - cpdef void repeat(self, uint64_t ts_now): - """ - Continue the timer. - - Parameters - ---------- - ts_now : uint64_t - The current time to continue timing from. - - """ - self._internal = self._start_timer(ts_now) - - cpdef void cancel(self): - """ - Cancels the timer (the timer will not generate an event). - """ - self._internal.cancel() - - cdef object _start_timer(self, uint64_t ts_now): - """Abstract method (implement in subclass).""" - raise NotImplementedError("method `_start_timer` must be implemented in the subclass") # pragma: no cover - - -cdef class ThreadTimer(LiveTimer): - """ - Provides a thread based timer for live trading. - - Parameters - ---------- - name : str - The name for the timer. - callback : Callable[[TimeEvent], None] - The delegate to call at the next time. - interval_ns : uint64_t - The time interval for the timer. - ts_now : uint64_t - The current UNIX time (nanoseconds). - start_time_ns : uint64_t - The start datetime for the timer (UTC). - stop_time_ns : uint64_t, optional - The stop datetime for the timer (UTC) (if None then timer repeats). - - Raises - ------ - TypeError - If `callback` is not of type `Callable`. - """ - - def __init__( - self, - str name not None, - callback not None: Callable[[TimeEvent], None], - uint64_t interval_ns, - uint64_t ts_now, - uint64_t start_time_ns, - uint64_t stop_time_ns=0, - ): - super().__init__( - name=name, - callback=callback, - interval_ns=interval_ns, - ts_now=ts_now, - start_time_ns=start_time_ns, - stop_time_ns=stop_time_ns, - ) - - cdef object _start_timer(self, uint64_t ts_now): - timer = TimerThread( - interval=nanos_to_secs(self.next_time_ns - ts_now), - function=self.callback, - args=[self], - ) - timer.daemon = True - timer.start() - - return timer - - -cdef class LoopTimer(LiveTimer): - """ - Provides an event loop based timer for live trading. - - Parameters - ---------- - loop : asyncio.AbstractEventLoop - The event loop to run the timer on. - name : str - The name for the timer. - callback : Callable[[TimeEvent], None] - The delegate to call at the next time. - interval_ns : uint64_t - The time interval for the timer (nanoseconds). - ts_now : uint64_t - The current UNIX epoch (nanoseconds). - start_time_ns : uint64_t - The start datetime for the timer (UTC). - stop_time_ns : uint64_t, optional - The stop datetime for the timer (UTC) (if None then timer repeats). - - Raises - ------ - TypeError - If `callback` is not of type `Callable`. - """ - - def __init__( - self, - loop not None, - str name not None, - callback not None: Callable[[TimeEvent], None], - uint64_t interval_ns, - uint64_t ts_now, - uint64_t start_time_ns, - uint64_t stop_time_ns=0, - ): - Condition.valid_string(name, "name") - - self._loop = loop # Assign here as `super().__init__` will call it - super().__init__( - name=name, - callback=callback, - interval_ns=interval_ns, - ts_now=ts_now, - start_time_ns=start_time_ns, - stop_time_ns=stop_time_ns, - ) - - cdef object _start_timer(self, uint64_t ts_now): - return self._loop.call_later( - nanos_to_secs(self.next_time_ns - ts_now), - self.callback, - self, - ) diff --git a/nautilus_trader/common/component.pxd b/nautilus_trader/common/component.pxd index 2c43c81edcd9..3b3dba8d954f 100644 --- a/nautilus_trader/common/component.pxd +++ b/nautilus_trader/common/component.pxd @@ -15,28 +15,162 @@ from typing import Callable +from cpython.datetime cimport datetime from cpython.datetime cimport timedelta +from cpython.datetime cimport tzinfo from libc.stdint cimport int64_t from libc.stdint cimport uint64_t -from nautilus_trader.common.clock cimport Clock -from nautilus_trader.common.clock cimport TimeEvent +from nautilus_trader.common.component cimport Clock from nautilus_trader.common.component cimport Logger from nautilus_trader.common.component cimport MessageBus +from nautilus_trader.common.component cimport TimeEvent from nautilus_trader.core.fsm cimport FiniteStateMachine +from nautilus_trader.core.message cimport Event from nautilus_trader.core.message cimport Request from nautilus_trader.core.message cimport Response from nautilus_trader.core.rust.common cimport ComponentState from nautilus_trader.core.rust.common cimport ComponentTrigger +from nautilus_trader.core.rust.common cimport LiveClock_API from nautilus_trader.core.rust.common cimport LogColor from nautilus_trader.core.rust.common cimport LogLevel from nautilus_trader.core.rust.common cimport MessageBus_API +from nautilus_trader.core.rust.common cimport TestClock_API +from nautilus_trader.core.rust.common cimport TimeEvent_t +from nautilus_trader.core.rust.core cimport CVec from nautilus_trader.core.uuid cimport UUID4 from nautilus_trader.model.identifiers cimport Identifier from nautilus_trader.model.identifiers cimport TraderId from nautilus_trader.serialization.base cimport Serializer +cdef class Clock: + cpdef double timestamp(self) + cpdef uint64_t timestamp_ms(self) + cpdef uint64_t timestamp_ns(self) + cpdef datetime utc_now(self) + cpdef datetime local_now(self, tzinfo tz=*) + cpdef uint64_t next_time_ns(self, str name) + cpdef void register_default_handler(self, handler: Callable[[TimeEvent], None]) + cpdef void set_time_alert( + self, + str name, + datetime alert_time, + callback: Callable[[TimeEvent], None]=*, + ) + cpdef void set_time_alert_ns( + self, + str name, + uint64_t alert_time_ns, + callback: Callable[[TimeEvent], None]=*, + ) + cpdef void set_timer( + self, + str name, + timedelta interval, + datetime start_time=*, + datetime stop_time=*, + callback: Callable[[TimeEvent], None]=*, + ) + cpdef void set_timer_ns( + self, + str name, + uint64_t interval_ns, + uint64_t start_time_ns, + uint64_t stop_time_ns, + callback: Callable[[TimeEvent], None]=*, + ) + cpdef void cancel_timer(self, str name) + cpdef void cancel_timers(self) + + +cdef class TestClock(Clock): + cdef TestClock_API _mem + + cpdef void set_time(self, uint64_t to_time_ns) + cdef CVec advance_time_c(self, uint64_t to_time_ns, bint set_time=*) + cpdef list advance_time(self, uint64_t to_time_ns, bint set_time=*) + + +cdef class LiveClock(Clock): + cdef LiveClock_API _mem + cdef object _default_handler + cdef dict _handlers + + cdef object _loop + cdef int _timer_count + cdef dict _timers + cdef LiveTimer[:] _stack + cdef tzinfo _utc + cdef uint64_t _next_event_time_ns + + cpdef void _raise_time_event(self, LiveTimer timer) + + cdef void _handle_time_event(self, TimeEvent event) + cdef void _add_timer(self, LiveTimer timer, handler: Callable[[TimeEvent], None]) + cdef void _remove_timer(self, LiveTimer timer) + cdef void _update_stack(self) + cdef void _update_timing(self) + cdef LiveTimer _create_timer( + self, + str name, + callback: Callable[[TimeEvent], None], + uint64_t interval_ns, + uint64_t start_time_ns, + uint64_t stop_time_ns, + ) + + +cdef class TimeEvent(Event): + cdef TimeEvent_t _mem + + cdef str to_str(self) + + @staticmethod + cdef TimeEvent from_mem_c(TimeEvent_t raw) + + +cdef class TimeEventHandler: + cdef object _handler + cdef readonly TimeEvent event + """The handlers event.\n\n:returns: `TimeEvent`""" + + cpdef void handle(self) + + +cdef class LiveTimer: + cdef object _internal + + cdef readonly str name + """The timers name using for hashing.\n\n:returns: `str`""" + cdef readonly object callback + """The timers callback function.\n\n:returns: `object`""" + cdef readonly uint64_t interval_ns + """The timers set interval.\n\n:returns: `uint64_t`""" + cdef readonly uint64_t start_time_ns + """The timers set start time.\n\n:returns: `uint64_t`""" + cdef readonly uint64_t next_time_ns + """The timers next alert timestamp.\n\n:returns: `uint64_t`""" + cdef readonly uint64_t stop_time_ns + """The timers set stop time (if set).\n\n:returns: `uint64_t`""" + cdef readonly bint is_expired + """If the timer is expired.\n\n:returns: `bool`""" + + cpdef TimeEvent pop_event(self, UUID4 event_id, uint64_t ts_init) + cpdef void iterate_next_time(self, uint64_t to_time_ns) + cpdef void cancel(self) + cpdef void repeat(self, uint64_t ts_now) + cdef object _start_timer(self, uint64_t ts_now) + + +cdef class ThreadTimer(LiveTimer): + pass + + +cdef class LoopTimer(LiveTimer): + cdef object _loop + + cdef str RECV cdef str SENT cdef str CMD diff --git a/nautilus_trader/common/component.pyx b/nautilus_trader/common/component.pyx index 131f4af98403..c7a883038b8a 100644 --- a/nautilus_trader/common/component.pyx +++ b/nautilus_trader/common/component.pyx @@ -13,6 +13,7 @@ # limitations under the License. # ------------------------------------------------------------------------------------------------- +import asyncio import copy import platform import socket @@ -21,6 +22,7 @@ import time import traceback from collections import deque from platform import python_version +from threading import Timer as TimerThread from typing import Any from typing import Callable @@ -37,27 +39,37 @@ from nautilus_trader.config.error import InvalidConfiguration from nautilus_trader.core.rust.common import ComponentState as PyComponentState cimport numpy as np +from cpython.datetime cimport datetime from cpython.datetime cimport timedelta +from cpython.datetime cimport tzinfo +from cpython.object cimport PyCallable_Check +from cpython.object cimport PyObject from libc.stdint cimport int64_t from libc.stdint cimport uint64_t +from libc.stdio cimport printf -from nautilus_trader.common.clock cimport Clock -from nautilus_trader.common.clock cimport TimeEvent -from nautilus_trader.common.component cimport LogColor -from nautilus_trader.common.component cimport Logger -from nautilus_trader.common.component cimport MessageBus from nautilus_trader.common.messages cimport ComponentStateChanged from nautilus_trader.core.correctness cimport Condition +from nautilus_trader.core.datetime cimport dt_to_unix_nanos +from nautilus_trader.core.datetime cimport maybe_dt_to_unix_nanos from nautilus_trader.core.fsm cimport FiniteStateMachine from nautilus_trader.core.fsm cimport InvalidStateTrigger +from nautilus_trader.core.message cimport Event from nautilus_trader.core.rust.common cimport ComponentState from nautilus_trader.core.rust.common cimport ComponentTrigger from nautilus_trader.core.rust.common cimport LogColor from nautilus_trader.core.rust.common cimport LogLevel +from nautilus_trader.core.rust.common cimport TimeEventHandler_t from nautilus_trader.core.rust.common cimport component_state_from_cstr from nautilus_trader.core.rust.common cimport component_state_to_cstr from nautilus_trader.core.rust.common cimport component_trigger_from_cstr from nautilus_trader.core.rust.common cimport component_trigger_to_cstr +from nautilus_trader.core.rust.common cimport live_clock_drop +from nautilus_trader.core.rust.common cimport live_clock_new +from nautilus_trader.core.rust.common cimport live_clock_timestamp +from nautilus_trader.core.rust.common cimport live_clock_timestamp_ms +from nautilus_trader.core.rust.common cimport live_clock_timestamp_ns +from nautilus_trader.core.rust.common cimport live_clock_timestamp_us from nautilus_trader.core.rust.common cimport log_color_from_cstr from nautilus_trader.core.rust.common cimport log_color_to_cstr from nautilus_trader.core.rust.common cimport log_level_from_cstr @@ -76,11 +88,35 @@ from nautilus_trader.core.rust.common cimport logging_shutdown from nautilus_trader.core.rust.common cimport msgbus_drop from nautilus_trader.core.rust.common cimport msgbus_new from nautilus_trader.core.rust.common cimport msgbus_publish_external +from nautilus_trader.core.rust.common cimport test_clock_advance_time +from nautilus_trader.core.rust.common cimport test_clock_cancel_timer +from nautilus_trader.core.rust.common cimport test_clock_cancel_timers +from nautilus_trader.core.rust.common cimport test_clock_drop +from nautilus_trader.core.rust.common cimport test_clock_new +from nautilus_trader.core.rust.common cimport test_clock_next_time_ns +from nautilus_trader.core.rust.common cimport test_clock_register_default_handler +from nautilus_trader.core.rust.common cimport test_clock_set_time +from nautilus_trader.core.rust.common cimport test_clock_set_time_alert_ns +from nautilus_trader.core.rust.common cimport test_clock_set_timer_ns +from nautilus_trader.core.rust.common cimport test_clock_timer_count +from nautilus_trader.core.rust.common cimport test_clock_timer_names +from nautilus_trader.core.rust.common cimport test_clock_timestamp +from nautilus_trader.core.rust.common cimport test_clock_timestamp_ms +from nautilus_trader.core.rust.common cimport test_clock_timestamp_ns +from nautilus_trader.core.rust.common cimport test_clock_timestamp_us +from nautilus_trader.core.rust.common cimport time_event_new +from nautilus_trader.core.rust.common cimport time_event_to_cstr from nautilus_trader.core.rust.common cimport tracing_init +from nautilus_trader.core.rust.common cimport vec_time_event_handlers_drop +from nautilus_trader.core.rust.core cimport CVec +from nautilus_trader.core.rust.core cimport nanos_to_millis +from nautilus_trader.core.rust.core cimport nanos_to_secs from nautilus_trader.core.rust.core cimport secs_to_nanos +from nautilus_trader.core.rust.core cimport uuid4_from_cstr from nautilus_trader.core.string cimport cstr_to_pystr from nautilus_trader.core.string cimport pybytes_to_cstr from nautilus_trader.core.string cimport pystr_to_cstr +from nautilus_trader.core.string cimport ustr_to_pystr from nautilus_trader.core.uuid cimport UUID4 from nautilus_trader.model.identifiers cimport ComponentId from nautilus_trader.model.identifiers cimport Identifier @@ -89,6 +125,1164 @@ from nautilus_trader.serialization.base cimport EXTERNAL_PUBLISHING_TYPES from nautilus_trader.serialization.base cimport Serializer +cdef class Clock: + """ + The base class for all clocks. + + Notes + ----- + An *active* timer is one which has not expired. + + Warnings + -------- + This class should not be used directly, but through a concrete subclass. + """ + + @property + def timer_names(self) -> list[str]: + """ + Return the names of *active* timers running in the clock. + + Returns + ------- + list[str] + + """ + raise NotImplementedError("method `timer_names` must be implemented in the subclass") # pragma: no cover + + @property + def timer_count(self) -> int: + """ + Return the count of *active* timers running in the clock. + + Returns + ------- + int + + """ + raise NotImplementedError("method `timer_count` must be implemented in the subclass") # pragma: no cover + + cpdef double timestamp(self): + """ + Return the current UNIX time in seconds. + + Returns + ------- + double + + References + ---------- + https://en.wikipedia.org/wiki/Unix_time + + """ + raise NotImplementedError("method `timestamp` must be implemented in the subclass") # pragma: no cover + + cpdef uint64_t timestamp_ms(self): + """ + Return the current UNIX time in milliseconds (ms). + + Returns + ------- + uint64_t + + References + ---------- + https://en.wikipedia.org/wiki/Unix_time + + """ + raise NotImplementedError("method `timestamp_ms` must be implemented in the subclass") # pragma: no cover + + cpdef uint64_t timestamp_ns(self): + """ + Return the current UNIX time in nanoseconds (ns). + + Returns + ------- + uint64_t + + References + ---------- + https://en.wikipedia.org/wiki/Unix_time + + """ + raise NotImplementedError("method `timestamp_ns` must be implemented in the subclass") # pragma: no cover + + cpdef datetime utc_now(self): + """ + Return the current time (UTC). + + Returns + ------- + datetime + The current tz-aware UTC time of the clock. + + """ + return pd.Timestamp(self.timestamp_ns(), tz=pytz.utc) + + cpdef datetime local_now(self, tzinfo tz = None): + """ + Return the current datetime of the clock in the given local timezone. + + Parameters + ---------- + tz : tzinfo, optional + The local timezone (if None the system local timezone is assumed for + the target timezone). + + Returns + ------- + datetime + tz-aware in local timezone. + + """ + return self.utc_now().astimezone(tz) + + cpdef void register_default_handler(self, handler: Callable[[TimeEvent], None]): + """ + Register the given handler as the clocks default handler. + + handler : Callable[[TimeEvent], None] + The handler to register. + + Raises + ------ + TypeError + If `handler` is not of type `Callable`. + + """ + raise NotImplementedError("method `register_default_handler` must be implemented in the subclass") # pragma: no cover + + cpdef uint64_t next_time_ns(self, str name): + """ + Find a particular timer. + + Parameters + ---------- + name : str + The name of the timer. + + Returns + ------- + uint64_t + + Raises + ------ + ValueError + If `name` is not a valid string. + + """ + raise NotImplementedError("method `next_time_ns` must be implemented in the subclass") # pragma: no cover + + cpdef void set_time_alert( + self, + str name, + datetime alert_time, + callback: Callable[[TimeEvent], None] = None, + ): + """ + Set a time alert for the given time. + + When the time is reached the handler will be passed the `TimeEvent` + containing the timers unique name. If no handler is passed then the + default handler (if registered) will receive the `TimeEvent`. + + Parameters + ---------- + name : str + The name for the alert (must be unique for this clock). + alert_time : datetime + The time for the alert. + callback : Callable[[TimeEvent], None], optional + The callback to receive time events. + + Raises + ------ + ValueError + If `name` is not a valid string. + KeyError + If `name` is not unique for this clock. + TypeError + If `handler` is not of type `Callable` or ``None``. + ValueError + If `handler` is ``None`` and no default handler is registered. + + Warnings + -------- + If `alert_time` is in the past or at current time, then an immediate + time event will be generated (rather than being invalid and failing a condition check). + + """ + self.set_time_alert_ns( + name=name, + alert_time_ns=dt_to_unix_nanos(alert_time), + callback=callback, + ) + + cpdef void set_time_alert_ns( + self, + str name, + uint64_t alert_time_ns, + callback: Callable[[TimeEvent], None] = None, + ): + """ + Set a time alert for the given time. + + When the time is reached the handler will be passed the `TimeEvent` + containing the timers unique name. If no callback is passed then the + default handler (if registered) will receive the `TimeEvent`. + + Parameters + ---------- + name : str + The name for the alert (must be unique for this clock). + alert_time_ns : uint64_t + The UNIX time (nanoseconds) for the alert. + callback : Callable[[TimeEvent], None], optional + The callback to receive time events. + + Raises + ------ + ValueError + If `name` is not a valid string. + ValueError + If `name` is not unique for this clock. + TypeError + If `callback` is not of type `Callable` or ``None``. + ValueError + If `callback` is ``None`` and no default handler is registered. + + Warnings + -------- + If `alert_time_ns` is in the past or at current time, then an immediate + time event will be generated (rather than being invalid and failing a condition check). + + """ + raise NotImplementedError("method `set_time_alert_ns` must be implemented in the subclass") # pragma: no cover + + cpdef void set_timer( + self, + str name, + timedelta interval, + datetime start_time = None, + datetime stop_time = None, + callback: Callable[[TimeEvent], None] | None = None, + ): + """ + Set a timer to run. + + The timer will run from the start time (optionally until the stop time). + When the intervals are reached the handlers will be passed the + `TimeEvent` containing the timers unique name. If no handler is passed + then the default handler (if registered) will receive the `TimeEvent`. + + Parameters + ---------- + name : str + The name for the timer (must be unique for this clock). + interval : timedelta + The time interval for the timer. + start_time : datetime, optional + The start time for the timer (if None then starts immediately). + stop_time : datetime, optional + The stop time for the timer (if None then repeats indefinitely). + callback : Callable[[TimeEvent], None], optional + The callback to receive time events. + + Raises + ------ + ValueError + If `name` is not a valid string. + KeyError + If `name` is not unique for this clock. + ValueError + If `interval` is not positive (> 0). + ValueError + If `stop_time` is not ``None`` and `stop_time` < time now. + ValueError + If `stop_time` is not ``None`` and `start_time` + `interval` > `stop_time`. + TypeError + If `handler` is not of type `Callable` or ``None``. + ValueError + If `handler` is ``None`` and no default handler is registered. + + """ + self.set_timer_ns( + name=name, + interval_ns=pd.Timedelta(interval).value, + start_time_ns=maybe_dt_to_unix_nanos(start_time) or 0, + stop_time_ns=maybe_dt_to_unix_nanos(stop_time) or 0, + callback=callback, + ) + + cpdef void set_timer_ns( + self, + str name, + uint64_t interval_ns, + uint64_t start_time_ns, + uint64_t stop_time_ns, + callback: Callable[[TimeEvent], None] | None = None, + ): + """ + Set a timer to run. + + The timer will run from the start time until the stop time. + When the intervals are reached the handlers will be passed the + `TimeEvent` containing the timers unique name. If no handler is passed + then the default handler (if registered) will receive the `TimeEvent`. + + Parameters + ---------- + name : str + The name for the timer (must be unique for this clock). + interval_ns : uint64_t + The time interval (nanoseconds) for the timer. + start_time_ns : uint64_t + The start UNIX time (nanoseconds) for the timer. + stop_time_ns : uint64_t + The stop UNIX time (nanoseconds) for the timer. + callback : Callable[[TimeEvent], None], optional + The callback to receive time events. + + Raises + ------ + ValueError + If `name` is not a valid string. + KeyError + If `name` is not unique for this clock. + ValueError + If `interval` is not positive (> 0). + ValueError + If `stop_time` is not ``None`` and `stop_time` < time now. + ValueError + If `stop_time` is not ``None`` and `start_time` + interval > `stop_time`. + TypeError + If `callback` is not of type `Callable` or ``None``. + ValueError + If `callback` is ``None`` and no default handler is registered. + + """ + raise NotImplementedError("method `set_timer_ns` must be implemented in the subclass") # pragma: no cover + + cpdef void cancel_timer(self, str name): + """ + Cancel the timer corresponding to the given label. + + Parameters + ---------- + name : str + The name for the timer to cancel. + + Raises + ------ + ValueError + If `name` is not a valid string. + KeyError + If `name` is not an active timer name for this clock. + + """ + raise NotImplementedError("method `cancel_timer` must be implemented in the subclass") # pragma: no cover + + cpdef void cancel_timers(self): + """ + Cancel all timers. + """ + raise NotImplementedError("method `cancel_timers` must be implemented in the subclass") # pragma: no cover + + +cdef class TestClock(Clock): + """ + Provides a monotonic clock for backtesting and unit testing. + + """ + + __test__ = False # Required so pytest does not consider this a test class + + def __init__(self): + self._mem = test_clock_new() + + def __del__(self) -> None: + if self._mem._0 != NULL: + test_clock_drop(self._mem) + + @property + def timer_names(self) -> list[str]: + return sorted(test_clock_timer_names(&self._mem)) + + @property + def timer_count(self) -> int: + return test_clock_timer_count(&self._mem) + + cpdef double timestamp(self): + return test_clock_timestamp(&self._mem) + + cpdef uint64_t timestamp_ms(self): + return test_clock_timestamp_ms(&self._mem) + + cpdef uint64_t timestamp_ns(self): + return test_clock_timestamp_ns(&self._mem) + + cpdef void register_default_handler(self, callback: Callable[[TimeEvent], None]): + Condition.callable(callback, "callback") + + test_clock_register_default_handler(&self._mem, callback) + + cpdef void set_time_alert_ns( + self, + str name, + uint64_t alert_time_ns, + callback: Callable[[TimeEvent], None] | None = None, + ): + Condition.valid_string(name, "name") + Condition.not_in(name, self.timer_names, "name", "self.timer_names") + + test_clock_set_time_alert_ns( + &self._mem, + pystr_to_cstr(name), + alert_time_ns, + callback, + ) + + cpdef void set_timer_ns( + self, + str name, + uint64_t interval_ns, + uint64_t start_time_ns, + uint64_t stop_time_ns, + callback: Callable[[TimeEvent], None] | None = None, + ): + Condition.valid_string(name, "name") + Condition.not_in(name, self.timer_names, "name", "self.timer_names") + Condition.positive_int(interval_ns, "interval_ns") + + cdef uint64_t ts_now = self.timestamp_ns() + + if start_time_ns == 0: + start_time_ns = ts_now + if stop_time_ns: + Condition.true(stop_time_ns > ts_now, "`stop_time_ns` was < `ts_now`") + Condition.true(start_time_ns + interval_ns <= stop_time_ns, "`start_time_ns` + `interval_ns` was > `stop_time_ns`") + + test_clock_set_timer_ns( + &self._mem, + pystr_to_cstr(name), + interval_ns, + start_time_ns, + stop_time_ns, + callback, + ) + + cpdef uint64_t next_time_ns(self, str name): + Condition.valid_string(name, "name") + return test_clock_next_time_ns(&self._mem, pystr_to_cstr(name)) + + cpdef void cancel_timer(self, str name): + Condition.valid_string(name, "name") + Condition.is_in(name, self.timer_names, "name", "self.timer_names") + + test_clock_cancel_timer(&self._mem, pystr_to_cstr(name)) + + cpdef void cancel_timers(self): + test_clock_cancel_timers(&self._mem) + + cpdef void set_time(self, uint64_t to_time_ns): + """ + Set the clocks datetime to the given time (UTC). + + Parameters + ---------- + to_time_ns : uint64_t + The UNIX time (nanoseconds) to set. + + """ + test_clock_set_time(&self._mem, to_time_ns) + + cdef CVec advance_time_c(self, uint64_t to_time_ns, bint set_time=True): + Condition.true(to_time_ns >= test_clock_timestamp_ns(&self._mem), "to_time_ns was < time_ns (not monotonic)") + + return test_clock_advance_time(&self._mem, to_time_ns, set_time) + + cpdef list advance_time(self, uint64_t to_time_ns, bint set_time=True): + """ + Advance the clocks time to the given `to_time_ns`. + + Parameters + ---------- + to_time_ns : uint64_t + The UNIX time (nanoseconds) to advance the clock to. + set_time : bool + If the clock should also be set to the given `to_time_ns`. + + Returns + ------- + list[TimeEventHandler] + Sorted chronologically. + + Raises + ------ + ValueError + If `to_time_ns` is < the clocks current time. + + """ + cdef CVec raw_handler_vec = self.advance_time_c(to_time_ns, set_time) + cdef TimeEventHandler_t* raw_handlers = raw_handler_vec.ptr + cdef list event_handlers = [] + + cdef: + uint64_t i + object callback + TimeEvent event + TimeEventHandler_t raw_handler + TimeEventHandler event_handler + for i in range(raw_handler_vec.len): + raw_handler = raw_handlers[i] + event = TimeEvent.from_mem_c(raw_handler.event) + + # Cast raw `PyObject *` to a `PyObject` + callback = raw_handler.callback_ptr + + event_handler = TimeEventHandler(event, callback) + event_handlers.append(event_handler) + + vec_time_event_handlers_drop(raw_handler_vec) + + return event_handlers + + +cdef class LiveClock(Clock): + """ + Provides a monotonic clock for live trading. + + All times are tz-aware UTC. + + Parameters + ---------- + loop : asyncio.AbstractEventLoop + The event loop for the clocks timers. + """ + + def __init__(self, loop: asyncio.AbstractEventLoop | None = None): + self._mem = live_clock_new() + self._default_handler = None + self._handlers: dict[str, Callable[[TimeEvent], None]] = {} + + self._loop = loop + self._timers: dict[str, LiveTimer] = {} + self._stack = np.ascontiguousarray([], dtype=LiveTimer) + + self._timer_count = 0 + self._next_event_time_ns = 0 + + def __del__(self) -> None: + if self._mem._0 != NULL: + live_clock_drop(self._mem) + + @property + def timer_names(self) -> list[str]: + return list(self._timers.keys()) + + @property + def timer_count(self) -> int: + return self._timer_count + + cpdef double timestamp(self): + return live_clock_timestamp(&self._mem) + + cpdef uint64_t timestamp_ms(self): + return live_clock_timestamp_ms(&self._mem) + + cpdef uint64_t timestamp_ns(self): + return live_clock_timestamp_ns(&self._mem) + + cpdef void register_default_handler(self, callback: Callable[[TimeEvent], None]): + Condition.callable(callback, "callback") + + self._default_handler = callback + + cpdef void set_time_alert_ns( + self, + str name, + uint64_t alert_time_ns, + callback: Callable[[TimeEvent], None] | None = None, + ): + Condition.valid_string(name, "name") + Condition.not_in(name, self.timer_names, "name", "self.timer_names") + if callback is None: + callback = self._default_handler + + cdef uint64_t ts_now = self.timestamp_ns() + + cdef LiveTimer timer = self._create_timer( + name=name, + callback=callback, + interval_ns=alert_time_ns - ts_now, + start_time_ns=ts_now, + stop_time_ns=alert_time_ns, + ) + self._add_timer(timer, callback) + + cpdef void set_timer_ns( + self, + str name, + uint64_t interval_ns, + uint64_t start_time_ns, + uint64_t stop_time_ns, + callback: Callable[[TimeEvent], None] | None = None, + ): + Condition.not_in(name, self.timer_names, "name", "self.timer_names") + + cdef uint64_t ts_now = self.timestamp_ns() # Call here for greater accuracy + + Condition.valid_string(name, "name") + if callback is None: + callback = self._default_handler + + Condition.not_in(name, self._timers, "name", "_timers") + Condition.not_in(name, self._handlers, "name", "_handlers") + Condition.true(interval_ns > 0, f"interval was {interval_ns}") + Condition.callable(callback, "callback") + + if start_time_ns == 0: + start_time_ns = ts_now + if stop_time_ns: + Condition.true(stop_time_ns > ts_now, "stop_time was < ts_now") + Condition.true(start_time_ns + interval_ns <= stop_time_ns, "start_time + interval was > stop_time") + + cdef LiveTimer timer = self._create_timer( + name=name, + callback=callback, + interval_ns=interval_ns, + start_time_ns=start_time_ns, + stop_time_ns=stop_time_ns, + ) + self._add_timer(timer, callback) + + cdef void _add_timer(self, LiveTimer timer, handler: Callable[[TimeEvent], None]): + self._timers[timer.name] = timer + self._handlers[timer.name] = handler + self._update_stack() + self._update_timing() + + cdef void _remove_timer(self, LiveTimer timer): + self._timers.pop(timer.name, None) + self._handlers.pop(timer.name, None) + self._update_stack() + self._update_timing() + + cdef void _update_stack(self): + self._timer_count = len(self._timers) + + if self._timer_count > 0: + # The call to `np.ascontiguousarray` here looks inefficient, its + # only called when a timer is added or removed. This then allows the + # construction of an efficient Timer[:] memoryview. + timers = list(self._timers.values()) + self._stack = np.ascontiguousarray(timers, dtype=LiveTimer) + else: + self._stack = None + + cpdef uint64_t next_time_ns(self, str name): + return self._timers[name].next_time_ns + + cpdef void cancel_timer(self, str name): + Condition.valid_string(name, "name") + Condition.is_in(name, self.timer_names, "name", "self.timer_names") + + cdef LiveTimer timer = self._timers.pop(name, None) + if not timer: + # No timer with given name + return + + timer.cancel() + self._handlers.pop(name, None) + self._remove_timer(timer) + + cpdef void cancel_timers(self): + cdef str name + for name in self.timer_names: + # Using a list of timer names from the property and passing this + # to cancel_timer() handles the clean removal of both the handler + # and timer. + self.cancel_timer(name) + + @cython.boundscheck(False) + @cython.wraparound(False) + cdef void _update_timing(self): + if self._timer_count == 0: + self._next_event_time_ns = 0 + return + + cdef LiveTimer first_timer = self._stack[0] + if self._timer_count == 1: + self._next_event_time_ns = first_timer.next_time_ns + return + + cdef uint64_t next_time_ns = first_timer.next_time_ns + cdef: + int i + LiveTimer timer + uint64_t observed_ns + for i in range(self._timer_count - 1): + timer = self._stack[i + 1] + observed_ns = timer.next_time_ns + if observed_ns < next_time_ns: + next_time_ns = observed_ns + + self._next_event_time_ns = next_time_ns + + cdef LiveTimer _create_timer( + self, + str name, + callback: Callable[[TimeEvent], None], + uint64_t interval_ns, + uint64_t start_time_ns, + uint64_t stop_time_ns, + ): + if self._loop is not None: + return LoopTimer( + loop=self._loop, + name=name, + callback=self._raise_time_event, + interval_ns=interval_ns, + ts_now=self.timestamp_ns(), # Timestamp here for accuracy + start_time_ns=start_time_ns, + stop_time_ns=stop_time_ns, + ) + else: + return ThreadTimer( + name=name, + callback=self._raise_time_event, + interval_ns=interval_ns, + ts_now=self.timestamp_ns(), # Timestamp here for accuracy + start_time_ns=start_time_ns, + stop_time_ns=stop_time_ns, + ) + + cpdef void _raise_time_event(self, LiveTimer timer): + cdef uint64_t now = self.timestamp_ns() + cdef TimeEvent event = timer.pop_event( + event_id=UUID4(), + ts_init=now, + ) + + if now < timer.next_time_ns: + timer.iterate_next_time(timer.next_time_ns) + else: + timer.iterate_next_time(now) + + self._handle_time_event(event) + + if timer.is_expired: + self._remove_timer(timer) + else: # Continue timing + timer.repeat(ts_now=self.timestamp_ns()) + self._update_timing() + + cdef void _handle_time_event(self, TimeEvent event): + handler = self._handlers.get(event.name) + if handler is not None: + handler(event) + + +cdef class TimeEvent(Event): + """ + Represents a time event occurring at the event timestamp. + + Parameters + ---------- + name : str + The event name. + event_id : UUID4 + The event ID. + ts_event : uint64_t + The UNIX timestamp (nanoseconds) when the time event occurred. + ts_init : uint64_t + The UNIX timestamp (nanoseconds) when the object was initialized. + """ + + def __init__( + self, + str name not None, + UUID4 event_id not None, + uint64_t ts_event, + uint64_t ts_init, + ): + # Precondition: `name` validated in Rust + self._mem = time_event_new( + pystr_to_cstr(name), + event_id._mem, + ts_event, + ts_init, + ) + + def __getstate__(self): + return ( + self.to_str(), + self.id.value, + self.ts_event, + self.ts_init, + ) + + def __setstate__(self, state): + self._mem = time_event_new( + pystr_to_cstr(state[0]), + uuid4_from_cstr(pystr_to_cstr(state[1])), + self.ts_event, + self.ts_init, + ) + + cdef str to_str(self): + return ustr_to_pystr(self._mem.name) + + def __eq__(self, TimeEvent other) -> bool: + return self.to_str() == other.to_str() + + def __hash__(self) -> int: + return hash(self.to_str()) + + def __str__(self) -> str: + return self.to_str() + + def __repr__(self) -> str: + return cstr_to_pystr(time_event_to_cstr(&self._mem)) + + @property + def name(self) -> str: + """ + Return the name of the time event. + + Returns + ------- + str + + """ + return ustr_to_pystr(self._mem.name) + + @property + def id(self) -> UUID4: + """ + The event message identifier. + + Returns + ------- + UUID4 + + """ + cdef UUID4 uuid4 = UUID4.__new__(UUID4) + uuid4._mem = self._mem.event_id + return uuid4 + + @property + def ts_event(self) -> int: + """ + The UNIX timestamp (nanoseconds) when the event occurred. + + Returns + ------- + int + + """ + return self._mem.ts_event + + @property + def ts_init(self) -> int: + """ + The UNIX timestamp (nanoseconds) when the object was initialized. + + Returns + ------- + int + + """ + return self._mem.ts_init + + @staticmethod + cdef TimeEvent from_mem_c(TimeEvent_t mem): + cdef TimeEvent event = TimeEvent.__new__(TimeEvent) + event._mem = mem + return event + + +cdef class TimeEventHandler: + """ + Represents a time event with its associated handler. + """ + + def __init__( + self, + TimeEvent event not None, + handler not None: Callable[[TimeEvent], None], + ): + self.event = event + self._handler = handler + + cpdef void handle(self): + """Call the handler with the contained time event.""" + self._handler(self.event) + + def __eq__(self, TimeEventHandler other) -> bool: + return self.event.ts_event == other.event.ts_event + + def __lt__(self, TimeEventHandler other) -> bool: + return self.event.ts_event < other.event.ts_event + + def __le__(self, TimeEventHandler other) -> bool: + return self.event.ts_event <= other.event.ts_event + + def __gt__(self, TimeEventHandler other) -> bool: + return self.event.ts_event > other.event.ts_event + + def __ge__(self, TimeEventHandler other) -> bool: + return self.event.ts_event >= other.event.ts_event + + def __repr__(self) -> str: + return ( + f"{type(self).__name__}(" + f"event={repr(self.event)})" + ) + + +cdef class LiveTimer: + """ + The base class for all live timers. + + Parameters + ---------- + name : str + The name for the timer. + callback : Callable[[TimeEvent], None] + The delegate to call at the next time. + interval_ns : uint64_t + The time interval for the timer. + ts_now : uint64_t + The current UNIX time (nanoseconds). + start_time_ns : uint64_t + The start datetime for the timer (UTC). + stop_time_ns : uint64_t, optional + The stop datetime for the timer (UTC) (if None then timer repeats). + + Raises + ------ + TypeError + If `callback` is not of type `Callable`. + + Warnings + -------- + This class should not be used directly, but through a concrete subclass. + """ + + def __init__( + self, + str name not None, + callback not None: Callable[[TimeEvent], None], + uint64_t interval_ns, + uint64_t ts_now, + uint64_t start_time_ns, + uint64_t stop_time_ns=0, + ): + Condition.valid_string(name, "name") + Condition.callable(callback, "callback") + + self.name = name + self.callback = callback + self.interval_ns = interval_ns + self.start_time_ns = start_time_ns + self.next_time_ns = start_time_ns + interval_ns + self.stop_time_ns = stop_time_ns + self.is_expired = False + + self._internal = self._start_timer(ts_now) + + def __eq__(self, LiveTimer other) -> bool: + return self.name == other.name + + def __hash__(self) -> int: + return hash(self.name) + + def __repr__(self) -> str: + return ( + f"{type(self).__name__}(" + f"name={self.name}, " + f"interval_ns={self.interval_ns}, " + f"start_time_ns={self.start_time_ns}, " + f"next_time_ns={self.next_time_ns}, " + f"stop_time_ns={self.stop_time_ns}, " + f"is_expired={self.is_expired})" + ) + + cpdef TimeEvent pop_event(self, UUID4 event_id, uint64_t ts_init): + """ + Return a generated time event with the given ID. + + Parameters + ---------- + event_id : UUID4 + The ID for the time event. + ts_init : uint64_t + The UNIX timestamp (nanoseconds) when the object was initialized. + + Returns + ------- + TimeEvent + + """ + # Precondition: `event_id` validated in `TimeEvent` + + return TimeEvent( + name=self.name, + event_id=event_id, + ts_event=self.next_time_ns, + ts_init=ts_init, + ) + + cpdef void iterate_next_time(self, uint64_t ts_now): + """ + Iterates the timers next time and checks if the timer is now expired. + + Parameters + ---------- + ts_now : uint64_t + The current UNIX time (nanoseconds). + + """ + self.next_time_ns += self.interval_ns + if self.stop_time_ns and ts_now >= self.stop_time_ns: + self.is_expired = True + + cpdef void repeat(self, uint64_t ts_now): + """ + Continue the timer. + + Parameters + ---------- + ts_now : uint64_t + The current time to continue timing from. + + """ + self._internal = self._start_timer(ts_now) + + cpdef void cancel(self): + """ + Cancels the timer (the timer will not generate an event). + """ + self._internal.cancel() + + cdef object _start_timer(self, uint64_t ts_now): + """Abstract method (implement in subclass).""" + raise NotImplementedError("method `_start_timer` must be implemented in the subclass") # pragma: no cover + + +cdef class ThreadTimer(LiveTimer): + """ + Provides a thread based timer for live trading. + + Parameters + ---------- + name : str + The name for the timer. + callback : Callable[[TimeEvent], None] + The delegate to call at the next time. + interval_ns : uint64_t + The time interval for the timer. + ts_now : uint64_t + The current UNIX time (nanoseconds). + start_time_ns : uint64_t + The start datetime for the timer (UTC). + stop_time_ns : uint64_t, optional + The stop datetime for the timer (UTC) (if None then timer repeats). + + Raises + ------ + TypeError + If `callback` is not of type `Callable`. + """ + + def __init__( + self, + str name not None, + callback not None: Callable[[TimeEvent], None], + uint64_t interval_ns, + uint64_t ts_now, + uint64_t start_time_ns, + uint64_t stop_time_ns=0, + ): + super().__init__( + name=name, + callback=callback, + interval_ns=interval_ns, + ts_now=ts_now, + start_time_ns=start_time_ns, + stop_time_ns=stop_time_ns, + ) + + cdef object _start_timer(self, uint64_t ts_now): + timer = TimerThread( + interval=nanos_to_secs(self.next_time_ns - ts_now), + function=self.callback, + args=[self], + ) + timer.daemon = True + timer.start() + + return timer + + +cdef class LoopTimer(LiveTimer): + """ + Provides an event loop based timer for live trading. + + Parameters + ---------- + loop : asyncio.AbstractEventLoop + The event loop to run the timer on. + name : str + The name for the timer. + callback : Callable[[TimeEvent], None] + The delegate to call at the next time. + interval_ns : uint64_t + The time interval for the timer (nanoseconds). + ts_now : uint64_t + The current UNIX epoch (nanoseconds). + start_time_ns : uint64_t + The start datetime for the timer (UTC). + stop_time_ns : uint64_t, optional + The stop datetime for the timer (UTC) (if None then timer repeats). + + Raises + ------ + TypeError + If `callback` is not of type `Callable`. + """ + + def __init__( + self, + loop not None, + str name not None, + callback not None: Callable[[TimeEvent], None], + uint64_t interval_ns, + uint64_t ts_now, + uint64_t start_time_ns, + uint64_t stop_time_ns=0, + ): + Condition.valid_string(name, "name") + + self._loop = loop # Assign here as `super().__init__` will call it + super().__init__( + name=name, + callback=callback, + interval_ns=interval_ns, + ts_now=ts_now, + start_time_ns=start_time_ns, + stop_time_ns=stop_time_ns, + ) + + cdef object _start_timer(self, uint64_t ts_now): + return self._loop.call_later( + nanos_to_secs(self.next_time_ns - ts_now), + self.callback, + self, + ) + + RECV = "<--" SENT = "-->" CMD = "[CMD]" @@ -1688,6 +2882,8 @@ cdef class Subscription: f"handler={self.handler}, " f"priority={self.priority})" ) + + cdef class Throttler: """ Provides a generic throttler which can either buffer or drop messages. diff --git a/nautilus_trader/common/factories.pxd b/nautilus_trader/common/factories.pxd index 6e1c79223d91..17e005499670 100644 --- a/nautilus_trader/common/factories.pxd +++ b/nautilus_trader/common/factories.pxd @@ -18,7 +18,7 @@ from decimal import Decimal from cpython.datetime cimport datetime from nautilus_trader.cache.base cimport CacheFacade -from nautilus_trader.common.clock cimport Clock +from nautilus_trader.common.component cimport Clock from nautilus_trader.common.generators cimport ClientOrderIdGenerator from nautilus_trader.common.generators cimport OrderListIdGenerator from nautilus_trader.core.rust.model cimport ContingencyType diff --git a/nautilus_trader/common/factories.pyx b/nautilus_trader/common/factories.pyx index a21ee69241c6..469a126fdec8 100644 --- a/nautilus_trader/common/factories.pyx +++ b/nautilus_trader/common/factories.pyx @@ -16,7 +16,7 @@ from cpython.datetime cimport datetime from nautilus_trader.cache.base cimport CacheFacade -from nautilus_trader.common.clock cimport Clock +from nautilus_trader.common.component cimport Clock from nautilus_trader.common.generators cimport ClientOrderIdGenerator from nautilus_trader.common.generators cimport OrderListIdGenerator from nautilus_trader.core.correctness cimport Condition diff --git a/nautilus_trader/common/generators.pxd b/nautilus_trader/common/generators.pxd index faf450e8521a..e4e29bbc2f34 100644 --- a/nautilus_trader/common/generators.pxd +++ b/nautilus_trader/common/generators.pxd @@ -13,7 +13,7 @@ # limitations under the License. # ------------------------------------------------------------------------------------------------- -from nautilus_trader.common.clock cimport Clock +from nautilus_trader.common.component cimport Clock from nautilus_trader.model.identifiers cimport ClientOrderId from nautilus_trader.model.identifiers cimport OrderListId from nautilus_trader.model.identifiers cimport PositionId diff --git a/nautilus_trader/common/generators.pyx b/nautilus_trader/common/generators.pyx index 7b53b7743cf3..6f2fd45639c6 100644 --- a/nautilus_trader/common/generators.pyx +++ b/nautilus_trader/common/generators.pyx @@ -15,7 +15,7 @@ from cpython.datetime cimport datetime -from nautilus_trader.common.clock cimport Clock +from nautilus_trader.common.component cimport Clock from nautilus_trader.core.correctness cimport Condition from nautilus_trader.model.identifiers cimport ClientOrderId from nautilus_trader.model.identifiers cimport PositionId diff --git a/nautilus_trader/data/aggregation.pxd b/nautilus_trader/data/aggregation.pxd index 667646353fca..ea4a356ee13a 100644 --- a/nautilus_trader/data/aggregation.pxd +++ b/nautilus_trader/data/aggregation.pxd @@ -18,9 +18,9 @@ from cpython.datetime cimport timedelta from libc.stdint cimport uint8_t from libc.stdint cimport uint64_t -from nautilus_trader.common.clock cimport Clock -from nautilus_trader.common.clock cimport TimeEvent +from nautilus_trader.common.component cimport Clock from nautilus_trader.common.component cimport Logger +from nautilus_trader.common.component cimport TimeEvent from nautilus_trader.model.data cimport Bar from nautilus_trader.model.data cimport BarType from nautilus_trader.model.data cimport QuoteTick diff --git a/nautilus_trader/data/aggregation.pyx b/nautilus_trader/data/aggregation.pyx index 32d66058be00..9bd2475b3d6b 100644 --- a/nautilus_trader/data/aggregation.pyx +++ b/nautilus_trader/data/aggregation.pyx @@ -20,9 +20,9 @@ from cpython.datetime cimport datetime from cpython.datetime cimport timedelta from libc.stdint cimport uint64_t -from nautilus_trader.common.clock cimport Clock -from nautilus_trader.common.clock cimport TimeEvent +from nautilus_trader.common.component cimport Clock from nautilus_trader.common.component cimport Logger +from nautilus_trader.common.component cimport TimeEvent from nautilus_trader.core.correctness cimport Condition from nautilus_trader.core.datetime cimport dt_to_unix_nanos from nautilus_trader.core.rust.core cimport millis_to_nanos diff --git a/nautilus_trader/data/client.pyx b/nautilus_trader/data/client.pyx index 15eb744b3fda..afef9d5d47e5 100644 --- a/nautilus_trader/data/client.pyx +++ b/nautilus_trader/data/client.pyx @@ -17,7 +17,7 @@ from nautilus_trader.config.common import NautilusConfig from cpython.datetime cimport datetime from nautilus_trader.cache.cache cimport Cache -from nautilus_trader.common.clock cimport Clock +from nautilus_trader.common.component cimport Clock from nautilus_trader.common.component cimport Component from nautilus_trader.common.component cimport MessageBus from nautilus_trader.core.correctness cimport Condition diff --git a/nautilus_trader/data/engine.pxd b/nautilus_trader/data/engine.pxd index 1e8ed7d9b04c..3bbc355e1244 100644 --- a/nautilus_trader/data/engine.pxd +++ b/nautilus_trader/data/engine.pxd @@ -14,8 +14,8 @@ # ------------------------------------------------------------------------------------------------- from nautilus_trader.cache.cache cimport Cache -from nautilus_trader.common.clock cimport TimeEvent from nautilus_trader.common.component cimport Component +from nautilus_trader.common.component cimport TimeEvent from nautilus_trader.core.data cimport Data from nautilus_trader.data.client cimport DataClient from nautilus_trader.data.client cimport MarketDataClient diff --git a/nautilus_trader/data/engine.pyx b/nautilus_trader/data/engine.pyx index 2e94aa09c3d2..54ccf2fa4315 100644 --- a/nautilus_trader/data/engine.pyx +++ b/nautilus_trader/data/engine.pyx @@ -39,11 +39,11 @@ from cpython.datetime cimport datetime from cpython.datetime cimport timedelta from libc.stdint cimport uint64_t -from nautilus_trader.common.clock cimport Clock from nautilus_trader.common.component cimport CMD from nautilus_trader.common.component cimport RECV from nautilus_trader.common.component cimport REQ from nautilus_trader.common.component cimport RES +from nautilus_trader.common.component cimport Clock from nautilus_trader.common.component cimport Component from nautilus_trader.common.component cimport Logger from nautilus_trader.common.component cimport MessageBus diff --git a/nautilus_trader/examples/algorithms/twap.py b/nautilus_trader/examples/algorithms/twap.py index aba09bc69c47..9e6fa0761b10 100644 --- a/nautilus_trader/examples/algorithms/twap.py +++ b/nautilus_trader/examples/algorithms/twap.py @@ -18,7 +18,7 @@ from decimal import ROUND_DOWN from decimal import Decimal -from nautilus_trader.common.clock import TimeEvent +from nautilus_trader.common.component import TimeEvent from nautilus_trader.common.enums import LogColor from nautilus_trader.config.common import ExecAlgorithmConfig from nautilus_trader.core.correctness import PyCondition diff --git a/nautilus_trader/execution/algorithm.pxd b/nautilus_trader/execution/algorithm.pxd index b34330f6a8ec..222d1fdba366 100644 --- a/nautilus_trader/execution/algorithm.pxd +++ b/nautilus_trader/execution/algorithm.pxd @@ -19,7 +19,7 @@ from libc.stdint cimport uint64_t from nautilus_trader.cache.base cimport CacheFacade from nautilus_trader.common.actor cimport Actor -from nautilus_trader.common.clock cimport Clock +from nautilus_trader.common.component cimport Clock from nautilus_trader.common.component cimport MessageBus from nautilus_trader.core.rust.model cimport ContingencyType from nautilus_trader.core.rust.model cimport TimeInForce diff --git a/nautilus_trader/execution/algorithm.pyx b/nautilus_trader/execution/algorithm.pyx index 331a163f54bb..649f9520f04b 100644 --- a/nautilus_trader/execution/algorithm.pyx +++ b/nautilus_trader/execution/algorithm.pyx @@ -24,11 +24,11 @@ from libc.stdint cimport uint64_t from nautilus_trader.cache.base cimport CacheFacade from nautilus_trader.common.actor cimport Actor -from nautilus_trader.common.clock cimport Clock from nautilus_trader.common.component cimport CMD from nautilus_trader.common.component cimport EVT from nautilus_trader.common.component cimport RECV from nautilus_trader.common.component cimport SENT +from nautilus_trader.common.component cimport Clock from nautilus_trader.common.component cimport LogColor from nautilus_trader.common.component cimport MessageBus from nautilus_trader.core.correctness cimport Condition diff --git a/nautilus_trader/execution/client.pyx b/nautilus_trader/execution/client.pyx index 38714f64531f..57c5c6f18695 100644 --- a/nautilus_trader/execution/client.pyx +++ b/nautilus_trader/execution/client.pyx @@ -21,7 +21,7 @@ from nautilus_trader.execution.reports import OrderStatusReport from libc.stdint cimport uint64_t from nautilus_trader.cache.cache cimport Cache -from nautilus_trader.common.clock cimport Clock +from nautilus_trader.common.component cimport Clock from nautilus_trader.common.component cimport Component from nautilus_trader.common.component cimport MessageBus from nautilus_trader.core.correctness cimport Condition diff --git a/nautilus_trader/execution/emulator.pyx b/nautilus_trader/execution/emulator.pyx index 199d9d220ddb..3faae62e5431 100644 --- a/nautilus_trader/execution/emulator.pyx +++ b/nautilus_trader/execution/emulator.pyx @@ -20,11 +20,11 @@ from libc.stdint cimport uint64_t from nautilus_trader.cache.cache cimport Cache from nautilus_trader.common.actor cimport Actor -from nautilus_trader.common.clock cimport Clock from nautilus_trader.common.component cimport CMD from nautilus_trader.common.component cimport EVT from nautilus_trader.common.component cimport RECV from nautilus_trader.common.component cimport SENT +from nautilus_trader.common.component cimport Clock from nautilus_trader.common.component cimport LogColor from nautilus_trader.common.component cimport MessageBus from nautilus_trader.core.correctness cimport Condition diff --git a/nautilus_trader/execution/engine.pyx b/nautilus_trader/execution/engine.pyx index 78d0352ad10d..26d049aeab3c 100644 --- a/nautilus_trader/execution/engine.pyx +++ b/nautilus_trader/execution/engine.pyx @@ -41,10 +41,10 @@ from libc.stdint cimport uint64_t from nautilus_trader.accounting.accounts.base cimport Account from nautilus_trader.cache.cache cimport Cache -from nautilus_trader.common.clock cimport Clock from nautilus_trader.common.component cimport CMD from nautilus_trader.common.component cimport EVT from nautilus_trader.common.component cimport RECV +from nautilus_trader.common.component cimport Clock from nautilus_trader.common.component cimport Component from nautilus_trader.common.component cimport LogColor from nautilus_trader.common.component cimport Logger diff --git a/nautilus_trader/execution/manager.pxd b/nautilus_trader/execution/manager.pxd index 3c23e6aee4aa..7b95d2ca4d33 100644 --- a/nautilus_trader/execution/manager.pxd +++ b/nautilus_trader/execution/manager.pxd @@ -14,7 +14,7 @@ # ------------------------------------------------------------------------------------------------- from nautilus_trader.cache.cache cimport Cache -from nautilus_trader.common.clock cimport Clock +from nautilus_trader.common.component cimport Clock from nautilus_trader.common.component cimport Logger from nautilus_trader.common.component cimport MessageBus from nautilus_trader.core.message cimport Event diff --git a/nautilus_trader/execution/manager.pyx b/nautilus_trader/execution/manager.pyx index 5acea3908241..d6b1363eb20c 100644 --- a/nautilus_trader/execution/manager.pyx +++ b/nautilus_trader/execution/manager.pyx @@ -17,11 +17,11 @@ from libc.stdint cimport uint8_t from libc.stdint cimport uint64_t from nautilus_trader.cache.cache cimport Cache -from nautilus_trader.common.clock cimport Clock from nautilus_trader.common.component cimport CMD from nautilus_trader.common.component cimport EVT from nautilus_trader.common.component cimport RECV from nautilus_trader.common.component cimport SENT +from nautilus_trader.common.component cimport Clock from nautilus_trader.common.component cimport LogColor from nautilus_trader.common.component cimport Logger from nautilus_trader.common.component cimport MessageBus diff --git a/nautilus_trader/live/data_client.py b/nautilus_trader/live/data_client.py index 648ef6d8c9da..dbf151799214 100644 --- a/nautilus_trader/live/data_client.py +++ b/nautilus_trader/live/data_client.py @@ -31,7 +31,7 @@ import pandas as pd from nautilus_trader.cache.cache import Cache -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.common.enums import LogColor from nautilus_trader.common.providers import InstrumentProvider diff --git a/nautilus_trader/live/data_engine.py b/nautilus_trader/live/data_engine.py index 505d5eee7d0f..048b44de1af0 100644 --- a/nautilus_trader/live/data_engine.py +++ b/nautilus_trader/live/data_engine.py @@ -18,7 +18,7 @@ from typing import Final from nautilus_trader.cache.cache import Cache -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.config import LiveDataEngineConfig from nautilus_trader.core.correctness import PyCondition diff --git a/nautilus_trader/live/execution_client.py b/nautilus_trader/live/execution_client.py index ed5a80f565d4..02c47ab81e13 100644 --- a/nautilus_trader/live/execution_client.py +++ b/nautilus_trader/live/execution_client.py @@ -28,7 +28,7 @@ import pandas as pd from nautilus_trader.cache.cache import Cache -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.common.enums import LogColor from nautilus_trader.common.providers import InstrumentProvider diff --git a/nautilus_trader/live/execution_engine.py b/nautilus_trader/live/execution_engine.py index ce49404e9ca5..d7a30579cd62 100644 --- a/nautilus_trader/live/execution_engine.py +++ b/nautilus_trader/live/execution_engine.py @@ -20,7 +20,7 @@ from typing import Any, Final from nautilus_trader.cache.cache import Cache -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.common.enums import LogColor from nautilus_trader.config import LiveExecEngineConfig diff --git a/nautilus_trader/live/factories.py b/nautilus_trader/live/factories.py index 872c563112b5..c39d0ffe5ab6 100644 --- a/nautilus_trader/live/factories.py +++ b/nautilus_trader/live/factories.py @@ -16,7 +16,7 @@ import asyncio from nautilus_trader.cache.cache import Cache -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.config import LiveDataClientConfig from nautilus_trader.config import LiveExecClientConfig diff --git a/nautilus_trader/live/node_builder.py b/nautilus_trader/live/node_builder.py index d94c03a1ea42..2c4078e03ae9 100644 --- a/nautilus_trader/live/node_builder.py +++ b/nautilus_trader/live/node_builder.py @@ -16,7 +16,7 @@ import asyncio from nautilus_trader.cache.cache import Cache -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import Logger from nautilus_trader.common.component import MessageBus from nautilus_trader.config import ImportableConfig diff --git a/nautilus_trader/live/risk_engine.py b/nautilus_trader/live/risk_engine.py index 49ba838de963..aede05a1bb72 100644 --- a/nautilus_trader/live/risk_engine.py +++ b/nautilus_trader/live/risk_engine.py @@ -18,7 +18,7 @@ from typing import Final from nautilus_trader.cache.base import CacheFacade -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.config import LiveRiskEngineConfig from nautilus_trader.core.correctness import PyCondition diff --git a/nautilus_trader/portfolio/portfolio.pxd b/nautilus_trader/portfolio/portfolio.pxd index 7c3d83a9ca62..90b7bb920bda 100644 --- a/nautilus_trader/portfolio/portfolio.pxd +++ b/nautilus_trader/portfolio/portfolio.pxd @@ -16,7 +16,7 @@ from nautilus_trader.accounting.accounts.base cimport Account from nautilus_trader.accounting.manager cimport AccountsManager from nautilus_trader.cache.cache cimport Cache -from nautilus_trader.common.clock cimport Clock +from nautilus_trader.common.component cimport Clock from nautilus_trader.common.component cimport Logger from nautilus_trader.common.component cimport MessageBus from nautilus_trader.core.rust.model cimport OrderSide diff --git a/nautilus_trader/risk/engine.pyx b/nautilus_trader/risk/engine.pyx index d333be7cd0fd..65e2d3c2b794 100644 --- a/nautilus_trader/risk/engine.pyx +++ b/nautilus_trader/risk/engine.pyx @@ -23,10 +23,10 @@ from libc.stdint cimport uint64_t from nautilus_trader.accounting.accounts.base cimport Account from nautilus_trader.cache.cache cimport Cache -from nautilus_trader.common.clock cimport Clock from nautilus_trader.common.component cimport CMD from nautilus_trader.common.component cimport EVT from nautilus_trader.common.component cimport RECV +from nautilus_trader.common.component cimport Clock from nautilus_trader.common.component cimport Component from nautilus_trader.common.component cimport LogColor from nautilus_trader.common.component cimport MessageBus diff --git a/nautilus_trader/system/kernel.py b/nautilus_trader/system/kernel.py index 19a5b95f0eb3..8332d5dca393 100644 --- a/nautilus_trader/system/kernel.py +++ b/nautilus_trader/system/kernel.py @@ -30,11 +30,11 @@ from nautilus_trader.cache.database import CacheDatabaseAdapter from nautilus_trader.common import Environment from nautilus_trader.common.actor import Actor -from nautilus_trader.common.clock import Clock -from nautilus_trader.common.clock import LiveClock -from nautilus_trader.common.clock import TestClock +from nautilus_trader.common.component import Clock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import Logger from nautilus_trader.common.component import MessageBus +from nautilus_trader.common.component import TestClock from nautilus_trader.common.component import init_logging from nautilus_trader.common.component import init_tracing from nautilus_trader.common.component import log_header diff --git a/nautilus_trader/test_kit/stubs/component.py b/nautilus_trader/test_kit/stubs/component.py index 8a9e16804d1e..b7f7a49a579e 100644 --- a/nautilus_trader/test_kit/stubs/component.py +++ b/nautilus_trader/test_kit/stubs/component.py @@ -20,7 +20,7 @@ from nautilus_trader.backtest.models import FillModel from nautilus_trader.backtest.node import BacktestNode from nautilus_trader.cache.cache import Cache -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.common.factories import OrderFactory from nautilus_trader.core.data import Data diff --git a/nautilus_trader/trading/strategy.pxd b/nautilus_trader/trading/strategy.pxd index f37dbec7ed25..74526e3cba13 100644 --- a/nautilus_trader/trading/strategy.pxd +++ b/nautilus_trader/trading/strategy.pxd @@ -15,9 +15,9 @@ from nautilus_trader.cache.base cimport CacheFacade from nautilus_trader.common.actor cimport Actor -from nautilus_trader.common.clock cimport Clock -from nautilus_trader.common.clock cimport TimeEvent +from nautilus_trader.common.component cimport Clock from nautilus_trader.common.component cimport MessageBus +from nautilus_trader.common.component cimport TimeEvent from nautilus_trader.common.factories cimport OrderFactory from nautilus_trader.core.rust.model cimport OmsType from nautilus_trader.core.rust.model cimport OrderSide diff --git a/nautilus_trader/trading/strategy.pyx b/nautilus_trader/trading/strategy.pyx index b86d0f08b2e8..51f70566c188 100644 --- a/nautilus_trader/trading/strategy.pyx +++ b/nautilus_trader/trading/strategy.pyx @@ -35,14 +35,14 @@ from libc.stdint cimport uint64_t from nautilus_trader.cache.base cimport CacheFacade from nautilus_trader.common.actor cimport Actor -from nautilus_trader.common.clock cimport Clock -from nautilus_trader.common.clock cimport TimeEvent from nautilus_trader.common.component cimport CMD from nautilus_trader.common.component cimport EVT from nautilus_trader.common.component cimport RECV from nautilus_trader.common.component cimport SENT +from nautilus_trader.common.component cimport Clock from nautilus_trader.common.component cimport LogColor from nautilus_trader.common.component cimport MessageBus +from nautilus_trader.common.component cimport TimeEvent from nautilus_trader.common.factories cimport OrderFactory from nautilus_trader.core.correctness cimport Condition from nautilus_trader.core.fsm cimport InvalidStateTrigger diff --git a/nautilus_trader/trading/trader.py b/nautilus_trader/trading/trader.py index b08c18ec591a..798d06b945fc 100644 --- a/nautilus_trader/trading/trader.py +++ b/nautilus_trader/trading/trader.py @@ -30,9 +30,9 @@ from nautilus_trader.analysis.reporter import ReportProvider from nautilus_trader.cache.cache import Cache from nautilus_trader.common.actor import Actor -from nautilus_trader.common.clock import Clock -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import Clock from nautilus_trader.common.component import Component +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.core.correctness import PyCondition from nautilus_trader.data.engine import DataEngine diff --git a/tests/integration_tests/adapters/betfair/test_betfair_factory.py b/tests/integration_tests/adapters/betfair/test_betfair_factory.py index 5ca1e6839bd2..6939e912343b 100644 --- a/tests/integration_tests/adapters/betfair/test_betfair_factory.py +++ b/tests/integration_tests/adapters/betfair/test_betfair_factory.py @@ -24,7 +24,7 @@ from nautilus_trader.adapters.betfair.execution import BetfairExecutionClient from nautilus_trader.adapters.betfair.factories import BetfairLiveDataClientFactory from nautilus_trader.adapters.betfair.factories import BetfairLiveExecClientFactory -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.test_kit.stubs.component import TestComponentStubs from nautilus_trader.test_kit.stubs.identifiers import TestIdStubs diff --git a/tests/integration_tests/adapters/betfair/test_betfair_parsing.py b/tests/integration_tests/adapters/betfair/test_betfair_parsing.py index 77f56975ed7d..bbfafb687d19 100644 --- a/tests/integration_tests/adapters/betfair/test_betfair_parsing.py +++ b/tests/integration_tests/adapters/betfair/test_betfair_parsing.py @@ -72,7 +72,7 @@ from nautilus_trader.adapters.betfair.parsing.streaming import market_definition_to_betfair_starting_prices from nautilus_trader.adapters.betfair.parsing.streaming import market_definition_to_instrument_closes from nautilus_trader.adapters.betfair.parsing.streaming import market_definition_to_instrument_status -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.core.data import Data from nautilus_trader.core.uuid import UUID4 from nautilus_trader.model.book import OrderBook diff --git a/tests/integration_tests/adapters/betfair/test_betfair_providers.py b/tests/integration_tests/adapters/betfair/test_betfair_providers.py index 4659d57e2e94..bebf558b5f93 100644 --- a/tests/integration_tests/adapters/betfair/test_betfair_providers.py +++ b/tests/integration_tests/adapters/betfair/test_betfair_providers.py @@ -29,7 +29,7 @@ from nautilus_trader.adapters.betfair.providers import load_markets_metadata from nautilus_trader.adapters.betfair.providers import make_instruments from nautilus_trader.adapters.betfair.providers import parse_market_catalog -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.model.enums import MarketStatus from tests.integration_tests.adapters.betfair.test_kit import BetfairResponses from tests.integration_tests.adapters.betfair.test_kit import BetfairStreaming diff --git a/tests/integration_tests/adapters/betfair/test_betting_account.py b/tests/integration_tests/adapters/betfair/test_betting_account.py index 83ac42e3c03a..0f2147162a11 100644 --- a/tests/integration_tests/adapters/betfair/test_betting_account.py +++ b/tests/integration_tests/adapters/betfair/test_betting_account.py @@ -16,7 +16,7 @@ import pytest from nautilus_trader.accounting.accounts.betting import BettingAccount -from nautilus_trader.common.clock import TestClock +from nautilus_trader.common.component import TestClock from nautilus_trader.common.factories import OrderFactory from nautilus_trader.core.uuid import UUID4 from nautilus_trader.model.currencies import GBP diff --git a/tests/integration_tests/adapters/binance/conftest.py b/tests/integration_tests/adapters/binance/conftest.py index 027df17f7dbf..1fdb36c852a3 100644 --- a/tests/integration_tests/adapters/binance/conftest.py +++ b/tests/integration_tests/adapters/binance/conftest.py @@ -19,7 +19,7 @@ from nautilus_trader.adapters.binance.common.constants import BINANCE_VENUE from nautilus_trader.adapters.binance.http.client import BinanceHttpClient -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import Logger from nautilus_trader.model.identifiers import Venue diff --git a/tests/integration_tests/adapters/binance/sandbox/sandbox_http_futures_account.py b/tests/integration_tests/adapters/binance/sandbox/sandbox_http_futures_account.py index 36e37428c388..63e90fd8263c 100644 --- a/tests/integration_tests/adapters/binance/sandbox/sandbox_http_futures_account.py +++ b/tests/integration_tests/adapters/binance/sandbox/sandbox_http_futures_account.py @@ -18,7 +18,7 @@ from nautilus_trader.adapters.binance.common.enums import BinanceAccountType from nautilus_trader.adapters.binance.factories import get_cached_binance_http_client from nautilus_trader.adapters.binance.futures.http.account import BinanceFuturesAccountHttpAPI -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock @pytest.mark.asyncio() diff --git a/tests/integration_tests/adapters/binance/sandbox/sandbox_http_futures_testnet_account.py b/tests/integration_tests/adapters/binance/sandbox/sandbox_http_futures_testnet_account.py index a695074b302b..b36765bc6df6 100644 --- a/tests/integration_tests/adapters/binance/sandbox/sandbox_http_futures_testnet_account.py +++ b/tests/integration_tests/adapters/binance/sandbox/sandbox_http_futures_testnet_account.py @@ -20,7 +20,7 @@ from nautilus_trader.adapters.binance.common.enums import BinanceAccountType from nautilus_trader.adapters.binance.factories import get_cached_binance_http_client from nautilus_trader.adapters.binance.futures.http.account import BinanceFuturesAccountHttpAPI -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock @pytest.mark.asyncio() diff --git a/tests/integration_tests/adapters/binance/sandbox/sandbox_http_futures_testnet_instrument_provider.py b/tests/integration_tests/adapters/binance/sandbox/sandbox_http_futures_testnet_instrument_provider.py index 5f186ad53f0c..48c80f3aec85 100644 --- a/tests/integration_tests/adapters/binance/sandbox/sandbox_http_futures_testnet_instrument_provider.py +++ b/tests/integration_tests/adapters/binance/sandbox/sandbox_http_futures_testnet_instrument_provider.py @@ -21,7 +21,7 @@ from nautilus_trader.adapters.binance.common.enums import BinanceAccountType from nautilus_trader.adapters.binance.factories import get_cached_binance_http_client from nautilus_trader.adapters.binance.futures.providers import BinanceFuturesInstrumentProvider -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.model.identifiers import InstrumentId from nautilus_trader.model.identifiers import Symbol diff --git a/tests/integration_tests/adapters/binance/sandbox/sandbox_http_futures_testnet_market.py b/tests/integration_tests/adapters/binance/sandbox/sandbox_http_futures_testnet_market.py index bc95668809d7..b11e13cac513 100644 --- a/tests/integration_tests/adapters/binance/sandbox/sandbox_http_futures_testnet_market.py +++ b/tests/integration_tests/adapters/binance/sandbox/sandbox_http_futures_testnet_market.py @@ -20,7 +20,7 @@ from nautilus_trader.adapters.binance.common.enums import BinanceAccountType from nautilus_trader.adapters.binance.factories import get_cached_binance_http_client from nautilus_trader.adapters.binance.futures.providers import BinanceFuturesInstrumentProvider -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock @pytest.mark.asyncio() diff --git a/tests/integration_tests/adapters/binance/sandbox/sandbox_http_futures_testnet_wallet.py b/tests/integration_tests/adapters/binance/sandbox/sandbox_http_futures_testnet_wallet.py index 9e3a1879bef8..1454fa49b0ee 100644 --- a/tests/integration_tests/adapters/binance/sandbox/sandbox_http_futures_testnet_wallet.py +++ b/tests/integration_tests/adapters/binance/sandbox/sandbox_http_futures_testnet_wallet.py @@ -21,7 +21,7 @@ from nautilus_trader.adapters.binance.common.enums import BinanceAccountType from nautilus_trader.adapters.binance.factories import get_cached_binance_http_client from nautilus_trader.adapters.binance.futures.http.wallet import BinanceFuturesWalletHttpAPI -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock @pytest.mark.asyncio() diff --git a/tests/integration_tests/adapters/binance/sandbox/sandbox_http_spot_account.py b/tests/integration_tests/adapters/binance/sandbox/sandbox_http_spot_account.py index b48f4f60e071..33645f08db7b 100644 --- a/tests/integration_tests/adapters/binance/sandbox/sandbox_http_spot_account.py +++ b/tests/integration_tests/adapters/binance/sandbox/sandbox_http_spot_account.py @@ -20,7 +20,7 @@ from nautilus_trader.adapters.binance.common.enums import BinanceAccountType from nautilus_trader.adapters.binance.factories import get_cached_binance_http_client from nautilus_trader.adapters.binance.spot.http.account import BinanceSpotAccountHttpAPI -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock @pytest.mark.asyncio() diff --git a/tests/integration_tests/adapters/binance/sandbox/sandbox_http_spot_instrument_provider.py b/tests/integration_tests/adapters/binance/sandbox/sandbox_http_spot_instrument_provider.py index 24b32f231b2f..031e86174489 100644 --- a/tests/integration_tests/adapters/binance/sandbox/sandbox_http_spot_instrument_provider.py +++ b/tests/integration_tests/adapters/binance/sandbox/sandbox_http_spot_instrument_provider.py @@ -20,7 +20,7 @@ from nautilus_trader.adapters.binance.common.enums import BinanceAccountType from nautilus_trader.adapters.binance.factories import get_cached_binance_http_client from nautilus_trader.adapters.binance.spot.providers import BinanceSpotInstrumentProvider -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock @pytest.mark.asyncio() diff --git a/tests/integration_tests/adapters/binance/sandbox/sandbox_http_spot_user.py b/tests/integration_tests/adapters/binance/sandbox/sandbox_http_spot_user.py index 169a5a425f03..7662bb0adf43 100644 --- a/tests/integration_tests/adapters/binance/sandbox/sandbox_http_spot_user.py +++ b/tests/integration_tests/adapters/binance/sandbox/sandbox_http_spot_user.py @@ -21,7 +21,7 @@ from nautilus_trader.adapters.binance.common.enums import BinanceAccountType from nautilus_trader.adapters.binance.factories import get_cached_binance_http_client from nautilus_trader.adapters.binance.spot.http.user import BinanceSpotUserDataHttpAPI -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock @pytest.mark.asyncio() diff --git a/tests/integration_tests/adapters/binance/sandbox/sandbox_http_wallet.py b/tests/integration_tests/adapters/binance/sandbox/sandbox_http_wallet.py index 6f643ceb3075..d9d36d0d3149 100644 --- a/tests/integration_tests/adapters/binance/sandbox/sandbox_http_wallet.py +++ b/tests/integration_tests/adapters/binance/sandbox/sandbox_http_wallet.py @@ -21,7 +21,7 @@ from nautilus_trader.adapters.binance.common.enums import BinanceAccountType from nautilus_trader.adapters.binance.factories import get_cached_binance_http_client from nautilus_trader.adapters.binance.spot.http.wallet import BinanceSpotWalletHttpAPI -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock @pytest.mark.asyncio() diff --git a/tests/integration_tests/adapters/binance/sandbox/sandbox_ws_futures_market.py b/tests/integration_tests/adapters/binance/sandbox/sandbox_ws_futures_market.py index 63ce477a66c5..bdb483f58475 100644 --- a/tests/integration_tests/adapters/binance/sandbox/sandbox_ws_futures_market.py +++ b/tests/integration_tests/adapters/binance/sandbox/sandbox_ws_futures_market.py @@ -18,7 +18,7 @@ import pytest from nautilus_trader.adapters.binance.websocket.client import BinanceWebSocketClient -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock @pytest.mark.asyncio() diff --git a/tests/integration_tests/adapters/binance/sandbox/sandbox_ws_spot_user.py b/tests/integration_tests/adapters/binance/sandbox/sandbox_ws_spot_user.py index 9e762ee1decb..30f02d834758 100644 --- a/tests/integration_tests/adapters/binance/sandbox/sandbox_ws_spot_user.py +++ b/tests/integration_tests/adapters/binance/sandbox/sandbox_ws_spot_user.py @@ -22,7 +22,7 @@ from nautilus_trader.adapters.binance.factories import get_cached_binance_http_client from nautilus_trader.adapters.binance.spot.http.user import BinanceSpotUserDataHttpAPI from nautilus_trader.adapters.binance.websocket.client import BinanceWebSocketClient -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock @pytest.mark.asyncio() diff --git a/tests/integration_tests/adapters/binance/test_data_spot.py b/tests/integration_tests/adapters/binance/test_data_spot.py index 220f87400df2..78ed8c409517 100644 --- a/tests/integration_tests/adapters/binance/test_data_spot.py +++ b/tests/integration_tests/adapters/binance/test_data_spot.py @@ -23,7 +23,7 @@ from nautilus_trader.adapters.binance.factories import BinanceLiveDataClientFactory from nautilus_trader.adapters.binance.http.client import BinanceHttpClient from nautilus_trader.adapters.binance.spot.providers import BinanceSpotInstrumentProvider -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.config import InstrumentProviderConfig from nautilus_trader.data.engine import DataEngine diff --git a/tests/integration_tests/adapters/binance/test_execution_futures.py b/tests/integration_tests/adapters/binance/test_execution_futures.py index b1a5d7ac975e..53f168e4b3ea 100644 --- a/tests/integration_tests/adapters/binance/test_execution_futures.py +++ b/tests/integration_tests/adapters/binance/test_execution_futures.py @@ -23,7 +23,7 @@ from nautilus_trader.adapters.binance.futures.execution import BinanceFuturesExecutionClient from nautilus_trader.adapters.binance.futures.providers import BinanceFuturesInstrumentProvider from nautilus_trader.adapters.binance.http.client import BinanceHttpClient -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.config import InstrumentProviderConfig from nautilus_trader.core.uuid import UUID4 diff --git a/tests/integration_tests/adapters/binance/test_execution_spot.py b/tests/integration_tests/adapters/binance/test_execution_spot.py index 9ffa8846eae0..96d893697844 100644 --- a/tests/integration_tests/adapters/binance/test_execution_spot.py +++ b/tests/integration_tests/adapters/binance/test_execution_spot.py @@ -26,7 +26,7 @@ from nautilus_trader.adapters.binance.http.client import BinanceHttpClient from nautilus_trader.adapters.binance.spot.execution import BinanceSpotExecutionClient from nautilus_trader.adapters.binance.spot.providers import BinanceSpotInstrumentProvider -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.config import InstrumentProviderConfig from nautilus_trader.core.nautilus_pyo3 import HttpMethod diff --git a/tests/integration_tests/adapters/binance/test_factories.py b/tests/integration_tests/adapters/binance/test_factories.py index b53d042c67b5..1a8d024dc557 100644 --- a/tests/integration_tests/adapters/binance/test_factories.py +++ b/tests/integration_tests/adapters/binance/test_factories.py @@ -29,7 +29,7 @@ from nautilus_trader.adapters.binance.spot.data import BinanceSpotDataClient from nautilus_trader.adapters.binance.spot.execution import BinanceSpotExecutionClient from nautilus_trader.cache.cache import Cache -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.test_kit.mocks.cache_database import MockCacheDatabase from nautilus_trader.test_kit.stubs.identifiers import TestIdStubs diff --git a/tests/integration_tests/adapters/binance/test_http_account.py b/tests/integration_tests/adapters/binance/test_http_account.py index b09ebf3d035b..5605483ff35b 100644 --- a/tests/integration_tests/adapters/binance/test_http_account.py +++ b/tests/integration_tests/adapters/binance/test_http_account.py @@ -22,7 +22,7 @@ from nautilus_trader.adapters.binance.http.account import BinanceOrderHttp from nautilus_trader.adapters.binance.http.client import BinanceHttpClient from nautilus_trader.adapters.binance.spot.http.account import BinanceSpotAccountHttpAPI -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock @pytest.mark.skip(reason="WIP") diff --git a/tests/integration_tests/adapters/binance/test_http_market.py b/tests/integration_tests/adapters/binance/test_http_market.py index 95afb3eedbf8..9387d2fde3bd 100644 --- a/tests/integration_tests/adapters/binance/test_http_market.py +++ b/tests/integration_tests/adapters/binance/test_http_market.py @@ -17,7 +17,7 @@ from nautilus_trader.adapters.binance.http.client import BinanceHttpClient from nautilus_trader.adapters.binance.spot.http.market import BinanceSpotMarketHttpAPI -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock @pytest.mark.skip(reason="WIP") diff --git a/tests/integration_tests/adapters/binance/test_http_user.py b/tests/integration_tests/adapters/binance/test_http_user.py index 23241b4336fb..95b734d9176a 100644 --- a/tests/integration_tests/adapters/binance/test_http_user.py +++ b/tests/integration_tests/adapters/binance/test_http_user.py @@ -19,7 +19,7 @@ from nautilus_trader.adapters.binance.common.enums import BinanceAccountType from nautilus_trader.adapters.binance.http.client import BinanceHttpClient from nautilus_trader.adapters.binance.spot.http.user import BinanceSpotUserDataHttpAPI -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock @pytest.mark.skip(reason="WIP") diff --git a/tests/integration_tests/adapters/binance/test_http_wallet.py b/tests/integration_tests/adapters/binance/test_http_wallet.py index ccb3401d47c8..074067268d0e 100644 --- a/tests/integration_tests/adapters/binance/test_http_wallet.py +++ b/tests/integration_tests/adapters/binance/test_http_wallet.py @@ -21,7 +21,7 @@ from nautilus_trader.adapters.binance.http.client import BinanceHttpClient from nautilus_trader.adapters.binance.spot.http.wallet import BinanceSpotWalletHttpAPI from nautilus_trader.adapters.binance.spot.schemas.wallet import BinanceSpotTradeFee -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock @pytest.mark.skip(reason="WIP") diff --git a/tests/integration_tests/adapters/binance/test_providers.py b/tests/integration_tests/adapters/binance/test_providers.py index a976a3d16232..66102e7e0fe8 100644 --- a/tests/integration_tests/adapters/binance/test_providers.py +++ b/tests/integration_tests/adapters/binance/test_providers.py @@ -22,7 +22,7 @@ from nautilus_trader.adapters.binance.futures.providers import BinanceFuturesInstrumentProvider from nautilus_trader.adapters.binance.http.client import BinanceHttpClient from nautilus_trader.adapters.binance.spot.providers import BinanceSpotInstrumentProvider -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.model.identifiers import InstrumentId from nautilus_trader.model.identifiers import Symbol from nautilus_trader.model.identifiers import Venue diff --git a/tests/integration_tests/adapters/bybit/conftest.py b/tests/integration_tests/adapters/bybit/conftest.py index 0e3acf644256..980e44cc5dda 100644 --- a/tests/integration_tests/adapters/bybit/conftest.py +++ b/tests/integration_tests/adapters/bybit/conftest.py @@ -20,7 +20,7 @@ from nautilus_trader.adapters.bybit.common.constants import BYBIT_VENUE from nautilus_trader.adapters.bybit.http.client import BybitHttpClient from nautilus_trader.adapters.bybit.schemas.symbol import BybitSymbol -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import Logger from nautilus_trader.model.identifiers import Venue diff --git a/tests/integration_tests/adapters/bybit/sandbox/sandbox_http_account.py b/tests/integration_tests/adapters/bybit/sandbox/sandbox_http_account.py index 94e09c8f31f2..d6117b529626 100644 --- a/tests/integration_tests/adapters/bybit/sandbox/sandbox_http_account.py +++ b/tests/integration_tests/adapters/bybit/sandbox/sandbox_http_account.py @@ -22,7 +22,7 @@ from nautilus_trader.adapters.bybit.common.enums import BybitInstrumentType from nautilus_trader.adapters.bybit.factories import get_bybit_http_client from nautilus_trader.adapters.bybit.http.account import BybitAccountHttpAPI -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock @pytest.mark.asyncio() diff --git a/tests/integration_tests/adapters/bybit/sandbox/sandbox_http_market.py b/tests/integration_tests/adapters/bybit/sandbox/sandbox_http_market.py index 6b877adaa465..e653f9c2890a 100644 --- a/tests/integration_tests/adapters/bybit/sandbox/sandbox_http_market.py +++ b/tests/integration_tests/adapters/bybit/sandbox/sandbox_http_market.py @@ -30,7 +30,7 @@ from nautilus_trader.adapters.bybit.endpoints.market.server_time import BybitServerTimeEndpoint from nautilus_trader.adapters.bybit.factories import get_bybit_http_client from nautilus_trader.adapters.bybit.http.client import BybitHttpClient -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from tests.integration_tests.adapters.bybit.utils.save_struct_to_file import save_struct_to_file diff --git a/tests/integration_tests/adapters/bybit/test_factories.py b/tests/integration_tests/adapters/bybit/test_factories.py index 0ec61cbaed25..e0396e11a045 100644 --- a/tests/integration_tests/adapters/bybit/test_factories.py +++ b/tests/integration_tests/adapters/bybit/test_factories.py @@ -27,7 +27,7 @@ from nautilus_trader.adapters.bybit.factories import _get_http_base_url from nautilus_trader.adapters.bybit.factories import _get_ws_base_url_public from nautilus_trader.cache.cache import Cache -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.test_kit.mocks.cache_database import MockCacheDatabase from nautilus_trader.test_kit.stubs.identifiers import TestIdStubs diff --git a/tests/integration_tests/adapters/bybit/test_http_account.py b/tests/integration_tests/adapters/bybit/test_http_account.py index e9af7212555c..11294e815d28 100644 --- a/tests/integration_tests/adapters/bybit/test_http_account.py +++ b/tests/integration_tests/adapters/bybit/test_http_account.py @@ -22,7 +22,7 @@ from nautilus_trader.adapters.bybit.http.account import BybitAccountHttpAPI from nautilus_trader.adapters.bybit.http.client import BybitHttpClient from nautilus_trader.adapters.bybit.schemas.account.fee_rate import BybitFeeRateResponse -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.core.nautilus_pyo3 import HttpClient from tests.integration_tests.adapters.bybit.utils.get_mock import get_mock diff --git a/tests/integration_tests/adapters/bybit/test_http_market.py b/tests/integration_tests/adapters/bybit/test_http_market.py index d8c569932431..54b87929465f 100644 --- a/tests/integration_tests/adapters/bybit/test_http_market.py +++ b/tests/integration_tests/adapters/bybit/test_http_market.py @@ -30,7 +30,7 @@ from nautilus_trader.adapters.bybit.schemas.market.ticker import BybitTickersLinearResponse from nautilus_trader.adapters.bybit.schemas.market.ticker import BybitTickersOptionResponse from nautilus_trader.adapters.bybit.schemas.market.ticker import BybitTickersSpotResponse -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.core.nautilus_pyo3 import HttpClient from tests.integration_tests.adapters.bybit.utils.get_mock import get_mock diff --git a/tests/integration_tests/adapters/bybit/test_providers.py b/tests/integration_tests/adapters/bybit/test_providers.py index 53490cc6798d..5b633d1dc3d4 100644 --- a/tests/integration_tests/adapters/bybit/test_providers.py +++ b/tests/integration_tests/adapters/bybit/test_providers.py @@ -21,7 +21,7 @@ from nautilus_trader.adapters.bybit.common.enums import BybitInstrumentType from nautilus_trader.adapters.bybit.http.client import BybitHttpClient from nautilus_trader.adapters.bybit.provider import BybitInstrumentProvider -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.config import InstrumentProviderConfig from nautilus_trader.core.nautilus_pyo3 import HttpClient from nautilus_trader.core.nautilus_pyo3 import HttpResponse diff --git a/tests/integration_tests/adapters/conftest.py b/tests/integration_tests/adapters/conftest.py index ae0974f7e6d0..cf0f0175d19c 100644 --- a/tests/integration_tests/adapters/conftest.py +++ b/tests/integration_tests/adapters/conftest.py @@ -19,9 +19,9 @@ from pytest_mock import MockerFixture from nautilus_trader.accounting.factory import AccountFactory -from nautilus_trader.common.clock import LiveClock -from nautilus_trader.common.clock import TestClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus +from nautilus_trader.common.component import TestClock from nautilus_trader.core.message import Event from nautilus_trader.data.engine import DataEngine from nautilus_trader.execution.engine import ExecutionEngine diff --git a/tests/integration_tests/adapters/databento/sandbox/sandbox_instrument_provider.py b/tests/integration_tests/adapters/databento/sandbox/sandbox_instrument_provider.py index a4e98dbb61fb..b00aadcd75bd 100644 --- a/tests/integration_tests/adapters/databento/sandbox/sandbox_instrument_provider.py +++ b/tests/integration_tests/adapters/databento/sandbox/sandbox_instrument_provider.py @@ -19,7 +19,7 @@ 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.component import LiveClock from nautilus_trader.model.identifiers import InstrumentId diff --git a/tests/integration_tests/infrastructure/test_cache_database.py b/tests/integration_tests/infrastructure/test_cache_database.py index 5b3c455daae3..c24babd1cc3c 100644 --- a/tests/integration_tests/infrastructure/test_cache_database.py +++ b/tests/integration_tests/infrastructure/test_cache_database.py @@ -24,8 +24,8 @@ from nautilus_trader.backtest.engine import BacktestEngine from nautilus_trader.backtest.engine import BacktestEngineConfig from nautilus_trader.cache.database import CacheDatabaseAdapter -from nautilus_trader.common.clock import TestClock from nautilus_trader.common.component import MessageBus +from nautilus_trader.common.component import TestClock from nautilus_trader.config import LoggingConfig from nautilus_trader.config.common import CacheConfig from nautilus_trader.config.common import DatabaseConfig diff --git a/tests/performance_tests/conftest.py b/tests/performance_tests/conftest.py index 56f14235d565..a0a7e88c9596 100644 --- a/tests/performance_tests/conftest.py +++ b/tests/performance_tests/conftest.py @@ -15,7 +15,7 @@ import pytest -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock @pytest.fixture(autouse=True) diff --git a/tests/performance_tests/test_perf_clock.py b/tests/performance_tests/test_perf_clock.py index f9089d4f85d3..792ac4df86d2 100644 --- a/tests/performance_tests/test_perf_clock.py +++ b/tests/performance_tests/test_perf_clock.py @@ -16,9 +16,9 @@ from datetime import timedelta from typing import Any -from nautilus_trader.common.clock import LiveClock -from nautilus_trader.common.clock import TestClock -from nautilus_trader.common.clock import TimeEvent +from nautilus_trader.common.component import LiveClock +from nautilus_trader.common.component import TestClock +from nautilus_trader.common.component import TimeEvent _LIVE_CLOCK = LiveClock() diff --git a/tests/performance_tests/test_perf_live_execution.py b/tests/performance_tests/test_perf_live_execution.py index 5f9f6eb7baf2..578cbcd96e9f 100644 --- a/tests/performance_tests/test_perf_live_execution.py +++ b/tests/performance_tests/test_perf_live_execution.py @@ -17,7 +17,7 @@ import pytest -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.core.uuid import UUID4 from nautilus_trader.execution.messages import SubmitOrder diff --git a/tests/performance_tests/test_perf_order.py b/tests/performance_tests/test_perf_order.py index de284efbaecc..597a2e487921 100644 --- a/tests/performance_tests/test_perf_order.py +++ b/tests/performance_tests/test_perf_order.py @@ -13,8 +13,8 @@ # limitations under the License. # ------------------------------------------------------------------------------------------------- -from nautilus_trader.common.clock import LiveClock -from nautilus_trader.common.clock import TestClock +from nautilus_trader.common.component import LiveClock +from nautilus_trader.common.component import TestClock from nautilus_trader.common.factories import OrderFactory from nautilus_trader.common.generators import ClientOrderIdGenerator from nautilus_trader.model.enums import OrderSide diff --git a/tests/performance_tests/test_perf_serialization.py b/tests/performance_tests/test_perf_serialization.py index ce56ac6147f7..65b7af8b277d 100644 --- a/tests/performance_tests/test_perf_serialization.py +++ b/tests/performance_tests/test_perf_serialization.py @@ -15,7 +15,7 @@ import msgspec -from nautilus_trader.common.clock import TestClock +from nautilus_trader.common.component import TestClock from nautilus_trader.common.factories import OrderFactory from nautilus_trader.core.uuid import UUID4 from nautilus_trader.execution.messages import SubmitOrder diff --git a/tests/unit_tests/accounting/test_cash.py b/tests/unit_tests/accounting/test_cash.py index 0c34c33c9aad..b0808ce45e53 100644 --- a/tests/unit_tests/accounting/test_cash.py +++ b/tests/unit_tests/accounting/test_cash.py @@ -16,7 +16,7 @@ import pytest from nautilus_trader.accounting.accounts.cash import CashAccount -from nautilus_trader.common.clock import TestClock +from nautilus_trader.common.component import TestClock from nautilus_trader.common.factories import OrderFactory from nautilus_trader.core.uuid import UUID4 from nautilus_trader.model.currencies import ADA diff --git a/tests/unit_tests/accounting/test_margin.py b/tests/unit_tests/accounting/test_margin.py index 6dd007c50fde..7cfbecb47725 100644 --- a/tests/unit_tests/accounting/test_margin.py +++ b/tests/unit_tests/accounting/test_margin.py @@ -17,7 +17,7 @@ import pytest -from nautilus_trader.common.clock import TestClock +from nautilus_trader.common.component import TestClock from nautilus_trader.common.factories import OrderFactory from nautilus_trader.model.currencies import BTC from nautilus_trader.model.currencies import USD diff --git a/tests/unit_tests/analysis/test_analyzer.py b/tests/unit_tests/analysis/test_analyzer.py index 1c844f91bc1a..38bb4f93a561 100644 --- a/tests/unit_tests/analysis/test_analyzer.py +++ b/tests/unit_tests/analysis/test_analyzer.py @@ -17,7 +17,7 @@ from nautilus_trader.analysis.analyzer import PortfolioAnalyzer from nautilus_trader.analysis.statistics.sharpe_ratio import SharpeRatio -from nautilus_trader.common.clock import TestClock +from nautilus_trader.common.component import TestClock from nautilus_trader.common.factories import OrderFactory from nautilus_trader.model.currencies import AUD from nautilus_trader.model.currencies import USD diff --git a/tests/unit_tests/analysis/test_reports.py b/tests/unit_tests/analysis/test_reports.py index f51ceddce61e..4333fdc8a077 100644 --- a/tests/unit_tests/analysis/test_reports.py +++ b/tests/unit_tests/analysis/test_reports.py @@ -17,7 +17,7 @@ from nautilus_trader.accounting.accounts.margin import MarginAccount from nautilus_trader.analysis.reporter import ReportProvider -from nautilus_trader.common.clock import TestClock +from nautilus_trader.common.component import TestClock from nautilus_trader.common.factories import OrderFactory from nautilus_trader.core.uuid import UUID4 from nautilus_trader.model.currencies import BTC diff --git a/tests/unit_tests/analysis/test_statistics_long_ratio.py b/tests/unit_tests/analysis/test_statistics_long_ratio.py index c95cf060f294..83106077b004 100644 --- a/tests/unit_tests/analysis/test_statistics_long_ratio.py +++ b/tests/unit_tests/analysis/test_statistics_long_ratio.py @@ -14,7 +14,7 @@ # ------------------------------------------------------------------------------------------------- from nautilus_trader.analysis.statistics.long_ratio import LongRatio -from nautilus_trader.common.clock import TestClock +from nautilus_trader.common.component import TestClock from nautilus_trader.common.factories import OrderFactory from nautilus_trader.model.enums import OrderSide from nautilus_trader.model.identifiers import PositionId diff --git a/tests/unit_tests/backtest/test_data_wranglers.py b/tests/unit_tests/backtest/test_data_wranglers.py index ca8bd4af875c..69f34a01a3e1 100644 --- a/tests/unit_tests/backtest/test_data_wranglers.py +++ b/tests/unit_tests/backtest/test_data_wranglers.py @@ -16,7 +16,7 @@ import pandas as pd -from nautilus_trader.common.clock import TestClock +from nautilus_trader.common.component import TestClock from nautilus_trader.model.enums import AggressorSide from nautilus_trader.model.identifiers import TradeId from nautilus_trader.model.objects import Price diff --git a/tests/unit_tests/backtest/test_exchange.py b/tests/unit_tests/backtest/test_exchange.py index 567b1e1a4994..0256978bbdf3 100644 --- a/tests/unit_tests/backtest/test_exchange.py +++ b/tests/unit_tests/backtest/test_exchange.py @@ -24,8 +24,8 @@ from nautilus_trader.backtest.models import LatencyModel from nautilus_trader.backtest.modules import SimulationModule from nautilus_trader.backtest.modules import SimulationModuleConfig -from nautilus_trader.common.clock import TestClock from nautilus_trader.common.component import MessageBus +from nautilus_trader.common.component import TestClock from nautilus_trader.config import ExecEngineConfig from nautilus_trader.config import RiskEngineConfig from nautilus_trader.core.datetime import secs_to_nanos diff --git a/tests/unit_tests/backtest/test_exchange_bitmex.py b/tests/unit_tests/backtest/test_exchange_bitmex.py index 7ed98316f602..d52638222524 100644 --- a/tests/unit_tests/backtest/test_exchange_bitmex.py +++ b/tests/unit_tests/backtest/test_exchange_bitmex.py @@ -19,8 +19,8 @@ from nautilus_trader.backtest.execution_client import BacktestExecClient from nautilus_trader.backtest.models import FillModel from nautilus_trader.backtest.models import LatencyModel -from nautilus_trader.common.clock import TestClock from nautilus_trader.common.component import MessageBus +from nautilus_trader.common.component import TestClock from nautilus_trader.data.engine import DataEngine from nautilus_trader.execution.engine import ExecutionEngine from nautilus_trader.model.currencies import BTC diff --git a/tests/unit_tests/backtest/test_exchange_bracket_if_touched_entries.py b/tests/unit_tests/backtest/test_exchange_bracket_if_touched_entries.py index 0fe2dc2e479d..431365d88625 100644 --- a/tests/unit_tests/backtest/test_exchange_bracket_if_touched_entries.py +++ b/tests/unit_tests/backtest/test_exchange_bracket_if_touched_entries.py @@ -21,8 +21,8 @@ from nautilus_trader.backtest.execution_client import BacktestExecClient from nautilus_trader.backtest.models import FillModel from nautilus_trader.backtest.models import LatencyModel -from nautilus_trader.common.clock import TestClock from nautilus_trader.common.component import MessageBus +from nautilus_trader.common.component import TestClock from nautilus_trader.data.engine import DataEngine from nautilus_trader.execution.emulator import OrderEmulator from nautilus_trader.execution.engine import ExecutionEngine diff --git a/tests/unit_tests/backtest/test_exchange_contingencies.py b/tests/unit_tests/backtest/test_exchange_contingencies.py index 2ef01414efef..f6c6f0988a86 100644 --- a/tests/unit_tests/backtest/test_exchange_contingencies.py +++ b/tests/unit_tests/backtest/test_exchange_contingencies.py @@ -19,8 +19,8 @@ from nautilus_trader.backtest.execution_client import BacktestExecClient from nautilus_trader.backtest.models import FillModel from nautilus_trader.backtest.models import LatencyModel -from nautilus_trader.common.clock import TestClock from nautilus_trader.common.component import MessageBus +from nautilus_trader.common.component import TestClock from nautilus_trader.data.engine import DataEngine from nautilus_trader.execution.engine import ExecutionEngine from nautilus_trader.model.currencies import ETH diff --git a/tests/unit_tests/backtest/test_exchange_l2_mbp.py b/tests/unit_tests/backtest/test_exchange_l2_mbp.py index 1c35fa644fc6..7ef4d2e351a8 100644 --- a/tests/unit_tests/backtest/test_exchange_l2_mbp.py +++ b/tests/unit_tests/backtest/test_exchange_l2_mbp.py @@ -21,8 +21,8 @@ from nautilus_trader.backtest.execution_client import BacktestExecClient from nautilus_trader.backtest.models import FillModel from nautilus_trader.backtest.models import LatencyModel -from nautilus_trader.common.clock import TestClock from nautilus_trader.common.component import MessageBus +from nautilus_trader.common.component import TestClock from nautilus_trader.data.engine import DataEngine from nautilus_trader.execution.engine import ExecutionEngine from nautilus_trader.model.book import OrderBook diff --git a/tests/unit_tests/backtest/test_exchange_stop_limits.py b/tests/unit_tests/backtest/test_exchange_stop_limits.py index dfab189ee49f..5175988c733a 100644 --- a/tests/unit_tests/backtest/test_exchange_stop_limits.py +++ b/tests/unit_tests/backtest/test_exchange_stop_limits.py @@ -19,8 +19,8 @@ from nautilus_trader.backtest.execution_client import BacktestExecClient from nautilus_trader.backtest.models import FillModel from nautilus_trader.backtest.models import LatencyModel -from nautilus_trader.common.clock import TestClock from nautilus_trader.common.component import MessageBus +from nautilus_trader.common.component import TestClock from nautilus_trader.config import ExecEngineConfig from nautilus_trader.config import RiskEngineConfig from nautilus_trader.data.engine import DataEngine diff --git a/tests/unit_tests/backtest/test_exchange_trailing_stops.py b/tests/unit_tests/backtest/test_exchange_trailing_stops.py index 113c758550d7..11520183a55d 100644 --- a/tests/unit_tests/backtest/test_exchange_trailing_stops.py +++ b/tests/unit_tests/backtest/test_exchange_trailing_stops.py @@ -21,8 +21,8 @@ from nautilus_trader.backtest.execution_client import BacktestExecClient from nautilus_trader.backtest.models import FillModel from nautilus_trader.backtest.models import LatencyModel -from nautilus_trader.common.clock import TestClock from nautilus_trader.common.component import MessageBus +from nautilus_trader.common.component import TestClock from nautilus_trader.config import ExecEngineConfig from nautilus_trader.config import RiskEngineConfig from nautilus_trader.data.engine import DataEngine diff --git a/tests/unit_tests/backtest/test_matching_engine.py b/tests/unit_tests/backtest/test_matching_engine.py index 57e1cb07b215..54d36a284a73 100644 --- a/tests/unit_tests/backtest/test_matching_engine.py +++ b/tests/unit_tests/backtest/test_matching_engine.py @@ -19,8 +19,8 @@ from nautilus_trader.backtest.matching_engine import OrderMatchingEngine from nautilus_trader.backtest.models import FillModel -from nautilus_trader.common.clock import TestClock from nautilus_trader.common.component import MessageBus +from nautilus_trader.common.component import TestClock from nautilus_trader.model.enums import BookType from nautilus_trader.model.enums import MarketStatus from nautilus_trader.model.enums import OmsType diff --git a/tests/unit_tests/cache/test_execution.py b/tests/unit_tests/cache/test_execution.py index 925a79a91ed6..70360603d6d6 100644 --- a/tests/unit_tests/cache/test_execution.py +++ b/tests/unit_tests/cache/test_execution.py @@ -20,8 +20,8 @@ from nautilus_trader.backtest.engine import BacktestEngine from nautilus_trader.backtest.engine import BacktestEngineConfig from nautilus_trader.cache.cache import Cache -from nautilus_trader.common.clock import TestClock from nautilus_trader.common.component import MessageBus +from nautilus_trader.common.component import TestClock from nautilus_trader.config import LoggingConfig from nautilus_trader.data.engine import DataEngine from nautilus_trader.examples.strategies.ema_cross import EMACross diff --git a/tests/unit_tests/common/test_actor.py b/tests/unit_tests/common/test_actor.py index e88d211d2a8c..088a9603cb17 100644 --- a/tests/unit_tests/common/test_actor.py +++ b/tests/unit_tests/common/test_actor.py @@ -19,8 +19,8 @@ from nautilus_trader.backtest.data_client import BacktestMarketDataClient from nautilus_trader.common.actor import Actor -from nautilus_trader.common.clock import TestClock from nautilus_trader.common.component import MessageBus +from nautilus_trader.common.component import TestClock from nautilus_trader.common.enums import ComponentState from nautilus_trader.common.executor import TaskId from nautilus_trader.config import ActorConfig diff --git a/tests/unit_tests/common/test_clock.py b/tests/unit_tests/common/test_clock.py index 372c836c594f..5fc112529960 100644 --- a/tests/unit_tests/common/test_clock.py +++ b/tests/unit_tests/common/test_clock.py @@ -22,10 +22,10 @@ import pytest import pytz -from nautilus_trader.common.clock import LiveClock -from nautilus_trader.common.clock import TestClock -from nautilus_trader.common.clock import TimeEvent -from nautilus_trader.common.clock import TimeEventHandler +from nautilus_trader.common.component import LiveClock +from nautilus_trader.common.component import TestClock +from nautilus_trader.common.component import TimeEvent +from nautilus_trader.common.component import TimeEventHandler from nautilus_trader.core.datetime import millis_to_nanos from nautilus_trader.test_kit.stubs.data import UNIX_EPOCH diff --git a/tests/unit_tests/common/test_events.py b/tests/unit_tests/common/test_events.py index 7fb8a6fdbc27..a22500ff9f7e 100644 --- a/tests/unit_tests/common/test_events.py +++ b/tests/unit_tests/common/test_events.py @@ -17,7 +17,7 @@ import pytest -from nautilus_trader.common.clock import TimeEvent +from nautilus_trader.common.component import TimeEvent from nautilus_trader.common.enums import ComponentState from nautilus_trader.common.messages import ComponentStateChanged from nautilus_trader.common.messages import TradingStateChanged diff --git a/tests/unit_tests/common/test_factories.py b/tests/unit_tests/common/test_factories.py index 484c58ff6014..98c0e250e4bb 100644 --- a/tests/unit_tests/common/test_factories.py +++ b/tests/unit_tests/common/test_factories.py @@ -13,7 +13,7 @@ # limitations under the License. # ------------------------------------------------------------------------------------------------- -from nautilus_trader.common.clock import TestClock +from nautilus_trader.common.component import TestClock from nautilus_trader.common.factories import OrderFactory from nautilus_trader.model.enums import OrderSide from nautilus_trader.model.identifiers import ClientOrderId diff --git a/tests/unit_tests/common/test_generators.py b/tests/unit_tests/common/test_generators.py index 61ba5c8aa963..02df0e19b384 100644 --- a/tests/unit_tests/common/test_generators.py +++ b/tests/unit_tests/common/test_generators.py @@ -13,7 +13,7 @@ # limitations under the License. # ------------------------------------------------------------------------------------------------- -from nautilus_trader.common.clock import TestClock +from nautilus_trader.common.component import TestClock from nautilus_trader.common.generators import ClientOrderIdGenerator from nautilus_trader.common.generators import OrderListIdGenerator from nautilus_trader.common.generators import PositionIdGenerator diff --git a/tests/unit_tests/common/test_throttler.py b/tests/unit_tests/common/test_throttler.py index e1ba62fccde7..2ba8aba044a0 100644 --- a/tests/unit_tests/common/test_throttler.py +++ b/tests/unit_tests/common/test_throttler.py @@ -15,7 +15,7 @@ from datetime import timedelta -from nautilus_trader.common.clock import TestClock +from nautilus_trader.common.component import TestClock from nautilus_trader.common.component import Throttler diff --git a/tests/unit_tests/data/test_aggregation.py b/tests/unit_tests/data/test_aggregation.py index ce08bd676554..f3ebe126ce05 100644 --- a/tests/unit_tests/data/test_aggregation.py +++ b/tests/unit_tests/data/test_aggregation.py @@ -19,7 +19,7 @@ import pandas as pd import pytest -from nautilus_trader.common.clock import TestClock +from nautilus_trader.common.component import TestClock from nautilus_trader.core.datetime import dt_to_unix_nanos from nautilus_trader.data.aggregation import BarBuilder from nautilus_trader.data.aggregation import TickBarAggregator diff --git a/tests/unit_tests/data/test_client.py b/tests/unit_tests/data/test_client.py index 3239b5767231..bad82a7fa3fc 100644 --- a/tests/unit_tests/data/test_client.py +++ b/tests/unit_tests/data/test_client.py @@ -13,8 +13,8 @@ # limitations under the License. # ------------------------------------------------------------------------------------------------- -from nautilus_trader.common.clock import TestClock from nautilus_trader.common.component import MessageBus +from nautilus_trader.common.component import TestClock from nautilus_trader.core.data import Data from nautilus_trader.core.uuid import UUID4 from nautilus_trader.data.client import DataClient diff --git a/tests/unit_tests/data/test_engine.py b/tests/unit_tests/data/test_engine.py index 3fab4cbefc59..914cb0ef7a2d 100644 --- a/tests/unit_tests/data/test_engine.py +++ b/tests/unit_tests/data/test_engine.py @@ -18,8 +18,8 @@ import pytest from nautilus_trader.backtest.data_client import BacktestMarketDataClient -from nautilus_trader.common.clock import TestClock from nautilus_trader.common.component import MessageBus +from nautilus_trader.common.component import TestClock from nautilus_trader.core.data import Data from nautilus_trader.core.uuid import UUID4 from nautilus_trader.data.engine import DataEngine diff --git a/tests/unit_tests/data/test_messages.py b/tests/unit_tests/data/test_messages.py index 3455c4407244..9f485e1e70df 100644 --- a/tests/unit_tests/data/test_messages.py +++ b/tests/unit_tests/data/test_messages.py @@ -15,7 +15,7 @@ import pytest -from nautilus_trader.common.clock import TestClock +from nautilus_trader.common.component import TestClock from nautilus_trader.core.data import Data from nautilus_trader.core.uuid import UUID4 from nautilus_trader.data.messages import DataRequest diff --git a/tests/unit_tests/execution/test_algorithm.py b/tests/unit_tests/execution/test_algorithm.py index 187f4d9ecc8c..c40f2657bb2a 100644 --- a/tests/unit_tests/execution/test_algorithm.py +++ b/tests/unit_tests/execution/test_algorithm.py @@ -22,9 +22,9 @@ from nautilus_trader.backtest.execution_client import BacktestExecClient from nautilus_trader.backtest.models import FillModel from nautilus_trader.cache.cache import Cache -from nautilus_trader.common.clock import TestClock -from nautilus_trader.common.clock import TimeEventHandler from nautilus_trader.common.component import MessageBus +from nautilus_trader.common.component import TestClock +from nautilus_trader.common.component import TimeEventHandler from nautilus_trader.config import DataEngineConfig from nautilus_trader.config import ExecEngineConfig from nautilus_trader.config import RiskEngineConfig diff --git a/tests/unit_tests/execution/test_client.py b/tests/unit_tests/execution/test_client.py index 48b9e36f516d..a142ea13402c 100644 --- a/tests/unit_tests/execution/test_client.py +++ b/tests/unit_tests/execution/test_client.py @@ -13,8 +13,8 @@ # limitations under the License. # ------------------------------------------------------------------------------------------------- -from nautilus_trader.common.clock import TestClock from nautilus_trader.common.component import MessageBus +from nautilus_trader.common.component import TestClock from nautilus_trader.common.factories import OrderFactory from nautilus_trader.execution.client import ExecutionClient from nautilus_trader.execution.engine import ExecutionEngine diff --git a/tests/unit_tests/execution/test_emulator.py b/tests/unit_tests/execution/test_emulator.py index 6e71d78da831..804c54dedb1b 100644 --- a/tests/unit_tests/execution/test_emulator.py +++ b/tests/unit_tests/execution/test_emulator.py @@ -18,8 +18,8 @@ import pytest from nautilus_trader.cache.cache import Cache -from nautilus_trader.common.clock import TestClock from nautilus_trader.common.component import MessageBus +from nautilus_trader.common.component import TestClock from nautilus_trader.config import DataEngineConfig from nautilus_trader.config import ExecEngineConfig from nautilus_trader.config import RiskEngineConfig diff --git a/tests/unit_tests/execution/test_emulator_list.py b/tests/unit_tests/execution/test_emulator_list.py index e640dad9b284..340fa6ee2b4a 100644 --- a/tests/unit_tests/execution/test_emulator_list.py +++ b/tests/unit_tests/execution/test_emulator_list.py @@ -22,8 +22,8 @@ from nautilus_trader.backtest.execution_client import BacktestExecClient from nautilus_trader.backtest.models import FillModel from nautilus_trader.cache.cache import Cache -from nautilus_trader.common.clock import TestClock from nautilus_trader.common.component import MessageBus +from nautilus_trader.common.component import TestClock from nautilus_trader.config import DataEngineConfig from nautilus_trader.config import ExecEngineConfig from nautilus_trader.config import RiskEngineConfig diff --git a/tests/unit_tests/execution/test_engine.py b/tests/unit_tests/execution/test_engine.py index 62b0663a4e47..ac90b576f831 100644 --- a/tests/unit_tests/execution/test_engine.py +++ b/tests/unit_tests/execution/test_engine.py @@ -16,8 +16,8 @@ import pytest from nautilus_trader.cache.cache import Cache -from nautilus_trader.common.clock import TestClock from nautilus_trader.common.component import MessageBus +from nautilus_trader.common.component import TestClock from nautilus_trader.common.factories import OrderFactory from nautilus_trader.config import ExecEngineConfig from nautilus_trader.config import StrategyConfig diff --git a/tests/unit_tests/execution/test_messages.py b/tests/unit_tests/execution/test_messages.py index 73ced3f1941c..f842d28e3a45 100644 --- a/tests/unit_tests/execution/test_messages.py +++ b/tests/unit_tests/execution/test_messages.py @@ -13,7 +13,7 @@ # limitations under the License. # ------------------------------------------------------------------------------------------------- -from nautilus_trader.common.clock import TestClock +from nautilus_trader.common.component import TestClock from nautilus_trader.common.factories import OrderFactory from nautilus_trader.core.uuid import UUID4 from nautilus_trader.execution.messages import BatchCancelOrders diff --git a/tests/unit_tests/live/test_data_client.py b/tests/unit_tests/live/test_data_client.py index 85035f2e1459..b84a257d614b 100644 --- a/tests/unit_tests/live/test_data_client.py +++ b/tests/unit_tests/live/test_data_client.py @@ -15,7 +15,7 @@ import asyncio -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.common.providers import InstrumentProvider from nautilus_trader.live.data_client import LiveDataClient diff --git a/tests/unit_tests/live/test_data_engine.py b/tests/unit_tests/live/test_data_engine.py index 337f5aa4c511..d2b22d291739 100644 --- a/tests/unit_tests/live/test_data_engine.py +++ b/tests/unit_tests/live/test_data_engine.py @@ -17,7 +17,7 @@ import pytest -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.config import LiveDataEngineConfig from nautilus_trader.core.data import Data diff --git a/tests/unit_tests/live/test_execution_engine.py b/tests/unit_tests/live/test_execution_engine.py index 6b94ff01f5fd..cc2bc524c694 100644 --- a/tests/unit_tests/live/test_execution_engine.py +++ b/tests/unit_tests/live/test_execution_engine.py @@ -18,7 +18,7 @@ import pytest -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.common.factories import OrderFactory from nautilus_trader.common.providers import InstrumentProvider diff --git a/tests/unit_tests/live/test_execution_recon.py b/tests/unit_tests/live/test_execution_recon.py index 4722561c7af1..c744d6d0ef1a 100644 --- a/tests/unit_tests/live/test_execution_recon.py +++ b/tests/unit_tests/live/test_execution_recon.py @@ -19,7 +19,7 @@ import pandas as pd import pytest -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.common.factories import OrderFactory from nautilus_trader.common.providers import InstrumentProvider diff --git a/tests/unit_tests/live/test_risk_engine.py b/tests/unit_tests/live/test_risk_engine.py index b8fe2cc2da09..dffed0e21e5f 100644 --- a/tests/unit_tests/live/test_risk_engine.py +++ b/tests/unit_tests/live/test_risk_engine.py @@ -17,7 +17,7 @@ import pytest -from nautilus_trader.common.clock import LiveClock +from nautilus_trader.common.component import LiveClock from nautilus_trader.common.component import MessageBus from nautilus_trader.common.factories import OrderFactory from nautilus_trader.config import LiveRiskEngineConfig diff --git a/tests/unit_tests/model/test_events.py b/tests/unit_tests/model/test_events.py index 10b253570e82..918149d570dc 100644 --- a/tests/unit_tests/model/test_events.py +++ b/tests/unit_tests/model/test_events.py @@ -13,7 +13,7 @@ # limitations under the License. # ------------------------------------------------------------------------------------------------- -from nautilus_trader.common.clock import TestClock +from nautilus_trader.common.component import TestClock from nautilus_trader.common.factories import OrderFactory from nautilus_trader.core.uuid import UUID4 from nautilus_trader.model.currencies import USD diff --git a/tests/unit_tests/model/test_orders.py b/tests/unit_tests/model/test_orders.py index 150115296449..0a5b8fcf141a 100644 --- a/tests/unit_tests/model/test_orders.py +++ b/tests/unit_tests/model/test_orders.py @@ -18,7 +18,7 @@ import pytest -from nautilus_trader.common.clock import TestClock +from nautilus_trader.common.component import TestClock from nautilus_trader.common.factories import OrderFactory from nautilus_trader.core.uuid import UUID4 from nautilus_trader.model.currencies import USD diff --git a/tests/unit_tests/model/test_position.py b/tests/unit_tests/model/test_position.py index a5889fe28626..20567b80aff6 100644 --- a/tests/unit_tests/model/test_position.py +++ b/tests/unit_tests/model/test_position.py @@ -17,7 +17,7 @@ import pytest -from nautilus_trader.common.clock import TestClock +from nautilus_trader.common.component import TestClock from nautilus_trader.common.factories import OrderFactory from nautilus_trader.core.uuid import UUID4 from nautilus_trader.model.currencies import BTC diff --git a/tests/unit_tests/msgbus/test_bus.py b/tests/unit_tests/msgbus/test_bus.py index e883f901ba75..08939fd05a4e 100644 --- a/tests/unit_tests/msgbus/test_bus.py +++ b/tests/unit_tests/msgbus/test_bus.py @@ -15,8 +15,8 @@ import pytest -from nautilus_trader.common.clock import TestClock from nautilus_trader.common.component import MessageBus +from nautilus_trader.common.component import TestClock from nautilus_trader.common.component import is_matching_py from nautilus_trader.core.message import Request from nautilus_trader.core.message import Response diff --git a/tests/unit_tests/portfolio/test_portfolio.py b/tests/unit_tests/portfolio/test_portfolio.py index 4c863fdb4d6d..a67bd3df0ca2 100644 --- a/tests/unit_tests/portfolio/test_portfolio.py +++ b/tests/unit_tests/portfolio/test_portfolio.py @@ -18,8 +18,8 @@ import pytest from nautilus_trader.accounting.factory import AccountFactory -from nautilus_trader.common.clock import TestClock from nautilus_trader.common.component import MessageBus +from nautilus_trader.common.component import TestClock from nautilus_trader.common.factories import OrderFactory from nautilus_trader.core.uuid import UUID4 from nautilus_trader.execution.engine import ExecutionEngine diff --git a/tests/unit_tests/risk/test_engine.py b/tests/unit_tests/risk/test_engine.py index 65a41ff7a300..115a75b830d6 100644 --- a/tests/unit_tests/risk/test_engine.py +++ b/tests/unit_tests/risk/test_engine.py @@ -18,8 +18,8 @@ import pytest -from nautilus_trader.common.clock import TestClock from nautilus_trader.common.component import MessageBus +from nautilus_trader.common.component import TestClock from nautilus_trader.common.messages import TradingStateChanged from nautilus_trader.config import ExecEngineConfig from nautilus_trader.config import RiskEngineConfig diff --git a/tests/unit_tests/serialization/test_arrow.py b/tests/unit_tests/serialization/test_arrow.py index 0ef45b031763..cd98f879896d 100644 --- a/tests/unit_tests/serialization/test_arrow.py +++ b/tests/unit_tests/serialization/test_arrow.py @@ -19,7 +19,7 @@ import pytest -from nautilus_trader.common.clock import TestClock +from nautilus_trader.common.component import TestClock from nautilus_trader.common.factories import OrderFactory from nautilus_trader.common.messages import ComponentStateChanged from nautilus_trader.common.messages import TradingStateChanged diff --git a/tests/unit_tests/serialization/test_msgpack.py b/tests/unit_tests/serialization/test_msgpack.py index 4761b60b21a9..6b36f53308b4 100644 --- a/tests/unit_tests/serialization/test_msgpack.py +++ b/tests/unit_tests/serialization/test_msgpack.py @@ -18,7 +18,7 @@ import msgspec -from nautilus_trader.common.clock import TestClock +from nautilus_trader.common.component import TestClock from nautilus_trader.common.enums import ComponentState from nautilus_trader.common.factories import OrderFactory from nautilus_trader.common.messages import ComponentStateChanged diff --git a/tests/unit_tests/trading/test_strategy.py b/tests/unit_tests/trading/test_strategy.py index f90ccfef1813..f4ee35131126 100644 --- a/tests/unit_tests/trading/test_strategy.py +++ b/tests/unit_tests/trading/test_strategy.py @@ -26,8 +26,8 @@ from nautilus_trader.backtest.execution_client import BacktestExecClient from nautilus_trader.backtest.models import FillModel from nautilus_trader.backtest.models import LatencyModel -from nautilus_trader.common.clock import TestClock from nautilus_trader.common.component import MessageBus +from nautilus_trader.common.component import TestClock from nautilus_trader.common.enums import ComponentState from nautilus_trader.config import ImportableStrategyConfig from nautilus_trader.config import StrategyConfig diff --git a/tests/unit_tests/trading/test_trader.py b/tests/unit_tests/trading/test_trader.py index 6422aad87ad9..dcf00a017020 100644 --- a/tests/unit_tests/trading/test_trader.py +++ b/tests/unit_tests/trading/test_trader.py @@ -23,8 +23,8 @@ from nautilus_trader.backtest.execution_client import BacktestExecClient from nautilus_trader.backtest.models import FillModel from nautilus_trader.common.actor import Actor -from nautilus_trader.common.clock import TestClock from nautilus_trader.common.component import MessageBus +from nautilus_trader.common.component import TestClock from nautilus_trader.config import ActorConfig from nautilus_trader.config import StrategyConfig from nautilus_trader.config.common import ExecAlgorithmConfig