stoploss on exchange canceled handled

This commit is contained in:
Misagh 2019-04-04 17:13:54 +02:00
parent 54f11ad603
commit a363d443bf
2 changed files with 63 additions and 31 deletions

View File

@ -690,46 +690,78 @@ class FreqtradeBot(object):
is enabled. is enabled.
""" """
result = False logger.debug('Handling stoploss on exchange %s ...', trade)
try: try:
# If trade is open and the buy order is fulfilled but there is no stoploss, # First we check if there is already a stoploss on exchange
# then we add a stoploss on exchange stoploss_order = self.exchange.get_order(trade.stoploss_order_id, trade.pair) \
if not trade.open_order_id and not trade.stoploss_order_id: if trade.stoploss_order_id else None
if self.edge: except DependencyException as exception:
stoploss = self.edge.stoploss(pair=trade.pair) logger.warning('Unable to fetch stoploss order: %s', exception)
else:
stoploss = self.strategy.stoploss
stop_price = trade.open_rate * (1 + stoploss)
# limit price should be less than stop price. # If there open order id does not exist,
# 0.99 is arbitrary here. # it means buy order is fulfilled
limit_price = stop_price * 0.99 buy_order_fulfilled = not trade.open_order_id
# Limit price threshold
# This is the limit price percentage below which you don't want to sell
# 0.99 is arbitrary here.
# As limit price should always be below price
limit_price_pct = 0.99
# If buy order is fulfilled but there is no stoploss,
# then we add a stoploss on exchange
if (buy_order_fulfilled and not stoploss_order):
if self.edge:
stoploss = self.edge.stoploss(pair=trade.pair)
else:
stoploss = self.strategy.stoploss
stop_price = trade.open_rate * (1 + stoploss)
# limit price should be less than stop price.
limit_price = stop_price * limit_price_pct
try:
stoploss_order_id = self.exchange.stoploss_limit( stoploss_order_id = self.exchange.stoploss_limit(
pair=trade.pair, amount=trade.amount, stop_price=stop_price, rate=limit_price pair=trade.pair, amount=trade.amount, stop_price=stop_price, rate=limit_price
)['id'] )['id']
trade.stoploss_order_id = str(stoploss_order_id) trade.stoploss_order_id = str(stoploss_order_id)
trade.stoploss_last_update = datetime.now() trade.stoploss_last_update = datetime.now()
return False
# Or the trade open and there is already a stoploss on exchange. except DependencyException as exception:
# so we check if it is hit ... logger.warning('Unable to place a stoploss order on exchange: %s', exception)
elif trade.stoploss_order_id:
logger.debug('Handling stoploss on exchange %s ...', trade)
order = self.exchange.get_order(trade.stoploss_order_id, trade.pair) # If stoploss order is canceled for some reason we add it
if order['status'] == 'closed': if stoploss_order and stoploss_order['status'] == 'canceled':
trade.sell_reason = SellType.STOPLOSS_ON_EXCHANGE.value try:
trade.update(order) stoploss_order_id = self.exchange.stoploss_limit(
self.notify_sell(trade) pair=trade.pair, amount=trade.amount,
result = True stop_price=trade.stop_loss, rate=trade.stop_loss * limit_price_pct
elif self.config.get('trailing_stop', False): )['id']
# if trailing stoploss is enabled we check if stoploss value has changed trade.stoploss_order_id = str(stoploss_order_id)
# in which case we cancel stoploss order and put another one with new return False
# value immediately except DependencyException as exception:
self.handle_trailing_stoploss_on_exchange(trade, order) logger.warning('Stoploss order was cancelled, but unable to recreate one: %s', exception)
except DependencyException as exception:
logger.warning('Unable to create stoploss order: %s', exception) # We check if stoploss order is fulfilled
return result if stoploss_order and stoploss_order['status'] == 'closed':
trade.sell_reason = SellType.STOPLOSS_ON_EXCHANGE.value
trade.update(stoploss_order)
self.notify_sell(trade)
return True
# Finally we check if stoploss on exchange should be moved up because of trailing.
if stoploss_order and self.config.get('trailing_stop', False):
# if trailing stoploss is enabled we check if stoploss value has changed
# in which case we cancel stoploss order and put another one with new
# value immediately
self.handle_trailing_stoploss_on_exchange(trade, stoploss_order)
return False
def handle_trailing_stoploss_on_exchange(self, trade: Trade, order): def handle_trailing_stoploss_on_exchange(self, trade: Trade, order):
""" """

View File

@ -1036,7 +1036,7 @@ def test_handle_stoploss_on_exchange(mocker, default_conf, fee, caplog,
side_effect=DependencyException() side_effect=DependencyException()
) )
freqtrade.handle_stoploss_on_exchange(trade) freqtrade.handle_stoploss_on_exchange(trade)
assert log_has('Unable to create stoploss order: ', caplog.record_tuples) assert log_has('Unable to place a stoploss order on exchange: ', caplog.record_tuples)
def test_handle_stoploss_on_exchange_trailing(mocker, default_conf, fee, caplog, def test_handle_stoploss_on_exchange_trailing(mocker, default_conf, fee, caplog,