def test_cannot_cancel_filled_order(): instrument_id = "AAPL" order_direction = OrderDirection.buy quantity = 100 price = 10 limit_order = LimitOrder(instrument_id=instrument_id, order_direction=order_direction, quantity=quantity, price=price) limit_order.order_id = 1 limit_order.status = OrderStatus.filled instrument_id = "AAPL" order_id = 1 cancel_order = CancelOrder(instrument_id=instrument_id, order_id=order_id, order_direction=order_direction) cancel_order.cancel_order(order=limit_order) assert limit_order.status == OrderStatus.filled, "Test failed: order cancelled" assert not cancel_order.cancel_success, "Test Failed: order cancelled" pass
def add_cancel(self, order: CancelOrder) -> None: """ Cancelling an existing order Check all orders to find the first matching order_id and cancel it if possible. """ if order.order_direction == OrderDirection.buy and self.best_bid is not None: best_bid = self.best_bid bids = self.bids if order.order_id == best_bid.order_id: order.cancel_order(best_bid) self.complete_orders.append(best_bid) if bids: self.best_bid = bids.pop(0) self.attempt_match = True else: self.best_bid = None elif bids: matched_order = self.find_in_list(bids, order.order_id) if matched_order: bids.remove(matched_order) self.complete_orders.append(matched_order) order.cancel_order(matched_order) elif order.order_direction == OrderDirection.sell and self.best_ask is not None: best_ask = self.best_ask asks = self.asks if order.order_id == best_ask.order_id: order.cancel_order(best_ask) self.complete_orders.append(best_ask) if self.asks: self.best_ask = self.asks.pop(0) self.attempt_match = True else: self.best_ask = None elif asks: matched_order = self.find_in_list(asks, order.order_id) if matched_order: asks.remove(matched_order) self.complete_orders.append(matched_order) order.cancel_order(matched_order) return None