From 3bcc60333d8ea9d46d6b22af0f0e9cf936d905e6 Mon Sep 17 00:00:00 2001 From: NatanNMB15 Date: Sun, 21 Apr 2019 13:49:07 -0300 Subject: [PATCH 1/4] Added command for Wallets Sync after a trade is closed in "update_trade" method in "freqtradebot" class, this will help the Wallets get updated after a trade is sold and closed, specifically LIMIT_SELL trades, then bot can work properly with new trades. --- freqtrade/freqtradebot.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 038510755..6a825eadd 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -522,6 +522,10 @@ class FreqtradeBot(object): trade.update(order) + # Updating wallets when order is closed + if trade.is_open == False: + self.wallets.update() + def get_sell_rate(self, pair: str, refresh: bool) -> float: """ Get sell rate - either using get-ticker bid or first bid based on orderbook From 706b30f4d245da345833d9c3b88ad9ccc9283767 Mon Sep 17 00:00:00 2001 From: NatanNMB15 Date: Sun, 21 Apr 2019 14:20:28 -0300 Subject: [PATCH 2/4] Fix "if" condition with "if not" for check if trade is open. --- freqtrade/freqtradebot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 6a825eadd..2fe55336f 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -523,7 +523,7 @@ class FreqtradeBot(object): trade.update(order) # Updating wallets when order is closed - if trade.is_open == False: + if not trade.is_open: self.wallets.update() def get_sell_rate(self, pair: str, refresh: bool) -> float: From a9de2f80f21b0b18da074db59bcefefb7fb271fd Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 22 Apr 2019 13:31:07 +0200 Subject: [PATCH 3/4] Add tests to update wallets after closing a limit-sell --- freqtrade/tests/test_freqtradebot.py | 31 +++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/freqtrade/tests/test_freqtradebot.py b/freqtrade/tests/test_freqtradebot.py index 103c0777e..008974a6b 100644 --- a/freqtrade/tests/test_freqtradebot.py +++ b/freqtrade/tests/test_freqtradebot.py @@ -1407,7 +1407,8 @@ def test_update_trade_state_withorderdict(default_conf, trades_for_order, limit_ amount=amount, exchange='binance', open_rate=0.245441, - open_order_id="123456" + open_order_id="123456", + is_open=True, ) freqtrade.update_trade_state(trade, limit_buy_order) assert trade.amount != amount @@ -1432,6 +1433,34 @@ def test_update_trade_state_exception(mocker, default_conf, assert log_has('Could not update trade amount: ', caplog.record_tuples) +def test_update_trade_state_sell(default_conf, trades_for_order, limit_sell_order, mocker): + mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=trades_for_order) + # get_order should not be called!! + mocker.patch('freqtrade.exchange.Exchange.get_order', MagicMock(side_effect=ValueError)) + wallet_mock = MagicMock() + mocker.patch('freqtrade.wallets.Wallets.update', wallet_mock) + + patch_exchange(mocker) + Trade.session = MagicMock() + amount = limit_sell_order["amount"] + freqtrade = get_patched_freqtradebot(mocker, default_conf) + wallet_mock.reset_mock() + trade = Trade( + pair='LTC/ETH', + amount=amount, + exchange='binance', + open_rate=0.245441, + fee_open=0.0025, + fee_close=0.0025, + open_order_id="123456", + is_open=True, + ) + freqtrade.update_trade_state(trade, limit_sell_order) + assert trade.amount == limit_sell_order['amount'] + # Wallet needs to be updated after closing a limit-sell order to reenable buying + assert wallet_mock.call_count == 1 + + def test_handle_trade(default_conf, limit_buy_order, limit_sell_order, fee, markets, mocker) -> None: patch_RPCManager(mocker) From 676cd6ffee6aead3e9fe2bd4e451918d3744139e Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 22 Apr 2019 13:36:14 +0200 Subject: [PATCH 4/4] Add assert to make sure trade was closed --- freqtrade/tests/test_freqtradebot.py | 1 + 1 file changed, 1 insertion(+) diff --git a/freqtrade/tests/test_freqtradebot.py b/freqtrade/tests/test_freqtradebot.py index 008974a6b..67b05ac3e 100644 --- a/freqtrade/tests/test_freqtradebot.py +++ b/freqtrade/tests/test_freqtradebot.py @@ -1459,6 +1459,7 @@ def test_update_trade_state_sell(default_conf, trades_for_order, limit_sell_orde assert trade.amount == limit_sell_order['amount'] # Wallet needs to be updated after closing a limit-sell order to reenable buying assert wallet_mock.call_count == 1 + assert not trade.is_open def test_handle_trade(default_conf, limit_buy_order, limit_sell_order,