From e579ff9532dbcd0c98c462c3dff3c394e8a9fbd4 Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 16 Mar 2023 06:48:12 +0100 Subject: [PATCH] Simplify pairlock querying --- freqtrade/commands/db_commands.py | 2 +- freqtrade/persistence/pairlock.py | 4 ++++ freqtrade/persistence/pairlock_middleware.py | 2 +- freqtrade/persistence/trade_model.py | 13 +++++++------ 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/freqtrade/commands/db_commands.py b/freqtrade/commands/db_commands.py index b4997582d..c819ca243 100644 --- a/freqtrade/commands/db_commands.py +++ b/freqtrade/commands/db_commands.py @@ -36,7 +36,7 @@ def start_convert_db(args: Dict[str, Any]) -> None: session_target.commit() - for pairlock in PairLock.query: + for pairlock in PairLock.get_all_locks(): pairlock_count += 1 make_transient(pairlock) session_target.add(pairlock) diff --git a/freqtrade/persistence/pairlock.py b/freqtrade/persistence/pairlock.py index af56a392c..e787b5fa0 100644 --- a/freqtrade/persistence/pairlock.py +++ b/freqtrade/persistence/pairlock.py @@ -56,6 +56,10 @@ class PairLock(ModelBase): return PairLock.session.scalars(select(PairLock).filter(*filters)) + @staticmethod + def get_all_locks() -> ScalarResult['PairLock']: + return PairLock.session.scalars(select(PairLock)) + def to_json(self) -> Dict[str, Any]: return { 'id': self.id, diff --git a/freqtrade/persistence/pairlock_middleware.py b/freqtrade/persistence/pairlock_middleware.py index 0a3d78c4a..29169a50d 100644 --- a/freqtrade/persistence/pairlock_middleware.py +++ b/freqtrade/persistence/pairlock_middleware.py @@ -172,6 +172,6 @@ class PairLocks(): Return all locks, also locks with expired end date """ if PairLocks.use_db: - return PairLock.session.scalars(select(PairLock)).all() + return PairLock.get_all_locks().all() else: return PairLocks.locks diff --git a/freqtrade/persistence/trade_model.py b/freqtrade/persistence/trade_model.py index 101638828..892707810 100644 --- a/freqtrade/persistence/trade_model.py +++ b/freqtrade/persistence/trade_model.py @@ -1607,12 +1607,13 @@ class Trade(ModelBase, LocalTrade): NOTE: Not supported in Backtesting. :returns: Tuple containing (pair, profit_sum) """ - trading_volume = Order.query.with_entities( - func.sum(Order.cost).label('volume') - ).filter( - Order.order_filled_date >= start_date, - Order.status == 'closed' - ).scalar() + trading_volume = Trade.session.execute( + select( + func.sum(Order.cost).label('volume') + ).filter( + Order.order_filled_date >= start_date, + Order.status == 'closed' + )).scalar_one() return trading_volume @staticmethod