From ea197b79caaceb7f4f72ff9cfe2b2e069e4a16e2 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 2 Feb 2022 20:40:40 +0100 Subject: [PATCH] Add some more logic to stoploss --- freqtrade/exchange/binance.py | 2 +- freqtrade/exchange/exchange.py | 28 +++++++++++++++++++--------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/freqtrade/exchange/binance.py b/freqtrade/exchange/binance.py index a195788dd..37ead6dd8 100644 --- a/freqtrade/exchange/binance.py +++ b/freqtrade/exchange/binance.py @@ -14,7 +14,7 @@ class Binance(Exchange): _ft_has: Dict = { "stoploss_on_exchange": True, - "stoploss_order_type": "stop_loss_limit", + "stoploss_order_types": {"limit": "stop_loss_limit"}, "order_time_in_force": ['gtc', 'fok', 'ioc'], "time_in_force_parameter": "timeInForce", "ohlcv_candle_limit": 1000, diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py index cd4c2ce83..d8644dcb9 100644 --- a/freqtrade/exchange/exchange.py +++ b/freqtrade/exchange/exchange.py @@ -791,13 +791,18 @@ class Exchange: """ raise OperationalException(f"stoploss is not implemented for {self.name}.") + def _get_stop_params(self, ordertype: str, stop_price: float) -> Dict: + params = self._params.copy() + # Verify if stopPrice works for your exchange! + params.update({'stopPrice': stop_price}) + return params + @retrier(retries=0) def stoploss(self, pair: str, amount: float, stop_price: float, order_types: Dict) -> Dict: """ creates a stoploss order. - creates a stoploss limit order. - Should an exchange support more ordertypes, the exchange should implement this method, - using `order_types.get('stoploss', 'market')` to get the correct ordertype (e.g. FTX). + requires `_ft_has['stoploss_order_types']` to be set as a dict mapping limit and market + to the corresponding exchange type. The precise ordertype is determined by the order_types dict or exchange default. @@ -812,12 +817,18 @@ class Exchange: if not self._ft_has['stoploss_on_exchange']: raise OperationalException(f"stoploss is not implemented for {self.name}.") - # Limit price threshold: As limit price should always be below stop-price + user_order_type = order_types.get('stoploss', 'market') + if user_order_type in self._ft_has["stoploss_order_types"].keys(): + ordertype = self._ft_has["stoploss_order_types"][user_order_type] + else: + # Otherwise pick only one available + ordertype = list(self._ft_has["stoploss_order_types"].values())[0] + + # if user_order_type == 'limit': + # Limit price threshold: As limit price should always be below stop-price limit_price_pct = order_types.get('stoploss_on_exchange_limit_ratio', 0.99) rate = stop_price * limit_price_pct - ordertype = self._ft_has["stoploss_order_type"] - stop_price = self.price_to_precision(pair, stop_price) # Ensure rate is less than stop price @@ -826,14 +837,13 @@ class Exchange: 'In stoploss limit order, stop price should be more than limit price') if self._config['dry_run']: + # TODO: will this work if ordertype is limit?? dry_order = self.create_dry_run_order( pair, ordertype, "sell", amount, stop_price) return dry_order try: - params = self._params.copy() - # Verify if stopPrice works for your exchange! - params.update({'stopPrice': stop_price}) + params = self._get_stop_params(ordertype=ordertype, stop_price=stop_price) amount = self.amount_to_precision(pair, amount)