From 224f289ec8fc6d91ee32e2a3d90f4749d4da9ca0 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 12 Mar 2023 15:19:56 +0100 Subject: [PATCH] OKX Stop: Add some more okx specific logic --- freqtrade/exchange/exchange.py | 1 - freqtrade/exchange/okx.py | 31 +++++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py index 728e997f1..e5f897c2a 100644 --- a/freqtrade/exchange/exchange.py +++ b/freqtrade/exchange/exchange.py @@ -1193,7 +1193,6 @@ class Exchange: try: params = self._get_stop_params(side=side, ordertype=ordertype, stop_price=stop_price_norm) - # TODO: reduceOnly is invalid for OKX stop orders if self.trading_mode == TradingMode.FUTURES: params['reduceOnly'] = True if 'stoploss_price_type' in order_types and 'stop_price_type_field' in self._ft_has: diff --git a/freqtrade/exchange/okx.py b/freqtrade/exchange/okx.py index 5acf039cb..5acfe7fcc 100644 --- a/freqtrade/exchange/okx.py +++ b/freqtrade/exchange/okx.py @@ -173,7 +173,30 @@ class Okx(Exchange): params['posSide'] = self._get_posSide(side, True) return params + def stoploss_adjust(self, stop_loss: float, order: Dict, side: str) -> bool: + """ + OKX uses non-default stoploss price naming. + """ + if not self._ft_has.get('stoploss_on_exchange'): + raise OperationalException(f"stoploss is not implemented for {self.name}.") + + return ( + order.get('stopLossPrice', None) is None + or ((side == "sell" and stop_loss > float(order['stopLossPrice'])) or + (side == "buy" and stop_loss < float(order['stopLossPrice']))) + ) + def fetch_stoploss_order(self, order_id: str, pair: str, params: Dict = {}) -> Dict: + if self._config['dry_run']: + return self.fetch_dry_run_order(order_id) + + try: + params1 = {'stop': True} + order_reg = self._api.fetch_order(order_id, pair, params=params1) + self._log_exchange_response('fetch_stoploss_order1', order_reg) + return order_reg + except ccxt.OrderNotFound: + pass params1 = {'stop': True, 'ordType': 'conditional'} for method in (self._api.fetch_open_orders, self._api.fetch_closed_orders, self._api.fetch_canceled_orders): @@ -192,9 +215,10 @@ class Okx(Exchange): order_reg['type'] = 'stop' order_reg['status_stop'] = 'triggered' return order_reg + order['type'] = 'stoploss' return order except ccxt.BaseError: - logger.exception() + pass raise RetryableOrderError( f'StoplossOrder not found (pair: {pair} id: {order_id}).') @@ -204,8 +228,11 @@ class Okx(Exchange): return order['id'] def cancel_stoploss_order(self, order_id: str, pair: str, params: Dict = {}) -> Dict: + params1 = {'stop': True} + # 'ordType': 'conditional' + # return self.cancel_order( order_id=order_id, pair=pair, - params={'ordType': 'conditional'} + params=params1, )