From f8783c908ec3e29f79edb6f8e06ea0e9685c94c9 Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 4 Apr 2022 16:48:27 +0200 Subject: [PATCH] Add side to custom_entry_price --- docs/strategy-callbacks.md | 2 +- docs/strategy_migration.md | 21 +++++++++++++++++++++ freqtrade/freqtradebot.py | 4 +++- freqtrade/optimize/backtesting.py | 4 +++- freqtrade/strategy/interface.py | 3 ++- 5 files changed, 30 insertions(+), 4 deletions(-) diff --git a/docs/strategy-callbacks.md b/docs/strategy-callbacks.md index f265bfe61..7b8769f0c 100644 --- a/docs/strategy-callbacks.md +++ b/docs/strategy-callbacks.md @@ -365,7 +365,7 @@ class AwesomeStrategy(IStrategy): # ... populate_* methods def custom_entry_price(self, pair: str, current_time: datetime, proposed_rate: float, - entry_tag: Optional[str], **kwargs) -> float: + entry_tag: Optional[str], side: str, **kwargs) -> float: dataframe, last_updated = self.dp.get_analyzed_dataframe(pair=pair, timeframe=self.timeframe) diff --git a/docs/strategy_migration.md b/docs/strategy_migration.md index 40c1ee760..5721537c6 100644 --- a/docs/strategy_migration.md +++ b/docs/strategy_migration.md @@ -18,6 +18,7 @@ You can use the quick summary as checklist. Please refer to the detailed section * New `side` argument to callbacks without trade object * [`custom_stake_amount`](#custom-stake-amount) * [`confirm_trade_entry`](#confirm_trade_entry) + * [`custom_entry_price`](#custom_entry_price) * [Changed argument name in `confirm_trade_exit`](#confirm_trade_exit) * Dataframe columns: * [`buy` -> `enter_long`](#populate_buy_trend) @@ -227,6 +228,26 @@ class AwesomeStrategy(IStrategy): return True ``` +### `custom_entry_price` + +New string argument `side` - which can be either `"long"` or `"short"`. + +``` python hl_lines="3" +class AwesomeStrategy(IStrategy): + def custom_entry_price(self, pair: str, current_time: datetime, proposed_rate: float, + entry_tag: Optional[str], **kwargs) -> float: + return proposed_rate +``` + +After: + +``` python hl_lines="3" +class AwesomeStrategy(IStrategy): + def custom_entry_price(self, pair: str, current_time: datetime, proposed_rate: float, + entry_tag: Optional[str], side: str, **kwargs) -> float: + return proposed_rate +``` + ### Adjust trade position changes While adjust-trade-position itself did not change, you should no longer use `trade.nr_of_successful_buys` - and instead use `trade.nr_of_successful_entries`, which will also include short entries. diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 95a728c99..9e01a150c 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -760,7 +760,9 @@ class FreqtradeBot(LoggingMixin): custom_entry_price = strategy_safe_wrapper(self.strategy.custom_entry_price, default_retval=proposed_enter_rate)( pair=pair, current_time=datetime.now(timezone.utc), - proposed_rate=proposed_enter_rate, entry_tag=entry_tag) + proposed_rate=proposed_enter_rate, entry_tag=entry_tag, + side=trade_side, + ) enter_limit_requested = self.get_valid_price(custom_entry_price, proposed_enter_rate) diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index 943426679..aab340c21 100644 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -643,7 +643,9 @@ class Backtesting: propose_rate = strategy_safe_wrapper(self.strategy.custom_entry_price, default_retval=propose_rate)( pair=pair, current_time=current_time, - proposed_rate=propose_rate, entry_tag=entry_tag) # default value is the open rate + proposed_rate=propose_rate, entry_tag=entry_tag, + side=direction, + ) # default value is the open rate # We can't place orders higher than current high (otherwise it'd be a stop limit buy) # which freqtrade does not support in live. if direction == "short": diff --git a/freqtrade/strategy/interface.py b/freqtrade/strategy/interface.py index 30ac4f355..508aa5c73 100644 --- a/freqtrade/strategy/interface.py +++ b/freqtrade/strategy/interface.py @@ -339,7 +339,7 @@ class IStrategy(ABC, HyperStrategyMixin): return self.stoploss def custom_entry_price(self, pair: str, current_time: datetime, proposed_rate: float, - entry_tag: Optional[str], **kwargs) -> float: + entry_tag: Optional[str], side: str, **kwargs) -> float: """ Custom entry price logic, returning the new entry price. @@ -351,6 +351,7 @@ class IStrategy(ABC, HyperStrategyMixin): :param current_time: datetime object, containing the current datetime :param proposed_rate: Rate, calculated based on pricing settings in exit_pricing. :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 :param **kwargs: Ensure to keep this here so updates to this won't break your strategy. :return float: New entry price value if provided """