fix sqlalchemy session to use contextual session

This commit is contained in:
Samuel Husso 2017-09-06 17:13:21 +03:00
parent 5bf244b92d
commit cb97a30087
3 changed files with 9 additions and 9 deletions

View File

@ -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

View File

@ -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)

View File

@ -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') \