From 9d5961e2247295d086023cacc9d959722404d0f7 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 20 Dec 2020 11:17:50 +0100 Subject: [PATCH] Rename method to custom_stoploss --- docs/strategy-advanced.md | 16 ++++++++-------- freqtrade/strategy/interface.py | 6 +++--- .../subtemplates/strategy_methods_advanced.j2 | 4 ++-- tests/strategy/test_default_strategy.py | 8 +++++--- tests/strategy/test_interface.py | 11 +++++------ 5 files changed, 23 insertions(+), 22 deletions(-) diff --git a/docs/strategy-advanced.md b/docs/strategy-advanced.md index f2d8e4151..bb455ff9f 100644 --- a/docs/strategy-advanced.md +++ b/docs/strategy-advanced.md @@ -23,8 +23,8 @@ E.g. `current_profit = 0.05` (5% profit) - stoploss returns `0.02` - then you "l ``` python use_custom_stoploss = True - def stoploss_value(self, pair: str, trade: Trade, current_time: datetime, current_rate: float, - current_profit: float, **kwargs) -> float: + def custom_stoploss(self, pair: str, trade: Trade, current_time: datetime, current_rate: float, + current_profit: float, **kwargs) -> float: # TODO: Add full docstring here return 0.04 ``` @@ -47,8 +47,8 @@ Use the initial stoploss for the first 60 minutes, after this change to 10% trai ``` python use_custom_stoploss = True - def stoploss_value(self, pair: str, trade: Trade, current_time: datetime, current_rate: float, - current_profit: float, **kwargs) -> float: + def custom_stoploss(self, pair: str, trade: Trade, current_time: datetime, current_rate: float, + current_profit: float, **kwargs) -> float: # TODO: Add full docstring here # Make sure you have the longest interval first - these conditions are evaluated from top to bottom. @@ -67,8 +67,8 @@ In this example, we'll trail the highest price with 10% trailing stoploss for `E ``` python use_custom_stoploss = True - def stoploss_value(self, pair: str, trade: Trade, current_time: datetime, current_rate: float, - current_profit: float, **kwargs) -> float: + def custom_stoploss(self, pair: str, trade: Trade, current_time: datetime, current_rate: float, + current_profit: float, **kwargs) -> float: # TODO: Add full docstring here if pair in ('ETH/BTC', 'XRP/BTC'): @@ -90,8 +90,8 @@ The below example sets absolute profit levels based on the current profit. ``` python use_custom_stoploss = True - def stoploss_value(self, pair: str, trade: Trade, current_time: datetime, current_rate: float, - current_profit: float, **kwargs) -> float: + def custom_stoploss(self, pair: str, trade: Trade, current_time: datetime, current_rate: float, + current_profit: float, **kwargs) -> float: # TODO: Add full docstring here # Calculate as `-desired_stop_from_open + current_profit` to get the distance between current_profit and initial price diff --git a/freqtrade/strategy/interface.py b/freqtrade/strategy/interface.py index d93dda849..61c879e8f 100644 --- a/freqtrade/strategy/interface.py +++ b/freqtrade/strategy/interface.py @@ -255,8 +255,8 @@ class IStrategy(ABC): """ return True - def stoploss_value(self, pair: str, trade: Trade, current_time: datetime, current_rate: float, - current_profit: float, **kwargs) -> float: + def custom_stoploss(self, pair: str, trade: Trade, current_time: datetime, current_rate: float, + current_profit: float, **kwargs) -> float: """ Custom stoploss logic, returning the new distance relative to current_rate (as ratio). e.g. returning -0.05 would create a stoploss 5% below current_rate. @@ -555,7 +555,7 @@ class IStrategy(ABC): trade.adjust_stop_loss(trade.open_rate, stop_loss_value, initial=True) if self.use_custom_stoploss: - stop_loss_value = strategy_safe_wrapper(self.stoploss_value, default_retval=None + stop_loss_value = strategy_safe_wrapper(self.custom_stoploss, default_retval=None )(pair=trade.pair, trade=trade, current_time=current_time, current_rate=current_rate, diff --git a/freqtrade/templates/subtemplates/strategy_methods_advanced.j2 b/freqtrade/templates/subtemplates/strategy_methods_advanced.j2 index 0ae3e077c..f4cda2e89 100644 --- a/freqtrade/templates/subtemplates/strategy_methods_advanced.j2 +++ b/freqtrade/templates/subtemplates/strategy_methods_advanced.j2 @@ -14,8 +14,8 @@ def bot_loop_start(self, **kwargs) -> None: use_custom_stoploss = True -def stoploss_value(self, pair: str, trade: 'Trade', current_time: 'datetime', current_rate: float, - current_profit: float, **kwargs) -> float: +def custom_stoploss(self, pair: str, trade: 'Trade', current_time: 'datetime', current_rate: float, + current_profit: float, **kwargs) -> float: """ Custom stoploss logic, returning the new distance relative to current_rate (as ratio). e.g. returning -0.05 would create a stoploss 5% below current_rate. diff --git a/tests/strategy/test_default_strategy.py b/tests/strategy/test_default_strategy.py index c5d76b4c5..ec7b3c33d 100644 --- a/tests/strategy/test_default_strategy.py +++ b/tests/strategy/test_default_strategy.py @@ -1,7 +1,9 @@ from datetime import datetime -from freqtrade.persistence.models import Trade + from pandas import DataFrame +from freqtrade.persistence.models import Trade + from .strats.default_strategy import DefaultStrategy @@ -38,5 +40,5 @@ def test_default_strategy(result, fee): assert strategy.confirm_trade_exit(pair='ETH/BTC', trade=trade, order_type='limit', amount=0.1, rate=20000, time_in_force='gtc', sell_reason='roi') is True - assert strategy.stoploss_value(pair='ETH/BTC', trade=trade, current_time=datetime.now(), - current_rate=20_000, current_profit=0.05) == strategy.stoploss + assert strategy.custom_stoploss(pair='ETH/BTC', trade=trade, current_time=datetime.now(), + current_rate=20_000, current_profit=0.05) == strategy.stoploss diff --git a/tests/strategy/test_interface.py b/tests/strategy/test_interface.py index 5922ad9b2..7eed43302 100644 --- a/tests/strategy/test_interface.py +++ b/tests/strategy/test_interface.py @@ -1,5 +1,4 @@ # pragma pylint: disable=missing-docstring, C0103 -from freqtrade.strategy.interface import SellCheckTuple, SellType import logging from datetime import datetime, timedelta, timezone from unittest.mock import MagicMock @@ -11,9 +10,10 @@ from pandas import DataFrame from freqtrade.configuration import TimeRange from freqtrade.data.dataprovider import DataProvider from freqtrade.data.history import load_data -from freqtrade.exceptions import OperationalException, StrategyError +from freqtrade.exceptions import StrategyError from freqtrade.persistence import PairLocks, Trade from freqtrade.resolvers import StrategyResolver +from freqtrade.strategy.interface import SellCheckTuple, SellType from freqtrade.strategy.strategy_wrapper import strategy_safe_wrapper from tests.conftest import log_has, log_has_re @@ -329,9 +329,9 @@ def test_stop_loss_reached(default_conf, fee, profit, adjusted, expected, traili strategy.trailing_stop = trailing strategy.trailing_stop_positive = -0.05 strategy.use_custom_stoploss = custom - original_stopvalue = strategy.stoploss_value + original_stopvalue = strategy.custom_stoploss if custom_stop: - strategy.stoploss_value = custom_stop + strategy.custom_stoploss = custom_stop now = arrow.utcnow().datetime sl_flag = strategy.stop_loss_reached(current_rate=trade.open_rate * (1 + profit), trade=trade, @@ -355,8 +355,7 @@ def test_stop_loss_reached(default_conf, fee, profit, adjusted, expected, traili assert sl_flag.sell_flag is True assert round(trade.stop_loss, 2) == adjusted2 - strategy.stoploss_value = original_stopvalue - + strategy.custom_stoploss = original_stopvalue def test_analyze_ticker_default(ohlcv_history, mocker, caplog) -> None: