From 87a3115073562e9abc7efd792569020435a01f6e Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 21 Aug 2022 10:03:04 +0200 Subject: [PATCH] Add get_open_trade_count() to simplify getting open trade count. --- freqtrade/freqtradebot.py | 2 +- freqtrade/persistence/trade_model.py | 10 ++++++++++ tests/test_freqtradebot.py | 2 -- tests/test_persistence.py | 2 ++ 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 4e3af64ea..5b71d6ec4 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -271,7 +271,7 @@ class FreqtradeBot(LoggingMixin): Return the number of free open trades slots or 0 if max number of open trades reached """ - open_trades = len(Trade.get_open_trades()) + open_trades = Trade.get_open_trade_count() return max(0, self.config['max_open_trades'] - open_trades) def update_funding_fees(self): diff --git a/freqtrade/persistence/trade_model.py b/freqtrade/persistence/trade_model.py index b954fee20..0c438956a 100644 --- a/freqtrade/persistence/trade_model.py +++ b/freqtrade/persistence/trade_model.py @@ -1044,6 +1044,16 @@ class LocalTrade(): """ return Trade.get_trades_proxy(is_open=True) + @staticmethod + def get_open_trade_count() -> int: + """ + get open trade count + """ + if Trade.use_db: + return Trade.query.filter(Trade.is_open.is_(True)).count() + else: + return len(LocalTrade.trades_open) + @staticmethod def stoploss_reinitialization(desired_stoploss): """ diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index a1a16c039..138527053 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -473,8 +473,6 @@ def test_create_trade_no_signal(default_conf_usdt, fee, mocker) -> None: freqtrade = FreqtradeBot(default_conf_usdt) patch_get_signal(freqtrade, enter_long=False, exit_long=False) - Trade.query = MagicMock() - Trade.query.filter = MagicMock() assert not freqtrade.create_trade('ETH/USDT') diff --git a/tests/test_persistence.py b/tests/test_persistence.py index 50d0788ca..d8973e9d0 100644 --- a/tests/test_persistence.py +++ b/tests/test_persistence.py @@ -1689,6 +1689,7 @@ def test_get_open(fee, is_short, use_db): create_mock_trades(fee, is_short, use_db) assert len(Trade.get_open_trades()) == 4 + assert Trade.get_open_trade_count() == 4 Trade.use_db = True @@ -1701,6 +1702,7 @@ def test_get_open_lev(fee, use_db): create_mock_trades_with_leverage(fee, use_db) assert len(Trade.get_open_trades()) == 5 + assert Trade.get_open_trade_count() == 5 Trade.use_db = True