Skip to content

Commit

Permalink
Refine OrderManager and reduce duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed Oct 30, 2023
1 parent 19e63fd commit fd701b2
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 52 deletions.
4 changes: 2 additions & 2 deletions nautilus_trader/execution/emulator.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ cdef class OrderEmulator(Actor):
)

if order.exec_algorithm_id is not None:
self._manager.send_algo_command(command)
self._manager.send_algo_command(command, order.exec_algorithm_id)
else:
self._manager.send_exec_command(command)

Expand Down Expand Up @@ -794,7 +794,7 @@ cdef class OrderEmulator(Actor):
)

if order.exec_algorithm_id is not None:
self._manager.send_algo_command(command)
self._manager.send_algo_command(command, order.exec_algorithm_id)
else:
self._manager.send_exec_command(command)

Expand Down
3 changes: 2 additions & 1 deletion nautilus_trader/execution/manager.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ from nautilus_trader.model.events.order cimport OrderUpdated
from nautilus_trader.model.events.position cimport PositionEvent
from nautilus_trader.model.identifiers cimport ClientId
from nautilus_trader.model.identifiers cimport ClientOrderId
from nautilus_trader.model.identifiers cimport ExecAlgorithmId
from nautilus_trader.model.identifiers cimport InstrumentId
from nautilus_trader.model.identifiers cimport PositionId
from nautilus_trader.model.identifiers cimport StrategyId
Expand Down Expand Up @@ -84,7 +85,7 @@ cdef class OrderManager:
# -- EGRESS ---------------------------------------------------------------------------------------

cpdef void send_emulator_command(self, TradingCommand command)
cpdef void send_algo_command(self, TradingCommand command)
cpdef void send_algo_command(self, TradingCommand command, ExecAlgorithmId exec_algorithm_id)
cpdef void send_risk_command(self, TradingCommand command)
cpdef void send_exec_command(self, TradingCommand command)
cpdef void send_risk_event(self, OrderEvent event)
Expand Down
11 changes: 6 additions & 5 deletions nautilus_trader/execution/manager.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ cdef class OrderManager:
The handler to call when submitting orders.
cancel_order_handler : Callable[[Order], None], optional
The handler to call when canceling orders.
modify_order_handler : Callable[[Order], None], optional
The handler to call when modifying orders.
modify_order_handler : Callable[[Order, Quantity], None], optional
The handler to call when modifying orders (limited to modifying quantity).
debug : bool, default False
If debug mode is active (will provide extra debug logging).
Expand Down Expand Up @@ -258,7 +258,7 @@ cdef class OrderManager:
self.cache_submit_order_command(submit)

if order.exec_algorithm_id is not None:
self.send_algo_command(submit)
self.send_algo_command(submit, order.exec_algorithm_id)
else:
self.send_risk_command(submit)
else:
Expand Down Expand Up @@ -561,12 +561,13 @@ cdef class OrderManager:
self._log.info(f"{CMD}{SENT} {command}.") # pragma: no cover (no logging in tests)
self._msgbus.send(endpoint="OrderEmulator.execute", msg=command)

cpdef void send_algo_command(self, TradingCommand command):
cpdef void send_algo_command(self, TradingCommand command, ExecAlgorithmId exec_algorithm_id):
Condition.not_none(command, "command")
Condition.not_none(exec_algorithm_id, "exec_algorithm_id")

if not self._log.is_bypassed:
self._log.info(f"{CMD}{SENT} {command}.") # pragma: no cover (no logging in tests)
self._msgbus.send(endpoint=f"{command.exec_algorithm_id}.execute", msg=command)
self._msgbus.send(endpoint=f"{exec_algorithm_id}.execute", msg=command)

cpdef void send_risk_command(self, TradingCommand command):
Condition.not_none(command, "command")
Expand Down
7 changes: 0 additions & 7 deletions nautilus_trader/trading/strategy.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,3 @@ cdef class Strategy(Actor):
cdef OrderPendingCancel _generate_order_pending_cancel(self, Order order)
cdef void _deny_order(self, Order order, str reason)
cdef void _deny_order_list(self, OrderList order_list, str reason)

# -- EGRESS ---------------------------------------------------------------------------------------

cdef void _send_emulator_command(self, TradingCommand command)
cdef void _send_algo_command(self, TradingCommand command, ExecAlgorithmId exec_algorithm_id)
cdef void _send_risk_command(self, TradingCommand command)
cdef void _send_exec_command(self, TradingCommand command)
52 changes: 15 additions & 37 deletions nautilus_trader/trading/strategy.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -804,11 +804,11 @@ cdef class Strategy(Actor):

# Route order
if order.emulation_trigger != TriggerType.NO_TRIGGER:
self._send_emulator_command(command)
self._manager.send_emulator_command(command)
elif order.exec_algorithm_id is not None:
self._send_algo_command(command, order.exec_algorithm_id)
self._manager.send_algo_command(command, order.exec_algorithm_id)
else:
self._send_risk_command(command)
self._manager.send_risk_command(command)

cpdef void submit_order_list(
self,
Expand Down Expand Up @@ -901,11 +901,11 @@ cdef class Strategy(Actor):

# Route order
if command.has_emulated_order:
self._send_emulator_command(command)
self._manager.send_emulator_command(command)
elif order_list.first.exec_algorithm_id is not None:
self._send_algo_command(command, order_list.first.exec_algorithm_id)
self._manager.send_algo_command(command, order_list.first.exec_algorithm_id)
else:
self._send_risk_command(command)
self._manager.send_risk_command(command)

cpdef void modify_order(
self,
Expand Down Expand Up @@ -973,9 +973,9 @@ cdef class Strategy(Actor):
return

if order.is_emulated_c():
self._send_emulator_command(command)
self._manager.send_emulator_command(command)
else:
self._send_risk_command(command)
self._manager.send_risk_command(command)

cpdef void cancel_order(self, Order order, ClientId client_id = None):
"""
Expand Down Expand Up @@ -1004,11 +1004,11 @@ cdef class Strategy(Actor):
return

if order.is_emulated_c() or order.emulation_trigger != TriggerType.NO_TRIGGER:
self._send_emulator_command(command)
self._manager.send_emulator_command(command)
elif order.exec_algorithm_id is not None and order.is_active_local_c():
self._send_algo_command(command, order.exec_algorithm_id)
self._manager.send_algo_command(command, order.exec_algorithm_id)
else:
self._send_exec_command(command)
self._manager.send_exec_command(command)

# Cancel any GTD expiry timer
if self.manage_gtd_expiry:
Expand Down Expand Up @@ -1087,7 +1087,7 @@ cdef class Strategy(Actor):
client_id=client_id,
)

self._send_exec_command(command)
self._manager.send_exec_command(command)

cpdef void cancel_all_orders(
self,
Expand Down Expand Up @@ -1185,8 +1185,8 @@ cdef class Strategy(Actor):
if order.strategy_id == self.id and not order.is_closed_c():
self.cancel_order(order)

self._send_exec_command(command)
self._send_emulator_command(command)
self._manager.send_exec_command(command)
self._manager.send_emulator_command(command)

cpdef void close_position(
self,
Expand Down Expand Up @@ -1317,7 +1317,7 @@ cdef class Strategy(Actor):
client_id=client_id,
)

self._send_exec_command(command)
self._manager.send_exec_command(command)

cdef ModifyOrder _create_modify_order(
self,
Expand Down Expand Up @@ -1666,25 +1666,3 @@ cdef class Strategy(Actor):
for order in order_list.orders:
if not order.is_closed_c():
self._deny_order(order=order, reason=reason)

# -- EGRESS ---------------------------------------------------------------------------------------

cdef void _send_emulator_command(self, TradingCommand command):
if not self.log.is_bypassed:
self.log.info(f"{CMD}{SENT} {command}.")
self._msgbus.send(endpoint="OrderEmulator.execute", msg=command)

cdef void _send_algo_command(self, TradingCommand command, ExecAlgorithmId exec_algorithm_id):
if not self.log.is_bypassed:
self.log.info(f"{CMD}{SENT} {command}.")
self._msgbus.send(endpoint=f"{exec_algorithm_id}.execute", msg=command)

cdef void _send_risk_command(self, TradingCommand command):
if not self.log.is_bypassed:
self.log.info(f"{CMD}{SENT} {command}.")
self._msgbus.send(endpoint="RiskEngine.execute", msg=command)

cdef void _send_exec_command(self, TradingCommand command):
if not self.log.is_bypassed:
self.log.info(f"{CMD}{SENT} {command}.")
self._msgbus.send(endpoint="ExecEngine.execute", msg=command)

0 comments on commit fd701b2

Please sign in to comment.