Rename _session to sessoin

This commit is contained in:
Matthias
2023-03-15 21:12:06 +01:00
parent 8073989c98
commit aa54b77702
7 changed files with 33 additions and 33 deletions

View File

@@ -54,12 +54,12 @@ def init_db(db_url: str) -> None:
# https://docs.sqlalchemy.org/en/13/orm/contextual.html#thread-local-scope
# Scoped sessions proxy requests to the appropriate thread-local session.
# We should use the scoped_session object - not a seperately initialized version
Trade._session = scoped_session(sessionmaker(bind=engine, autoflush=False))
Order._session = Trade._session
PairLock._session = Trade._session
Trade.query = Trade._session.query_property()
Order.query = Trade._session.query_property()
PairLock.query = Trade._session.query_property()
Trade.session = scoped_session(sessionmaker(bind=engine, autoflush=False))
Order.session = Trade.session
PairLock.session = Trade.session
Trade.query = Trade.session.query_property()
Order.query = Trade.session.query_property()
PairLock.query = Trade.session.query_property()
previous_tables = inspect(engine).get_table_names()
ModelBase.metadata.create_all(engine)

View File

@@ -14,7 +14,7 @@ class PairLock(ModelBase):
"""
__tablename__ = 'pairlocks'
query: ClassVar[QueryPropertyDescriptor]
_session: ClassVar[SessionType]
session: ClassVar[SessionType]
id: Mapped[int] = mapped_column(primary_key=True)

View File

@@ -51,8 +51,8 @@ class PairLocks():
active=True
)
if PairLocks.use_db:
PairLock._session.add(lock)
PairLock._session.commit()
PairLock.session.add(lock)
PairLock.session.commit()
else:
PairLocks.locks.append(lock)
return lock
@@ -106,7 +106,7 @@ class PairLocks():
for lock in locks:
lock.active = False
if PairLocks.use_db:
PairLock._session.commit()
PairLock.session.commit()
@staticmethod
def unlock_reason(reason: str, now: Optional[datetime] = None) -> None:
@@ -130,7 +130,7 @@ class PairLocks():
for lock in locks:
logger.info(f"Releasing lock for {lock.pair} with reason '{reason}'.")
lock.active = False
PairLock._session.commit()
PairLock.session.commit()
else:
# used in backtesting mode; don't show log messages for speed
locksb = PairLocks.get_pair_locks(None)

View File

@@ -37,7 +37,7 @@ class Order(ModelBase):
"""
__tablename__ = 'orders'
query: ClassVar[QueryPropertyDescriptor]
_session: ClassVar[SessionType]
session: ClassVar[SessionType]
# Uniqueness should be ensured over pair, order_id
# its likely that order_id is unique per Pair on some exchanges.
@@ -1153,7 +1153,7 @@ class LocalTrade():
get open trade count
"""
if Trade.use_db:
return Trade._session.execute(
return Trade.session.execute(
select(func.count(Trade.id)).filter(Trade.is_open.is_(True))
).scalar_one()
else:
@@ -1189,7 +1189,7 @@ class Trade(ModelBase, LocalTrade):
"""
__tablename__ = 'trades'
query: ClassVar[QueryPropertyDescriptor]
_session: ClassVar[SessionType]
session: ClassVar[SessionType]
use_db: bool = True
@@ -1289,18 +1289,18 @@ class Trade(ModelBase, LocalTrade):
def delete(self) -> None:
for order in self.orders:
Order._session.delete(order)
Order.session.delete(order)
Trade._session.delete(self)
Trade.session.delete(self)
Trade.commit()
@staticmethod
def commit():
Trade._session.commit()
Trade.session.commit()
@staticmethod
def rollback():
Trade._session.rollback()
Trade.session.rollback()
@staticmethod
def get_trades_proxy(*, pair: Optional[str] = None, is_open: Optional[bool] = None,
@@ -1369,7 +1369,7 @@ class Trade(ModelBase, LocalTrade):
e.g. `(trade_filter=Trade.id == trade_id)`
:return: unsorted query object
"""
return Trade._session.scalars(Trade.get_trades_query(trade_filter, include_orders))
return Trade.session.scalars(Trade.get_trades_query(trade_filter, include_orders))
@staticmethod
def get_open_order_trades() -> List['Trade']:
@@ -1407,7 +1407,7 @@ class Trade(ModelBase, LocalTrade):
Retrieves total realized profit
"""
if Trade.use_db:
total_profit: float = Trade._session.execute(
total_profit: float = Trade.session.execute(
select(func.sum(Trade.close_profit_abs)).filter(Trade.is_open.is_(False))
).scalar_one()
else:
@@ -1422,7 +1422,7 @@ class Trade(ModelBase, LocalTrade):
in stake currency
"""
if Trade.use_db:
total_open_stake_amount = Trade._session.scalar(
total_open_stake_amount = Trade.session.scalar(
select(func.sum(Trade.stake_amount)).filter(Trade.is_open.is_(True))
)
else:
@@ -1441,7 +1441,7 @@ class Trade(ModelBase, LocalTrade):
start_date = datetime.now(timezone.utc) - timedelta(minutes=minutes)
filters.append(Trade.close_date >= start_date)
pair_rates = Trade._session.execute(
pair_rates = Trade.session.execute(
select(
Trade.pair,
func.sum(Trade.close_profit).label('profit_sum'),
@@ -1476,7 +1476,7 @@ class Trade(ModelBase, LocalTrade):
if (pair is not None):
filters.append(Trade.pair == pair)
enter_tag_perf = Trade._session.execute(
enter_tag_perf = Trade.session.execute(
select(
Trade.enter_tag,
func.sum(Trade.close_profit).label('profit_sum'),
@@ -1509,7 +1509,7 @@ class Trade(ModelBase, LocalTrade):
filters: List = [Trade.is_open.is_(False)]
if (pair is not None):
filters.append(Trade.pair == pair)
sell_tag_perf = Trade._session.execute(
sell_tag_perf = Trade.session.execute(
select(
Trade.exit_reason,
func.sum(Trade.close_profit).label('profit_sum'),
@@ -1542,7 +1542,7 @@ class Trade(ModelBase, LocalTrade):
filters: List = [Trade.is_open.is_(False)]
if (pair is not None):
filters.append(Trade.pair == pair)
mix_tag_perf = Trade._session.execute(
mix_tag_perf = Trade.session.execute(
select(
Trade.id,
Trade.enter_tag,
@@ -1589,7 +1589,7 @@ class Trade(ModelBase, LocalTrade):
NOTE: Not supported in Backtesting.
:returns: Tuple containing (pair, profit_sum)
"""
best_pair = Trade._session.execute(
best_pair = Trade.session.execute(
select(
Trade.pair,
func.sum(Trade.close_profit).label('profit_sum')