Repository: nautechsystems/nautilus_trader
File: nautilus_trader/adapters/bybit/execution.py (Python)

Code before:
                if not self._check_order_validity(order, product_type):
                    self._log.error(f"Error on {command}")
                    return  # Do not submit batch

                match order.order_type:
                    case OrderType.MARKET:
                        batch_order = self._create_market_batch_order(order)
                    case OrderType.LIMIT:
                        batch_order = self._create_limit_batch_order(order)
                    case OrderType.LIMIT_IF_TOUCHED:
                        batch_order = self._create_limit_if_touched_batch_order(order)
                    case OrderType.STOP_MARKET:
                        batch_order = self._create_stop_market_batch_order(order)
                    case OrderType.MARKET_IF_TOUCHED:
                        batch_order = self._create_market_if_touched_batch_order(order)
                    case _:
                        self._log.error(f"Unsupported order type for 'submit_order_list': {order}")
                        self._log.error(f"Error on {command}")
                        return

                submit_orders.append(batch_order)

            now_ns = self._clock.timestamp_ns()
            for order in batch_submits:
                self.generate_order_submitted(
                    strategy_id=order.strategy_id,
                    instrument_id=order.instrument_id,
                    client_order_id=order.client_order_id,
                    ts_event=now_ns,
                )

            async with self._retry_manager_pool as retry_manager:
                await retry_manager.run(
                    "submit_order_list",
                    None,
                    self._order_batch_client.batch_place_orders,
                    product_type=product_type,
                    submit_orders=submit_orders,
                )

    def _check_order_validity(self, order: Order, product_type: BybitProductType) -> bool:
        # Check post only
        if order.is_post_only and order.order_type != OrderType.LIMIT:
            self._log.error(
                f"Cannot submit {order} has invalid post only {order.is_post_only}, unsupported on Bybit",
            )
            return False

        # Check reduce only
        if order.is_reduce_only and product_type == BybitProductType.SPOT:
            self._log.error(

Review comment (performance):
If we're optimizing for performance, then this creates a separate method call for every order.
I think it's also clearer from an event perspective that the orders in the batch were submitted together?

Code after:
                    return  # Do not submit batch

                match order.order_type:
                    case OrderType.MARKET:
                        batch_order = self._create_market_batch_order(order)
                    case OrderType.LIMIT:
                        batch_order = self._create_limit_batch_order(order)
                    case OrderType.LIMIT_IF_TOUCHED:
                        batch_order = self._create_limit_if_touched_batch_order(order)
                    case OrderType.STOP_MARKET:
                        batch_order = self._create_stop_market_batch_order(order)
                    case OrderType.MARKET_IF_TOUCHED:
                        batch_order = self._create_market_if_touched_batch_order(order)
                    case _:
                        self._log.error(f"Unsupported order type for 'submit_order_list': {order}")
                        self._log.error(f"Error on {command}")
                        return

                submit_orders.append(batch_order)

            for order in batch_submits:
                self.generate_order_submitted(
                    strategy_id=order.strategy_id,
                    instrument_id=order.instrument_id,
                    client_order_id=order.client_order_id,
                    ts_event=self._clock.timestamp_ns(),
                )

            async with self._retry_manager_pool as retry_manager:
                await retry_manager.run(
                    "submit_order_list",
                    None,
                    self._order_batch_client.batch_place_orders,
                    product_type=product_type,
                    submit_orders=submit_orders,
                )

    def _check_order_validity(self, order: Order, product_type: BybitProductType) -> bool:
        # Check post only
        if order.is_post_only and order.order_type != OrderType.LIMIT:
            self._log.error(
                f"Cannot submit {order} has invalid post only {order.is_post_only}, unsupported on Bybit",
            )
            return False

        # Check reduce only
        if order.is_reduce_only and product_type == BybitProductType.SPOT:
            self._log.error(
                f"Cannot submit {order} is reduce_only, unsupported on Bybit SPOT",
            )
            return False