From a56340061e2047a1ced86a3cb7c6c8b7095ea366 Mon Sep 17 00:00:00 2001 From: Samuel Husso Date: Sun, 3 Sep 2017 09:50:48 +0300 Subject: [PATCH 1/3] fix Session lint issues --- .gitignore | 1 + main.py | 6 +++--- persistence.py | 8 ++++---- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 24d1caaeb..2d4af4dac 100644 --- a/.gitignore +++ b/.gitignore @@ -80,3 +80,4 @@ preprocessor.py .env .venv .idea +.vscode diff --git a/main.py b/main.py index a97419c94..06901b40d 100755 --- a/main.py +++ b/main.py @@ -53,7 +53,7 @@ class TradeThread(threading.Thread): msg = 'Got {} during _process()'.format(error.__class__.__name__) logger.exception(msg) finally: - Session.flush() + Session().flush() time.sleep(25) except (RuntimeError, JSONDecodeError): TelegramHandler.send_msg('*Status:* Got RuntimeError: ```\n{}\n```'.format(traceback.format_exc())) @@ -75,7 +75,7 @@ class TradeThread(threading.Thread): # Create entity and execute trade trade = create_trade(float(CONFIG['stake_amount']), api_wrapper.exchange) if trade: - Session.add(trade) + Session().add(trade) else: logging.info('Got no buy signal...') except ValueError: @@ -134,7 +134,7 @@ def close_trade_if_fulfilled(trade: Trade) -> bool: # we can close this trade. if trade.close_profit and trade.close_date and trade.close_rate and not trade.open_order_id: trade.is_open = False - Session.flush() + Session().flush() return True return False diff --git a/persistence.py b/persistence.py index f8c954983..c43ad5af6 100644 --- a/persistence.py +++ b/persistence.py @@ -2,7 +2,9 @@ from datetime import datetime from sqlalchemy import Boolean, Column, DateTime, Float, Integer, String, create_engine from sqlalchemy.ext.declarative import declarative_base -from sqlalchemy.orm import scoped_session, sessionmaker +from sqlalchemy.orm.scoping import scoped_session +from sqlalchemy.orm.session import sessionmaker + from sqlalchemy.types import Enum from exchange import Exchange, get_exchange_api @@ -18,10 +20,8 @@ engine = create_engine(db_handle, echo=False) Session = scoped_session(sessionmaker(bind=engine, autoflush=True, autocommit=True)) Base = declarative_base() - class Trade(Base): __tablename__ = 'trades' - query = Session.query_property() id = Column(Integer, primary_key=True) @@ -61,7 +61,7 @@ class Trade(Base): self.close_profit = profit self.close_date = datetime.utcnow() self.open_order_id = order_id - Session.flush() + Session().flush() return profit Base.metadata.create_all(engine) From 5bf244b92d388507389e85429a71838f266fb8cb Mon Sep 17 00:00:00 2001 From: Samuel Husso Date: Sun, 3 Sep 2017 09:56:35 +0300 Subject: [PATCH 2/3] fix telegram Session lint issues --- rpc/telegram.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rpc/telegram.py b/rpc/telegram.py index efb8746fa..d74636b5b 100644 --- a/rpc/telegram.py +++ b/rpc/telegram.py @@ -124,7 +124,7 @@ class TelegramHandler(object): profit_amounts.append((profit / 100) * trade.btc_amount) profits.append(profit) - bp_pair, bp_rate = Session.query(Trade.pair, func.sum(Trade.close_profit).label('profit_sum')) \ + bp_pair, bp_rate = Session().query(Trade.pair, func.sum(Trade.close_profit).label('profit_sum')) \ .filter(Trade.is_open.is_(False)) \ .group_by(Trade.pair) \ .order_by('profit_sum DESC') \ @@ -245,7 +245,7 @@ class TelegramHandler(object): TelegramHandler.send_msg('`trader is not running`', bot=bot) return - pair_rates = Session.query(Trade.pair, func.sum(Trade.close_profit).label('profit_sum')) \ + pair_rates = Session().query(Trade.pair, func.sum(Trade.close_profit).label('profit_sum')) \ .filter(Trade.is_open.is_(False)) \ .group_by(Trade.pair) \ .order_by('profit_sum DESC') \ From cb97a30087ebf3735b9156599f5e702701154140 Mon Sep 17 00:00:00 2001 From: Samuel Husso Date: Wed, 6 Sep 2017 17:13:21 +0300 Subject: [PATCH 3/3] fix sqlalchemy session to use contextual session --- main.py | 6 +++--- persistence.py | 7 ++++--- rpc/telegram.py | 5 ++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/main.py b/main.py index 06901b40d..a97419c94 100755 --- a/main.py +++ b/main.py @@ -53,7 +53,7 @@ class TradeThread(threading.Thread): msg = 'Got {} during _process()'.format(error.__class__.__name__) logger.exception(msg) finally: - Session().flush() + Session.flush() time.sleep(25) except (RuntimeError, JSONDecodeError): TelegramHandler.send_msg('*Status:* Got RuntimeError: ```\n{}\n```'.format(traceback.format_exc())) @@ -75,7 +75,7 @@ class TradeThread(threading.Thread): # Create entity and execute trade trade = create_trade(float(CONFIG['stake_amount']), api_wrapper.exchange) if trade: - Session().add(trade) + Session.add(trade) else: logging.info('Got no buy signal...') except ValueError: @@ -134,7 +134,7 @@ def close_trade_if_fulfilled(trade: Trade) -> bool: # we can close this trade. if trade.close_profit and trade.close_date and trade.close_rate and not trade.open_order_id: trade.is_open = False - Session().flush() + Session.flush() return True return False diff --git a/persistence.py b/persistence.py index c43ad5af6..1abe208b8 100644 --- a/persistence.py +++ b/persistence.py @@ -17,12 +17,13 @@ else: db_handle = 'sqlite:///tradesv2.sqlite' engine = create_engine(db_handle, echo=False) -Session = scoped_session(sessionmaker(bind=engine, autoflush=True, autocommit=True)) +session = scoped_session(sessionmaker(bind=engine, autoflush=True, autocommit=True)) +Session = session() Base = declarative_base() class Trade(Base): __tablename__ = 'trades' - query = Session.query_property() + query = session.query_property() id = Column(Integer, primary_key=True) exchange = Column(Enum(Exchange), nullable=False) @@ -61,7 +62,7 @@ class Trade(Base): self.close_profit = profit self.close_date = datetime.utcnow() self.open_order_id = order_id - Session().flush() + Session.flush() return profit Base.metadata.create_all(engine) diff --git a/rpc/telegram.py b/rpc/telegram.py index d74636b5b..7472f7faf 100644 --- a/rpc/telegram.py +++ b/rpc/telegram.py @@ -124,7 +124,7 @@ class TelegramHandler(object): profit_amounts.append((profit / 100) * trade.btc_amount) profits.append(profit) - bp_pair, bp_rate = Session().query(Trade.pair, func.sum(Trade.close_profit).label('profit_sum')) \ + bp_pair, bp_rate = Session.query(Trade.pair, func.sum(Trade.close_profit).label('profit_sum')) \ .filter(Trade.is_open.is_(False)) \ .group_by(Trade.pair) \ .order_by('profit_sum DESC') \ @@ -244,8 +244,7 @@ class TelegramHandler(object): if not get_instance().is_alive(): TelegramHandler.send_msg('`trader is not running`', bot=bot) return - - pair_rates = Session().query(Trade.pair, func.sum(Trade.close_profit).label('profit_sum')) \ + pair_rates = Session.query(Trade.pair, func.sum(Trade.close_profit).label('profit_sum')) \ .filter(Trade.is_open.is_(False)) \ .group_by(Trade.pair) \ .order_by('profit_sum DESC') \