Add some more logic to stoploss
This commit is contained in:
parent
1d57ce19eb
commit
ea197b79ca
@ -14,7 +14,7 @@ class Binance(Exchange):
|
|||||||
|
|
||||||
_ft_has: Dict = {
|
_ft_has: Dict = {
|
||||||
"stoploss_on_exchange": True,
|
"stoploss_on_exchange": True,
|
||||||
"stoploss_order_type": "stop_loss_limit",
|
"stoploss_order_types": {"limit": "stop_loss_limit"},
|
||||||
"order_time_in_force": ['gtc', 'fok', 'ioc'],
|
"order_time_in_force": ['gtc', 'fok', 'ioc'],
|
||||||
"time_in_force_parameter": "timeInForce",
|
"time_in_force_parameter": "timeInForce",
|
||||||
"ohlcv_candle_limit": 1000,
|
"ohlcv_candle_limit": 1000,
|
||||||
|
@ -791,13 +791,18 @@ class Exchange:
|
|||||||
"""
|
"""
|
||||||
raise OperationalException(f"stoploss is not implemented for {self.name}.")
|
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)
|
@retrier(retries=0)
|
||||||
def stoploss(self, pair: str, amount: float, stop_price: float, order_types: Dict) -> Dict:
|
def stoploss(self, pair: str, amount: float, stop_price: float, order_types: Dict) -> Dict:
|
||||||
"""
|
"""
|
||||||
creates a stoploss order.
|
creates a stoploss order.
|
||||||
creates a stoploss limit order.
|
requires `_ft_has['stoploss_order_types']` to be set as a dict mapping limit and market
|
||||||
Should an exchange support more ordertypes, the exchange should implement this method,
|
to the corresponding exchange type.
|
||||||
using `order_types.get('stoploss', 'market')` to get the correct ordertype (e.g. FTX).
|
|
||||||
|
|
||||||
The precise ordertype is determined by the order_types dict or exchange default.
|
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']:
|
if not self._ft_has['stoploss_on_exchange']:
|
||||||
raise OperationalException(f"stoploss is not implemented for {self.name}.")
|
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)
|
limit_price_pct = order_types.get('stoploss_on_exchange_limit_ratio', 0.99)
|
||||||
rate = stop_price * limit_price_pct
|
rate = stop_price * limit_price_pct
|
||||||
|
|
||||||
ordertype = self._ft_has["stoploss_order_type"]
|
|
||||||
|
|
||||||
stop_price = self.price_to_precision(pair, stop_price)
|
stop_price = self.price_to_precision(pair, stop_price)
|
||||||
|
|
||||||
# Ensure rate is less than 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')
|
'In stoploss limit order, stop price should be more than limit price')
|
||||||
|
|
||||||
if self._config['dry_run']:
|
if self._config['dry_run']:
|
||||||
|
# TODO: will this work if ordertype is limit??
|
||||||
dry_order = self.create_dry_run_order(
|
dry_order = self.create_dry_run_order(
|
||||||
pair, ordertype, "sell", amount, stop_price)
|
pair, ordertype, "sell", amount, stop_price)
|
||||||
return dry_order
|
return dry_order
|
||||||
|
|
||||||
try:
|
try:
|
||||||
params = self._params.copy()
|
params = self._get_stop_params(ordertype=ordertype, stop_price=stop_price)
|
||||||
# Verify if stopPrice works for your exchange!
|
|
||||||
params.update({'stopPrice': stop_price})
|
|
||||||
|
|
||||||
amount = self.amount_to_precision(pair, amount)
|
amount = self.amount_to_precision(pair, amount)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user