From e52f82b565fa713951ffc70b84215aab217a1d73 Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 8 Jul 2022 19:44:20 +0200 Subject: [PATCH] Add leverage to custom_stake_amount callback closes #7047 --- docs/strategy-callbacks.md | 10 ++++++---- freqtrade/freqtradebot.py | 2 +- freqtrade/optimize/backtesting.py | 2 +- freqtrade/strategy/interface.py | 4 +++- .../subtemplates/strategy_methods_advanced.j2 | 6 ++++-- 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/docs/strategy-callbacks.md b/docs/strategy-callbacks.md index beffba56b..f584bd1bb 100644 --- a/docs/strategy-callbacks.md +++ b/docs/strategy-callbacks.md @@ -82,8 +82,9 @@ Called before entering a trade, makes it possible to manage your position size w ```python class AwesomeStrategy(IStrategy): def custom_stake_amount(self, pair: str, current_time: datetime, current_rate: float, - proposed_stake: float, min_stake: float, max_stake: float, - entry_tag: Optional[str], side: str, **kwargs) -> float: + proposed_stake: float, min_stake: Optional[float], max_stake: float, + leverage: float, entry_tag: Optional[str], side: str, + **kwargs) -> float: dataframe, _ = self.dp.get_analyzed_dataframe(pair=pair, timeframe=self.timeframe) current_candle = dataframe.iloc[-1].squeeze() @@ -673,9 +674,10 @@ class DigDeeperStrategy(IStrategy): max_dca_multiplier = 5.5 # This is called when placing the initial order (opening trade) - def custom_stake_amount(self, pair: str, current_time: datetime, current_rate: float, +def custom_stake_amount(self, pair: str, current_time: datetime, current_rate: float, proposed_stake: float, min_stake: Optional[float], max_stake: float, - entry_tag: Optional[str], side: str, **kwargs) -> float: + leverage: float, entry_tag: Optional[str], side: str, + **kwargs) -> float: # We need to leave most of the funds for possible further DCA orders # This also applies to fixed stakes diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index f242e001b..d1404807d 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -814,7 +814,7 @@ class FreqtradeBot(LoggingMixin): pair=pair, current_time=datetime.now(timezone.utc), current_rate=enter_limit_requested, proposed_stake=stake_amount, min_stake=min_stake_amount, max_stake=min(max_stake_amount, stake_available), - entry_tag=entry_tag, side=trade_side + leverage=leverage, entry_tag=entry_tag, side=trade_side ) stake_amount = self.wallets.validate_stake_amount( diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index 030d7bdf0..da28a8d93 100755 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -722,7 +722,7 @@ class Backtesting: pair=pair, current_time=current_time, current_rate=propose_rate, proposed_stake=stake_amount, min_stake=min_stake_amount, max_stake=min(stake_available, max_stake_amount), - entry_tag=entry_tag, side=direction) + leverage=leverage, entry_tag=entry_tag, side=direction) stake_amount_val = self.wallets.validate_stake_amount( pair=pair, diff --git a/freqtrade/strategy/interface.py b/freqtrade/strategy/interface.py index d4ccfc5db..c60817c99 100644 --- a/freqtrade/strategy/interface.py +++ b/freqtrade/strategy/interface.py @@ -442,7 +442,8 @@ class IStrategy(ABC, HyperStrategyMixin): def custom_stake_amount(self, pair: str, current_time: datetime, current_rate: float, proposed_stake: float, min_stake: Optional[float], max_stake: float, - entry_tag: Optional[str], side: str, **kwargs) -> float: + leverage: float, entry_tag: Optional[str], side: str, + **kwargs) -> float: """ Customize stake size for each new trade. @@ -452,6 +453,7 @@ class IStrategy(ABC, HyperStrategyMixin): :param proposed_stake: A stake amount proposed by the bot. :param min_stake: Minimal stake size allowed by exchange. :param max_stake: Balance available for trading. + :param leverage: Leverage selected for this trade. :param entry_tag: Optional entry_tag (buy_tag) if provided with the buy signal. :param side: 'long' or 'short' - indicating the direction of the proposed trade :return: A stake size, which is between min_stake and max_stake. diff --git a/freqtrade/templates/subtemplates/strategy_methods_advanced.j2 b/freqtrade/templates/subtemplates/strategy_methods_advanced.j2 index 815ca7cd3..989f1d37a 100644 --- a/freqtrade/templates/subtemplates/strategy_methods_advanced.j2 +++ b/freqtrade/templates/subtemplates/strategy_methods_advanced.j2 @@ -79,9 +79,10 @@ def custom_exit_price(self, pair: str, trade: 'Trade', """ return proposed_rate -def custom_stake_amount(self, pair: str, current_time: 'datetime', current_rate: float, +def custom_stake_amount(self, pair: str, current_time: datetime, current_rate: float, proposed_stake: float, min_stake: Optional[float], max_stake: float, - entry_tag: 'Optional[str]', side: str, **kwargs) -> float: + leverage: float, entry_tag: Optional[str], side: str, + **kwargs) -> float: """ Customize stake size for each new trade. @@ -91,6 +92,7 @@ def custom_stake_amount(self, pair: str, current_time: 'datetime', current_rate: :param proposed_stake: A stake amount proposed by the bot. :param min_stake: Minimal stake size allowed by exchange. :param max_stake: Balance available for trading. + :param leverage: Leverage selected for this trade. :param entry_tag: Optional entry_tag (buy_tag) if provided with the buy signal. :param side: 'long' or 'short' - indicating the direction of the proposed trade :return: A stake size, which is between min_stake and max_stake.