Skip to content

Commit a3aa8b5

Browse files
committed
Refine arrangement of CancelAllOrders commands
Reduce redundant commands.
1 parent 49514a7 commit a3aa8b5

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

nautilus_trader/trading/strategy.pyx

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,17 +1198,6 @@ cdef class Strategy(Actor):
11981198

11991199
self.cache.update_order(order)
12001200

1201-
cdef CancelAllOrders command = CancelAllOrders(
1202-
trader_id=self.trader_id,
1203-
strategy_id=self.id,
1204-
instrument_id=instrument_id,
1205-
order_side=order_side,
1206-
command_id=UUID4(),
1207-
ts_init=self.clock.timestamp_ns(),
1208-
client_id=client_id,
1209-
params=params,
1210-
)
1211-
12121201
# Cancel all execution algorithm orders
12131202
cdef set exec_algorithm_ids = self.cache.exec_algorithm_ids()
12141203

@@ -1220,8 +1209,33 @@ cdef class Strategy(Actor):
12201209
if order.strategy_id == self.id and not order.is_closed_c():
12211210
self.cancel_order(order)
12221211

1223-
self._manager.send_exec_command(command)
1224-
self._manager.send_emulator_command(command)
1212+
cdef CancelAllOrders command
1213+
1214+
if open_count > 0:
1215+
command = CancelAllOrders(
1216+
trader_id=self.trader_id,
1217+
strategy_id=self.id,
1218+
instrument_id=instrument_id,
1219+
order_side=order_side,
1220+
command_id=UUID4(),
1221+
ts_init=self.clock.timestamp_ns(),
1222+
client_id=client_id,
1223+
params=params,
1224+
)
1225+
self._manager.send_exec_command(command)
1226+
1227+
if emulated_count > 0:
1228+
command = CancelAllOrders(
1229+
trader_id=self.trader_id,
1230+
strategy_id=self.id,
1231+
instrument_id=instrument_id,
1232+
order_side=order_side,
1233+
command_id=UUID4(),
1234+
ts_init=self.clock.timestamp_ns(),
1235+
client_id=client_id,
1236+
params=params,
1237+
)
1238+
self._manager.send_emulator_command(command)
12251239

12261240
cpdef void close_position(
12271241
self,

tests/acceptance_tests/test_backtest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ def test_run_ema_cross_stop_entry_trail_strategy_with_emulation(self):
367367
self.engine.run()
368368

369369
# Assert
370-
assert self.engine.kernel.msgbus.sent_count == 74_084
370+
assert self.engine.kernel.msgbus.sent_count == 74_083
371371
assert self.engine.kernel.msgbus.pub_count == 468_652
372372
assert strategy.fast_ema.count == 41_761
373373
assert self.engine.iteration == 120_468
@@ -867,7 +867,7 @@ def test_run_market_maker(self):
867867
self.engine.run()
868868

869869
# Assert
870-
assert self.engine.kernel.msgbus.sent_count == 17_312
870+
assert self.engine.kernel.msgbus.sent_count == 16_575
871871
assert self.engine.kernel.msgbus.pub_count == 16_146
872872
assert self.engine.iteration == 4_216
873873
account = self.engine.portfolio.account(self.venue)

0 commit comments

Comments
 (0)