From 7c464f3237ef1495da363dd0d496d33b9c5576ef Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 26 May 2022 20:38:09 +0200 Subject: [PATCH] Simplify and align adjust_trade_position interface --- docs/strategy-callbacks.md | 29 ++++++++++++++----- freqtrade/freqtradebot.py | 9 ++---- freqtrade/optimize/backtesting.py | 3 +- freqtrade/strategy/interface.py | 19 ++++++++---- .../subtemplates/strategy_methods_advanced.j2 | 26 ++++++++++++----- 5 files changed, 57 insertions(+), 29 deletions(-) diff --git a/docs/strategy-callbacks.md b/docs/strategy-callbacks.md index 06e7152aa..f39f81d8e 100644 --- a/docs/strategy-callbacks.md +++ b/docs/strategy-callbacks.md @@ -675,20 +675,35 @@ class DigDeeperStrategy(IStrategy): return proposed_stake / self.max_dca_multiplier def adjust_trade_position(self, trade: Trade, current_time: datetime, - current_rate: float, current_profit: float, min_stake: Optional[float], - max_stake: float, **kwargs): + current_rate: float, current_profit: float, + min_stake: Optional[float], max_stake: float, + current_entry_rate: float, current_exit_rate: float, + current_entry_profit: float, current_exit_profit: float, + **kwargs) -> Optional[float]: """ - Custom trade adjustment logic, returning the stake amount that a trade should be increased. - This means extra buy orders with additional fees. + Custom trade adjustment logic, returning the stake amount that a trade should be + increased or decreased. + This means extra buy or sell orders with additional fees. + Only called when `position_adjustment_enable` is set to True. + + For full documentation please go to https://www.freqtrade.io/en/latest/strategy-advanced/ + + When not implemented by a strategy, returns None :param trade: trade object. :param current_time: datetime object, containing the current datetime :param current_rate: Current buy rate. :param current_profit: Current profit (as ratio), calculated based on current_rate. - :param min_stake: Minimal stake size allowed by exchange. - :param max_stake: Balance available for trading. + :param min_stake: Minimal stake size allowed by exchange (for both entries and exits) + :param max_stake: Maximum stake allowed (either through balance, or by exchange limits). + :param current_entry_rate: Current rate using entry pricing. + :param current_exit_rate: Current rate using exit pricing. + :param current_entry_profit: Current profit using entry pricing. + :param current_exit_profit: Current profit using exit pricing. :param **kwargs: Ensure to keep this here so updates to this won't break your strategy. - :return float: Stake amount to adjust your trade + :return float: Stake amount to adjust your trade, + Positive values to increase position, Negative values to decrease position. + Return None for no action. """ if current_profit > -0.05: diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 229218149..c6c532fe6 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -526,7 +526,6 @@ class FreqtradeBot(LoggingMixin): current_exit_rate, self.strategy.stoploss) max_entry_stake = self.exchange.get_max_pair_stake_amount(trade.pair, current_entry_rate) - max_exit_stake = self.exchange.get_max_pair_stake_amount(trade.pair, current_exit_rate) stake_available = self.wallets.get_available_stake_amount() logger.debug(f"Calling adjust_trade_position for pair {trade.pair}") stake_amount = strategy_safe_wrapper(self.strategy.adjust_trade_position, @@ -535,10 +534,7 @@ class FreqtradeBot(LoggingMixin): current_profit=current_entry_profit, min_stake=min_entry_stake, max_stake=min(max_entry_stake, stake_available), current_entry_rate=current_entry_rate, current_exit_rate=current_exit_rate, - current_entry_profit=current_entry_profit, current_exit_profit=current_exit_profit, - min_entry_stake=min_entry_stake, min_exit_stake=min_exit_stake, - max_entry_stake=min(max_entry_stake, stake_available), - max_exit_stake=min(max_exit_stake, stake_available) + current_entry_profit=current_entry_profit, current_exit_profit=current_exit_profit ) if stake_amount is not None and stake_amount > 0.0: @@ -555,10 +551,9 @@ class FreqtradeBot(LoggingMixin): if stake_amount is not None and stake_amount < 0.0: # We should decrease our position - # Strategy should return value as Decimal for accuracy. amount = abs(float(Decimal(stake_amount) / Decimal(current_exit_rate))) if (trade.amount - amount) * current_exit_rate < min_exit_stake: - logger.info('Remaining amount would be too small') + logger.info('Remaining amount would be too small.') return if amount > trade.amount: logger.info( diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index 7e0a66138..b3538d00c 100644 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -506,8 +506,7 @@ class Backtesting: current_profit=current_profit, min_stake=min_stake, max_stake=min(max_stake, stake_available), current_entry_rate=current_rate, current_exit_rate=current_rate, - max_entry_stake=min(max_stake, stake_available), - max_exit_stake=min(max_stake, stake_available)) + current_entry_profit=current_profit, current_exit_profit=current_profit) # Check if we should increase our position if stake_amount is not None and stake_amount > 0.0: diff --git a/freqtrade/strategy/interface.py b/freqtrade/strategy/interface.py index c521943b1..43444d511 100644 --- a/freqtrade/strategy/interface.py +++ b/freqtrade/strategy/interface.py @@ -456,10 +456,13 @@ class IStrategy(ABC, HyperStrategyMixin): def adjust_trade_position(self, trade: Trade, current_time: datetime, current_rate: float, current_profit: float, min_stake: Optional[float], max_stake: float, + current_entry_rate: float, current_exit_rate: float, + current_entry_profit: float, current_exit_profit: float, **kwargs) -> Optional[float]: """ - Custom trade adjustment logic, returning the stake amount that a trade should be increased. - This means extra buy orders with additional fees. + Custom trade adjustment logic, returning the stake amount that a trade should be + increased or decreased. + This means extra buy or sell orders with additional fees. Only called when `position_adjustment_enable` is set to True. For full documentation please go to https://www.freqtrade.io/en/latest/strategy-advanced/ @@ -470,10 +473,16 @@ class IStrategy(ABC, HyperStrategyMixin): :param current_time: datetime object, containing the current datetime :param current_rate: Current buy rate. :param current_profit: Current profit (as ratio), calculated based on current_rate. - :param min_stake: Minimal stake size allowed by exchange. - :param max_stake: Balance available for trading. + :param min_stake: Minimal stake size allowed by exchange (for both entries and exits) + :param max_stake: Maximum stake allowed (either through balance, or by exchange limits). + :param current_entry_rate: Current rate using entry pricing. + :param current_exit_rate: Current rate using exit pricing. + :param current_entry_profit: Current profit using entry pricing. + :param current_exit_profit: Current profit using exit pricing. :param **kwargs: Ensure to keep this here so updates to this won't break your strategy. - :return float: Stake amount to adjust your trade + :return float: Stake amount to adjust your trade, + Positive values to increase position, Negative values to decrease position. + Return None for no action. """ return None diff --git a/freqtrade/templates/subtemplates/strategy_methods_advanced.j2 b/freqtrade/templates/subtemplates/strategy_methods_advanced.j2 index 3854efd85..0187fa79b 100644 --- a/freqtrade/templates/subtemplates/strategy_methods_advanced.j2 +++ b/freqtrade/templates/subtemplates/strategy_methods_advanced.j2 @@ -243,12 +243,16 @@ def check_exit_timeout(self, pair: str, trade: 'Trade', order: 'Order', """ return False -def adjust_trade_position(self, trade: 'Trade', current_time: 'datetime', - current_rate: float, current_profit: float, min_stake: Optional[float], - max_stake: float, **kwargs) -> 'Optional[float]': +def adjust_trade_position(self, trade: Trade, current_time: datetime, + current_rate: float, current_profit: float, + min_stake: Optional[float], max_stake: float, + current_entry_rate: float, current_exit_rate: float, + current_entry_profit: float, current_exit_profit: float, + **kwargs) -> Optional[float]: """ - Custom trade adjustment logic, returning the stake amount that a trade should be increased. - This means extra buy orders with additional fees. + Custom trade adjustment logic, returning the stake amount that a trade should be + increased or decreased. + This means extra buy or sell orders with additional fees. Only called when `position_adjustment_enable` is set to True. For full documentation please go to https://www.freqtrade.io/en/latest/strategy-advanced/ @@ -259,10 +263,16 @@ def adjust_trade_position(self, trade: 'Trade', current_time: 'datetime', :param current_time: datetime object, containing the current datetime :param current_rate: Current buy rate. :param current_profit: Current profit (as ratio), calculated based on current_rate. - :param min_stake: Minimal stake size allowed by exchange. - :param max_stake: Balance available for trading. + :param min_stake: Minimal stake size allowed by exchange (for both entries and exits) + :param max_stake: Maximum stake allowed (either through balance, or by exchange limits). + :param current_entry_rate: Current rate using entry pricing. + :param current_exit_rate: Current rate using exit pricing. + :param current_entry_profit: Current profit using entry pricing. + :param current_exit_profit: Current profit using exit pricing. :param **kwargs: Ensure to keep this here so updates to this won't break your strategy. - :return float: Stake amount to adjust your trade + :return float: Stake amount to adjust your trade, + Positive values to increase position, Negative values to decrease position. + Return None for no action. """ return None