Merge pull request #2772 from freqtrade/safe_sell_amount_update_wallet

Safe sell amount update wallet
This commit is contained in:
hroff-1902 2020-01-16 00:15:34 +03:00 committed by GitHub
commit 889929f782
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 4 deletions

View File

@ -902,15 +902,19 @@ class FreqtradeBot:
:return: amount to sell :return: amount to sell
:raise: DependencyException: if available balance is not within 2% of the available amount. :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]) 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: if wallet_amount >= amount:
return amount return amount
elif wallet_amount > amount * 0.98: elif wallet_amount > amount * 0.98:
logger.info(f"{pair} - Falling back to wallet-amount.") logger.info(f"{pair} - Falling back to wallet-amount.")
return wallet_amount return wallet_amount
else: 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: def execute_sell(self, trade: Trade, limit: float, sell_reason: SellType) -> None:
""" """

View File

@ -513,6 +513,7 @@ def test_rpc_forcesell(default_conf, ticker, fee, mocker) -> None:
), ),
get_fee=fee, get_fee=fee,
) )
mocker.patch('freqtrade.wallets.Wallets.get_free', return_value=1000)
freqtradebot = get_patched_freqtradebot(mocker, default_conf) freqtradebot = get_patched_freqtradebot(mocker, default_conf)
patch_get_signal(freqtradebot, (True, False)) patch_get_signal(freqtradebot, (True, False))

View File

@ -2750,6 +2750,7 @@ def test__safe_sell_amount(default_conf, fee, caplog, mocker):
amount = 95.33 amount = 95.33
amount_wallet = 95.29 amount_wallet = 95.29
mocker.patch('freqtrade.wallets.Wallets.get_free', MagicMock(return_value=amount_wallet)) mocker.patch('freqtrade.wallets.Wallets.get_free', MagicMock(return_value=amount_wallet))
wallet_update = mocker.patch('freqtrade.wallets.Wallets.update')
trade = Trade( trade = Trade(
pair='LTC/ETH', pair='LTC/ETH',
amount=amount, amount=amount,
@ -2762,11 +2763,15 @@ def test__safe_sell_amount(default_conf, fee, caplog, mocker):
freqtrade = FreqtradeBot(default_conf) freqtrade = FreqtradeBot(default_conf)
patch_get_signal(freqtrade) patch_get_signal(freqtrade)
wallet_update.reset_mock()
assert freqtrade._safe_sell_amount(trade.pair, trade.amount) == amount_wallet assert freqtrade._safe_sell_amount(trade.pair, trade.amount) == amount_wallet
assert log_has_re(r'.*Falling back to wallet-amount.', caplog) assert log_has_re(r'.*Falling back to wallet-amount.', caplog)
assert wallet_update.call_count == 1
caplog.clear() caplog.clear()
wallet_update.reset_mock()
assert freqtrade._safe_sell_amount(trade.pair, amount_wallet) == amount_wallet assert freqtrade._safe_sell_amount(trade.pair, amount_wallet) == amount_wallet
assert not log_has_re(r'.*Falling back to wallet-amount.', caplog) 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): def test__safe_sell_amount_error(default_conf, fee, caplog, mocker):

View File

@ -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 # Only order for 3rd trade needs to be cancelled
assert cancel_order_mock.call_count == 1 assert cancel_order_mock.call_count == 1
# Wallets should only be called once per sell cycle # Wallets must be updated between stoploss cancellation and selling.
assert wallets_mock.call_count == 1 assert wallets_mock.call_count == 2
trade = trades[0] trade = trades[0]
assert trade.sell_reason == SellType.STOPLOSS_ON_EXCHANGE.value assert trade.sell_reason == SellType.STOPLOSS_ON_EXCHANGE.value