Repository: freqtrade/freqtrade

Issue:
Unable to sell trade: Not enough amount to sell.
Hi!
I use latest build of freqtrade: 09621b3. OS ubuntu, exchange Binance.
On all sales order it write:
freqtrade.freqtradebot - WARNING - Unable to sell trade: Not enough amount to sell.





Discussion:
We'll need the logs of the buy of this trade - as well as a few lines prior to this sell (unless that's only "pings").


Also, what is the amount in the database - and what is the amount it on the exchange?
What are the configurations (which order_types are you using, stoploss_on_exchange, limit / market, ...).


Log:
```
2020-01-15 20:17:26,765 - freqtrade.rpc.rpc_manager - INFO - Sending rpc message: {'type': buy, 'exchange': 'Binance', 'pair': 'INS/BTC', 'limit': 2.188e-05, 'order_type': 'limit', 'stake_amount': 0.0719427852, 'stake_currency': 'BTC', 'fiat_currency': 'USD'}
2020-01-15 20:17:27,750 - freqtrade.wallets - INFO - Wallets synced.
2020-01-15 20:17:28,254 - freqtrade.freqtradebot - INFO - Bot heartbeat. PID=11486
...................
2020-01-15 20:25:15,449 - freqtrade.freqtradebot - INFO - Found open order for Trade(id=33, pair=INS/BTC, amount=3288.06148080, open_rate=0.00002188, open_since=2020-01-15 17:17:26)
2020-01-15 20:25:16,225 - freqtrade.persistence - INFO - Updating trade (id=33) ...
2020-01-15 20:25:16,226 - freqtrade.persistence - INFO - LIMIT_BUY has been fulfilled for Trade(id=33, pair=INS/BTC, amount=3288.00000000, open_rate=0.00002188, open_since=2020-01-15 17:17:26).
2020-01-15 20:26:02,123 - freqtrade.freqtradebot - INFO - Bot heartbeat. PID=11486
2020-01-15 20:27:33,323 - freqtrade.exchange.exchange - INFO - returning cached ticker-data for INS/BTC
2020-01-15 20:27:41,228 - freqtrade.exchange.exchange - INFO - returning cached ticker-data for INS/BTC
2020-01-15 20:28:12,203 - freqtrade.freqtradebot - INFO - Bot heartbeat. PID=11486
2020-01-15 20:28:23,873 - freqtrade.exchange.exchange - INFO - returning cached ticker-data for INS/BTC
2020-01-15 20:28:52,238 - freqtrade.rpc.rpc - WARNING -  Could not get rate for pair VTHO.
2020-01-15 20:28:59,999 - freqtrade.rpc.telegram - INFO - Executing handler: _status for chat_id: 549974653
2020-01-15 20:29:00,002 - freqtrade.exchange.exchange - INFO - returning cached ticker-data for INS/BTC
2020-01-15 20:29:17,262 - freqtrade.freqtradebot - INFO - Bot heartbeat. PID=11486
2020-01-15 20:35:18,740 - freqtrade.freqtradebot - WARNING - Unable to sell trade: Not enough amount to sell.
2020-01-15 20:35:19,444 - freqtrade.freqtradebot - WARNING - Unable to sell trade: Not enough amount to sell.
2020-01-15 20:35:24,075 - freqtrade.freqtradebot - WARNING - Unable to sell trade: Not enough amount to sell.
```
----------------------------------------------------

Sum on exchange was 3288, on DB this record (I already have  close it thrue build 2019.11), sum 3288.0 :
```
33|binance|INS/BTC|0|0.001|0.001|2.188e-05|2.188e-05|0.07201338144|2.1266496350365e-05|2.129e-05|-0.02998145|0.0719427852|3288.0|2020-01-15 17:17:26.765563|2020-01-15 18:25:47.567588||2.1308932e-05|-0.0261|2.1308932e-05|-0.0261||2020-01-15 17:25:16.720136|2.222e-05|2.076e-05|stop_loss|ststrat_opt5|5
```
---------------------------------------------------
My config:
"order_types": {
        "buy": "limit",
        "sell": "limit",
        "emergencysell": "market",
        "stoploss": "market",
        "stoploss_on_exchange": false,
        "stoploss_on_exchange_interval": 60
    },
    "order_time_in_force": {
        "buy": "gtc",
        "sell": "gtc"
    }
Thanks for the additional info.

#2771 will/should fix this - it's a stupid oversight of not comparing for equality ...



Resolving patch:
diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py
--- a/freqtrade/freqtradebot.py
+++ b/freqtrade/freqtradebot.py
@@ -902,15 +902,19 @@ def _safe_sell_amount(self, pair: str, amount: float) -> float:
         :return: amount to sell
         :raise: DependencyException: if available balance is not within 2% of the available amount.
         """
+        # Update wallets to ensure amounts tied up in a stoploss is now free!
+        self.wallets.update()
+
         wallet_amount = self.wallets.get_free(pair.split('/')[0])
-        logger.info(f"Selling {pair} - Wallet: {wallet_amount} - Trade-amount: {amount}")
+        logger.debug(f"{pair} - Wallet: {wallet_amount} - Trade-amount: {amount}")
         if wallet_amount >= amount:
             return amount
         elif wallet_amount > amount * 0.98:
             logger.info(f"{pair} - Falling back to wallet-amount.")
             return wallet_amount
         else:
-            raise DependencyException("Not enough amount to sell.")
+            raise DependencyException(
+                f"Not enough amount to sell. Trade-amount: {amount}, Wallet: {wallet_amount}")
 
     def execute_sell(self, trade: Trade, limit: float, sell_reason: SellType) -> None:
         """


Test patch:
diff --git a/tests/rpc/test_rpc.py b/tests/rpc/test_rpc.py
--- a/tests/rpc/test_rpc.py
+++ b/tests/rpc/test_rpc.py
@@ -513,6 +513,7 @@ def test_rpc_forcesell(default_conf, ticker, fee, mocker) -> None:
         ),
         get_fee=fee,
     )
+    mocker.patch('freqtrade.wallets.Wallets.get_free', return_value=1000)
 
     freqtradebot = get_patched_freqtradebot(mocker, default_conf)
     patch_get_signal(freqtradebot, (True, False))
diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py
--- a/tests/test_freqtradebot.py
+++ b/tests/test_freqtradebot.py
@@ -2750,6 +2750,7 @@ def test__safe_sell_amount(default_conf, fee, caplog, mocker):
     amount = 95.33
     amount_wallet = 95.29
     mocker.patch('freqtrade.wallets.Wallets.get_free', MagicMock(return_value=amount_wallet))
+    wallet_update = mocker.patch('freqtrade.wallets.Wallets.update')
     trade = Trade(
         pair='LTC/ETH',
         amount=amount,
@@ -2762,11 +2763,15 @@ def test__safe_sell_amount(default_conf, fee, caplog, mocker):
     freqtrade = FreqtradeBot(default_conf)
     patch_get_signal(freqtrade)
 
+    wallet_update.reset_mock()
     assert freqtrade._safe_sell_amount(trade.pair, trade.amount) == amount_wallet
     assert log_has_re(r'.*Falling back to wallet-amount.', caplog)
+    assert wallet_update.call_count == 1
     caplog.clear()
+    wallet_update.reset_mock()
     assert freqtrade._safe_sell_amount(trade.pair, amount_wallet) == amount_wallet
     assert not log_has_re(r'.*Falling back to wallet-amount.', caplog)
+    assert wallet_update.call_count == 1
 
 
 def test__safe_sell_amount_error(default_conf, fee, caplog, mocker):
diff --git a/tests/test_integration.py b/tests/test_integration.py
--- a/tests/test_integration.py
+++ b/tests/test_integration.py
@@ -97,8 +97,8 @@ def test_may_execute_sell_stoploss_on_exchange_multi(default_conf, ticker, fee,
 
     # Only order for 3rd trade needs to be cancelled
     assert cancel_order_mock.call_count == 1
-    # Wallets should only be called once per sell cycle
-    assert wallets_mock.call_count == 1
+    # Wallets must be updated between stoploss cancellation and selling.
+    assert wallets_mock.call_count == 2
 
     trade = trades[0]
     assert trade.sell_reason == SellType.STOPLOSS_ON_EXCHANGE.value