diff --git a/docs/concepts/adapters.md b/docs/concepts/adapters.md index 3ba5e3e9b1ab..c1d11ab5b416 100644 --- a/docs/concepts/adapters.md +++ b/docs/concepts/adapters.md @@ -62,6 +62,8 @@ as configured: - All instruments are automatically loaded on start: ```python +from nautilus_trader.config import InstrumentProviderConfig + InstrumentProviderConfig(load_all=True) ``` @@ -124,6 +126,10 @@ cpdef void request_instrument(self, InstrumentId instrument_id, ClientId client_ The handler on the `ExecutionClient`: ```python +from nautilus_trader.core.uuid import UUID4 +from nautilus_trader.model.data import DataType +from nautilus_trader.model.identifiers import InstrumentId + # nautilus_trader/adapters/binance/spot/data.py def request_instrument(self, instrument_id: InstrumentId, correlation_id: UUID4): instrument: Optional[Instrument] = self._instrument_provider.find(instrument_id) diff --git a/docs/concepts/data.md b/docs/concepts/data.md index d8bb8f328448..0a70447ff902 100644 --- a/docs/concepts/data.md +++ b/docs/concepts/data.md @@ -130,6 +130,10 @@ The data catalog can be initialized from a `NAUTILUS_PATH` environment variable, The following example shows how to initialize a data catalog where there is pre-existing data already written to disk at the given path. ```python +import os +from nautilus_trader.persistence.catalog import ParquetDataCatalog + + CATALOG_PATH = os.getcwd() + "/catalog" # Create a new catalog instance @@ -159,6 +163,9 @@ Rust Arrow schema implementations and available for the follow data types (enhan ### Reading data Any stored data can then we read back into memory: ```python +from nautilus_trader.core.datetime import dt_to_unix_nanos +import pandas as pd + start = dt_to_unix_nanos(pd.Timestamp("2020-01-03", tz=pytz.utc)) end = dt_to_unix_nanos(pd.Timestamp("2020-01-04", tz=pytz.utc)) @@ -170,6 +177,9 @@ When running backtests in streaming mode with a `BacktestNode`, the data catalog The following example shows how to achieve this by initializing a `BacktestDataConfig` configuration object: ```python +from nautilus_trader.config import BacktestDataConfig +from nautilus_trader.model.data import OrderBookDelta + data_config = BacktestDataConfig( catalog_path=str(catalog.path), data_cls=OrderBookDelta, diff --git a/docs/concepts/execution.md b/docs/concepts/execution.md index 8ebf0f533ecc..e4462a63e319 100644 --- a/docs/concepts/execution.md +++ b/docs/concepts/execution.md @@ -159,6 +159,8 @@ Received orders will arrive via the following `on_order(...)` method. These rece know as "primary" (original) orders when being handled by an execution algorithm. ```python +from nautilus_trader.model.orders.base import Order + def on_order(self, order: Order) -> None: # noqa (too complex) """ Actions to be performed when running and receives an order. diff --git a/docs/concepts/instruments.md b/docs/concepts/instruments.md index e520fd78b3b9..2b95546e2b90 100644 --- a/docs/concepts/instruments.md +++ b/docs/concepts/instruments.md @@ -26,13 +26,18 @@ An incorrectly specified instrument may truncate data or otherwise produce surpr Generic test instruments can be instantiated through the `TestInstrumentProvider`: ```python +from nautilus_trader.test_kit.providers import TestInstrumentProvider + audusd = TestInstrumentProvider.default_fx_ccy("AUD/USD") ``` Exchange specific instruments can be discovered from live exchange data using an adapters `InstrumentProvider`: ```python -provider = BinanceInstrumentProvider( +from nautilus_trader.adapters.binance.spot.providers import BinanceSpotInstrumentProvider +from nautilus_trader.model.identifiers import InstrumentId + +provider = BinanceSpotInstrumentProvider( client=binance_http_client, logger=live_logger, ) @@ -45,6 +50,8 @@ instrument = provider.find(btcusdt) Or flexibly defined by the user through an `Instrument` constructor, or one of its more specific subclasses: ```python +from nautilus_trader.model.instruments import Instrument + instrument = Instrument(...) # <-- provide all necessary parameters ``` See the full instrument [API Reference](../api_reference/model/instruments.md). diff --git a/docs/concepts/logging.md b/docs/concepts/logging.md index 9ae31dbab046..caced0e9183e 100644 --- a/docs/concepts/logging.md +++ b/docs/concepts/logging.md @@ -68,6 +68,9 @@ The input value should be a dictionary of component ID strings to log level stri Below is an example of a trading node logging configuration that includes some of the options mentioned above: ```python +from nautilus_trader.config import LoggingConfig +from nautilus_trader.config import TradingNodeConfig + config_node = TradingNodeConfig( trader_id="TESTER-001", logging=LoggingConfig( diff --git a/docs/concepts/orders.md b/docs/concepts/orders.md index bd59817c05a7..e1a0672a13f6 100644 --- a/docs/concepts/orders.md +++ b/docs/concepts/orders.md @@ -133,6 +133,12 @@ In the following example we create a _Market_ order on the Interactive Brokers [ to BUY 100,000 AUD using USD: ```python +from nautilus_trader.model.enums import OrderSide +from nautilus_trader.model.enums import TimeInForce +from nautilus_trader.model.identifiers import InstrumentId +from nautilus_trader.model.objects import Quantity +from nautilus_trader.model.orders import MarketOrder + order: MarketOrder = self.order_factory.market( instrument_id=InstrumentId.from_str("AUD/USD.IDEALPRO"), order_side=OrderSide.BUY, @@ -152,6 +158,13 @@ In the following example we create a _Limit_ order on the Binance Futures Crypto contracts at a limit price of 5000 USDT, as a market maker. ```python +from nautilus_trader.model.enums import OrderSide +from nautilus_trader.model.enums import TimeInForce +from nautilus_trader.model.identifiers import InstrumentId +from nautilus_trader.model.objects import Price +from nautilus_trader.model.objects import Quantity +from nautilus_trader.model.orders import LimitOrder + order: LimitOrder = self.order_factory.limit( instrument_id=InstrumentId.from_str("ETHUSDT-PERP.BINANCE"), order_side=OrderSide.SELL, @@ -176,6 +189,14 @@ In the following example we create a _Stop-Market_ order on the Binance Spot/Mar to SELL 1 BTC at a trigger price of 100,000 USDT, active until further notice: ```python +from nautilus_trader.model.enums import OrderSide +from nautilus_trader.model.enums import TimeInForce +from nautilus_trader.model.enums import TriggerType +from nautilus_trader.model.identifiers import InstrumentId +from nautilus_trader.model.objects import Price +from nautilus_trader.model.objects import Quantity +from nautilus_trader.model.orders import StopMarketOrder + order: StopMarketOrder = self.order_factory.stop_market( instrument_id=InstrumentId.from_str("BTCUSDT.BINANCE"), order_side=OrderSide.SELL, @@ -198,6 +219,15 @@ In the following example we create a _Stop-Limit_ order on the Currenex FX ECN t once the market hits the trigger price of 1.30010 USD, active until midday 6th June, 2022 (UTC): ```python +import pandas as pd +from nautilus_trader.model.enums import OrderSide +from nautilus_trader.model.enums import TimeInForce +from nautilus_trader.model.enums import TriggerType +from nautilus_trader.model.identifiers import InstrumentId +from nautilus_trader.model.objects import Price +from nautilus_trader.model.objects import Quantity +from nautilus_trader.model.orders import StopLimitOrder + order: StopLimitOrder = self.order_factory.stop_limit( instrument_id=InstrumentId.from_str("GBP/USD.CURRENEX"), order_side=OrderSide.BUY, @@ -223,6 +253,12 @@ In the following example we create a _Market-To-Limit_ order on the Interactive to BUY 200,000 USD using JPY: ```python +from nautilus_trader.model.enums import OrderSide +from nautilus_trader.model.enums import TimeInForce +from nautilus_trader.model.identifiers import InstrumentId +from nautilus_trader.model.objects import Quantity +from nautilus_trader.model.orders import MarketToLimitOrder + order: MarketToLimitOrder = self.order_factory.market_to_limit( instrument_id=InstrumentId.from_str("USD/JPY.IDEALPRO"), order_side=OrderSide.BUY, @@ -246,6 +282,14 @@ In the following example we create a _Market-If-Touched_ order on the Binance Fu to SELL 10 ETHUSDT-PERP Perpetual Futures contracts at a trigger price of 10,000 USDT, active until further notice: ```python +from nautilus_trader.model.enums import OrderSide +from nautilus_trader.model.enums import TimeInForce +from nautilus_trader.model.enums import TriggerType +from nautilus_trader.model.identifiers import InstrumentId +from nautilus_trader.model.objects import Price +from nautilus_trader.model.objects import Quantity +from nautilus_trader.model.orders import MarketIfTouchedOrder + order: MarketIfTouchedOrder = self.order_factory.market_if_touched( instrument_id=InstrumentId.from_str("ETHUSDT-PERP.BINANCE"), order_side=OrderSide.SELL, @@ -270,6 +314,15 @@ Binance Futures exchange at a limit price of 30_100 USDT (once the market hits t active until midday 6th June, 2022 (UTC): ```python +import pandas as pd +from nautilus_trader.model.enums import OrderSide +from nautilus_trader.model.enums import TimeInForce +from nautilus_trader.model.enums import TriggerType +from nautilus_trader.model.identifiers import InstrumentId +from nautilus_trader.model.objects import Price +from nautilus_trader.model.objects import Quantity +from nautilus_trader.model.orders import StopLimitOrder + order: StopLimitOrder = self.order_factory.limit_if_touched( instrument_id=InstrumentId.from_str("BTCUSDT-PERP.BINANCE"), order_side=OrderSide.BUY, @@ -296,6 +349,17 @@ In the following example we create a _Trailing-Stop-Market_ order on the Binance Perpetual Futures Contracts activating at a trigger price of 5000 USD, then trailing at an offset of 1% (in basis points) away from the current last traded price: ```python +import pandas as pd +from decimal import Decimal +from nautilus_trader.model.enums import OrderSide +from nautilus_trader.model.enums import TimeInForce +from nautilus_trader.model.enums import TriggerType +from nautilus_trader.model.enums import TrailingOffsetType +from nautilus_trader.model.identifiers import InstrumentId +from nautilus_trader.model.objects import Price +from nautilus_trader.model.objects import Quantity +from nautilus_trader.model.orders import TrailingStopMarketOrder + order: TrailingStopMarketOrder = self.order_factory.trailing_stop_market( instrument_id=InstrumentId.from_str("ETHUSD-PERP.BINANCE"), order_side=OrderSide.SELL, @@ -323,6 +387,17 @@ at a limit price of 0.72000 USD, activating at 0.71000 USD then trailing at a st away from the current ask price, active until further notice: ```python +import pandas as pd +from decimal import Decimal +from nautilus_trader.model.enums import OrderSide +from nautilus_trader.model.enums import TimeInForce +from nautilus_trader.model.enums import TriggerType +from nautilus_trader.model.enums import TrailingOffsetType +from nautilus_trader.model.identifiers import InstrumentId +from nautilus_trader.model.objects import Price +from nautilus_trader.model.objects import Quantity +from nautilus_trader.model.orders import TrailingStopLimitOrder + order: TrailingStopLimitOrder = self.order_factory.trailing_stop_limit( instrument_id=InstrumentId.from_str("AUD/USD.CURRENEX"), order_side=OrderSide.BUY, diff --git a/docs/concepts/strategies.md b/docs/concepts/strategies.md index 83c78dbc5f3b..6a8ce60e0e77 100644 --- a/docs/concepts/strategies.md +++ b/docs/concepts/strategies.md @@ -32,6 +32,8 @@ Since a trading strategy is a class which inherits from `Strategy`, you must def a constructor where you can handle initialization. Minimally the base/super class needs to be initialized: ```python +from nautilus_trader.trading.strategy import Strategy + class MyStrategy(Strategy): def __init__(self) -> None: super().__init__() # <-- the super class must be called to initialize the strategy @@ -75,6 +77,18 @@ These handlers deal with market data updates. You can use these handlers to define actions upon receiving new market data. ```python +from nautilus_trader.core.data import Data +from nautilus_trader.model.data import Bar +from nautilus_trader.model.data import QuoteTick +from nautilus_trader.model.data import TradeTick +from nautilus_trader.model.data.book import OrderBookDeltas +from nautilus_trader.model.data.status import InstrumentClose +from nautilus_trader.model.data.status import InstrumentStatus +from nautilus_trader.model.data.status import VenueStatus +from nautilus_trader.model.data.ticker import Ticker +from nautilus_trader.model.instruments import Instrument +from nautilus_trader.model.orderbook import OrderBook + def on_order_book_deltas(self, deltas: OrderBookDeltas) -> None: def on_order_book(self, order_book: OrderBook) -> None: def on_ticker(self, ticker: Ticker) -> None: @@ -99,6 +113,24 @@ Handlers in this category are triggered by events related to orders. 3. `on_event(...)` ```python +from nautilus_trader.model.events import OrderAccepted +from nautilus_trader.model.events import OrderCanceled +from nautilus_trader.model.events import OrderCancelRejected +from nautilus_trader.model.events import OrderDenied +from nautilus_trader.model.events import OrderEmulated +from nautilus_trader.model.events import OrderEvent +from nautilus_trader.model.events import OrderExpired +from nautilus_trader.model.events import OrderFilled +from nautilus_trader.model.events import OrderInitialized +from nautilus_trader.model.events import OrderModifyRejected +from nautilus_trader.model.events import OrderPendingCancel +from nautilus_trader.model.events import OrderPendingUpdate +from nautilus_trader.model.events import OrderRejected +from nautilus_trader.model.events import OrderReleased +from nautilus_trader.model.events import OrderSubmitted +from nautilus_trader.model.events import OrderTriggered +from nautilus_trader.model.events import OrderUpdated + def on_order_initialized(self, event: OrderInitialized) -> None: def on_order_denied(self, event: OrderDenied) -> None: def on_order_emulated(self, event: OrderEmulated) -> None: @@ -128,6 +160,11 @@ Handlers in this category are triggered by events related to positions. 3. `on_event(...)` ```python +from nautilus_trader.model.events import PositionChanged +from nautilus_trader.model.events import PositionClosed +from nautilus_trader.model.events import PositionEvent +from nautilus_trader.model.events import PositionOpened + def on_position_opened(self, event: PositionOpened) -> None: def on_position_changed(self, event: PositionChanged) -> None: def on_position_closed(self, event: PositionClosed) -> None: @@ -140,6 +177,8 @@ This handler will eventually receive all event messages which arrive at the stra which no other specific handler exists. ```python +from nautilus_trader.core.message import Event + def on_event(self, event: Event) -> None: ``` @@ -189,6 +228,8 @@ While there are multiple ways to obtain current timestamps, here are two commonl **UTC Timestamp:** This method returns a timezone-aware (UTC) timestamp: ```python +import pandas as pd + now: pd.Timestamp = self.clock.utc_now() ``` @@ -259,6 +300,14 @@ The following shows a general outline of available methods. #### Account and positional information ```python +import decimal + +from nautilus_trader.model.identifiers import Venue +from nautilus_trader.accounting.accounts.base import Account +from nautilus_trader.model.currency import Currency +from nautilus_trader.model.objects import Money +from nautilus_trader.model.identifiers import InstrumentId + def account(self, venue: Venue) -> Account def balances_locked(self, venue: Venue) -> dict[Currency, Money] @@ -316,6 +365,10 @@ The following examples show method implementations for a `Strategy`. This example submits a `LIMIT` BUY order for emulation (see [OrderEmulator](advanced/emulated_orders.md)): ```python +from nautilus_trader.model.enums import OrderSide +from nautilus_trader.model.enums import TriggerType +from nautilus_trader.model.orders import LimitOrder + def buy(self) -> None: """ Users simple buy method (example). @@ -337,6 +390,10 @@ It's possible to specify both order emulation, and an execution algorithm. This example submits a `MARKET` BUY order to a TWAP execution algorithm: ```python +from nautilus_trader.model.enums import OrderSide +from nautilus_trader.model.enums import TimeInForce +from nautilus_trader.model.identifiers import ExecAlgorithmId + def buy(self) -> None: """ Users simple buy method (example). @@ -444,4 +501,3 @@ example the above config would result in a strategy ID of `MyStrategy-001`. ```{tip} See the `StrategyId` [documentation](../api_reference/model/identifiers.md) for further details. ``` - diff --git a/docs/getting_started/installation.md b/docs/getting_started/installation.md index 6e2d8e3d5952..91f7ae5bea30 100644 --- a/docs/getting_started/installation.md +++ b/docs/getting_started/installation.md @@ -75,3 +75,4 @@ To install a binary wheel from GitHub, first navigate to the [latest release](ht Download the appropriate `.whl` for your operating system and Python version, then run: pip install .whl + \ No newline at end of file diff --git a/docs/integrations/betfair.md b/docs/integrations/betfair.md index e71ba17d4871..6ae6938739ee 100644 --- a/docs/integrations/betfair.md +++ b/docs/integrations/betfair.md @@ -16,6 +16,8 @@ data and execution clients. To achieve this, add a `BETFAIR` section to your cli configuration(s): ```python +from nautilus_trader.config import TradingNodeConfig + config = TradingNodeConfig( ..., # Omitted data_clients={ @@ -41,6 +43,10 @@ config = TradingNodeConfig( Then, create a `TradingNode` and add the client factories: ```python +from nautilus_trader.adapters.betfair.factories import BetfairLiveDataClientFactory +from nautilus_trader.adapters.betfair.factories import BetfairLiveExecClientFactory +from nautilus_trader.live.node import TradingNode + # Instantiate the live trading node with a configuration node = TradingNode(config=config) diff --git a/docs/integrations/binance.md b/docs/integrations/binance.md index 77a7c1152649..d37a745931db 100644 --- a/docs/integrations/binance.md +++ b/docs/integrations/binance.md @@ -70,6 +70,8 @@ data and execution clients. To achieve this, add a `BINANCE` section to your cli configuration(s): ```python +from nautilus_trader.live.node import TradingNode + config = TradingNodeConfig( ..., # Omitted data_clients={ @@ -98,6 +100,10 @@ config = TradingNodeConfig( Then, create a `TradingNode` and add the client factories: ```python +from nautilus_trader.adapters.binance.factories import BinanceLiveDataClientFactory +from nautilus_trader.adapters.binance.factories import BinanceLiveExecClientFactory +from nautilus_trader.live.node import TradingNode + # Instantiate the live trading node with a configuration node = TradingNode(config=config) @@ -197,6 +203,8 @@ configure the provider to not log the warnings, as per the client configuration example below: ```python +from nautilus_trader.config import InstrumentProviderConfig + instrument_provider=InstrumentProviderConfig( load_all=True, log_warnings=False, @@ -218,6 +226,10 @@ You can subscribe to `BinanceFuturesMarkPriceUpdate` (included funding rating in data streams by subscribing in the following way from your actor or strategy: ```python +from nautilus_trader.adapters.binance.futures.types import BinanceFuturesMarkPriceUpdate +from nautilus_trader.model.data import DataType +from nautilus_trader.model.identifiers import ClientId + # In your `on_start` method self.subscribe_data( data_type=DataType(BinanceFuturesMarkPriceUpdate, metadata={"instrument_id": self.instrument.id}), @@ -230,6 +242,8 @@ objects to your `on_data` method. You will need to check the type, as this method acts as a flexible handler for all custom/generic data. ```python +from nautilus_trader.core.data import Data + def on_data(self, data: Data): # First check the type of data if isinstance(data, BinanceFuturesMarkPriceUpdate): diff --git a/docs/integrations/ib.md b/docs/integrations/ib.md index ef2622b6befd..4cc5bd9a2e98 100644 --- a/docs/integrations/ib.md +++ b/docs/integrations/ib.md @@ -26,6 +26,11 @@ queries below for common use cases Example config: ```python +from nautilus_trader.adapters.interactive_brokers.common import IBContract +from nautilus_trader.adapters.interactive_brokers.config import InteractiveBrokersDataClientConfig +from nautilus_trader.adapters.interactive_brokers.config import InteractiveBrokersInstrumentProviderConfig +from nautilus_trader.config import TradingNodeConfig + config_node = TradingNodeConfig( data_clients={ "IB": InteractiveBrokersDataClientConfig( @@ -56,6 +61,9 @@ configuration(s) and set the environment variables to your TWS (Traders Workstat ```python import os +from nautilus_trader.adapters.interactive_brokers.config import InteractiveBrokersExecClientConfig +from nautilus_trader.config import TradingNodeConfig + config = TradingNodeConfig( data_clients={ @@ -65,7 +73,7 @@ config = TradingNodeConfig( ... # Omitted }, exec_clients = { - "IB": InteractiveBrokersExecutionClientConfig( + "IB": InteractiveBrokersExecClientConfig( username=os.getenv("TWS_USERNAME"), password=os.getenv("TWS_PASSWORD"), ... # Omitted @@ -77,6 +85,9 @@ config = TradingNodeConfig( Then, create a `TradingNode` and add the client factories: ```python +from nautilus_trader.adapters.interactive_brokers.factories import InteractiveBrokersLiveDataClientFactory +from nautilus_trader.adapters.interactive_brokers.factories import InteractiveBrokersLiveExecClientFactory + # Instantiate the live trading node with a configuration node = TradingNode(config=config)