Skip to content

Commit 8e59be1

Browse files
committed
[RestExchange] add option to edit_order_by_replacing
tmp tmp
1 parent 995390e commit 8e59be1

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

octobot_trading/exchanges/types/rest_exchange.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,46 @@ async def _edit_order(self, order_id: str, order_type: enums.TraderOrderType, sy
168168
quantity, price, stop_price, side,
169169
current_price, params)
170170

171+
async def edit_order_by_replacing(
172+
self,
173+
order_id: str,
174+
order_type: enums.TraderOrderType,
175+
symbol: str,
176+
quantity: decimal.Decimal,
177+
price: decimal.Decimal,
178+
stop_price: decimal.Decimal = None,
179+
side: enums.TradeOrderSide = None,
180+
):
181+
# Can be used if api doesnt have an endpoint to edit orders
182+
# see binance USD m as an example
183+
order_to_cancle = (
184+
self.exchange_manager.exchange_personal_data.orders_manager.get_order(
185+
order_id
186+
)
187+
)
188+
await self.cancel_order(order_id=order_id, symbol=symbol)
189+
await order_to_cancel.state.wait_for_terminate(
190+
constants.INDIVIDUAL_ORDER_SYNC_TIMEOUT
191+
)
192+
replaced_order = await self.create_order(
193+
order_type,
194+
symbol,
195+
quantity,
196+
price=stop_price or price,
197+
stop_price=stop_price,
198+
side=side,
199+
current_price=order_to_cancle.created_last_price,
200+
reduce_only=order_to_cancle.reduce_only,
201+
)
202+
if replaced_order:
203+
return replaced_order
204+
raise RuntimeError(
205+
"Not able to edit a order by replacing. The new order was not created, "
206+
f"while the original order got already cancelled. ({order_id} - "
207+
f"{order_type} - {symbol} - quantity: {quantity} - "
208+
f"price: {price} - {side})"
209+
)
210+
171211
@contextlib.asynccontextmanager
172212
async def _order_operation(self, order_type, symbol, quantity, price, stop_price):
173213
try:

0 commit comments

Comments
 (0)