Skip to content

Commit

Permalink
Move Portfolio down to Actor
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed Dec 22, 2023
1 parent 4a638b0 commit f0370a7
Show file tree
Hide file tree
Showing 30 changed files with 181 additions and 25 deletions.
5 changes: 5 additions & 0 deletions nautilus_trader/adapters/sandbox/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
from nautilus_trader.model.objects import AccountBalance
from nautilus_trader.model.objects import Currency
from nautilus_trader.model.objects import Money
from nautilus_trader.portfolio.base import PortfolioFacade


class SandboxExecutionClient(LiveExecutionClient):
Expand All @@ -60,6 +61,8 @@ class SandboxExecutionClient(LiveExecutionClient):
----------
loop : asyncio.AbstractEventLoop
The event loop for the client.
portfolio : PortfolioFacade
The read-only portfolio for the client.
msgbus : MessageBus
The message bus for the client.
cache : Cache
Expand All @@ -76,6 +79,7 @@ class SandboxExecutionClient(LiveExecutionClient):
def __init__(
self,
loop: asyncio.AbstractEventLoop,
portfolio: PortfolioFacade,
msgbus: MessageBus,
cache: Cache,
clock: LiveClock,
Expand Down Expand Up @@ -116,6 +120,7 @@ def __init__(
leverages={},
instruments=self.INSTRUMENTS,
modules=[],
portfolio=portfolio,
msgbus=self._msgbus,
cache=cache,
fill_model=FillModel(),
Expand Down
5 changes: 5 additions & 0 deletions nautilus_trader/adapters/sandbox/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from nautilus_trader.common.component import MessageBus
from nautilus_trader.common.logging import Logger
from nautilus_trader.live.factories import LiveExecClientFactory
from nautilus_trader.portfolio import PortfolioFacade


class SandboxLiveExecClientFactory(LiveExecClientFactory):
Expand All @@ -34,6 +35,7 @@ def create( # type: ignore
loop: asyncio.AbstractEventLoop,
name: str,
config: SandboxExecutionClientConfig,
portfolio: PortfolioFacade,
msgbus: MessageBus,
cache: Cache,
clock: LiveClock,
Expand All @@ -50,6 +52,8 @@ def create( # type: ignore
The client name.
config : dict[str, object]
The configuration for the client.
portfolio : PortfolioFacade
The read-only portfolio for the client.
msgbus : MessageBus
The message bus for the client.
cache : Cache
Expand All @@ -67,6 +71,7 @@ def create( # type: ignore
exec_client = SandboxExecutionClient(
loop=loop,
clock=clock,
portfolio=portfolio,
msgbus=msgbus,
cache=cache,
logger=logger,
Expand Down
1 change: 1 addition & 0 deletions nautilus_trader/backtest/engine.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ cdef class BacktestEngine:
leverages=leverages or {},
instruments=[],
modules=modules,
portfolio=self.kernel.portfolio,
msgbus=self.kernel.msgbus,
cache=self.kernel.cache,
fill_model=fill_model,
Expand Down
5 changes: 5 additions & 0 deletions nautilus_trader/backtest/exchange.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ from nautilus_trader.model.objects cimport AccountBalance
from nautilus_trader.model.objects cimport Money
from nautilus_trader.model.objects cimport Price
from nautilus_trader.model.orders.base cimport Order
from nautilus_trader.portfolio.base cimport PortfolioFacade


cdef class SimulatedExchange:
Expand All @@ -80,6 +81,8 @@ cdef class SimulatedExchange:
The account default leverage (for margin accounts).
leverages : dict[InstrumentId, Decimal]
The instrument specific leverage configuration (for margin accounts).
portfolio : PortfolioFacade
The read-only portfolio for the exchange.
msgbus : MessageBus
The message bus for the exchange.
cache : CacheFacade
Expand Down Expand Up @@ -139,6 +142,7 @@ cdef class SimulatedExchange:
leverages not None: dict[InstrumentId, Decimal],
list instruments not None,
list modules not None,
PortfolioFacade portfolio not None,
MessageBus msgbus not None,
CacheFacade cache not None,
TestClock clock not None,
Expand Down Expand Up @@ -204,6 +208,7 @@ cdef class SimulatedExchange:
Condition.not_in(module, self.modules, "module", "modules")
module.register_venue(self)
module.register_base(
portfolio=portfolio,
msgbus=msgbus,
cache=cache,
clock=clock,
Expand Down
4 changes: 4 additions & 0 deletions nautilus_trader/common/actor.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ from nautilus_trader.model.identifiers cimport InstrumentId
from nautilus_trader.model.identifiers cimport Venue
from nautilus_trader.model.instruments.base cimport Instrument
from nautilus_trader.model.instruments.synthetic cimport SyntheticInstrument
from nautilus_trader.portfolio.base cimport PortfolioFacade


cdef class Actor(Component):
Expand All @@ -60,6 +61,8 @@ cdef class Actor(Component):
cdef dict _indicators_for_trades
cdef dict _indicators_for_bars

cdef readonly PortfolioFacade portfolio
"""The read-only portfolio for the actor.\n\n:returns: `PortfolioFacade`"""
cdef readonly config
"""The actors configuration.\n\n:returns: `NautilusConfig`"""
cdef readonly Clock clock
Expand Down Expand Up @@ -102,6 +105,7 @@ cdef class Actor(Component):

cpdef void register_base(
self,
PortfolioFacade portfolio,
MessageBus msgbus,
CacheFacade cache,
Clock clock,
Expand Down
6 changes: 6 additions & 0 deletions nautilus_trader/common/actor.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ from nautilus_trader.model.identifiers cimport InstrumentId
from nautilus_trader.model.identifiers cimport Venue
from nautilus_trader.model.instruments.base cimport Instrument
from nautilus_trader.model.instruments.synthetic cimport SyntheticInstrument
from nautilus_trader.portfolio.base cimport PortfolioFacade


cdef class Actor(Component):
Expand Down Expand Up @@ -546,6 +547,7 @@ cdef class Actor(Component):

cpdef void register_base(
self,
PortfolioFacade portfolio,
MessageBus msgbus,
CacheFacade cache,
Clock clock,
Expand All @@ -556,6 +558,8 @@ cdef class Actor(Component):
Parameters
----------
portfolio : PortfolioFacade
The read-only portfolio for the actor.
msgbus : MessageBus
The message bus for the actor.
cache : CacheFacade
Expand All @@ -570,6 +574,7 @@ cdef class Actor(Component):
System method (not intended to be called by user code).
"""
Condition.not_none(portfolio, "portfolio")
Condition.not_none(msgbus, "msgbus")
Condition.not_none(cache, "cache")
Condition.not_none(clock, "clock")
Expand All @@ -580,6 +585,7 @@ cdef class Actor(Component):
self._change_logger(logger)
self._change_msgbus(msgbus) # The trader ID is assigned here

self.portfolio = portfolio # Assigned as PortfolioFacade
self.msgbus = msgbus
self.cache = cache
self.clock = self._clock
Expand Down
3 changes: 0 additions & 3 deletions nautilus_trader/execution/algorithm.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ cdef class ExecAlgorithm(Actor):
cdef dict _exec_spawn_ids
cdef set _subscribed_strategies

cdef readonly PortfolioFacade portfolio
"""The read-only portfolio for the strategy.\n\n:returns: `PortfolioFacade`"""

# -- REGISTRATION ---------------------------------------------------------------------------------

cpdef void register(
Expand Down
3 changes: 1 addition & 2 deletions nautilus_trader/execution/algorithm.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,13 @@ cdef class ExecAlgorithm(Actor):
Condition.not_none(logger, "logger")

self.register_base(
portfolio=portfolio,
msgbus=msgbus,
cache=cache,
clock=clock,
logger=logger,
)

self.portfolio = portfolio

# Register endpoints
self._msgbus.register(endpoint=f"{self.id}.execute", handler=self.execute)

Expand Down
8 changes: 5 additions & 3 deletions nautilus_trader/execution/emulator.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ from nautilus_trader.model.objects cimport Quantity
from nautilus_trader.model.orders.base cimport Order
from nautilus_trader.model.orders.limit cimport LimitOrder
from nautilus_trader.model.orders.market cimport MarketOrder
from nautilus_trader.portfolio.base cimport PortfolioFacade


cdef set SUPPORTED_TRIGGERS = {TriggerType.DEFAULT, TriggerType.BID_ASK, TriggerType.LAST_TRADE}
Expand All @@ -83,8 +84,8 @@ cdef class OrderEmulator(Actor):
Parameters
----------
trader_id : TraderId
The trader ID for the order emulator.
portfolio : PortfolioFacade
The read-only portfolio for the order emulator.
msgbus : MessageBus
The message bus for the order emulator.
cache : Cache
Expand All @@ -100,7 +101,7 @@ cdef class OrderEmulator(Actor):

def __init__(
self,
TraderId trader_id not None,
PortfolioFacade portfolio,
MessageBus msgbus not None,
Cache cache not None,
Clock clock not None,
Expand All @@ -113,6 +114,7 @@ cdef class OrderEmulator(Actor):
super().__init__()

self.register_base(
portfolio=portfolio,
msgbus=msgbus,
cache=cache,
clock=clock,
Expand Down
5 changes: 3 additions & 2 deletions nautilus_trader/system/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ def __init__( # noqa (too complex)
self.exec_engine.load_cache()

self._emulator = OrderEmulator(
trader_id=self._trader_id,
portfolio=self._portfolio,
msgbus=self._msgbus,
cache=self._cache,
clock=self._clock,
Expand Down Expand Up @@ -419,8 +419,9 @@ def __init__( # noqa (too complex)
trader=self._trader,
)
self._controller.register_base(
cache=self._cache,
portfolio=self._portfolio,
msgbus=self._msgbus,
cache=self._cache,
clock=self._clock,
logger=self._logger,
)
Expand Down
2 changes: 0 additions & 2 deletions nautilus_trader/trading/strategy.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ from nautilus_trader.portfolio.base cimport PortfolioFacade
cdef class Strategy(Actor):
cdef OrderManager _manager

cdef readonly PortfolioFacade portfolio
"""The read-only portfolio for the strategy.\n\n:returns: `PortfolioFacade`"""
cdef readonly OrderFactory order_factory
"""The order factory for the strategy.\n\n:returns: `OrderFactory`"""
cdef readonly str order_id_tag
Expand Down
4 changes: 2 additions & 2 deletions nautilus_trader/trading/strategy.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ from nautilus_trader.model.orders.base cimport Order
from nautilus_trader.model.orders.list cimport OrderList
from nautilus_trader.model.orders.market cimport MarketOrder
from nautilus_trader.model.position cimport Position
from nautilus_trader.portfolio.base cimport PortfolioFacade


cdef class Strategy(Actor):
Expand Down Expand Up @@ -277,14 +278,13 @@ cdef class Strategy(Actor):
Condition.not_none(logger, "logger")

self.register_base(
portfolio=portfolio,
msgbus=msgbus,
cache=cache,
clock=clock,
logger=logger,
)

self.portfolio = portfolio # Assigned as PortfolioFacade

self.order_factory = OrderFactory(
trader_id=self.trader_id,
strategy_id=self.id,
Expand Down
1 change: 1 addition & 0 deletions nautilus_trader/trading/trader.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ def add_actor(self, actor: Actor) -> None:

# Wire component into trader
actor.register_base(
portfolio=self._portfolio,
msgbus=self._msgbus,
cache=self._cache,
clock=clock, # Clock per component
Expand Down
2 changes: 2 additions & 0 deletions tests/integration_tests/adapters/sandbox/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def venue() -> Venue:
def exec_client(
instrument,
event_loop,
portfolio,
msgbus,
cache,
clock,
Expand All @@ -41,6 +42,7 @@ def exec_client(
SandboxExecutionClient.INSTRUMENTS = [instrument]
return SandboxExecutionClient(
loop=event_loop,
portfolio=portfolio,
msgbus=msgbus,
cache=cache,
clock=clock,
Expand Down
2 changes: 2 additions & 0 deletions tests/integration_tests/infrastructure/test_cache_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ async def test_update_actor(self):
# Arrange
actor = MockActor()
actor.register_base(
portfolio=self.portfolio,
msgbus=self.msgbus,
cache=self.cache,
clock=self.clock,
Expand Down Expand Up @@ -1018,6 +1019,7 @@ async def test_delete_actor(self):
# Arrange, Act
actor = MockActor()
actor.register_base(
portfolio=self.portfolio,
msgbus=self.msgbus,
cache=self.cache,
clock=self.clock,
Expand Down
3 changes: 3 additions & 0 deletions tests/unit_tests/backtest/test_exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ def setup(self) -> None:
instruments=[USDJPY_SIM],
modules=[],
fill_model=FillModel(),
portfolio=self.portfolio,
msgbus=self.msgbus,
cache=self.cache,
clock=self.clock,
Expand Down Expand Up @@ -2629,6 +2630,7 @@ def test_adjust_account_when_account_frozen_does_not_change_balance(self) -> Non
instruments=[USDJPY_SIM],
modules=[],
fill_model=FillModel(),
portfolio=self.portfolio,
msgbus=self.msgbus,
cache=self.cache,
clock=self.clock,
Expand Down Expand Up @@ -2938,6 +2940,7 @@ def reset(self):
instruments=[USDJPY_SIM],
modules=[self.module],
fill_model=FillModel(),
portfolio=self.portfolio,
msgbus=self.msgbus,
cache=self.cache,
clock=self.clock,
Expand Down
1 change: 1 addition & 0 deletions tests/unit_tests/backtest/test_exchange_bitmex.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def setup(self):
starting_balances=[Money(20, BTC)],
default_leverage=Decimal(50),
leverages={},
portfolio=self.portfolio,
msgbus=self.msgbus,
cache=self.cache,
instruments=[XBTUSD_BITMEX],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def setup(self):
)

self.emulator = OrderEmulator(
trader_id=self.trader_id,
portfolio=self.portfolio,
msgbus=self.msgbus,
cache=self.cache,
clock=self.clock,
Expand All @@ -121,6 +121,7 @@ def setup(self):
instruments=[ETHUSDT_PERP_BINANCE],
modules=[],
fill_model=FillModel(),
portfolio=self.portfolio,
msgbus=self.msgbus,
cache=self.cache,
clock=self.clock,
Expand Down
1 change: 1 addition & 0 deletions tests/unit_tests/backtest/test_exchange_contingencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def setup(self):
instruments=[ETHUSDT_PERP_BINANCE],
modules=[],
fill_model=FillModel(),
portfolio=self.portfolio,
msgbus=self.msgbus,
cache=self.cache,
clock=self.clock,
Expand Down
1 change: 1 addition & 0 deletions tests/unit_tests/backtest/test_exchange_l2_mbp.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ def setup(self):
instruments=[USDJPY_SIM],
modules=[],
fill_model=FillModel(),
portfolio=self.portfolio,
msgbus=self.msgbus,
cache=self.cache,
clock=self.clock,
Expand Down
Loading

0 comments on commit f0370a7

Please sign in to comment.