From 8e59be1befa989c2427481172cb04d463d81d9ba Mon Sep 17 00:00:00 2001 From: Max Date: Sat, 18 Mar 2023 10:41:30 +0000 Subject: [PATCH] [RestExchange] add option to edit_order_by_replacing tmp tmp --- .../exchanges/types/rest_exchange.py | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/octobot_trading/exchanges/types/rest_exchange.py b/octobot_trading/exchanges/types/rest_exchange.py index 9f0e8281d5..e42e492124 100644 --- a/octobot_trading/exchanges/types/rest_exchange.py +++ b/octobot_trading/exchanges/types/rest_exchange.py @@ -168,6 +168,46 @@ async def _edit_order(self, order_id: str, order_type: enums.TraderOrderType, sy quantity, price, stop_price, side, current_price, params) + async def edit_order_by_replacing( + self, + order_id: str, + order_type: enums.TraderOrderType, + symbol: str, + quantity: decimal.Decimal, + price: decimal.Decimal, + stop_price: decimal.Decimal = None, + side: enums.TradeOrderSide = None, + ): + # Can be used if api doesnt have an endpoint to edit orders + # see binance USD m as an example + order_to_cancle = ( + self.exchange_manager.exchange_personal_data.orders_manager.get_order( + order_id + ) + ) + await self.cancel_order(order_id=order_id, symbol=symbol) + await order_to_cancel.state.wait_for_terminate( + constants.INDIVIDUAL_ORDER_SYNC_TIMEOUT + ) + replaced_order = await self.create_order( + order_type, + symbol, + quantity, + price=stop_price or price, + stop_price=stop_price, + side=side, + current_price=order_to_cancle.created_last_price, + reduce_only=order_to_cancle.reduce_only, + ) + if replaced_order: + return replaced_order + raise RuntimeError( + "Not able to edit a order by replacing. The new order was not created, " + f"while the original order got already cancelled. ({order_id} - " + f"{order_type} - {symbol} - quantity: {quantity} - " + f"price: {price} - {side})" + ) + @contextlib.asynccontextmanager async def _order_operation(self, order_type, symbol, quantity, price, stop_price): try: