From 50fc63251ecf33edc8afebfe38cc2ce8d479b6a2 Mon Sep 17 00:00:00 2001 From: Misagh Date: Thu, 28 Mar 2019 21:18:26 +0100 Subject: [PATCH 1/3] added SL pct to DB --- freqtrade/persistence.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/freqtrade/persistence.py b/freqtrade/persistence.py index a1ac65c04..0b46768c1 100644 --- a/freqtrade/persistence.py +++ b/freqtrade/persistence.py @@ -83,7 +83,7 @@ def check_migrate(engine) -> None: logger.debug(f'trying {table_back_name}') # Check for latest column - if not has_column(cols, 'min_rate'): + if not has_column(cols, 'stop_loss_pct'): logger.info(f'Running database migration - backup available as {table_back_name}') fee_open = get_column_def(cols, 'fee_open', 'fee') @@ -91,7 +91,9 @@ def check_migrate(engine) -> None: open_rate_requested = get_column_def(cols, 'open_rate_requested', 'null') close_rate_requested = get_column_def(cols, 'close_rate_requested', 'null') stop_loss = get_column_def(cols, 'stop_loss', '0.0') + stop_loss_pct = get_column_def(cols, 'stop_loss_pct', '0.0') initial_stop_loss = get_column_def(cols, 'initial_stop_loss', '0.0') + initial_stop_loss_pct = get_column_def(cols, 'initial_stop_loss_pct', '0.0') 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') @@ -113,7 +115,8 @@ def check_migrate(engine) -> None: (id, exchange, pair, is_open, fee_open, fee_close, open_rate, open_rate_requested, close_rate, close_rate_requested, close_profit, stake_amount, amount, open_date, close_date, open_order_id, - stop_loss, initial_stop_loss, stoploss_order_id, stoploss_last_update, + stop_loss, stop_loss_pct, initial_stop_loss, initial_stop_loss_pct, + stoploss_order_id, stoploss_last_update, max_rate, min_rate, sell_reason, strategy, ticker_interval ) @@ -129,7 +132,9 @@ def check_migrate(engine) -> None: open_rate, {open_rate_requested} open_rate_requested, close_rate, {close_rate_requested} close_rate_requested, close_profit, stake_amount, amount, open_date, close_date, open_order_id, - {stop_loss} stop_loss, {initial_stop_loss} initial_stop_loss, + {stop_loss} stop_loss, {stop_loss_pct} stop_loss_pct, + {initial_stop_loss} initial_stop_loss, + {initial_stop_loss_pct} initial_stop_loss_pct, {stoploss_order_id} stoploss_order_id, {stoploss_last_update} stoploss_last_update, {max_rate} max_rate, {min_rate} min_rate, {sell_reason} sell_reason, {strategy} strategy, {ticker_interval} ticker_interval @@ -184,8 +189,12 @@ class Trade(_DECL_BASE): open_order_id = Column(String) # absolute value of the stop loss stop_loss = Column(Float, nullable=True, default=0.0) + # percentage value of the stop loss + stop_loss_pct = Column(Float, nullable=True, default=0.0) # absolute value of the initial stop loss initial_stop_loss = Column(Float, nullable=True, default=0.0) + # percentage value of the initial stop loss + initial_stop_loss_pct = Column(Float, nullable=True, default=0.0) # stoploss order id which is on exchange stoploss_order_id = Column(String, nullable=True, index=True) # last update time of the stoploss order on exchange @@ -231,13 +240,16 @@ class Trade(_DECL_BASE): if not self.stop_loss: logger.debug("assigning new stop loss") self.stop_loss = new_loss + self.stop_loss_pct = stoploss self.initial_stop_loss = new_loss + self.initial_stop_loss_pct = stoploss self.stoploss_last_update = datetime.utcnow() # evaluate if the stop loss needs to be updated else: if new_loss > self.stop_loss: # stop losses only walk up, never down! self.stop_loss = new_loss + self.stop_loss_pct = stoploss self.stoploss_last_update = datetime.utcnow() logger.debug("adjusted stop loss") else: From f2599ffe9029acea5a0a08137376c6e894a8fa2b Mon Sep 17 00:00:00 2001 From: Misagh Date: Fri, 29 Mar 2019 08:08:29 +0100 Subject: [PATCH 2/3] pct default to None --- freqtrade/persistence.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/freqtrade/persistence.py b/freqtrade/persistence.py index 0b46768c1..5be61393e 100644 --- a/freqtrade/persistence.py +++ b/freqtrade/persistence.py @@ -91,9 +91,9 @@ def check_migrate(engine) -> None: open_rate_requested = get_column_def(cols, 'open_rate_requested', 'null') close_rate_requested = get_column_def(cols, 'close_rate_requested', 'null') stop_loss = get_column_def(cols, 'stop_loss', '0.0') - stop_loss_pct = get_column_def(cols, 'stop_loss_pct', '0.0') + 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', '0.0') + initial_stop_loss_pct = get_column_def(cols, 'initial_stop_loss_pct', 'null') 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') @@ -190,11 +190,11 @@ class Trade(_DECL_BASE): # absolute value of the stop loss stop_loss = Column(Float, nullable=True, default=0.0) # percentage value of the stop loss - stop_loss_pct = Column(Float, nullable=True, default=0.0) + stop_loss_pct = Column(Float, nullable=True) # absolute value of the initial stop loss initial_stop_loss = Column(Float, nullable=True, default=0.0) # percentage value of the initial stop loss - initial_stop_loss_pct = Column(Float, nullable=True, default=0.0) + initial_stop_loss_pct = Column(Float, nullable=True) # stoploss order id which is on exchange stoploss_order_id = Column(String, nullable=True, index=True) # last update time of the stoploss order on exchange From 9b38c04579d22bc371a6ff417a21397ae4de05ac Mon Sep 17 00:00:00 2001 From: Misagh Date: Sun, 31 Mar 2019 13:15:35 +0200 Subject: [PATCH 3/3] negating SL pct and adding tests --- freqtrade/persistence.py | 6 +++--- freqtrade/tests/test_persistence.py | 10 ++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/freqtrade/persistence.py b/freqtrade/persistence.py index 5be61393e..5a18a922a 100644 --- a/freqtrade/persistence.py +++ b/freqtrade/persistence.py @@ -240,16 +240,16 @@ class Trade(_DECL_BASE): if not self.stop_loss: logger.debug("assigning new stop loss") self.stop_loss = new_loss - self.stop_loss_pct = stoploss + self.stop_loss_pct = -1 * abs(stoploss) self.initial_stop_loss = new_loss - self.initial_stop_loss_pct = stoploss + self.initial_stop_loss_pct = -1 * abs(stoploss) self.stoploss_last_update = datetime.utcnow() # evaluate if the stop loss needs to be updated else: if new_loss > self.stop_loss: # stop losses only walk up, never down! self.stop_loss = new_loss - self.stop_loss_pct = stoploss + self.stop_loss_pct = -1 * abs(stoploss) self.stoploss_last_update = datetime.utcnow() logger.debug("adjusted stop loss") else: diff --git a/freqtrade/tests/test_persistence.py b/freqtrade/tests/test_persistence.py index 042237ce7..f57a466e3 100644 --- a/freqtrade/tests/test_persistence.py +++ b/freqtrade/tests/test_persistence.py @@ -599,32 +599,42 @@ def test_adjust_stop_loss(fee): trade.adjust_stop_loss(trade.open_rate, 0.05, True) assert trade.stop_loss == 0.95 + assert trade.stop_loss_pct == -0.05 assert trade.initial_stop_loss == 0.95 + assert trade.initial_stop_loss_pct == -0.05 # Get percent of profit with a lower rate trade.adjust_stop_loss(0.96, 0.05) assert trade.stop_loss == 0.95 + assert trade.stop_loss_pct == -0.05 assert trade.initial_stop_loss == 0.95 + assert trade.initial_stop_loss_pct == -0.05 # Get percent of profit with a custom rate (Higher than open rate) trade.adjust_stop_loss(1.3, -0.1) assert round(trade.stop_loss, 8) == 1.17 + assert trade.stop_loss_pct == -0.1 assert trade.initial_stop_loss == 0.95 + assert trade.initial_stop_loss_pct == -0.05 # current rate lower again ... should not change trade.adjust_stop_loss(1.2, 0.1) assert round(trade.stop_loss, 8) == 1.17 assert trade.initial_stop_loss == 0.95 + assert trade.initial_stop_loss_pct == -0.05 # current rate higher... should raise stoploss trade.adjust_stop_loss(1.4, 0.1) assert round(trade.stop_loss, 8) == 1.26 assert trade.initial_stop_loss == 0.95 + assert trade.initial_stop_loss_pct == -0.05 # Initial is true but stop_loss set - so doesn't do anything trade.adjust_stop_loss(1.7, 0.1, True) assert round(trade.stop_loss, 8) == 1.26 assert trade.initial_stop_loss == 0.95 + assert trade.initial_stop_loss_pct == -0.05 + assert trade.stop_loss_pct == -0.1 def test_adjust_min_max_rates(fee):