Use commit instead of .flush()

This commit is contained in:
Matthias 2021-04-13 19:52:33 +02:00
parent eaa47ff335
commit 6fb32c3594
4 changed files with 15 additions and 11 deletions

View File

@ -187,7 +187,7 @@ class FreqtradeBot(LoggingMixin):
if self.get_free_open_trades(): if self.get_free_open_trades():
self.enter_positions() self.enter_positions()
Trade.query.session.flush() Trade.query.session.commit()
def process_stopped(self) -> None: def process_stopped(self) -> None:
""" """
@ -620,7 +620,7 @@ class FreqtradeBot(LoggingMixin):
self.update_trade_state(trade, order_id, order) self.update_trade_state(trade, order_id, order)
Trade.query.session.add(trade) Trade.query.session.add(trade)
Trade.query.session.flush() Trade.query.session.commit()
# Updating wallets # Updating wallets
self.wallets.update() self.wallets.update()
@ -706,6 +706,7 @@ class FreqtradeBot(LoggingMixin):
if (self.strategy.order_types.get('stoploss_on_exchange') and if (self.strategy.order_types.get('stoploss_on_exchange') and
self.handle_stoploss_on_exchange(trade)): self.handle_stoploss_on_exchange(trade)):
trades_closed += 1 trades_closed += 1
Trade.query.session.commit()
continue continue
# Check if we can sell our current pair # Check if we can sell our current pair
if trade.open_order_id is None and trade.is_open and self.handle_trade(trade): if trade.open_order_id is None and trade.is_open and self.handle_trade(trade):
@ -1036,6 +1037,7 @@ class FreqtradeBot(LoggingMixin):
elif order['side'] == 'sell': elif order['side'] == 'sell':
self.handle_cancel_sell(trade, order, constants.CANCEL_REASON['ALL_CANCELLED']) self.handle_cancel_sell(trade, order, constants.CANCEL_REASON['ALL_CANCELLED'])
Trade.query.session.commit()
def handle_cancel_buy(self, trade: Trade, order: Dict, reason: str) -> bool: def handle_cancel_buy(self, trade: Trade, order: Dict, reason: str) -> bool:
""" """
@ -1233,7 +1235,7 @@ class FreqtradeBot(LoggingMixin):
# In case of market sell orders the order can be closed immediately # In case of market sell orders the order can be closed immediately
if order.get('status', 'unknown') == 'closed': if order.get('status', 'unknown') == 'closed':
self.update_trade_state(trade, trade.open_order_id, order) self.update_trade_state(trade, trade.open_order_id, order)
Trade.query.session.flush() Trade.query.session.commit()
# Lock pair for one candle to prevent immediate re-buys # Lock pair for one candle to prevent immediate re-buys
self.strategy.lock_pair(trade.pair, datetime.now(timezone.utc), self.strategy.lock_pair(trade.pair, datetime.now(timezone.utc),
@ -1374,6 +1376,7 @@ class FreqtradeBot(LoggingMixin):
# Handling of this will happen in check_handle_timeout. # Handling of this will happen in check_handle_timeout.
return True return True
trade.update(order) trade.update(order)
Trade.query.session.commit()
# Updating wallets when order is closed # Updating wallets when order is closed
if not trade.is_open: if not trade.is_open:

View File

@ -74,7 +74,7 @@ def cleanup_db() -> None:
Flushes all pending operations to disk. Flushes all pending operations to disk.
:return: None :return: None
""" """
Trade.query.session.flush() Trade.query.session.commit()
def clean_dry_run_db() -> None: def clean_dry_run_db() -> None:
@ -86,6 +86,7 @@ def clean_dry_run_db() -> None:
# Check we are updating only a dry_run order not a prod one # Check we are updating only a dry_run order not a prod one
if 'dry_run' in trade.open_order_id: if 'dry_run' in trade.open_order_id:
trade.open_order_id = None trade.open_order_id = None
Trade.query.session.commit()
class Order(_DECL_BASE): class Order(_DECL_BASE):
@ -174,6 +175,7 @@ class Order(_DECL_BASE):
if filtered_orders: if filtered_orders:
oobj = filtered_orders[0] oobj = filtered_orders[0]
oobj.update_from_ccxt_object(order) oobj.update_from_ccxt_object(order)
Order.query.session.commit()
else: else:
logger.warning(f"Did not find order for {order}.") logger.warning(f"Did not find order for {order}.")
@ -709,7 +711,7 @@ class Trade(_DECL_BASE, LocalTrade):
Order.query.session.delete(order) Order.query.session.delete(order)
Trade.query.session.delete(self) Trade.query.session.delete(self)
Trade.query.session.flush() Trade.query.session.commit()
@staticmethod @staticmethod
def get_trades_proxy(*, pair: str = None, is_open: bool = None, def get_trades_proxy(*, pair: str = None, is_open: bool = None,

View File

@ -49,7 +49,7 @@ class PairLocks():
) )
if PairLocks.use_db: if PairLocks.use_db:
PairLock.query.session.add(lock) PairLock.query.session.add(lock)
PairLock.query.session.flush() PairLock.query.session.commit()
else: else:
PairLocks.locks.append(lock) PairLocks.locks.append(lock)
@ -99,7 +99,7 @@ class PairLocks():
for lock in locks: for lock in locks:
lock.active = False lock.active = False
if PairLocks.use_db: if PairLocks.use_db:
PairLock.query.session.flush() PairLock.query.session.commit()
@staticmethod @staticmethod
def is_global_lock(now: Optional[datetime] = None) -> bool: def is_global_lock(now: Optional[datetime] = None) -> bool:

View File

@ -569,7 +569,7 @@ class RPC:
# Execute sell for all open orders # Execute sell for all open orders
for trade in Trade.get_open_trades(): for trade in Trade.get_open_trades():
_exec_forcesell(trade) _exec_forcesell(trade)
Trade.query.session.flush() Trade.query.session.commit()
self._freqtrade.wallets.update() self._freqtrade.wallets.update()
return {'result': 'Created sell orders for all open trades.'} return {'result': 'Created sell orders for all open trades.'}
@ -582,7 +582,7 @@ class RPC:
raise RPCException('invalid argument') raise RPCException('invalid argument')
_exec_forcesell(trade) _exec_forcesell(trade)
Trade.query.session.flush() Trade.query.session.commit()
self._freqtrade.wallets.update() self._freqtrade.wallets.update()
return {'result': f'Created sell order for trade {trade_id}.'} return {'result': f'Created sell order for trade {trade_id}.'}
@ -705,8 +705,7 @@ class RPC:
lock.active = False lock.active = False
lock.lock_end_time = datetime.now(timezone.utc) lock.lock_end_time = datetime.now(timezone.utc)
# session is always the same PairLock.query.session.commit()
PairLock.query.session.flush()
return self._rpc_locks() return self._rpc_locks()