OKX Stop: Add some more okx specific logic

This commit is contained in:
Matthias 2023-03-12 15:19:56 +01:00
parent d84ece7258
commit 224f289ec8
2 changed files with 29 additions and 3 deletions
freqtrade/exchange

View File

@ -1193,7 +1193,6 @@ class Exchange:
try: try:
params = self._get_stop_params(side=side, ordertype=ordertype, params = self._get_stop_params(side=side, ordertype=ordertype,
stop_price=stop_price_norm) stop_price=stop_price_norm)
# TODO: reduceOnly is invalid for OKX stop orders
if self.trading_mode == TradingMode.FUTURES: if self.trading_mode == TradingMode.FUTURES:
params['reduceOnly'] = True params['reduceOnly'] = True
if 'stoploss_price_type' in order_types and 'stop_price_type_field' in self._ft_has: if 'stoploss_price_type' in order_types and 'stop_price_type_field' in self._ft_has:

View File

@ -173,7 +173,30 @@ class Okx(Exchange):
params['posSide'] = self._get_posSide(side, True) params['posSide'] = self._get_posSide(side, True)
return params 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: 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'} params1 = {'stop': True, 'ordType': 'conditional'}
for method in (self._api.fetch_open_orders, self._api.fetch_closed_orders, for method in (self._api.fetch_open_orders, self._api.fetch_closed_orders,
self._api.fetch_canceled_orders): self._api.fetch_canceled_orders):
@ -192,9 +215,10 @@ class Okx(Exchange):
order_reg['type'] = 'stop' order_reg['type'] = 'stop'
order_reg['status_stop'] = 'triggered' order_reg['status_stop'] = 'triggered'
return order_reg return order_reg
order['type'] = 'stoploss'
return order return order
except ccxt.BaseError: except ccxt.BaseError:
logger.exception() pass
raise RetryableOrderError( raise RetryableOrderError(
f'StoplossOrder not found (pair: {pair} id: {order_id}).') f'StoplossOrder not found (pair: {pair} id: {order_id}).')
@ -204,8 +228,11 @@ class Okx(Exchange):
return order['id'] return order['id']
def cancel_stoploss_order(self, order_id: str, pair: str, params: Dict = {}) -> Dict: def cancel_stoploss_order(self, order_id: str, pair: str, params: Dict = {}) -> Dict:
params1 = {'stop': True}
# 'ordType': 'conditional'
#
return self.cancel_order( return self.cancel_order(
order_id=order_id, order_id=order_id,
pair=pair, pair=pair,
params={'ordType': 'conditional'} params=params1,
) )