From 0704cfb05b611a2f88bf5b44d02df273d1d8e628 Mon Sep 17 00:00:00 2001 From: hoeckxer Date: Mon, 4 Jan 2021 14:14:52 +0100 Subject: [PATCH 1/2] Added an example with a positive offset for a custom stoploss Signed-off-by: hoeckxer --- docs/strategy-advanced.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/docs/strategy-advanced.md b/docs/strategy-advanced.md index b1fcb50fc..9d5930518 100644 --- a/docs/strategy-advanced.md +++ b/docs/strategy-advanced.md @@ -119,6 +119,41 @@ class AwesomeStrategy(IStrategy): return -0.15 ``` +#### Trailing stoploss with positive offset + +Use the initial stoploss until the profit is above 4%, then use a trailing stoploss of 50% of the current profit with a minimum of 2.5% and a maximum of 5%. + +Please note that the stoploss can only increase, values lower than the current stoploss are ignored. + +``` python +from datetime import datetime, timedelta +from freqtrade.persistence import Trade + +class AwesomeStrategy(IStrategy): + + # ... populate_* methods + + use_custom_stoploss = True + + def custom_stoploss(self, pair: str, trade: Trade, current_time: datetime, current_rate: float, + current_profit: float, **kwargs) -> float: + + if current_profit < 0.04: + return -1 # return a value bigger than the inital stoploss to keep using the inital stoploss + + # After reaching the desired offset, allow the stoploss to trail by half the profit + stoploss = current_profit / 2 + + if abs(stoploss) < 0.025: + # Maintain a minimum of 2.5% trailing stoploss + stoploss = 0.025 + if abs(stoploss) > 0.05: + # Maximize the stoploss at 5% + stoploss = 0.05 + + return stoploss +``` + #### Absolute stoploss The below example sets absolute profit levels based on the current profit. From 1cf6e2c957de86054d5611ddf78230754e3fb112 Mon Sep 17 00:00:00 2001 From: hoeckxer Date: Mon, 4 Jan 2021 14:37:22 +0100 Subject: [PATCH 2/2] Changed documentation based on review comments Signed-off-by: hoeckxer --- docs/strategy-advanced.md | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/docs/strategy-advanced.md b/docs/strategy-advanced.md index 9d5930518..2431274d7 100644 --- a/docs/strategy-advanced.md +++ b/docs/strategy-advanced.md @@ -142,16 +142,10 @@ class AwesomeStrategy(IStrategy): return -1 # return a value bigger than the inital stoploss to keep using the inital stoploss # After reaching the desired offset, allow the stoploss to trail by half the profit - stoploss = current_profit / 2 + desired_stoploss = current_profit / 2 - if abs(stoploss) < 0.025: - # Maintain a minimum of 2.5% trailing stoploss - stoploss = 0.025 - if abs(stoploss) > 0.05: - # Maximize the stoploss at 5% - stoploss = 0.05 - - return stoploss + # Use a minimum of 2.5% and a maximum of 5% + return max(min(desired_stoploss, 0.05), 0.025) ``` #### Absolute stoploss