From ae9f73062446b6671896fabbcb534f380676386d Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 13 Aug 2023 11:31:17 +0200 Subject: [PATCH] Add explicit "is_trailing_stop" field to database --- freqtrade/persistence/migrations.py | 9 ++++++--- freqtrade/persistence/trade_model.py | 6 +++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/freqtrade/persistence/migrations.py b/freqtrade/persistence/migrations.py index 87b172846..32972e7ff 100644 --- a/freqtrade/persistence/migrations.py +++ b/freqtrade/persistence/migrations.py @@ -88,6 +88,8 @@ def migrate_trades_and_orders_table( stop_loss_pct = get_column_def(cols, 'stop_loss_pct', 'null') initial_stop_loss = get_column_def(cols, 'initial_stop_loss', '0.0') initial_stop_loss_pct = get_column_def(cols, 'initial_stop_loss_pct', 'null') + is_stop_loss_trailing = get_column_def(cols, 'is_stop_loss_trailing', + 'stop_loss_pct <> initial_stop_loss_pct') stoploss_order_id = get_column_def(cols, 'stoploss_order_id', 'null') stoploss_last_update = get_column_def(cols, 'stoploss_last_update', 'null') max_rate = get_column_def(cols, 'max_rate', '0.0') @@ -156,7 +158,7 @@ def migrate_trades_and_orders_table( open_rate_requested, close_rate, close_rate_requested, close_profit, stake_amount, amount, amount_requested, open_date, close_date, open_order_id, stop_loss, stop_loss_pct, initial_stop_loss, initial_stop_loss_pct, - stoploss_order_id, stoploss_last_update, + is_stop_loss_trailing, stoploss_order_id, stoploss_last_update, max_rate, min_rate, exit_reason, exit_order_status, strategy, enter_tag, timeframe, open_trade_value, close_profit_abs, trading_mode, leverage, liquidation_price, is_short, @@ -175,6 +177,7 @@ def migrate_trades_and_orders_table( {stop_loss} stop_loss, {stop_loss_pct} stop_loss_pct, {initial_stop_loss} initial_stop_loss, {initial_stop_loss_pct} initial_stop_loss_pct, + {is_stop_loss_trailing} is_stop_loss_trailing, {stoploss_order_id} stoploss_order_id, {stoploss_last_update} stoploss_last_update, {max_rate} max_rate, {min_rate} min_rate, case when {exit_reason} = 'sell_signal' then 'exit_signal' @@ -316,8 +319,8 @@ def check_migrate(engine, decl_base, previous_tables) -> None: # if ('orders' not in previous_tables # or not has_column(cols_orders, 'funding_fee')): migrating = False - # if not has_column(cols_trades, 'max_stake_amount'): - if not has_column(cols_orders, 'ft_price'): + # if not has_column(cols_orders, 'ft_price'): + if not has_column(cols_trades, 'is_stop_loss_trailing'): migrating = True logger.info(f"Running database migration for trades - " f"backup: {table_back_name}, {order_table_bak_name}") diff --git a/freqtrade/persistence/trade_model.py b/freqtrade/persistence/trade_model.py index 3e4fcc939..0e72c0519 100644 --- a/freqtrade/persistence/trade_model.py +++ b/freqtrade/persistence/trade_model.py @@ -350,6 +350,7 @@ class LocalTrade: initial_stop_loss: Optional[float] = 0.0 # percentage value of the initial stop loss initial_stop_loss_pct: Optional[float] = None + is_stop_loss_trailing: bool = False # stoploss order id which is on exchange stoploss_order_id: Optional[str] = None # last update time of the stoploss order on exchange @@ -662,6 +663,7 @@ class LocalTrade: # ? decreasing the minimum stoploss if (higher_stop and not self.is_short) or (lower_stop and self.is_short): logger.debug(f"{self.pair} - Adjusting stoploss...") + self.is_stop_loss_trailing = True self.__set_stop_loss(stop_loss_norm, stoploss) else: logger.debug(f"{self.pair} - Keeping current stoploss...") @@ -1195,7 +1197,7 @@ class LocalTrade: logger.info(f"Found open trade: {trade}") # skip case if trailing-stop changed the stoploss already. - if (trade.stop_loss == trade.initial_stop_loss + if (not trade.is_stop_loss_trailing and trade.initial_stop_loss_pct != desired_stoploss): # Stoploss value got changed @@ -1268,6 +1270,8 @@ class Trade(ModelBase, LocalTrade): # percentage value of the initial stop loss initial_stop_loss_pct: Mapped[Optional[float]] = mapped_column( Float(), nullable=True) # type: ignore + is_stop_loss_trailing: Mapped[bool] = mapped_column( + nullable=False, default=False) # type: ignore # stoploss order id which is on exchange stoploss_order_id: Mapped[Optional[str]] = mapped_column( String(255), nullable=True, index=True) # type: ignore