Simplify pairlock querying

This commit is contained in:
Matthias 2023-03-16 06:48:12 +01:00
parent ae361e1d5d
commit e579ff9532
4 changed files with 13 additions and 8 deletions

View File

@ -36,7 +36,7 @@ def start_convert_db(args: Dict[str, Any]) -> None:
session_target.commit() session_target.commit()
for pairlock in PairLock.query: for pairlock in PairLock.get_all_locks():
pairlock_count += 1 pairlock_count += 1
make_transient(pairlock) make_transient(pairlock)
session_target.add(pairlock) session_target.add(pairlock)

View File

@ -56,6 +56,10 @@ class PairLock(ModelBase):
return PairLock.session.scalars(select(PairLock).filter(*filters)) 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]: def to_json(self) -> Dict[str, Any]:
return { return {
'id': self.id, 'id': self.id,

View File

@ -172,6 +172,6 @@ class PairLocks():
Return all locks, also locks with expired end date Return all locks, also locks with expired end date
""" """
if PairLocks.use_db: if PairLocks.use_db:
return PairLock.session.scalars(select(PairLock)).all() return PairLock.get_all_locks().all()
else: else:
return PairLocks.locks return PairLocks.locks

View File

@ -1607,12 +1607,13 @@ class Trade(ModelBase, LocalTrade):
NOTE: Not supported in Backtesting. NOTE: Not supported in Backtesting.
:returns: Tuple containing (pair, profit_sum) :returns: Tuple containing (pair, profit_sum)
""" """
trading_volume = Order.query.with_entities( trading_volume = Trade.session.execute(
select(
func.sum(Order.cost).label('volume') func.sum(Order.cost).label('volume')
).filter( ).filter(
Order.order_filled_date >= start_date, Order.order_filled_date >= start_date,
Order.status == 'closed' Order.status == 'closed'
).scalar() )).scalar_one()
return trading_volume return trading_volume
@staticmethod @staticmethod