Repository: freqtrade/freqtrade

Issue:
Binance - Crash caused by "Unknown order sent" 
## Step 1: Have you search for this issue before posting it?
Yes, related issue here: https://github.com/freqtrade/freqtrade/issues/3208

## Step 2: Describe your environment

  * Operating system: Raspbian 2020-02-13
  * Python Version: 3.7.3 (`python -V`)
  * CCXT version: ccxt==1.27.1 (`pip freeze | grep ccxt`)
  * Branch:  Develop
  * Last Commit ID: 25d9001f8864d6a641037f7d6f8e0177df18d752 (`git log --format="%H" -n 1`)
  
## Step 3: Describe the problem:
Bot runs into an Error while live trading on Binance:
> ERROR - Could not cancel order. Message: binance {"code":-2011,"msg":"Unknown order sent."}
My sell config is: 
"ask_strategy": 
{
"price_side": "ask",
"use_order_book": true,
"order_book_min": 1,
"order_book_max": 5,
"use_sell_signal": true,
"sell_profit_only": false,
"ignore_roi_if_buy_signal": false
},

### Relevant code exceptions or logs:

  ```
2020-05-08 01:08:41,485 - freqtrade.worker - INFO - Bot heartbeat. PID=3083, version='develop-25d9001f', state='RUNNING'
2020-05-08 01:09:42,183 - freqtrade.worker - INFO - Bot heartbeat. PID=3083, version='develop-25d9001f', state='RUNNING'
2020-05-08 01:10:43,760 - freqtrade.worker - INFO - Bot heartbeat. PID=3083, version='develop-25d9001f', state='RUNNING'
2020-05-08 01:11:00,121 - freqtrade.freqtradebot - INFO - Executing Sell for KNC/ETH. Reason: SellType.ROI
2020-05-08 01:11:00,941 - freqtrade.wallets - INFO - Wallets synced.
2020-05-08 01:11:01,474 - freqtrade.freqtradebot - INFO - Getting price from order book Ask side.
2020-05-08 01:11:01,921 - freqtrade.rpc.rpc_manager - INFO - Sending rpc message: {'type': sell, 'exchange': 'Binance', 'pair': 'KNC/ETH', 'gain': 'profit', 'limit': 0.0029946, 'order_type': 'limit', 'amount': 49.0, 'open_rate': 0.0029714, 'current_rate': 0.0029859, 'profit_amount': 0.00084447, 'profit_ratio': 0.00579417, 'sell_reason': 'roi', 'open_date': datetime.datetime(2020, 5, 7, 22, 56, 16, 859404), 'close_date': datetime.datetime(2020, 5, 7, 23, 11, 1, 921528), 'stake_currency': 'ETH', 'fiat_currency': 'EUR'}
2020-05-08 01:11:02,991 - freqtrade.wallets - INFO - Wallets synced.
2020-05-08 01:11:17,821 - freqtrade.freqtradebot - INFO - Found open order for Trade(id=42, pair=KNC/ETH, amount=49.00000000, open_rate=0.00297140, open_since=2020-05-07 22:56:16)
2020-05-08 01:11:28,632 - freqtrade.freqtradebot - INFO - Found open order for Trade(id=42, pair=KNC/ETH, amount=49.00000000, open_rate=0.00297140, open_since=2020-05-07 22:56:16)
2020-05-08 01:11:32,104 - freqtrade.freqtradebot - INFO - Found open order for Trade(id=42, pair=KNC/ETH, amount=49.00000000, open_rate=0.00297140, open_since=2020-05-07 22:56:16)
2020-05-08 01:11:37,105 - freqtrade.freqtradebot - INFO - Found open order for Trade(id=42, pair=KNC/ETH, amount=49.00000000, open_rate=0.00297140, open_since=2020-05-07 22:56:16)
2020-05-08 01:11:42,116 - freqtrade.freqtradebot - INFO - Found open order for Trade(id=42, pair=KNC/ETH, amount=49.00000000, open_rate=0.00297140, open_since=2020-05-07 22:56:16)
2020-05-08 01:11:46,787 - freqtrade.worker - INFO - Bot heartbeat. PID=3083, version='develop-25d9001f', state='RUNNING'
2020-05-08 01:11:47,112 - freqtrade.freqtradebot - INFO - Found open order for Trade(id=42, pair=KNC/ETH, amount=49.00000000, open_rate=0.00297140, open_since=2020-05-07 22:56:16)
2020-05-08 01:11:52,113 - freqtrade.freqtradebot - INFO - Found open order for Trade(id=42, pair=KNC/ETH, amount=49.00000000, open_rate=0.00297140, open_since=2020-05-07 22:56:16)
2020-05-08 01:11:57,116 - freqtrade.freqtradebot - INFO - Found open order for Trade(id=42, pair=KNC/ETH, amount=49.00000000, open_rate=0.00297140, open_since=2020-05-07 22:56:16)
2020-05-08 01:12:15,073 - freqtrade.freqtradebot - INFO - Found open order for Trade(id=42, pair=KNC/ETH, amount=49.00000000, open_rate=0.00297140, open_since=2020-05-07 22:56:16)
2020-05-08 01:12:28,972 - freqtrade.freqtradebot - INFO - Found open order for Trade(id=42, pair=KNC/ETH, amount=49.00000000, open_rate=0.00297140, open_since=2020-05-07 22:56:16)
2020-05-08 01:12:33,517 - freqtrade.freqtradebot - INFO - Found open order for Trade(id=42, pair=KNC/ETH, amount=49.00000000, open_rate=0.00297140, open_since=2020-05-07 22:56:16)
2020-05-08 01:12:38,522 - freqtrade.freqtradebot - INFO - Found open order for Trade(id=42, pair=KNC/ETH, amount=49.00000000, open_rate=0.00297140, open_since=2020-05-07 22:56:16)
2020-05-08 01:12:43,555 - freqtrade.freqtradebot - INFO - Found open order for Trade(id=42, pair=KNC/ETH, amount=49.00000000, open_rate=0.00297140, open_since=2020-05-07 22:56:16)
2020-05-08 01:12:48,205 - freqtrade.worker - INFO - Bot heartbeat. PID=3083, version='develop-25d9001f', state='RUNNING'
2020-05-08 01:12:48,566 - freqtrade.freqtradebot - INFO - Found open order for Trade(id=42, pair=KNC/ETH, amount=49.00000000, open_rate=0.00297140, open_since=2020-05-07 22:56:16)
2020-05-08 01:12:53,556 - freqtrade.freqtradebot - INFO - Found open order for Trade(id=42, pair=KNC/ETH, amount=49.00000000, open_rate=0.00297140, open_since=2020-05-07 22:56:16)
2020-05-08 01:12:58,563 - freqtrade.freqtradebot - INFO - Found open order for Trade(id=42, pair=KNC/ETH, amount=49.00000000, open_rate=0.00297140, open_since=2020-05-07 22:56:16)
2020-05-08 01:13:16,926 - freqtrade.freqtradebot - INFO - Found open order for Trade(id=42, pair=KNC/ETH, amount=49.00000000, open_rate=0.00297140, open_since=2020-05-07 22:56:16)
2020-05-08 01:13:28,346 - freqtrade.freqtradebot - INFO - Found open order for Trade(id=42, pair=KNC/ETH, amount=49.00000000, open_rate=0.00297140, open_since=2020-05-07 22:56:16)
2020-05-08 01:13:32,515 - freqtrade.freqtradebot - INFO - Found open order for Trade(id=42, pair=KNC/ETH, amount=49.00000000, open_rate=0.00297140, open_since=2020-05-07 22:56:16)
2020-05-08 01:13:37,493 - freqtrade.freqtradebot - INFO - Found open order for Trade(id=42, pair=KNC/ETH, amount=49.00000000, open_rate=0.00297140, open_since=2020-05-07 22:56:16)
2020-05-08 01:13:42,504 - freqtrade.freqtradebot - INFO - Found open order for Trade(id=42, pair=KNC/ETH, amount=49.00000000, open_rate=0.00297140, open_since=2020-05-07 22:56:16)
2020-05-08 01:13:47,503 - freqtrade.freqtradebot - INFO - Found open order for Trade(id=42, pair=KNC/ETH, amount=49.00000000, open_rate=0.00297140, open_since=2020-05-07 22:56:16)
2020-05-08 01:13:52,182 - freqtrade.worker - INFO - Bot heartbeat. PID=3083, version='develop-25d9001f', state='RUNNING'
2020-05-08 01:13:52,509 - freqtrade.freqtradebot - INFO - Found open order for Trade(id=42, pair=KNC/ETH, amount=49.00000000, open_rate=0.00297140, open_since=2020-05-07 22:56:16)
2020-05-08 01:13:57,507 - freqtrade.freqtradebot - INFO - Found open order for Trade(id=42, pair=KNC/ETH, amount=49.00000000, open_rate=0.00297140, open_since=2020-05-07 22:56:16)
2020-05-08 01:14:15,745 - freqtrade.freqtradebot - INFO - Found open order for Trade(id=42, pair=KNC/ETH, amount=49.00000000, open_rate=0.00297140, open_since=2020-05-07 22:56:16)
2020-05-08 01:14:16,254 - freqtrade.commands.trade_commands - INFO - worker found ... calling exit
2020-05-08 01:14:16,254 - freqtrade.rpc.rpc_manager - INFO - Sending rpc message: {'type': status, 'status': 'process died'}
2020-05-08 01:14:16,317 - freqtrade.freqtradebot - INFO - Cleaning up modules ...
2020-05-08 01:14:16,317 - freqtrade.rpc.rpc_manager - INFO - Cleaning up rpc modules ...
2020-05-08 01:14:32,299 - freqtrade - ERROR - Could not cancel order. Message: binance {"code":-2011,"msg":"Unknown order sent."}

  ```



Discussion:
There should be a stacktrace printed right after the last line ...

without that it's not possible to determine where the error came from - so identifying the problem  will not be possible in that case.
Sorry but there was no stack trace, it just went back to the prompt from there. Is there some additional logging I have to activate to get a stacktrace?
![aa](https://user-images.githubusercontent.com/64589799/81389033-7a487400-9119-11ea-9a60-baf695920850.png)
So just to be sure, thats what is shown in the terminal. 
no ... that should be printed by default.

Either way - what exactly did happen (on the exchange)  with this trade?

has freqtrade cancelled it? did you cancel it yourself ?  has it been filled (?) 

It was filled on the exchange and after restarting the bot it printed this:

> 2020-05-08 09:17:02,294 - freqtrade.persistence - INFO - Updating trade (id=42) ...                                                                                   
> 2020-05-08 09:17:02,296 - freqtrade.persistence - INFO - Marking Trade(id=42, pair=KNC/ETH, amount=49.00000000, open_rate=0.00297140, open_since=closed) as closed as t
> he trade is fulfilled and found no open orders for it.                                                                                                                
> 2020-05-08 09:17:02,297 - freqtrade.persistence - INFO - LIMIT_SELL has been fulfilled for Trade(id=42, pair=KNC/ETH, amount=49.00000000, open_rate=0.00297140, open_si
> nce=closed).                                                                                                                                                          
> 2020-05-08 09:17:03,359 - freqtrade.wallets - INFO - Wallets synced.                                                                                                   
> 2020-05-08 09:17:03,679 - freqtrade.freqtradebot - WARNING - Unable to sell trade: Attempt to handle closed trade: Trade(id=42, pair=KNC/ETH, amount=49.00000000, open_
> rate=0.00297140, open_since=closed)                                                                                                                                    
> 2020-05-08 09:17:07,420 - freqtrade.worker - INFO - Bot heartbeat. PID=4694, version='develop-25d9001f', state='RUNNING'                                              
> 2020-05-08 09:18:16,066 - freqtrade.worker - INFO - Bot heartbeat. PID=4694, version='develop-25d9001f', state='RUNNING'                                              
> 2
hm interresting ... will have a look at it shortly.

Resolving patch:
diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py
--- a/freqtrade/freqtradebot.py
+++ b/freqtrade/freqtradebot.py
@@ -949,8 +949,12 @@ def handle_timedout_limit_sell(self, trade: Trade, order: Dict) -> str:
         if order['remaining'] == order['amount'] or order.get('filled') == 0.0:
             if not self.exchange.check_order_canceled_empty(order):
                 reason = "cancelled due to timeout"
-                # if trade is not partially completed, just delete the trade
-                self.exchange.cancel_order(trade.open_order_id, trade.pair)
+                try:
+                    # if trade is not partially completed, just delete the trade
+                    self.exchange.cancel_order(trade.open_order_id, trade.pair)
+                except InvalidOrderException:
+                    logger.exception(f"Could not cancel sell order {trade.open_order_id}")
+                    return 'error cancelling order'
                 logger.info('Sell order %s for %s.', reason, trade)
             else:
                 reason = "cancelled on exchange"


Test patch:
diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py
--- a/tests/test_freqtradebot.py
+++ b/tests/test_freqtradebot.py
@@ -2405,6 +2405,21 @@ def test_handle_timedout_limit_sell(mocker, default_conf) -> None:
     assert cancel_order_mock.call_count == 1
 
 
+def test_handle_timedout_limit_sell_cancel_exception(mocker, default_conf) -> None:
+    patch_RPCManager(mocker)
+    patch_exchange(mocker)
+    mocker.patch(
+        'freqtrade.exchange.Exchange.cancel_order', side_effect=InvalidOrderException())
+
+    freqtrade = FreqtradeBot(default_conf)
+
+    trade = MagicMock()
+    order = {'remaining': 1,
+             'amount': 1,
+             'status': "open"}
+    assert freqtrade.handle_timedout_limit_sell(trade, order) == 'error cancelling order'
+
+
 def test_execute_sell_up(default_conf, ticker, fee, ticker_sell_up, mocker) -> None:
     rpc_mock = patch_RPCManager(mocker)
     patch_exchange(mocker)