Add Trade.commit method for easy use

This commit is contained in:
Matthias 2021-04-15 07:57:52 +02:00
parent 6fb32c3594
commit a01d05997e
4 changed files with 15 additions and 10 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.commit() Trade.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.commit() Trade.commit()
# Updating wallets # Updating wallets
self.wallets.update() self.wallets.update()
@ -706,7 +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() Trade.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):
@ -1037,7 +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() Trade.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:
""" """
@ -1235,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.commit() Trade.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),
@ -1376,7 +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() Trade.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.commit() Trade.commit()
def clean_dry_run_db() -> None: def clean_dry_run_db() -> None:
@ -86,7 +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() Trade.commit()
class Order(_DECL_BASE): class Order(_DECL_BASE):
@ -711,6 +711,10 @@ 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.commit()
@staticmethod
def commit():
Trade.query.session.commit() Trade.query.session.commit()
@staticmethod @staticmethod

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.commit() Trade.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.commit() Trade.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}.'}

View File

@ -1291,6 +1291,7 @@ def test_Trade_object_idem():
excludes = ( excludes = (
'delete', 'delete',
'session', 'session',
'commit',
'query', 'query',
'open_date', 'open_date',
'get_best_pair', 'get_best_pair',