From d4a7d2d444354d7e0f71c5d0706ef750cdf326f3 Mon Sep 17 00:00:00 2001 From: Sam Germain Date: Sun, 8 Aug 2021 03:38:34 -0600 Subject: [PATCH 01/31] Added short and exit_short to strategy --- freqtrade/edge/edge_positioning.py | 11 +- freqtrade/enums/signaltype.py | 3 + freqtrade/optimize/backtesting.py | 4 +- freqtrade/optimize/hyperopt.py | 7 +- freqtrade/resolvers/hyperopt_resolver.py | 1 + freqtrade/resolvers/strategy_resolver.py | 7 +- freqtrade/rpc/api_server/uvicorn_threaded.py | 2 +- freqtrade/strategy/hyper.py | 2 + freqtrade/strategy/interface.py | 203 +++++++++++------- freqtrade/strategy/strategy_helper.py | 15 +- freqtrade/templates/sample_hyperopt.py | 122 +++++++++++ .../templates/sample_hyperopt_advanced.py | 126 +++++++++++ freqtrade/templates/sample_strategy.py | 41 +++- tests/optimize/hyperopts/default_hyperopt.py | 156 ++++++++++++++ tests/optimize/test_backtest_detail.py | 4 +- tests/optimize/test_backtesting.py | 20 +- tests/optimize/test_hyperopt.py | 19 +- tests/rpc/test_rpc_apiserver.py | 2 +- tests/strategy/strats/default_strategy.py | 45 ++++ .../strategy/strats/hyperoptable_strategy.py | 62 +++++- tests/strategy/strats/legacy_strategy.py | 31 +++ tests/strategy/test_default_strategy.py | 28 ++- tests/strategy/test_interface.py | 60 +++--- tests/strategy/test_strategy_loading.py | 43 +++- 24 files changed, 862 insertions(+), 152 deletions(-) diff --git a/freqtrade/edge/edge_positioning.py b/freqtrade/edge/edge_positioning.py index 243043d31..b366059da 100644 --- a/freqtrade/edge/edge_positioning.py +++ b/freqtrade/edge/edge_positioning.py @@ -167,8 +167,15 @@ class Edge: pair_data = pair_data.sort_values(by=['date']) pair_data = pair_data.reset_index(drop=True) - df_analyzed = self.strategy.advise_sell( - self.strategy.advise_buy(pair_data, {'pair': pair}), {'pair': pair})[headers].copy() + df_analyzed = self.strategy.advise_exit( + dataframe=self.strategy.advise_enter( + dataframe=pair_data, + metadata={'pair': pair}, + is_short=False + ), + metadata={'pair': pair}, + is_short=False + )[headers].copy() trades += self._find_trades_for_stoploss_range(df_analyzed, pair, self._stoploss_range) diff --git a/freqtrade/enums/signaltype.py b/freqtrade/enums/signaltype.py index d2995d57a..ffba5ee90 100644 --- a/freqtrade/enums/signaltype.py +++ b/freqtrade/enums/signaltype.py @@ -7,6 +7,8 @@ class SignalType(Enum): """ BUY = "buy" SELL = "sell" + SHORT = "short" + EXIT_SHORT = "exit_short" class SignalTagType(Enum): @@ -14,3 +16,4 @@ class SignalTagType(Enum): Enum for signal columns """ BUY_TAG = "buy_tag" + SELL_TAG = "sell_tag" diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index 3079e326d..550ceecd8 100644 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -231,8 +231,8 @@ class Backtesting: if has_buy_tag: pair_data.loc[:, 'buy_tag'] = None # cleanup if buy_tag is exist - df_analyzed = self.strategy.advise_sell( - self.strategy.advise_buy(pair_data, {'pair': pair}), {'pair': pair}).copy() + df_analyzed = self.strategy.advise_exit( + self.strategy.advise_enter(pair_data, {'pair': pair}), {'pair': pair}).copy() # Trim startup period from analyzed dataframe df_analyzed = trim_dataframe(df_analyzed, self.timerange, startup_candles=self.required_startup) diff --git a/freqtrade/optimize/hyperopt.py b/freqtrade/optimize/hyperopt.py index 0db78aa39..4c07419b8 100644 --- a/freqtrade/optimize/hyperopt.py +++ b/freqtrade/optimize/hyperopt.py @@ -110,7 +110,7 @@ class Hyperopt: self.backtesting.strategy.advise_indicators = ( # type: ignore self.custom_hyperopt.populate_indicators) # type: ignore if hasattr(self.custom_hyperopt, 'populate_buy_trend'): - self.backtesting.strategy.advise_buy = ( # type: ignore + self.backtesting.strategy.advise_enter = ( # type: ignore self.custom_hyperopt.populate_buy_trend) # type: ignore if hasattr(self.custom_hyperopt, 'populate_sell_trend'): self.backtesting.strategy.advise_sell = ( # type: ignore @@ -283,12 +283,13 @@ class Hyperopt: params_dict = self._get_params_dict(self.dimensions, raw_params) # Apply parameters + # TODO-lev: These don't take a side, how can I pass is_short=True/False to it if HyperoptTools.has_space(self.config, 'buy'): - self.backtesting.strategy.advise_buy = ( # type: ignore + self.backtesting.strategy.advise_enter = ( # type: ignore self.custom_hyperopt.buy_strategy_generator(params_dict)) if HyperoptTools.has_space(self.config, 'sell'): - self.backtesting.strategy.advise_sell = ( # type: ignore + self.backtesting.strategy.advise_exit = ( # type: ignore self.custom_hyperopt.sell_strategy_generator(params_dict)) if HyperoptTools.has_space(self.config, 'protection'): diff --git a/freqtrade/resolvers/hyperopt_resolver.py b/freqtrade/resolvers/hyperopt_resolver.py index 8327a4d13..fd7d3dbf6 100644 --- a/freqtrade/resolvers/hyperopt_resolver.py +++ b/freqtrade/resolvers/hyperopt_resolver.py @@ -51,6 +51,7 @@ class HyperOptResolver(IResolver): if not hasattr(hyperopt, 'populate_sell_trend'): logger.info("Hyperopt class does not provide populate_sell_trend() method. " "Using populate_sell_trend from the strategy.") + # TODO-lev: Short equivelents? return hyperopt diff --git a/freqtrade/resolvers/strategy_resolver.py b/freqtrade/resolvers/strategy_resolver.py index e7c077e84..38a5b4850 100644 --- a/freqtrade/resolvers/strategy_resolver.py +++ b/freqtrade/resolvers/strategy_resolver.py @@ -202,9 +202,14 @@ class StrategyResolver(IResolver): strategy._populate_fun_len = len(getfullargspec(strategy.populate_indicators).args) strategy._buy_fun_len = len(getfullargspec(strategy.populate_buy_trend).args) strategy._sell_fun_len = len(getfullargspec(strategy.populate_sell_trend).args) + strategy._short_fun_len = len(getfullargspec(strategy.populate_short_trend).args) + strategy._exit_short_fun_len = len( + getfullargspec(strategy.populate_exit_short_trend).args) if any(x == 2 for x in [strategy._populate_fun_len, strategy._buy_fun_len, - strategy._sell_fun_len]): + strategy._sell_fun_len, + strategy._short_fun_len, + strategy._exit_short_fun_len]): strategy.INTERFACE_VERSION = 1 return strategy diff --git a/freqtrade/rpc/api_server/uvicorn_threaded.py b/freqtrade/rpc/api_server/uvicorn_threaded.py index 2f72cb74c..7d76d52ed 100644 --- a/freqtrade/rpc/api_server/uvicorn_threaded.py +++ b/freqtrade/rpc/api_server/uvicorn_threaded.py @@ -44,5 +44,5 @@ class UvicornServer(uvicorn.Server): time.sleep(1e-3) def cleanup(self): - self.should_exit = True + self.should_sell = True self.thread.join() diff --git a/freqtrade/strategy/hyper.py b/freqtrade/strategy/hyper.py index dad282d7e..87d4241f1 100644 --- a/freqtrade/strategy/hyper.py +++ b/freqtrade/strategy/hyper.py @@ -22,6 +22,8 @@ from freqtrade.exceptions import OperationalException logger = logging.getLogger(__name__) +# TODO-lev: This file + class BaseParameter(ABC): """ diff --git a/freqtrade/strategy/interface.py b/freqtrade/strategy/interface.py index bf5cc10af..26ad2fcd4 100644 --- a/freqtrade/strategy/interface.py +++ b/freqtrade/strategy/interface.py @@ -62,6 +62,8 @@ class IStrategy(ABC, HyperStrategyMixin): _populate_fun_len: int = 0 _buy_fun_len: int = 0 _sell_fun_len: int = 0 + _short_fun_len: int = 0 + _exit_short_fun_len: int = 0 _ft_params_from_file: Dict = {} # associated minimal roi minimal_roi: Dict @@ -135,7 +137,7 @@ class IStrategy(ABC, HyperStrategyMixin): @abstractmethod def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame: """ - Populate indicators that will be used in the Buy and Sell strategy + Populate indicators that will be used in the Buy, Sell, Short, Exit_short strategy :param dataframe: DataFrame with data from the exchange :param metadata: Additional information, like the currently traded pair :return: a Dataframe with all mandatory indicators for the strategies @@ -143,7 +145,7 @@ class IStrategy(ABC, HyperStrategyMixin): return dataframe @abstractmethod - def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: + def populate_enter_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: """ Based on TA indicators, populates the buy signal for the given dataframe :param dataframe: DataFrame @@ -153,7 +155,7 @@ class IStrategy(ABC, HyperStrategyMixin): return dataframe @abstractmethod - def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: + def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: """ Based on TA indicators, populates the sell signal for the given dataframe :param dataframe: DataFrame @@ -164,9 +166,9 @@ class IStrategy(ABC, HyperStrategyMixin): def check_buy_timeout(self, pair: str, trade: Trade, order: dict, **kwargs) -> bool: """ - Check buy timeout function callback. - This method can be used to override the buy-timeout. - It is called whenever a limit buy order has been created, + Check enter timeout function callback. + This method can be used to override the enter-timeout. + It is called whenever a limit buy/short order has been created, and is not yet fully filled. Configuration options in `unfilledtimeout` will be verified before this, so ensure to set these timeouts high enough. @@ -176,16 +178,16 @@ class IStrategy(ABC, HyperStrategyMixin): :param trade: trade object. :param order: Order dictionary as returned from CCXT. :param **kwargs: Ensure to keep this here so updates to this won't break your strategy. - :return bool: When True is returned, then the buy-order is cancelled. + :return bool: When True is returned, then the buy/short-order is cancelled. """ return False def check_sell_timeout(self, pair: str, trade: Trade, order: dict, **kwargs) -> bool: """ - Check sell timeout function callback. - This method can be used to override the sell-timeout. - It is called whenever a limit sell order has been created, - and is not yet fully filled. + Check exit timeout function callback. + This method can be used to override the exit-timeout. + It is called whenever a (long) limit sell order or (short) limit buy + has been created, and is not yet fully filled. Configuration options in `unfilledtimeout` will be verified before this, so ensure to set these timeouts high enough. @@ -194,7 +196,7 @@ class IStrategy(ABC, HyperStrategyMixin): :param trade: trade object. :param order: Order dictionary as returned from CCXT. :param **kwargs: Ensure to keep this here so updates to this won't break your strategy. - :return bool: When True is returned, then the sell-order is cancelled. + :return bool: When True is returned, then the (long)sell/(short)buy-order is cancelled. """ return False @@ -210,7 +212,7 @@ class IStrategy(ABC, HyperStrategyMixin): def confirm_trade_entry(self, pair: str, order_type: str, amount: float, rate: float, time_in_force: str, current_time: datetime, **kwargs) -> bool: """ - Called right before placing a buy order. + Called right before placing a buy/short order. Timing for this function is critical, so avoid doing heavy computations or network requests in this method. @@ -218,7 +220,7 @@ class IStrategy(ABC, HyperStrategyMixin): When not implemented by a strategy, returns True (always confirming). - :param pair: Pair that's about to be bought. + :param pair: Pair that's about to be bought/shorted. :param order_type: Order type (as configured in order_types). usually limit or market. :param amount: Amount in target (quote) currency that's going to be traded. :param rate: Rate that's going to be used when using limit orders @@ -234,7 +236,7 @@ class IStrategy(ABC, HyperStrategyMixin): rate: float, time_in_force: str, sell_reason: str, current_time: datetime, **kwargs) -> bool: """ - Called right before placing a regular sell order. + Called right before placing a regular sell/exit_short order. Timing for this function is critical, so avoid doing heavy computations or network requests in this method. @@ -242,18 +244,18 @@ class IStrategy(ABC, HyperStrategyMixin): When not implemented by a strategy, returns True (always confirming). - :param pair: Pair that's about to be sold. + :param pair: Pair for trade that's about to be exited. :param trade: trade object. :param order_type: Order type (as configured in order_types). usually limit or market. :param amount: Amount in quote currency. :param rate: Rate that's going to be used when using limit orders :param time_in_force: Time in force. Defaults to GTC (Good-til-cancelled). - :param sell_reason: Sell reason. + :param sell_reason: Exit reason. Can be any of ['roi', 'stop_loss', 'stoploss_on_exchange', 'trailing_stop_loss', 'sell_signal', 'force_sell', 'emergency_sell'] :param current_time: datetime object, containing the current datetime :param **kwargs: Ensure to keep this here so updates to this won't break your strategy. - :return bool: When True is returned, then the sell-order is placed on the exchange. + :return bool: When True, then the sell-order/exit_short-order is placed on the exchange. False aborts the process """ return True @@ -283,15 +285,15 @@ class IStrategy(ABC, HyperStrategyMixin): def custom_sell(self, pair: str, trade: Trade, current_time: datetime, current_rate: float, current_profit: float, **kwargs) -> Optional[Union[str, bool]]: """ - Custom sell signal logic indicating that specified position should be sold. Returning a - string or True from this method is equal to setting sell signal on a candle at specified - time. This method is not called when sell signal is set. + Custom exit signal logic indicating that specified position should be sold. Returning a + string or True from this method is equal to setting exit signal on a candle at specified + time. This method is not called when exit signal is set. - This method should be overridden to create sell signals that depend on trade parameters. For - example you could implement a sell relative to the candle when the trade was opened, + This method should be overridden to create exit signals that depend on trade parameters. For + example you could implement an exit relative to the candle when the trade was opened, or a custom 1:2 risk-reward ROI. - Custom sell reason max length is 64. Exceeding characters will be removed. + Custom exit reason max length is 64. Exceeding characters will be removed. :param pair: Pair that's currently analyzed :param trade: trade object. @@ -299,7 +301,7 @@ class IStrategy(ABC, HyperStrategyMixin): :param current_rate: Rate, calculated based on pricing settings in ask_strategy. :param current_profit: Current profit (as ratio), calculated based on current_rate. :param **kwargs: Ensure to keep this here so updates to this won't break your strategy. - :return: To execute sell, return a string with custom sell reason or True. Otherwise return + :return: To execute exit, return a string with custom sell reason or True. Otherwise return None or False. """ return None @@ -371,7 +373,7 @@ class IStrategy(ABC, HyperStrategyMixin): Checks if a pair is currently locked The 2nd, optional parameter ensures that locks are applied until the new candle arrives, and not stop at 14:00:00 - while the next candle arrives at 14:00:02 leaving a gap - of 2 seconds for a buy to happen on an old signal. + of 2 seconds for a buy/short to happen on an old signal. :param pair: "Pair to check" :param candle_date: Date of the last candle. Optional, defaults to current date :returns: locking state of the pair in question. @@ -387,15 +389,17 @@ class IStrategy(ABC, HyperStrategyMixin): def analyze_ticker(self, dataframe: DataFrame, metadata: dict) -> DataFrame: """ Parses the given candle (OHLCV) data and returns a populated DataFrame - add several TA indicators and buy signal to it + add several TA indicators and buy/short signal to it :param dataframe: Dataframe containing data from exchange :param metadata: Metadata dictionary with additional data (e.g. 'pair') :return: DataFrame of candle (OHLCV) data with indicator data and signals added """ logger.debug("TA Analysis Launched") dataframe = self.advise_indicators(dataframe, metadata) - dataframe = self.advise_buy(dataframe, metadata) - dataframe = self.advise_sell(dataframe, metadata) + dataframe = self.advise_enter(dataframe, metadata, is_short=False) + dataframe = self.advise_exit(dataframe, metadata, is_short=False) + dataframe = self.advise_enter(dataframe, metadata, is_short=True) + dataframe = self.advise_exit(dataframe, metadata, is_short=True) return dataframe def _analyze_ticker_internal(self, dataframe: DataFrame, metadata: dict) -> DataFrame: @@ -422,7 +426,10 @@ class IStrategy(ABC, HyperStrategyMixin): logger.debug("Skipping TA Analysis for already analyzed candle") dataframe['buy'] = 0 dataframe['sell'] = 0 + dataframe['short'] = 0 + dataframe['exit_short'] = 0 dataframe['buy_tag'] = None + dataframe['short_tag'] = None # Other Defs in strategy that want to be called every loop here # twitter_sell = self.watch_twitter_feed(dataframe, metadata) @@ -482,6 +489,7 @@ class IStrategy(ABC, HyperStrategyMixin): if dataframe is None: message = "No dataframe returned (return statement missing?)." elif 'buy' not in dataframe: + # TODO-lev: Something? message = "Buy column not set." elif df_len != len(dataframe): message = message_template.format("length") @@ -499,15 +507,18 @@ class IStrategy(ABC, HyperStrategyMixin): self, pair: str, timeframe: str, - dataframe: DataFrame + dataframe: DataFrame, + is_short: bool = False ) -> Tuple[bool, bool, Optional[str]]: """ - Calculates current signal based based on the buy / sell columns of the dataframe. - Used by Bot to get the signal to buy or sell + Calculates current signal based based on the buy/short or sell/exit_short + columns of the dataframe. + Used by Bot to get the signal to buy, sell, short, or exit_short :param pair: pair in format ANT/BTC :param timeframe: timeframe to use :param dataframe: Analyzed dataframe to get signal from. - :return: (Buy, Sell) A bool-tuple indicating buy/sell signal + :return: (Buy, Sell)/(Short, Exit_short) A bool-tuple indicating + (buy/sell)/(short/exit_short) signal """ if not isinstance(dataframe, DataFrame) or dataframe.empty: logger.warning(f'Empty candle (OHLCV) data for pair {pair}') @@ -528,42 +539,49 @@ class IStrategy(ABC, HyperStrategyMixin): ) return False, False, None - buy = latest[SignalType.BUY.value] == 1 + (enter_type, enter_tag) = ( + (SignalType.SHORT, SignalTagType.SHORT_TAG) + if is_short else + (SignalType.BUY, SignalTagType.BUY_TAG) + ) + exit_type = SignalType.EXIT_SHORT if is_short else SignalType.SELL - sell = False - if SignalType.SELL.value in latest: - sell = latest[SignalType.SELL.value] == 1 + enter = latest[enter_type.value] == 1 - buy_tag = latest.get(SignalTagType.BUY_TAG.value, None) + exit = False + if exit_type.value in latest: + exit = latest[exit_type.value] == 1 - logger.debug('trigger: %s (pair=%s) buy=%s sell=%s', - latest['date'], pair, str(buy), str(sell)) + enter_tag_value = latest.get(enter_tag.value, None) + + logger.debug(f'trigger: %s (pair=%s) {enter_type.value}=%s {exit_type.value}=%s', + latest['date'], pair, str(enter), str(exit)) timeframe_seconds = timeframe_to_seconds(timeframe) if self.ignore_expired_candle(latest_date=latest_date, current_time=datetime.now(timezone.utc), timeframe_seconds=timeframe_seconds, - buy=buy): - return False, sell, buy_tag - return buy, sell, buy_tag + enter=enter): + return False, exit, enter_tag_value + return enter, exit, enter_tag_value def ignore_expired_candle(self, latest_date: datetime, current_time: datetime, - timeframe_seconds: int, buy: bool): - if self.ignore_buying_expired_candle_after and buy: + timeframe_seconds: int, enter: bool): + if self.ignore_buying_expired_candle_after and enter: time_delta = current_time - (latest_date + timedelta(seconds=timeframe_seconds)) return time_delta.total_seconds() > self.ignore_buying_expired_candle_after else: return False - def should_sell(self, trade: Trade, rate: float, date: datetime, buy: bool, - sell: bool, low: float = None, high: float = None, + def should_sell(self, trade: Trade, rate: float, date: datetime, enter: bool, + exit: bool, low: float = None, high: float = None, force_stoploss: float = 0) -> SellCheckTuple: """ - This function evaluates if one of the conditions required to trigger a sell - has been reached, which can either be a stop-loss, ROI or sell-signal. - :param low: Only used during backtesting to simulate stoploss - :param high: Only used during backtesting, to simulate ROI + This function evaluates if one of the conditions required to trigger a sell/exit_short + has been reached, which can either be a stop-loss, ROI or exit-signal. + :param low: Only used during backtesting to simulate (long)stoploss/(short)ROI + :param high: Only used during backtesting, to simulate (short)stoploss/(long)ROI :param force_stoploss: Externally provided stoploss - :return: True if trade should be sold, False otherwise + :return: True if trade should be exited, False otherwise """ current_rate = rate current_profit = trade.calc_profit_ratio(current_rate) @@ -578,8 +596,8 @@ class IStrategy(ABC, HyperStrategyMixin): current_rate = high or rate current_profit = trade.calc_profit_ratio(current_rate) - # if buy signal and ignore_roi is set, we don't need to evaluate min_roi. - roi_reached = (not (buy and self.ignore_roi_if_buy_signal) + # if enter signal and ignore_roi is set, we don't need to evaluate min_roi. + roi_reached = (not (enter and self.ignore_roi_if_buy_signal) and self.min_roi_reached(trade=trade, current_profit=current_profit, current_time=date)) @@ -592,10 +610,11 @@ class IStrategy(ABC, HyperStrategyMixin): if (self.sell_profit_only and current_profit <= self.sell_profit_offset): # sell_profit_only and profit doesn't reach the offset - ignore sell signal pass - elif self.use_sell_signal and not buy: - if sell: + elif self.use_sell_signal and not enter: + if exit: sell_signal = SellType.SELL_SIGNAL else: + trade_type = "exit_short" if trade.is_short else "sell" custom_reason = strategy_safe_wrapper(self.custom_sell, default_retval=False)( pair=trade.pair, trade=trade, current_time=date, current_rate=current_rate, current_profit=current_profit) @@ -603,18 +622,18 @@ class IStrategy(ABC, HyperStrategyMixin): sell_signal = SellType.CUSTOM_SELL if isinstance(custom_reason, str): if len(custom_reason) > CUSTOM_SELL_MAX_LENGTH: - logger.warning(f'Custom sell reason returned from custom_sell is too ' - f'long and was trimmed to {CUSTOM_SELL_MAX_LENGTH} ' - f'characters.') + logger.warning(f'Custom {trade_type} reason returned from ' + f'custom_{trade_type} is too long and was trimmed' + f'to {CUSTOM_SELL_MAX_LENGTH} characters.') custom_reason = custom_reason[:CUSTOM_SELL_MAX_LENGTH] else: custom_reason = None - # TODO: return here if sell-signal should be favored over ROI + # TODO: return here if exit-signal should be favored over ROI # Start evaluations # Sequence: # ROI (if not stoploss) - # Sell-signal + # Exit-signal # Stoploss if roi_reached and stoplossflag.sell_type != SellType.STOP_LOSS: logger.debug(f"{trade.pair} - Required profit reached. sell_type=SellType.ROI") @@ -632,7 +651,7 @@ class IStrategy(ABC, HyperStrategyMixin): return stoplossflag # This one is noisy, commented out... - # logger.debug(f"{trade.pair} - No sell signal.") + # logger.debug(f"{trade.pair} - No exit signal.") return SellCheckTuple(sell_type=SellType.NONE) def stop_loss_reached(self, current_rate: float, trade: Trade, @@ -641,7 +660,7 @@ class IStrategy(ABC, HyperStrategyMixin): high: float = None) -> SellCheckTuple: """ Based on current profit of the trade and configured (trailing) stoploss, - decides to sell or not + decides to exit or not :param current_profit: current profit as ratio :param low: Low value of this candle, only set in backtesting :param high: High value of this candle, only set in backtesting @@ -651,7 +670,12 @@ class IStrategy(ABC, HyperStrategyMixin): # Initiate stoploss with open_rate. Does nothing if stoploss is already set. trade.adjust_stop_loss(trade.open_rate, stop_loss_value, initial=True) - if self.use_custom_stoploss and trade.stop_loss < (low or current_rate): + dir_correct = ( + trade.stop_loss < (low or current_rate) and not trade.is_short or + trade.stop_loss > (low or current_rate) and trade.is_short + ) + + if self.use_custom_stoploss and dir_correct: stop_loss_value = strategy_safe_wrapper(self.custom_stoploss, default_retval=None )(pair=trade.pair, trade=trade, current_time=current_time, @@ -735,7 +759,7 @@ class IStrategy(ABC, HyperStrategyMixin): def ohlcvdata_to_dataframe(self, data: Dict[str, DataFrame]) -> Dict[str, DataFrame]: """ Populates indicators for given candle (OHLCV) data (for multiple pairs) - Does not run advise_buy or advise_sell! + Does not run advise_enter or advise_exit! Used by optimize operations only, not during dry / live runs. Using .copy() to get a fresh copy of the dataframe for every strategy run. Has positive effects on memory usage for whatever reason - also when @@ -746,7 +770,7 @@ class IStrategy(ABC, HyperStrategyMixin): def advise_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame: """ - Populate indicators that will be used in the Buy and Sell strategy + Populate indicators that will be used in the Buy, Sell, short, exit_short strategy This method should not be overridden. :param dataframe: Dataframe with data from the exchange :param metadata: Additional information, like the currently traded pair @@ -760,37 +784,60 @@ class IStrategy(ABC, HyperStrategyMixin): else: return self.populate_indicators(dataframe, metadata) - def advise_buy(self, dataframe: DataFrame, metadata: dict) -> DataFrame: + def advise_enter( + self, + dataframe: DataFrame, + metadata: dict, + is_short: bool = False + ) -> DataFrame: """ - Based on TA indicators, populates the buy signal for the given dataframe + Based on TA indicators, populates the buy/short signal for the given dataframe This method should not be overridden. :param dataframe: DataFrame :param metadata: Additional information dictionary, with details like the currently traded pair :return: DataFrame with buy column """ - logger.debug(f"Populating buy signals for pair {metadata.get('pair')}.") + (type, fun_len) = ( + ("short", self._short_fun_len) + if is_short else + ("buy", self._buy_fun_len) + ) - if self._buy_fun_len == 2: + logger.debug(f"Populating {type} signals for pair {metadata.get('pair')}.") + + if fun_len == 2: warnings.warn("deprecated - check out the Sample strategy to see " "the current function headers!", DeprecationWarning) - return self.populate_buy_trend(dataframe) # type: ignore + return self.populate_enter_trend(dataframe) # type: ignore else: - return self.populate_buy_trend(dataframe, metadata) + return self.populate_enter_trend(dataframe, metadata) - def advise_sell(self, dataframe: DataFrame, metadata: dict) -> DataFrame: + def advise_exit( + self, + dataframe: DataFrame, + metadata: dict, + is_short: bool = False + ) -> DataFrame: """ - Based on TA indicators, populates the sell signal for the given dataframe + Based on TA indicators, populates the sell/exit_short signal for the given dataframe This method should not be overridden. :param dataframe: DataFrame :param metadata: Additional information dictionary, with details like the currently traded pair :return: DataFrame with sell column """ - logger.debug(f"Populating sell signals for pair {metadata.get('pair')}.") - if self._sell_fun_len == 2: + + (type, fun_len) = ( + ("exit_short", self._exit_short_fun_len) + if is_short else + ("sell", self._sell_fun_len) + ) + + logger.debug(f"Populating {type} signals for pair {metadata.get('pair')}.") + if fun_len == 2: warnings.warn("deprecated - check out the Sample strategy to see " "the current function headers!", DeprecationWarning) - return self.populate_sell_trend(dataframe) # type: ignore + return self.populate_exit_trend(dataframe) # type: ignore else: - return self.populate_sell_trend(dataframe, metadata) + return self.populate_exit_trend(dataframe, metadata) diff --git a/freqtrade/strategy/strategy_helper.py b/freqtrade/strategy/strategy_helper.py index e089ebf31..e7dbfbac7 100644 --- a/freqtrade/strategy/strategy_helper.py +++ b/freqtrade/strategy/strategy_helper.py @@ -58,7 +58,11 @@ def merge_informative_pair(dataframe: pd.DataFrame, informative: pd.DataFrame, return dataframe -def stoploss_from_open(open_relative_stop: float, current_profit: float) -> float: +def stoploss_from_open( + open_relative_stop: float, + current_profit: float, + for_short: bool = False +) -> float: """ Given the current profit, and a desired stop loss value relative to the open price, @@ -72,14 +76,17 @@ def stoploss_from_open(open_relative_stop: float, current_profit: float) -> floa :param open_relative_stop: Desired stop loss percentage relative to open price :param current_profit: The current profit percentage - :return: Positive stop loss value relative to current price + :return: Stop loss value relative to current price """ # formula is undefined for current_profit -1, return maximum value if current_profit == -1: return 1 - stoploss = 1-((1+open_relative_stop)/(1+current_profit)) + stoploss = 1-((1+open_relative_stop)/(1+current_profit)) # TODO-lev: Is this right? # negative stoploss values indicate the requested stop price is higher than the current price - return max(stoploss, 0.0) + if for_short: + return min(stoploss, 0.0) + else: + return max(stoploss, 0.0) diff --git a/freqtrade/templates/sample_hyperopt.py b/freqtrade/templates/sample_hyperopt.py index ed1af7718..6707ec8d4 100644 --- a/freqtrade/templates/sample_hyperopt.py +++ b/freqtrade/templates/sample_hyperopt.py @@ -172,3 +172,125 @@ class SampleHyperOpt(IHyperOpt): return dataframe return populate_sell_trend + + @staticmethod + def short_strategy_generator(params: Dict[str, Any]) -> Callable: + """ + Define the short strategy parameters to be used by Hyperopt. + """ + def populate_short_trend(dataframe: DataFrame, metadata: dict) -> DataFrame: + """ + Buy strategy Hyperopt will build and use. + """ + conditions = [] + + # GUARDS AND TRENDS + if 'mfi-enabled' in params and params['mfi-enabled']: + conditions.append(dataframe['mfi'] > params['mfi-value']) + if 'fastd-enabled' in params and params['fastd-enabled']: + conditions.append(dataframe['fastd'] > params['fastd-value']) + if 'adx-enabled' in params and params['adx-enabled']: + conditions.append(dataframe['adx'] < params['adx-value']) + if 'rsi-enabled' in params and params['rsi-enabled']: + conditions.append(dataframe['rsi'] > params['rsi-value']) + + # TRIGGERS + if 'trigger' in params: + if params['trigger'] == 'bb_upper': + conditions.append(dataframe['close'] > dataframe['bb_upperband']) + if params['trigger'] == 'macd_cross_signal': + conditions.append(qtpylib.crossed_below( + dataframe['macd'], dataframe['macdsignal'] + )) + if params['trigger'] == 'sar_reversal': + conditions.append(qtpylib.crossed_below( + dataframe['close'], dataframe['sar'] + )) + + if conditions: + dataframe.loc[ + reduce(lambda x, y: x & y, conditions), + 'short'] = 1 + + return dataframe + + return populate_short_trend + + @staticmethod + def short_indicator_space() -> List[Dimension]: + """ + Define your Hyperopt space for searching short strategy parameters. + """ + return [ + Integer(75, 90, name='mfi-value'), + Integer(55, 85, name='fastd-value'), + Integer(50, 80, name='adx-value'), + Integer(60, 80, name='rsi-value'), + Categorical([True, False], name='mfi-enabled'), + Categorical([True, False], name='fastd-enabled'), + Categorical([True, False], name='adx-enabled'), + Categorical([True, False], name='rsi-enabled'), + Categorical(['bb_upper', 'macd_cross_signal', 'sar_reversal'], name='trigger') + ] + + @staticmethod + def exit_short_strategy_generator(params: Dict[str, Any]) -> Callable: + """ + Define the exit_short strategy parameters to be used by Hyperopt. + """ + def populate_exit_short_trend(dataframe: DataFrame, metadata: dict) -> DataFrame: + """ + Exit_short strategy Hyperopt will build and use. + """ + conditions = [] + + # GUARDS AND TRENDS + if 'exit-short-mfi-enabled' in params and params['exit-short-mfi-enabled']: + conditions.append(dataframe['mfi'] < params['exit-short-mfi-value']) + if 'exit-short-fastd-enabled' in params and params['exit-short-fastd-enabled']: + conditions.append(dataframe['fastd'] < params['exit-short-fastd-value']) + if 'exit-short-adx-enabled' in params and params['exit-short-adx-enabled']: + conditions.append(dataframe['adx'] > params['exit-short-adx-value']) + if 'exit-short-rsi-enabled' in params and params['exit-short-rsi-enabled']: + conditions.append(dataframe['rsi'] < params['exit-short-rsi-value']) + + # TRIGGERS + if 'exit-short-trigger' in params: + if params['exit-short-trigger'] == 'exit-short-bb_lower': + conditions.append(dataframe['close'] < dataframe['bb_lowerband']) + if params['exit-short-trigger'] == 'exit-short-macd_cross_signal': + conditions.append(qtpylib.crossed_below( + dataframe['macdsignal'], dataframe['macd'] + )) + if params['exit-short-trigger'] == 'exit-short-sar_reversal': + conditions.append(qtpylib.crossed_below( + dataframe['sar'], dataframe['close'] + )) + + if conditions: + dataframe.loc[ + reduce(lambda x, y: x & y, conditions), + 'exit_short'] = 1 + + return dataframe + + return populate_exit_short_trend + + @staticmethod + def exit_short_indicator_space() -> List[Dimension]: + """ + Define your Hyperopt space for searching exit short strategy parameters. + """ + return [ + Integer(1, 25, name='exit_short-mfi-value'), + Integer(1, 50, name='exit_short-fastd-value'), + Integer(1, 50, name='exit_short-adx-value'), + Integer(1, 40, name='exit_short-rsi-value'), + Categorical([True, False], name='exit_short-mfi-enabled'), + Categorical([True, False], name='exit_short-fastd-enabled'), + Categorical([True, False], name='exit_short-adx-enabled'), + Categorical([True, False], name='exit_short-rsi-enabled'), + Categorical(['exit_short-bb_lower', + 'exit_short-macd_cross_signal', + 'exit_short-sar_reversal'], name='exit_short-trigger') + ] diff --git a/freqtrade/templates/sample_hyperopt_advanced.py b/freqtrade/templates/sample_hyperopt_advanced.py index cc13b6ba3..cee343bb6 100644 --- a/freqtrade/templates/sample_hyperopt_advanced.py +++ b/freqtrade/templates/sample_hyperopt_advanced.py @@ -187,9 +187,132 @@ class AdvancedSampleHyperOpt(IHyperOpt): return populate_sell_trend + @staticmethod + def short_strategy_generator(params: Dict[str, Any]) -> Callable: + """ + Define the short strategy parameters to be used by Hyperopt. + """ + def populate_short_trend(dataframe: DataFrame, metadata: dict) -> DataFrame: + """ + Buy strategy Hyperopt will build and use. + """ + conditions = [] + + # GUARDS AND TRENDS + if 'mfi-enabled' in params and params['mfi-enabled']: + conditions.append(dataframe['mfi'] > params['mfi-value']) + if 'fastd-enabled' in params and params['fastd-enabled']: + conditions.append(dataframe['fastd'] > params['fastd-value']) + if 'adx-enabled' in params and params['adx-enabled']: + conditions.append(dataframe['adx'] < params['adx-value']) + if 'rsi-enabled' in params and params['rsi-enabled']: + conditions.append(dataframe['rsi'] > params['rsi-value']) + + # TRIGGERS + if 'trigger' in params: + if params['trigger'] == 'bb_upper': + conditions.append(dataframe['close'] > dataframe['bb_upperband']) + if params['trigger'] == 'macd_cross_signal': + conditions.append(qtpylib.crossed_below( + dataframe['macd'], dataframe['macdsignal'] + )) + if params['trigger'] == 'sar_reversal': + conditions.append(qtpylib.crossed_below( + dataframe['close'], dataframe['sar'] + )) + + if conditions: + dataframe.loc[ + reduce(lambda x, y: x & y, conditions), + 'short'] = 1 + + return dataframe + + return populate_short_trend + + @staticmethod + def short_indicator_space() -> List[Dimension]: + """ + Define your Hyperopt space for searching short strategy parameters. + """ + return [ + Integer(75, 90, name='mfi-value'), + Integer(55, 85, name='fastd-value'), + Integer(50, 80, name='adx-value'), + Integer(60, 80, name='rsi-value'), + Categorical([True, False], name='mfi-enabled'), + Categorical([True, False], name='fastd-enabled'), + Categorical([True, False], name='adx-enabled'), + Categorical([True, False], name='rsi-enabled'), + Categorical(['bb_upper', 'macd_cross_signal', 'sar_reversal'], name='trigger') + ] + + @staticmethod + def exit_short_strategy_generator(params: Dict[str, Any]) -> Callable: + """ + Define the exit_short strategy parameters to be used by Hyperopt. + """ + def populate_exit_short_trend(dataframe: DataFrame, metadata: dict) -> DataFrame: + """ + Exit_short strategy Hyperopt will build and use. + """ + conditions = [] + + # GUARDS AND TRENDS + if 'exit-short-mfi-enabled' in params and params['exit-short-mfi-enabled']: + conditions.append(dataframe['mfi'] < params['exit-short-mfi-value']) + if 'exit-short-fastd-enabled' in params and params['exit-short-fastd-enabled']: + conditions.append(dataframe['fastd'] < params['exit-short-fastd-value']) + if 'exit-short-adx-enabled' in params and params['exit-short-adx-enabled']: + conditions.append(dataframe['adx'] > params['exit-short-adx-value']) + if 'exit-short-rsi-enabled' in params and params['exit-short-rsi-enabled']: + conditions.append(dataframe['rsi'] < params['exit-short-rsi-value']) + + # TRIGGERS + if 'exit-short-trigger' in params: + if params['exit-short-trigger'] == 'exit-short-bb_lower': + conditions.append(dataframe['close'] < dataframe['bb_lowerband']) + if params['exit-short-trigger'] == 'exit-short-macd_cross_signal': + conditions.append(qtpylib.crossed_below( + dataframe['macdsignal'], dataframe['macd'] + )) + if params['exit-short-trigger'] == 'exit-short-sar_reversal': + conditions.append(qtpylib.crossed_below( + dataframe['sar'], dataframe['close'] + )) + + if conditions: + dataframe.loc[ + reduce(lambda x, y: x & y, conditions), + 'exit_short'] = 1 + + return dataframe + + return populate_exit_short_trend + + @staticmethod + def exit_short_indicator_space() -> List[Dimension]: + """ + Define your Hyperopt space for searching exit short strategy parameters. + """ + return [ + Integer(1, 25, name='exit_short-mfi-value'), + Integer(1, 50, name='exit_short-fastd-value'), + Integer(1, 50, name='exit_short-adx-value'), + Integer(1, 40, name='exit_short-rsi-value'), + Categorical([True, False], name='exit_short-mfi-enabled'), + Categorical([True, False], name='exit_short-fastd-enabled'), + Categorical([True, False], name='exit_short-adx-enabled'), + Categorical([True, False], name='exit_short-rsi-enabled'), + Categorical(['exit_short-bb_lower', + 'exit_short-macd_cross_signal', + 'exit_short-sar_reversal'], name='exit_short-trigger') + ] + @staticmethod def generate_roi_table(params: Dict) -> Dict[int, float]: """ + # TODO-lev? Generate the ROI table that will be used by Hyperopt This implementation generates the default legacy Freqtrade ROI tables. @@ -211,6 +334,7 @@ class AdvancedSampleHyperOpt(IHyperOpt): @staticmethod def roi_space() -> List[Dimension]: """ + # TODO-lev? Values to search for each ROI steps Override it if you need some different ranges for the parameters in the @@ -231,6 +355,7 @@ class AdvancedSampleHyperOpt(IHyperOpt): @staticmethod def stoploss_space() -> List[Dimension]: """ + # TODO-lev? Stoploss Value to search Override it if you need some different range for the parameter in the @@ -243,6 +368,7 @@ class AdvancedSampleHyperOpt(IHyperOpt): @staticmethod def trailing_space() -> List[Dimension]: """ + # TODO-lev? Create a trailing stoploss space. You may override it in your custom Hyperopt class. diff --git a/freqtrade/templates/sample_strategy.py b/freqtrade/templates/sample_strategy.py index 574819949..3e73d3134 100644 --- a/freqtrade/templates/sample_strategy.py +++ b/freqtrade/templates/sample_strategy.py @@ -29,7 +29,7 @@ class SampleStrategy(IStrategy): You must keep: - the lib in the section "Do not remove these libs" - - the methods: populate_indicators, populate_buy_trend, populate_sell_trend + - the methods: populate_indicators, populate_buy_trend, populate_sell_trend, populate_short_trend, populate_exit_short_trend You should keep: - timeframe, minimal_roi, stoploss, trailing_* """ @@ -58,6 +58,8 @@ class SampleStrategy(IStrategy): # Hyperoptable parameters buy_rsi = IntParameter(low=1, high=50, default=30, space='buy', optimize=True, load=True) sell_rsi = IntParameter(low=50, high=100, default=70, space='sell', optimize=True, load=True) + short_rsi = IntParameter(low=51, high=100, default=70, space='sell', optimize=True, load=True) + exit_short_rsi = IntParameter(low=1, high=50, default=30, space='buy', optimize=True, load=True) # Optimal timeframe for the strategy. timeframe = '5m' @@ -373,3 +375,40 @@ class SampleStrategy(IStrategy): ), 'sell'] = 1 return dataframe + + def populate_short_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: + """ + Based on TA indicators, populates the short signal for the given dataframe + :param dataframe: DataFrame populated with indicators + :param metadata: Additional information, like the currently traded pair + :return: DataFrame with short column + """ + dataframe.loc[ + ( + # Signal: RSI crosses above 70 + (qtpylib.crossed_above(dataframe['rsi'], self.short_rsi.value)) & + (dataframe['tema'] > dataframe['bb_middleband']) & # Guard: tema above BB middle + (dataframe['tema'] < dataframe['tema'].shift(1)) & # Guard: tema is falling + (dataframe['volume'] > 0) # Make sure Volume is not 0 + ), + 'short'] = 1 + return dataframe + + def populate_exit_short_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: + """ + Based on TA indicators, populates the exit_short signal for the given dataframe + :param dataframe: DataFrame populated with indicators + :param metadata: Additional information, like the currently traded pair + :return: DataFrame with exit_short column + """ + dataframe.loc[ + ( + # Signal: RSI crosses above 30 + (qtpylib.crossed_above(dataframe['rsi'], self.exit_short_rsi.value)) & + (dataframe['tema'] <= dataframe['bb_middleband']) & # Guard: tema below BB middle + (dataframe['tema'] > dataframe['tema'].shift(1)) & # Guard: tema is raising + (dataframe['volume'] > 0) # Make sure Volume is not 0 + ), + 'exit_short'] = 1 + + return dataframe diff --git a/tests/optimize/hyperopts/default_hyperopt.py b/tests/optimize/hyperopts/default_hyperopt.py index 2e2bca3d0..cc8771d1b 100644 --- a/tests/optimize/hyperopts/default_hyperopt.py +++ b/tests/optimize/hyperopts/default_hyperopt.py @@ -105,6 +105,66 @@ class DefaultHyperOpt(IHyperOpt): Categorical(['bb_lower', 'macd_cross_signal', 'sar_reversal'], name='trigger') ] + @staticmethod + def short_strategy_generator(params: Dict[str, Any]) -> Callable: + """ + Define the short strategy parameters to be used by Hyperopt. + """ + def populate_short_trend(dataframe: DataFrame, metadata: dict) -> DataFrame: + """ + Buy strategy Hyperopt will build and use. + """ + conditions = [] + + # GUARDS AND TRENDS + if 'mfi-enabled' in params and params['mfi-enabled']: + conditions.append(dataframe['mfi'] > params['mfi-value']) + if 'fastd-enabled' in params and params['fastd-enabled']: + conditions.append(dataframe['fastd'] > params['fastd-value']) + if 'adx-enabled' in params and params['adx-enabled']: + conditions.append(dataframe['adx'] < params['adx-value']) + if 'rsi-enabled' in params and params['rsi-enabled']: + conditions.append(dataframe['rsi'] > params['rsi-value']) + + # TRIGGERS + if 'trigger' in params: + if params['trigger'] == 'bb_upper': + conditions.append(dataframe['close'] > dataframe['bb_upperband']) + if params['trigger'] == 'macd_cross_signal': + conditions.append(qtpylib.crossed_below( + dataframe['macd'], dataframe['macdsignal'] + )) + if params['trigger'] == 'sar_reversal': + conditions.append(qtpylib.crossed_below( + dataframe['close'], dataframe['sar'] + )) + + if conditions: + dataframe.loc[ + reduce(lambda x, y: x & y, conditions), + 'short'] = 1 + + return dataframe + + return populate_short_trend + + @staticmethod + def short_indicator_space() -> List[Dimension]: + """ + Define your Hyperopt space for searching short strategy parameters. + """ + return [ + Integer(75, 90, name='mfi-value'), + Integer(55, 85, name='fastd-value'), + Integer(50, 80, name='adx-value'), + Integer(60, 80, name='rsi-value'), + Categorical([True, False], name='mfi-enabled'), + Categorical([True, False], name='fastd-enabled'), + Categorical([True, False], name='adx-enabled'), + Categorical([True, False], name='rsi-enabled'), + Categorical(['bb_upper', 'macd_cross_signal', 'sar_reversal'], name='trigger') + ] + @staticmethod def sell_strategy_generator(params: Dict[str, Any]) -> Callable: """ @@ -148,6 +208,49 @@ class DefaultHyperOpt(IHyperOpt): return populate_sell_trend + @staticmethod + def exit_short_strategy_generator(params: Dict[str, Any]) -> Callable: + """ + Define the exit_short strategy parameters to be used by Hyperopt. + """ + def populate_exit_short_trend(dataframe: DataFrame, metadata: dict) -> DataFrame: + """ + Exit_short strategy Hyperopt will build and use. + """ + conditions = [] + + # GUARDS AND TRENDS + if 'exit-short-mfi-enabled' in params and params['exit-short-mfi-enabled']: + conditions.append(dataframe['mfi'] < params['exit-short-mfi-value']) + if 'exit-short-fastd-enabled' in params and params['exit-short-fastd-enabled']: + conditions.append(dataframe['fastd'] < params['exit-short-fastd-value']) + if 'exit-short-adx-enabled' in params and params['exit-short-adx-enabled']: + conditions.append(dataframe['adx'] > params['exit-short-adx-value']) + if 'exit-short-rsi-enabled' in params and params['exit-short-rsi-enabled']: + conditions.append(dataframe['rsi'] < params['exit-short-rsi-value']) + + # TRIGGERS + if 'exit-short-trigger' in params: + if params['exit-short-trigger'] == 'exit-short-bb_lower': + conditions.append(dataframe['close'] < dataframe['bb_lowerband']) + if params['exit-short-trigger'] == 'exit-short-macd_cross_signal': + conditions.append(qtpylib.crossed_below( + dataframe['macdsignal'], dataframe['macd'] + )) + if params['exit-short-trigger'] == 'exit-short-sar_reversal': + conditions.append(qtpylib.crossed_below( + dataframe['sar'], dataframe['close'] + )) + + if conditions: + dataframe.loc[ + reduce(lambda x, y: x & y, conditions), + 'exit_short'] = 1 + + return dataframe + + return populate_exit_short_trend + @staticmethod def sell_indicator_space() -> List[Dimension]: """ @@ -167,6 +270,25 @@ class DefaultHyperOpt(IHyperOpt): 'sell-sar_reversal'], name='sell-trigger') ] + @staticmethod + def exit_short_indicator_space() -> List[Dimension]: + """ + Define your Hyperopt space for searching exit short strategy parameters. + """ + return [ + Integer(1, 25, name='exit_short-mfi-value'), + Integer(1, 50, name='exit_short-fastd-value'), + Integer(1, 50, name='exit_short-adx-value'), + Integer(1, 40, name='exit_short-rsi-value'), + Categorical([True, False], name='exit_short-mfi-enabled'), + Categorical([True, False], name='exit_short-fastd-enabled'), + Categorical([True, False], name='exit_short-adx-enabled'), + Categorical([True, False], name='exit_short-rsi-enabled'), + Categorical(['exit_short-bb_lower', + 'exit_short-macd_cross_signal', + 'exit_short-sar_reversal'], name='exit_short-trigger') + ] + def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: """ Based on TA indicators. Should be a copy of same method from strategy. @@ -200,3 +322,37 @@ class DefaultHyperOpt(IHyperOpt): 'sell'] = 1 return dataframe + + def populate_short_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: + """ + Based on TA indicators. Should be a copy of same method from strategy. + Must align to populate_indicators in this file. + Only used when --spaces does not include short space. + """ + dataframe.loc[ + ( + (dataframe['close'] > dataframe['bb_upperband']) & + (dataframe['mfi'] < 84) & + (dataframe['adx'] > 75) & + (dataframe['rsi'] < 79) + ), + 'buy'] = 1 + + return dataframe + + def populate_exit_short_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: + """ + Based on TA indicators. Should be a copy of same method from strategy. + Must align to populate_indicators in this file. + Only used when --spaces does not include exit_short space. + """ + dataframe.loc[ + ( + (qtpylib.crossed_below( + dataframe['macdsignal'], dataframe['macd'] + )) & + (dataframe['fastd'] < 46) + ), + 'sell'] = 1 + + return dataframe diff --git a/tests/optimize/test_backtest_detail.py b/tests/optimize/test_backtest_detail.py index e5c037f3e..0205369ba 100644 --- a/tests/optimize/test_backtest_detail.py +++ b/tests/optimize/test_backtest_detail.py @@ -597,8 +597,8 @@ def test_backtest_results(default_conf, fee, mocker, caplog, data) -> None: backtesting = Backtesting(default_conf) backtesting._set_strategy(backtesting.strategylist[0]) backtesting.required_startup = 0 - backtesting.strategy.advise_buy = lambda a, m: frame - backtesting.strategy.advise_sell = lambda a, m: frame + backtesting.strategy.advise_enter = lambda a, m: frame + backtesting.strategy.advise_exit = lambda a, m: frame backtesting.strategy.use_custom_stoploss = data.use_custom_stoploss caplog.set_level(logging.DEBUG) diff --git a/tests/optimize/test_backtesting.py b/tests/optimize/test_backtesting.py index deaaf9f2f..afbfcb1c2 100644 --- a/tests/optimize/test_backtesting.py +++ b/tests/optimize/test_backtesting.py @@ -290,8 +290,8 @@ def test_backtesting_init(mocker, default_conf, order_types) -> None: assert backtesting.config == default_conf assert backtesting.timeframe == '5m' assert callable(backtesting.strategy.ohlcvdata_to_dataframe) - assert callable(backtesting.strategy.advise_buy) - assert callable(backtesting.strategy.advise_sell) + assert callable(backtesting.strategy.advise_enter) + assert callable(backtesting.strategy.advise_exit) assert isinstance(backtesting.strategy.dp, DataProvider) get_fee.assert_called() assert backtesting.fee == 0.5 @@ -700,8 +700,8 @@ def test_backtest_clash_buy_sell(mocker, default_conf, testdatadir): backtest_conf = _make_backtest_conf(mocker, conf=default_conf, datadir=testdatadir) backtesting = Backtesting(default_conf) backtesting._set_strategy(backtesting.strategylist[0]) - backtesting.strategy.advise_buy = fun # Override - backtesting.strategy.advise_sell = fun # Override + backtesting.strategy.advise_enter = fun # Override + backtesting.strategy.advise_exit = fun # Override result = backtesting.backtest(**backtest_conf) assert result['results'].empty @@ -716,8 +716,8 @@ def test_backtest_only_sell(mocker, default_conf, testdatadir): backtest_conf = _make_backtest_conf(mocker, conf=default_conf, datadir=testdatadir) backtesting = Backtesting(default_conf) backtesting._set_strategy(backtesting.strategylist[0]) - backtesting.strategy.advise_buy = fun # Override - backtesting.strategy.advise_sell = fun # Override + backtesting.strategy.advise_enter = fun # Override + backtesting.strategy.advise_exit = fun # Override result = backtesting.backtest(**backtest_conf) assert result['results'].empty @@ -731,8 +731,8 @@ def test_backtest_alternate_buy_sell(default_conf, fee, mocker, testdatadir): backtesting = Backtesting(default_conf) backtesting.required_startup = 0 backtesting._set_strategy(backtesting.strategylist[0]) - backtesting.strategy.advise_buy = _trend_alternate # Override - backtesting.strategy.advise_sell = _trend_alternate # Override + backtesting.strategy.advise_enter = _trend_alternate # Override + backtesting.strategy.advise_exit = _trend_alternate # Override result = backtesting.backtest(**backtest_conf) # 200 candles in backtest data # won't buy on first (shifted by 1) @@ -777,8 +777,8 @@ def test_backtest_multi_pair(default_conf, fee, mocker, tres, pair, testdatadir) backtesting = Backtesting(default_conf) backtesting._set_strategy(backtesting.strategylist[0]) - backtesting.strategy.advise_buy = _trend_alternate_hold # Override - backtesting.strategy.advise_sell = _trend_alternate_hold # Override + backtesting.strategy.advise_enter = _trend_alternate_hold # Override + backtesting.strategy.advise_exit = _trend_alternate_hold # Override processed = backtesting.strategy.ohlcvdata_to_dataframe(data) min_date, max_date = get_timerange(processed) diff --git a/tests/optimize/test_hyperopt.py b/tests/optimize/test_hyperopt.py index d146e84f1..855a752ac 100644 --- a/tests/optimize/test_hyperopt.py +++ b/tests/optimize/test_hyperopt.py @@ -25,6 +25,9 @@ from tests.conftest import (get_args, log_has, log_has_re, patch_exchange, from .hyperopts.default_hyperopt import DefaultHyperOpt +# TODO-lev: This file + + def test_setup_hyperopt_configuration_without_arguments(mocker, default_conf, caplog) -> None: patched_configuration_load_config_file(mocker, default_conf) @@ -363,8 +366,8 @@ def test_start_calls_optimizer(mocker, hyperopt_conf, capsys) -> None: # Should be called for historical candle data assert dumper.call_count == 1 assert dumper2.call_count == 1 - assert hasattr(hyperopt.backtesting.strategy, "advise_sell") - assert hasattr(hyperopt.backtesting.strategy, "advise_buy") + assert hasattr(hyperopt.backtesting.strategy, "advise_exit") + assert hasattr(hyperopt.backtesting.strategy, "advise_enter") assert hasattr(hyperopt, "max_open_trades") assert hyperopt.max_open_trades == hyperopt_conf['max_open_trades'] assert hasattr(hyperopt, "position_stacking") @@ -822,8 +825,8 @@ def test_simplified_interface_roi_stoploss(mocker, hyperopt_conf, capsys) -> Non assert dumper.call_count == 1 assert dumper2.call_count == 1 - assert hasattr(hyperopt.backtesting.strategy, "advise_sell") - assert hasattr(hyperopt.backtesting.strategy, "advise_buy") + assert hasattr(hyperopt.backtesting.strategy, "advise_exit") + assert hasattr(hyperopt.backtesting.strategy, "advise_enter") assert hasattr(hyperopt, "max_open_trades") assert hyperopt.max_open_trades == hyperopt_conf['max_open_trades'] assert hasattr(hyperopt, "position_stacking") @@ -903,8 +906,8 @@ def test_simplified_interface_buy(mocker, hyperopt_conf, capsys) -> None: assert dumper.called assert dumper.call_count == 1 assert dumper2.call_count == 1 - assert hasattr(hyperopt.backtesting.strategy, "advise_sell") - assert hasattr(hyperopt.backtesting.strategy, "advise_buy") + assert hasattr(hyperopt.backtesting.strategy, "advise_exit") + assert hasattr(hyperopt.backtesting.strategy, "advise_enter") assert hasattr(hyperopt, "max_open_trades") assert hyperopt.max_open_trades == hyperopt_conf['max_open_trades'] assert hasattr(hyperopt, "position_stacking") @@ -957,8 +960,8 @@ def test_simplified_interface_sell(mocker, hyperopt_conf, capsys) -> None: assert dumper.called assert dumper.call_count == 1 assert dumper2.call_count == 1 - assert hasattr(hyperopt.backtesting.strategy, "advise_sell") - assert hasattr(hyperopt.backtesting.strategy, "advise_buy") + assert hasattr(hyperopt.backtesting.strategy, "advise_exit") + assert hasattr(hyperopt.backtesting.strategy, "advise_enter") assert hasattr(hyperopt, "max_open_trades") assert hyperopt.max_open_trades == hyperopt_conf['max_open_trades'] assert hasattr(hyperopt, "position_stacking") diff --git a/tests/rpc/test_rpc_apiserver.py b/tests/rpc/test_rpc_apiserver.py index 1517b6fcc..439a99e2f 100644 --- a/tests/rpc/test_rpc_apiserver.py +++ b/tests/rpc/test_rpc_apiserver.py @@ -264,7 +264,7 @@ def test_api_UvicornServer(mocker): assert thread_mock.call_count == 1 s.cleanup() - assert s.should_exit is True + assert s.should_sell is True def test_api_UvicornServer_run(mocker): diff --git a/tests/strategy/strats/default_strategy.py b/tests/strategy/strats/default_strategy.py index 7171b93ae..3e5695a99 100644 --- a/tests/strategy/strats/default_strategy.py +++ b/tests/strategy/strats/default_strategy.py @@ -154,3 +154,48 @@ class DefaultStrategy(IStrategy): ), 'sell'] = 1 return dataframe + + def populate_short_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: + """ + Based on TA indicators, populates the short signal for the given dataframe + :param dataframe: DataFrame + :param metadata: Additional information, like the currently traded pair + :return: DataFrame with short column + """ + dataframe.loc[ + ( + (dataframe['rsi'] > 65) & + (dataframe['fastd'] > 65) & + (dataframe['adx'] < 70) & + (dataframe['plus_di'] < 0.5) # TODO-lev: What to do here + ) | + ( + (dataframe['adx'] < 35) & + (dataframe['plus_di'] < 0.5) # TODO-lev: What to do here + ), + 'short'] = 1 + + return dataframe + + def populate_exit_short_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: + """ + Based on TA indicators, populates the exit_short signal for the given dataframe + :param dataframe: DataFrame + :param metadata: Additional information, like the currently traded pair + :return: DataFrame with exit_short column + """ + dataframe.loc[ + ( + ( + (qtpylib.crossed_below(dataframe['rsi'], 30)) | + (qtpylib.crossed_below(dataframe['fastd'], 30)) + ) & + (dataframe['adx'] < 90) & + (dataframe['minus_di'] < 0) # TODO-lev: what to do here + ) | + ( + (dataframe['adx'] > 30) & + (dataframe['minus_di'] < 0.5) # TODO-lev: what to do here + ), + 'exit_short'] = 1 + return dataframe diff --git a/tests/strategy/strats/hyperoptable_strategy.py b/tests/strategy/strats/hyperoptable_strategy.py index 88bdd078e..8d428b33d 100644 --- a/tests/strategy/strats/hyperoptable_strategy.py +++ b/tests/strategy/strats/hyperoptable_strategy.py @@ -60,6 +60,15 @@ class HyperoptableStrategy(IStrategy): 'sell_minusdi': 0.4 } + short_params = { + 'short_rsi': 65, + } + + exit_short_params = { + 'exit_short_rsi': 26, + 'exit_short_minusdi': 0.6 + } + buy_rsi = IntParameter([0, 50], default=30, space='buy') buy_plusdi = RealParameter(low=0, high=1, default=0.5, space='buy') sell_rsi = IntParameter(low=50, high=100, default=70, space='sell') @@ -78,6 +87,12 @@ class HyperoptableStrategy(IStrategy): }) return prot + short_rsi = IntParameter([50, 100], default=70, space='sell') + short_plusdi = RealParameter(low=0, high=1, default=0.5, space='sell') + exit_short_rsi = IntParameter(low=0, high=50, default=30, space='buy') + exit_short_minusdi = DecimalParameter(low=0, high=1, default=0.4999, decimals=3, space='buy', + load=False) + def informative_pairs(self): """ Define additional, informative pair/interval combinations to be cached from the exchange. @@ -167,7 +182,7 @@ class HyperoptableStrategy(IStrategy): Based on TA indicators, populates the sell signal for the given dataframe :param dataframe: DataFrame :param metadata: Additional information, like the currently traded pair - :return: DataFrame with buy column + :return: DataFrame with sell column """ dataframe.loc[ ( @@ -184,3 +199,48 @@ class HyperoptableStrategy(IStrategy): ), 'sell'] = 1 return dataframe + + def populate_short_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: + """ + Based on TA indicators, populates the short signal for the given dataframe + :param dataframe: DataFrame + :param metadata: Additional information, like the currently traded pair + :return: DataFrame with short column + """ + dataframe.loc[ + ( + (dataframe['rsi'] > self.short_rsi.value) & + (dataframe['fastd'] > 65) & + (dataframe['adx'] < 70) & + (dataframe['plus_di'] < self.short_plusdi.value) + ) | + ( + (dataframe['adx'] < 35) & + (dataframe['plus_di'] < self.short_plusdi.value) + ), + 'short'] = 1 + + return dataframe + + def populate_exit_short_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: + """ + Based on TA indicators, populates the exit_short signal for the given dataframe + :param dataframe: DataFrame + :param metadata: Additional information, like the currently traded pair + :return: DataFrame with exit_short column + """ + dataframe.loc[ + ( + ( + (qtpylib.crossed_below(dataframe['rsi'], self.exit_short_rsi.value)) | + (qtpylib.crossed_below(dataframe['fastd'], 30)) + ) & + (dataframe['adx'] < 90) & + (dataframe['minus_di'] < 0) # TODO-lev: What should this be + ) | + ( + (dataframe['adx'] < 30) & + (dataframe['minus_di'] < self.exit_short_minusdi.value) + ), + 'exit_short'] = 1 + return dataframe diff --git a/tests/strategy/strats/legacy_strategy.py b/tests/strategy/strats/legacy_strategy.py index 9ef00b110..a5531b42f 100644 --- a/tests/strategy/strats/legacy_strategy.py +++ b/tests/strategy/strats/legacy_strategy.py @@ -85,3 +85,34 @@ class TestStrategyLegacy(IStrategy): ), 'sell'] = 1 return dataframe + + def populate_short_trend(self, dataframe: DataFrame) -> DataFrame: + """ + Based on TA indicators, populates the buy signal for the given dataframe + :param dataframe: DataFrame + :return: DataFrame with buy column + """ + dataframe.loc[ + ( + (dataframe['adx'] > 30) & + (dataframe['tema'] > dataframe['tema'].shift(1)) & + (dataframe['volume'] > 0) + ), + 'buy'] = 1 + + return dataframe + + def populate_exit_short_trend(self, dataframe: DataFrame) -> DataFrame: + """ + Based on TA indicators, populates the sell signal for the given dataframe + :param dataframe: DataFrame + :return: DataFrame with buy column + """ + dataframe.loc[ + ( + (dataframe['adx'] > 70) & + (dataframe['tema'] < dataframe['tema'].shift(1)) & + (dataframe['volume'] > 0) + ), + 'sell'] = 1 + return dataframe diff --git a/tests/strategy/test_default_strategy.py b/tests/strategy/test_default_strategy.py index 92ac9f63a..420cf8f46 100644 --- a/tests/strategy/test_default_strategy.py +++ b/tests/strategy/test_default_strategy.py @@ -14,6 +14,8 @@ def test_default_strategy_structure(): assert hasattr(DefaultStrategy, 'populate_indicators') assert hasattr(DefaultStrategy, 'populate_buy_trend') assert hasattr(DefaultStrategy, 'populate_sell_trend') + assert hasattr(DefaultStrategy, 'populate_short_trend') + assert hasattr(DefaultStrategy, 'populate_exit_short_trend') def test_default_strategy(result, fee): @@ -27,6 +29,10 @@ def test_default_strategy(result, fee): assert type(indicators) is DataFrame assert type(strategy.populate_buy_trend(indicators, metadata)) is DataFrame assert type(strategy.populate_sell_trend(indicators, metadata)) is DataFrame + # TODO-lev: I think these two should be commented out in the strategy by default + # TODO-lev: so they can be tested, but the tests can't really remain + assert type(strategy.populate_short_trend(indicators, metadata)) is DataFrame + assert type(strategy.populate_exit_short_trend(indicators, metadata)) is DataFrame trade = Trade( open_rate=19_000, @@ -37,10 +43,28 @@ def test_default_strategy(result, fee): assert strategy.confirm_trade_entry(pair='ETH/BTC', order_type='limit', amount=0.1, rate=20000, time_in_force='gtc', - current_time=datetime.utcnow()) is True + is_short=False, current_time=datetime.utcnow()) is True + 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', - current_time=datetime.utcnow()) is True + is_short=False, current_time=datetime.utcnow()) is True + # TODO-lev: Test for shorts? assert strategy.custom_stoploss(pair='ETH/BTC', trade=trade, current_time=datetime.now(), current_rate=20_000, current_profit=0.05) == strategy.stoploss + + short_trade = Trade( + open_rate=21_000, + amount=0.1, + pair='ETH/BTC', + fee_open=fee.return_value + ) + + assert strategy.confirm_trade_entry(pair='ETH/BTC', order_type='limit', amount=0.1, + rate=20000, time_in_force='gtc', + is_short=True, current_time=datetime.utcnow()) is True + + assert strategy.confirm_trade_exit(pair='ETH/BTC', trade=short_trade, order_type='limit', + amount=0.1, rate=20000, time_in_force='gtc', + sell_reason='roi', is_short=True, + current_time=datetime.utcnow()) is True diff --git a/tests/strategy/test_interface.py b/tests/strategy/test_interface.py index 0ad6d6f32..1e47575dc 100644 --- a/tests/strategy/test_interface.py +++ b/tests/strategy/test_interface.py @@ -156,17 +156,21 @@ def test_ignore_expired_candle(default_conf): # Add 1 candle length as the "latest date" defines candle open. current_time = latest_date + timedelta(seconds=80 + 300) - assert strategy.ignore_expired_candle(latest_date=latest_date, - current_time=current_time, - timeframe_seconds=300, - buy=True) is True + assert strategy.ignore_expired_candle( + latest_date=latest_date, + current_time=current_time, + timeframe_seconds=300, + enter=True + ) is True current_time = latest_date + timedelta(seconds=30 + 300) - assert not strategy.ignore_expired_candle(latest_date=latest_date, - current_time=current_time, - timeframe_seconds=300, - buy=True) is True + assert not strategy.ignore_expired_candle( + latest_date=latest_date, + current_time=current_time, + timeframe_seconds=300, + enter=True + ) is True def test_assert_df_raise(mocker, caplog, ohlcv_history): @@ -478,20 +482,20 @@ def test_custom_sell(default_conf, fee, caplog) -> None: def test_analyze_ticker_default(ohlcv_history, mocker, caplog) -> None: caplog.set_level(logging.DEBUG) ind_mock = MagicMock(side_effect=lambda x, meta: x) - buy_mock = MagicMock(side_effect=lambda x, meta: x) - sell_mock = MagicMock(side_effect=lambda x, meta: x) + enter_mock = MagicMock(side_effect=lambda x, meta, is_short: x) + exit_mock = MagicMock(side_effect=lambda x, meta, is_short: x) mocker.patch.multiple( 'freqtrade.strategy.interface.IStrategy', advise_indicators=ind_mock, - advise_buy=buy_mock, - advise_sell=sell_mock, + advise_enter=enter_mock, + advise_exit=exit_mock, ) strategy = DefaultStrategy({}) strategy.analyze_ticker(ohlcv_history, {'pair': 'ETH/BTC'}) assert ind_mock.call_count == 1 - assert buy_mock.call_count == 1 - assert buy_mock.call_count == 1 + assert enter_mock.call_count == 2 + assert enter_mock.call_count == 2 assert log_has('TA Analysis Launched', caplog) assert not log_has('Skipping TA Analysis for already analyzed candle', caplog) @@ -500,8 +504,8 @@ def test_analyze_ticker_default(ohlcv_history, mocker, caplog) -> None: strategy.analyze_ticker(ohlcv_history, {'pair': 'ETH/BTC'}) # No analysis happens as process_only_new_candles is true assert ind_mock.call_count == 2 - assert buy_mock.call_count == 2 - assert buy_mock.call_count == 2 + assert enter_mock.call_count == 4 + assert enter_mock.call_count == 4 assert log_has('TA Analysis Launched', caplog) assert not log_has('Skipping TA Analysis for already analyzed candle', caplog) @@ -509,13 +513,13 @@ def test_analyze_ticker_default(ohlcv_history, mocker, caplog) -> None: def test__analyze_ticker_internal_skip_analyze(ohlcv_history, mocker, caplog) -> None: caplog.set_level(logging.DEBUG) ind_mock = MagicMock(side_effect=lambda x, meta: x) - buy_mock = MagicMock(side_effect=lambda x, meta: x) - sell_mock = MagicMock(side_effect=lambda x, meta: x) + enter_mock = MagicMock(side_effect=lambda x, meta, is_short: x) + exit_mock = MagicMock(side_effect=lambda x, meta, is_short: x) mocker.patch.multiple( 'freqtrade.strategy.interface.IStrategy', advise_indicators=ind_mock, - advise_buy=buy_mock, - advise_sell=sell_mock, + advise_enter=enter_mock, + advise_exit=exit_mock, ) strategy = DefaultStrategy({}) @@ -528,8 +532,8 @@ def test__analyze_ticker_internal_skip_analyze(ohlcv_history, mocker, caplog) -> assert 'close' in ret.columns assert isinstance(ret, DataFrame) assert ind_mock.call_count == 1 - assert buy_mock.call_count == 1 - assert buy_mock.call_count == 1 + assert enter_mock.call_count == 2 # Once for buy, once for short + assert enter_mock.call_count == 2 assert log_has('TA Analysis Launched', caplog) assert not log_has('Skipping TA Analysis for already analyzed candle', caplog) caplog.clear() @@ -537,8 +541,8 @@ def test__analyze_ticker_internal_skip_analyze(ohlcv_history, mocker, caplog) -> ret = strategy._analyze_ticker_internal(ohlcv_history, {'pair': 'ETH/BTC'}) # No analysis happens as process_only_new_candles is true assert ind_mock.call_count == 1 - assert buy_mock.call_count == 1 - assert buy_mock.call_count == 1 + assert enter_mock.call_count == 2 + assert enter_mock.call_count == 2 # only skipped analyze adds buy and sell columns, otherwise it's all mocked assert 'buy' in ret.columns assert 'sell' in ret.columns @@ -743,10 +747,10 @@ def test_auto_hyperopt_interface(default_conf): assert strategy.sell_minusdi.value == 0.5 all_params = strategy.detect_all_parameters() assert isinstance(all_params, dict) - assert len(all_params['buy']) == 2 - assert len(all_params['sell']) == 2 - # Number of Hyperoptable parameters - assert all_params['count'] == 6 + # TODO-lev: Should these be 4,4 and 10? + assert len(all_params['buy']) == 4 + assert len(all_params['sell']) == 4 + assert all_params['count'] == 10 strategy.__class__.sell_rsi = IntParameter([0, 10], default=5, space='buy') diff --git a/tests/strategy/test_strategy_loading.py b/tests/strategy/test_strategy_loading.py index 115a2fbde..2cf77b172 100644 --- a/tests/strategy/test_strategy_loading.py +++ b/tests/strategy/test_strategy_loading.py @@ -117,12 +117,18 @@ def test_strategy(result, default_conf): df_indicators = strategy.advise_indicators(result, metadata=metadata) assert 'adx' in df_indicators - dataframe = strategy.advise_buy(df_indicators, metadata=metadata) + dataframe = strategy.advise_enter(df_indicators, metadata=metadata, is_short=False) assert 'buy' in dataframe.columns - dataframe = strategy.advise_sell(df_indicators, metadata=metadata) + dataframe = strategy.advise_exit(df_indicators, metadata=metadata, is_short=False) assert 'sell' in dataframe.columns + dataframe = strategy.advise_enter(df_indicators, metadata=metadata, is_short=True) + assert 'short' in dataframe.columns + + dataframe = strategy.advise_exit(df_indicators, metadata=metadata, is_short=True) + assert 'exit_short' in dataframe.columns + def test_strategy_override_minimal_roi(caplog, default_conf): caplog.set_level(logging.INFO) @@ -218,6 +224,7 @@ def test_strategy_override_process_only_new_candles(caplog, default_conf): def test_strategy_override_order_types(caplog, default_conf): caplog.set_level(logging.INFO) + # TODO-lev: Maybe change order_types = { 'buy': 'market', 'sell': 'limit', @@ -345,7 +352,7 @@ def test_deprecate_populate_indicators(result, default_conf): with warnings.catch_warnings(record=True) as w: # Cause all warnings to always be triggered. warnings.simplefilter("always") - strategy.advise_buy(indicators, {'pair': 'ETH/BTC'}) + strategy.advise_enter(indicators, {'pair': 'ETH/BTC'}, is_short=False) # TODO-lev assert len(w) == 1 assert issubclass(w[-1].category, DeprecationWarning) assert "deprecated - check out the Sample strategy to see the current function headers!" \ @@ -354,7 +361,7 @@ def test_deprecate_populate_indicators(result, default_conf): with warnings.catch_warnings(record=True) as w: # Cause all warnings to always be triggered. warnings.simplefilter("always") - strategy.advise_sell(indicators, {'pair': 'ETH_BTC'}) + strategy.advise_exit(indicators, {'pair': 'ETH_BTC'}, is_short=False) # TODO-lev assert len(w) == 1 assert issubclass(w[-1].category, DeprecationWarning) assert "deprecated - check out the Sample strategy to see the current function headers!" \ @@ -374,6 +381,8 @@ def test_call_deprecated_function(result, monkeypatch, default_conf, caplog): assert strategy._populate_fun_len == 2 assert strategy._buy_fun_len == 2 assert strategy._sell_fun_len == 2 + # assert strategy._short_fun_len == 2 + # assert strategy._exit_short_fun_len == 2 assert strategy.INTERFACE_VERSION == 1 assert strategy.timeframe == '5m' assert strategy.ticker_interval == '5m' @@ -382,14 +391,22 @@ def test_call_deprecated_function(result, monkeypatch, default_conf, caplog): assert isinstance(indicator_df, DataFrame) assert 'adx' in indicator_df.columns - buydf = strategy.advise_buy(result, metadata=metadata) + buydf = strategy.advise_enter(result, metadata=metadata, is_short=False) assert isinstance(buydf, DataFrame) assert 'buy' in buydf.columns - selldf = strategy.advise_sell(result, metadata=metadata) + selldf = strategy.advise_exit(result, metadata=metadata, is_short=False) assert isinstance(selldf, DataFrame) assert 'sell' in selldf + # shortdf = strategy.advise_enter(result, metadata=metadata, is_short=True) + # assert isinstance(shortdf, DataFrame) + # assert 'short' in shortdf.columns + + # exit_shortdf = strategy.advise_exit(result, metadata=metadata, is_short=True) + # assert isinstance(exit_shortdf, DataFrame) + # assert 'exit_short' in exit_shortdf + assert log_has("DEPRECATED: Please migrate to using 'timeframe' instead of 'ticker_interval'.", caplog) @@ -403,16 +420,26 @@ def test_strategy_interface_versioning(result, monkeypatch, default_conf): assert strategy._populate_fun_len == 3 assert strategy._buy_fun_len == 3 assert strategy._sell_fun_len == 3 + assert strategy._short_fun_len == 3 + assert strategy._exit_short_fun_len == 3 assert strategy.INTERFACE_VERSION == 2 indicator_df = strategy.advise_indicators(result, metadata=metadata) assert isinstance(indicator_df, DataFrame) assert 'adx' in indicator_df.columns - buydf = strategy.advise_buy(result, metadata=metadata) + buydf = strategy.advise_enter(result, metadata=metadata, is_short=False) assert isinstance(buydf, DataFrame) assert 'buy' in buydf.columns - selldf = strategy.advise_sell(result, metadata=metadata) + selldf = strategy.advise_exit(result, metadata=metadata, is_short=False) assert isinstance(selldf, DataFrame) assert 'sell' in selldf + + shortdf = strategy.advise_enter(result, metadata=metadata, is_short=True) + assert isinstance(shortdf, DataFrame) + assert 'short' in shortdf.columns + + exit_shortdf = strategy.advise_exit(result, metadata=metadata, is_short=True) + assert isinstance(exit_shortdf, DataFrame) + assert 'exit_short' in exit_shortdf From 092780df9d48de631bc09ea9d1b093c7f3e21ed0 Mon Sep 17 00:00:00 2001 From: Sam Germain Date: Wed, 18 Aug 2021 04:19:17 -0600 Subject: [PATCH 02/31] condensed strategy methods down to 2 --- freqtrade/edge/edge_positioning.py | 10 +- freqtrade/enums/signaltype.py | 2 +- freqtrade/optimize/backtesting.py | 9 +- freqtrade/optimize/hyperopt.py | 13 +- freqtrade/resolvers/strategy_resolver.py | 13 +- freqtrade/rpc/api_server/uvicorn_threaded.py | 2 +- freqtrade/strategy/interface.py | 87 +++--- freqtrade/strategy/strategy_helper.py | 9 +- freqtrade/templates/sample_hyperopt.py | 237 ++++++---------- .../templates/sample_hyperopt_advanced.py | 233 ++++++--------- freqtrade/templates/sample_strategy.py | 41 +-- tests/optimize/hyperopts/default_hyperopt.py | 267 ++++++------------ tests/optimize/test_backtest_detail.py | 4 +- tests/optimize/test_backtesting.py | 20 +- tests/optimize/test_hyperopt.py | 40 ++- tests/rpc/test_rpc_apiserver.py | 2 +- tests/strategy/strats/default_strategy.py | 44 +-- .../strategy/strats/hyperoptable_strategy.py | 50 ++-- tests/strategy/strats/legacy_strategy.py | 30 -- tests/strategy/test_default_strategy.py | 27 +- tests/strategy/test_interface.py | 32 +-- tests/strategy/test_strategy_loading.py | 52 +--- 22 files changed, 451 insertions(+), 773 deletions(-) diff --git a/freqtrade/edge/edge_positioning.py b/freqtrade/edge/edge_positioning.py index b366059da..9c1dd4d24 100644 --- a/freqtrade/edge/edge_positioning.py +++ b/freqtrade/edge/edge_positioning.py @@ -167,14 +167,12 @@ class Edge: pair_data = pair_data.sort_values(by=['date']) pair_data = pair_data.reset_index(drop=True) - df_analyzed = self.strategy.advise_exit( - dataframe=self.strategy.advise_enter( + df_analyzed = self.strategy.advise_sell( + dataframe=self.strategy.advise_buy( dataframe=pair_data, - metadata={'pair': pair}, - is_short=False + metadata={'pair': pair} ), - metadata={'pair': pair}, - is_short=False + metadata={'pair': pair} )[headers].copy() trades += self._find_trades_for_stoploss_range(df_analyzed, pair, self._stoploss_range) diff --git a/freqtrade/enums/signaltype.py b/freqtrade/enums/signaltype.py index ffba5ee90..fcebd9f0e 100644 --- a/freqtrade/enums/signaltype.py +++ b/freqtrade/enums/signaltype.py @@ -16,4 +16,4 @@ class SignalTagType(Enum): Enum for signal columns """ BUY_TAG = "buy_tag" - SELL_TAG = "sell_tag" + SHORT_TAG = "short_tag" diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index 550ceecd8..cce3b6a0d 100644 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -231,8 +231,13 @@ class Backtesting: if has_buy_tag: pair_data.loc[:, 'buy_tag'] = None # cleanup if buy_tag is exist - df_analyzed = self.strategy.advise_exit( - self.strategy.advise_enter(pair_data, {'pair': pair}), {'pair': pair}).copy() + df_analyzed = self.strategy.advise_sell( + self.strategy.advise_buy( + pair_data, + {'pair': pair} + ), + {'pair': pair} + ).copy() # Trim startup period from analyzed dataframe df_analyzed = trim_dataframe(df_analyzed, self.timerange, startup_candles=self.required_startup) diff --git a/freqtrade/optimize/hyperopt.py b/freqtrade/optimize/hyperopt.py index 4c07419b8..5c627df35 100644 --- a/freqtrade/optimize/hyperopt.py +++ b/freqtrade/optimize/hyperopt.py @@ -110,7 +110,7 @@ class Hyperopt: self.backtesting.strategy.advise_indicators = ( # type: ignore self.custom_hyperopt.populate_indicators) # type: ignore if hasattr(self.custom_hyperopt, 'populate_buy_trend'): - self.backtesting.strategy.advise_enter = ( # type: ignore + self.backtesting.strategy.advise_buy = ( # type: ignore self.custom_hyperopt.populate_buy_trend) # type: ignore if hasattr(self.custom_hyperopt, 'populate_sell_trend'): self.backtesting.strategy.advise_sell = ( # type: ignore @@ -283,14 +283,15 @@ class Hyperopt: params_dict = self._get_params_dict(self.dimensions, raw_params) # Apply parameters - # TODO-lev: These don't take a side, how can I pass is_short=True/False to it if HyperoptTools.has_space(self.config, 'buy'): - self.backtesting.strategy.advise_enter = ( # type: ignore - self.custom_hyperopt.buy_strategy_generator(params_dict)) + self.backtesting.strategy.advise_buy = ( # type: ignore + self.custom_hyperopt.buy_strategy_generator(params_dict) + ) if HyperoptTools.has_space(self.config, 'sell'): - self.backtesting.strategy.advise_exit = ( # type: ignore - self.custom_hyperopt.sell_strategy_generator(params_dict)) + self.backtesting.strategy.advise_sell = ( # type: ignore + self.custom_hyperopt.sell_strategy_generator(params_dict) + ) if HyperoptTools.has_space(self.config, 'protection'): for attr_name, attr in self.backtesting.strategy.enumerate_parameters('protection'): diff --git a/freqtrade/resolvers/strategy_resolver.py b/freqtrade/resolvers/strategy_resolver.py index 38a5b4850..afb5916f1 100644 --- a/freqtrade/resolvers/strategy_resolver.py +++ b/freqtrade/resolvers/strategy_resolver.py @@ -202,14 +202,11 @@ class StrategyResolver(IResolver): strategy._populate_fun_len = len(getfullargspec(strategy.populate_indicators).args) strategy._buy_fun_len = len(getfullargspec(strategy.populate_buy_trend).args) strategy._sell_fun_len = len(getfullargspec(strategy.populate_sell_trend).args) - strategy._short_fun_len = len(getfullargspec(strategy.populate_short_trend).args) - strategy._exit_short_fun_len = len( - getfullargspec(strategy.populate_exit_short_trend).args) - if any(x == 2 for x in [strategy._populate_fun_len, - strategy._buy_fun_len, - strategy._sell_fun_len, - strategy._short_fun_len, - strategy._exit_short_fun_len]): + if any(x == 2 for x in [ + strategy._populate_fun_len, + strategy._buy_fun_len, + strategy._sell_fun_len + ]): strategy.INTERFACE_VERSION = 1 return strategy diff --git a/freqtrade/rpc/api_server/uvicorn_threaded.py b/freqtrade/rpc/api_server/uvicorn_threaded.py index 7d76d52ed..2f72cb74c 100644 --- a/freqtrade/rpc/api_server/uvicorn_threaded.py +++ b/freqtrade/rpc/api_server/uvicorn_threaded.py @@ -44,5 +44,5 @@ class UvicornServer(uvicorn.Server): time.sleep(1e-3) def cleanup(self): - self.should_sell = True + self.should_exit = True self.thread.join() diff --git a/freqtrade/strategy/interface.py b/freqtrade/strategy/interface.py index 26ad2fcd4..b56a54d14 100644 --- a/freqtrade/strategy/interface.py +++ b/freqtrade/strategy/interface.py @@ -62,8 +62,6 @@ class IStrategy(ABC, HyperStrategyMixin): _populate_fun_len: int = 0 _buy_fun_len: int = 0 _sell_fun_len: int = 0 - _short_fun_len: int = 0 - _exit_short_fun_len: int = 0 _ft_params_from_file: Dict = {} # associated minimal roi minimal_roi: Dict @@ -145,7 +143,7 @@ class IStrategy(ABC, HyperStrategyMixin): return dataframe @abstractmethod - def populate_enter_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: + def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: """ Based on TA indicators, populates the buy signal for the given dataframe :param dataframe: DataFrame @@ -155,7 +153,7 @@ class IStrategy(ABC, HyperStrategyMixin): return dataframe @abstractmethod - def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: + def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: """ Based on TA indicators, populates the sell signal for the given dataframe :param dataframe: DataFrame @@ -166,7 +164,7 @@ class IStrategy(ABC, HyperStrategyMixin): def check_buy_timeout(self, pair: str, trade: Trade, order: dict, **kwargs) -> bool: """ - Check enter timeout function callback. + Check buy timeout function callback. This method can be used to override the enter-timeout. It is called whenever a limit buy/short order has been created, and is not yet fully filled. @@ -184,7 +182,7 @@ class IStrategy(ABC, HyperStrategyMixin): def check_sell_timeout(self, pair: str, trade: Trade, order: dict, **kwargs) -> bool: """ - Check exit timeout function callback. + Check sell timeout function callback. This method can be used to override the exit-timeout. It is called whenever a (long) limit sell order or (short) limit buy has been created, and is not yet fully filled. @@ -396,10 +394,8 @@ class IStrategy(ABC, HyperStrategyMixin): """ logger.debug("TA Analysis Launched") dataframe = self.advise_indicators(dataframe, metadata) - dataframe = self.advise_enter(dataframe, metadata, is_short=False) - dataframe = self.advise_exit(dataframe, metadata, is_short=False) - dataframe = self.advise_enter(dataframe, metadata, is_short=True) - dataframe = self.advise_exit(dataframe, metadata, is_short=True) + dataframe = self.advise_buy(dataframe, metadata) + dataframe = self.advise_sell(dataframe, metadata) return dataframe def _analyze_ticker_internal(self, dataframe: DataFrame, metadata: dict) -> DataFrame: @@ -426,7 +422,7 @@ class IStrategy(ABC, HyperStrategyMixin): logger.debug("Skipping TA Analysis for already analyzed candle") dataframe['buy'] = 0 dataframe['sell'] = 0 - dataframe['short'] = 0 + dataframe['enter_short'] = 0 dataframe['exit_short'] = 0 dataframe['buy_tag'] = None dataframe['short_tag'] = None @@ -572,8 +568,8 @@ class IStrategy(ABC, HyperStrategyMixin): else: return False - def should_sell(self, trade: Trade, rate: float, date: datetime, enter: bool, - exit: bool, low: float = None, high: float = None, + def should_sell(self, trade: Trade, rate: float, date: datetime, buy: bool, + sell: bool, low: float = None, high: float = None, force_stoploss: float = 0) -> SellCheckTuple: """ This function evaluates if one of the conditions required to trigger a sell/exit_short @@ -597,7 +593,7 @@ class IStrategy(ABC, HyperStrategyMixin): current_profit = trade.calc_profit_ratio(current_rate) # if enter signal and ignore_roi is set, we don't need to evaluate min_roi. - roi_reached = (not (enter and self.ignore_roi_if_buy_signal) + roi_reached = (not (buy and self.ignore_roi_if_buy_signal) and self.min_roi_reached(trade=trade, current_profit=current_profit, current_time=date)) @@ -610,8 +606,8 @@ class IStrategy(ABC, HyperStrategyMixin): if (self.sell_profit_only and current_profit <= self.sell_profit_offset): # sell_profit_only and profit doesn't reach the offset - ignore sell signal pass - elif self.use_sell_signal and not enter: - if exit: + elif self.use_sell_signal and not buy: + if sell: sell_signal = SellType.SELL_SIGNAL else: trade_type = "exit_short" if trade.is_short else "sell" @@ -759,7 +755,7 @@ class IStrategy(ABC, HyperStrategyMixin): def ohlcvdata_to_dataframe(self, data: Dict[str, DataFrame]) -> Dict[str, DataFrame]: """ Populates indicators for given candle (OHLCV) data (for multiple pairs) - Does not run advise_enter or advise_exit! + Does not run advise_buy or advise_sell! Used by optimize operations only, not during dry / live runs. Using .copy() to get a fresh copy of the dataframe for every strategy run. Has positive effects on memory usage for whatever reason - also when @@ -784,12 +780,7 @@ class IStrategy(ABC, HyperStrategyMixin): else: return self.populate_indicators(dataframe, metadata) - def advise_enter( - self, - dataframe: DataFrame, - metadata: dict, - is_short: bool = False - ) -> DataFrame: + def advise_buy(self, dataframe: DataFrame, metadata: dict) -> DataFrame: """ Based on TA indicators, populates the buy/short signal for the given dataframe This method should not be overridden. @@ -798,27 +789,17 @@ class IStrategy(ABC, HyperStrategyMixin): currently traded pair :return: DataFrame with buy column """ - (type, fun_len) = ( - ("short", self._short_fun_len) - if is_short else - ("buy", self._buy_fun_len) - ) - logger.debug(f"Populating {type} signals for pair {metadata.get('pair')}.") + logger.debug(f"Populating enter signals for pair {metadata.get('pair')}.") - if fun_len == 2: + if self._buy_fun_len == 2: warnings.warn("deprecated - check out the Sample strategy to see " "the current function headers!", DeprecationWarning) - return self.populate_enter_trend(dataframe) # type: ignore + return self.populate_buy_trend(dataframe) # type: ignore else: - return self.populate_enter_trend(dataframe, metadata) + return self.populate_buy_trend(dataframe, metadata) - def advise_exit( - self, - dataframe: DataFrame, - metadata: dict, - is_short: bool = False - ) -> DataFrame: + def advise_sell(self, dataframe: DataFrame, metadata: dict) -> DataFrame: """ Based on TA indicators, populates the sell/exit_short signal for the given dataframe This method should not be overridden. @@ -828,16 +809,26 @@ class IStrategy(ABC, HyperStrategyMixin): :return: DataFrame with sell column """ - (type, fun_len) = ( - ("exit_short", self._exit_short_fun_len) - if is_short else - ("sell", self._sell_fun_len) - ) - - logger.debug(f"Populating {type} signals for pair {metadata.get('pair')}.") - if fun_len == 2: + logger.debug(f"Populating exit signals for pair {metadata.get('pair')}.") + if self._sell_fun_len == 2: warnings.warn("deprecated - check out the Sample strategy to see " "the current function headers!", DeprecationWarning) - return self.populate_exit_trend(dataframe) # type: ignore + return self.populate_sell_trend(dataframe) # type: ignore else: - return self.populate_exit_trend(dataframe, metadata) + return self.populate_sell_trend(dataframe, metadata) + + def leverage(self, pair: str, current_time: datetime, current_rate: float, + proposed_leverage: float, max_leverage: float, + **kwargs) -> float: + """ + Customize leverage for each new trade. This method is not called when edge module is + enabled. + + :param pair: Pair that's currently analyzed + :param current_time: datetime object, containing the current datetime + :param current_rate: Rate, calculated based on pricing settings in ask_strategy. + :param proposed_leverage: A leverage proposed by the bot. + :param max_leverage: Max leverage allowed on this pair + :return: A stake size, which is between min_stake and max_stake. + """ + return proposed_leverage diff --git a/freqtrade/strategy/strategy_helper.py b/freqtrade/strategy/strategy_helper.py index e7dbfbac7..9c4d2bf2d 100644 --- a/freqtrade/strategy/strategy_helper.py +++ b/freqtrade/strategy/strategy_helper.py @@ -1,5 +1,6 @@ import pandas as pd +from freqtrade.exceptions import OperationalException from freqtrade.exchange import timeframe_to_minutes @@ -83,7 +84,13 @@ def stoploss_from_open( if current_profit == -1: return 1 - stoploss = 1-((1+open_relative_stop)/(1+current_profit)) # TODO-lev: Is this right? + if for_short is True: + # TODO-lev: How would this be calculated for short + raise OperationalException( + "Freqtrade hasn't figured out how to calculated stoploss on shorts") + # stoploss = 1-((1+open_relative_stop)/(1+current_profit)) + else: + stoploss = 1-((1+open_relative_stop)/(1+current_profit)) # negative stoploss values indicate the requested stop price is higher than the current price if for_short: diff --git a/freqtrade/templates/sample_hyperopt.py b/freqtrade/templates/sample_hyperopt.py index 6707ec8d4..c39558108 100644 --- a/freqtrade/templates/sample_hyperopt.py +++ b/freqtrade/templates/sample_hyperopt.py @@ -46,7 +46,7 @@ class SampleHyperOpt(IHyperOpt): """ @staticmethod - def indicator_space() -> List[Dimension]: + def buy_indicator_space() -> List[Dimension]: """ Define your Hyperopt space for searching buy strategy parameters. """ @@ -55,11 +55,16 @@ class SampleHyperOpt(IHyperOpt): Integer(15, 45, name='fastd-value'), Integer(20, 50, name='adx-value'), Integer(20, 40, name='rsi-value'), + Integer(75, 90, name='short-mfi-value'), + Integer(55, 85, name='short-fastd-value'), + Integer(50, 80, name='short-adx-value'), + Integer(60, 80, name='short-rsi-value'), Categorical([True, False], name='mfi-enabled'), Categorical([True, False], name='fastd-enabled'), Categorical([True, False], name='adx-enabled'), Categorical([True, False], name='rsi-enabled'), - Categorical(['bb_lower', 'macd_cross_signal', 'sar_reversal'], name='trigger') + Categorical(['boll', 'macd_cross_signal', 'sar_reversal'], name='trigger'), + ] @staticmethod @@ -71,39 +76,61 @@ class SampleHyperOpt(IHyperOpt): """ Buy strategy Hyperopt will build and use. """ - conditions = [] + long_conditions = [] + short_conditions = [] # GUARDS AND TRENDS if 'mfi-enabled' in params and params['mfi-enabled']: - conditions.append(dataframe['mfi'] < params['mfi-value']) + long_conditions.append(dataframe['mfi'] < params['mfi-value']) + short_conditions.append(dataframe['mfi'] > params['short-mfi-value']) if 'fastd-enabled' in params and params['fastd-enabled']: - conditions.append(dataframe['fastd'] < params['fastd-value']) + long_conditions.append(dataframe['fastd'] < params['fastd-value']) + short_conditions.append(dataframe['fastd'] > params['short-fastd-value']) if 'adx-enabled' in params and params['adx-enabled']: - conditions.append(dataframe['adx'] > params['adx-value']) + long_conditions.append(dataframe['adx'] > params['adx-value']) + short_conditions.append(dataframe['adx'] < params['short-adx-value']) if 'rsi-enabled' in params and params['rsi-enabled']: - conditions.append(dataframe['rsi'] < params['rsi-value']) + long_conditions.append(dataframe['rsi'] < params['rsi-value']) + short_conditions.append(dataframe['rsi'] > params['short-rsi-value']) # TRIGGERS if 'trigger' in params: - if params['trigger'] == 'bb_lower': - conditions.append(dataframe['close'] < dataframe['bb_lowerband']) + if params['trigger'] == 'boll': + long_conditions.append(dataframe['close'] < dataframe['bb_lowerband']) + short_conditions.append(dataframe['close'] > dataframe['bb_upperband']) if params['trigger'] == 'macd_cross_signal': - conditions.append(qtpylib.crossed_above( - dataframe['macd'], dataframe['macdsignal'] + long_conditions.append(qtpylib.crossed_above( + dataframe['macd'], + dataframe['macdsignal'] + )) + short_conditions.append(qtpylib.crossed_below( + dataframe['macd'], + dataframe['macdsignal'] )) if params['trigger'] == 'sar_reversal': - conditions.append(qtpylib.crossed_above( - dataframe['close'], dataframe['sar'] + long_conditions.append(qtpylib.crossed_above( + dataframe['close'], + dataframe['sar'] + )) + short_conditions.append(qtpylib.crossed_below( + dataframe['close'], + dataframe['sar'] )) # Check that volume is not 0 - conditions.append(dataframe['volume'] > 0) + long_conditions.append(dataframe['volume'] > 0) + short_conditions.append(dataframe['volume'] > 0) - if conditions: + if long_conditions: dataframe.loc[ - reduce(lambda x, y: x & y, conditions), + reduce(lambda x, y: x & y, long_conditions), 'buy'] = 1 + if short_conditions: + dataframe.loc[ + reduce(lambda x, y: x & y, short_conditions), + 'enter_short'] = 1 + return dataframe return populate_buy_trend @@ -118,13 +145,19 @@ class SampleHyperOpt(IHyperOpt): Integer(50, 100, name='sell-fastd-value'), Integer(50, 100, name='sell-adx-value'), Integer(60, 100, name='sell-rsi-value'), + Integer(1, 25, name='exit-short-mfi-value'), + Integer(1, 50, name='exit-short-fastd-value'), + Integer(1, 50, name='exit-short-adx-value'), + Integer(1, 40, name='exit-short-rsi-value'), Categorical([True, False], name='sell-mfi-enabled'), Categorical([True, False], name='sell-fastd-enabled'), Categorical([True, False], name='sell-adx-enabled'), Categorical([True, False], name='sell-rsi-enabled'), - Categorical(['sell-bb_upper', + Categorical(['sell-boll', 'sell-macd_cross_signal', - 'sell-sar_reversal'], name='sell-trigger') + 'sell-sar_reversal'], + name='sell-trigger' + ), ] @staticmethod @@ -136,161 +169,61 @@ class SampleHyperOpt(IHyperOpt): """ Sell strategy Hyperopt will build and use. """ - conditions = [] + exit_long_conditions = [] + exit_short_conditions = [] # GUARDS AND TRENDS if 'sell-mfi-enabled' in params and params['sell-mfi-enabled']: - conditions.append(dataframe['mfi'] > params['sell-mfi-value']) + exit_long_conditions.append(dataframe['mfi'] > params['sell-mfi-value']) + exit_short_conditions.append(dataframe['mfi'] < params['exit-short-mfi-value']) if 'sell-fastd-enabled' in params and params['sell-fastd-enabled']: - conditions.append(dataframe['fastd'] > params['sell-fastd-value']) + exit_long_conditions.append(dataframe['fastd'] > params['sell-fastd-value']) + exit_short_conditions.append(dataframe['fastd'] < params['exit-short-fastd-value']) if 'sell-adx-enabled' in params and params['sell-adx-enabled']: - conditions.append(dataframe['adx'] < params['sell-adx-value']) + exit_long_conditions.append(dataframe['adx'] < params['sell-adx-value']) + exit_short_conditions.append(dataframe['adx'] > params['exit-short-adx-value']) if 'sell-rsi-enabled' in params and params['sell-rsi-enabled']: - conditions.append(dataframe['rsi'] > params['sell-rsi-value']) + exit_long_conditions.append(dataframe['rsi'] > params['sell-rsi-value']) + exit_short_conditions.append(dataframe['rsi'] < params['exit-short-rsi-value']) # TRIGGERS if 'sell-trigger' in params: - if params['sell-trigger'] == 'sell-bb_upper': - conditions.append(dataframe['close'] > dataframe['bb_upperband']) + if params['sell-trigger'] == 'sell-boll': + exit_long_conditions.append(dataframe['close'] > dataframe['bb_upperband']) + exit_short_conditions.append(dataframe['close'] < dataframe['bb_lowerband']) if params['sell-trigger'] == 'sell-macd_cross_signal': - conditions.append(qtpylib.crossed_above( - dataframe['macdsignal'], dataframe['macd'] + exit_long_conditions.append(qtpylib.crossed_above( + dataframe['macdsignal'], + dataframe['macd'] + )) + exit_short_conditions.append(qtpylib.crossed_below( + dataframe['macdsignal'], + dataframe['macd'] )) if params['sell-trigger'] == 'sell-sar_reversal': - conditions.append(qtpylib.crossed_above( - dataframe['sar'], dataframe['close'] + exit_long_conditions.append(qtpylib.crossed_above( + dataframe['sar'], + dataframe['close'] + )) + exit_short_conditions.append(qtpylib.crossed_below( + dataframe['sar'], + dataframe['close'] )) # Check that volume is not 0 - conditions.append(dataframe['volume'] > 0) + exit_long_conditions.append(dataframe['volume'] > 0) + exit_short_conditions.append(dataframe['volume'] > 0) - if conditions: + if exit_long_conditions: dataframe.loc[ - reduce(lambda x, y: x & y, conditions), + reduce(lambda x, y: x & y, exit_long_conditions), 'sell'] = 1 - return dataframe - - return populate_sell_trend - - @staticmethod - def short_strategy_generator(params: Dict[str, Any]) -> Callable: - """ - Define the short strategy parameters to be used by Hyperopt. - """ - def populate_short_trend(dataframe: DataFrame, metadata: dict) -> DataFrame: - """ - Buy strategy Hyperopt will build and use. - """ - conditions = [] - - # GUARDS AND TRENDS - if 'mfi-enabled' in params and params['mfi-enabled']: - conditions.append(dataframe['mfi'] > params['mfi-value']) - if 'fastd-enabled' in params and params['fastd-enabled']: - conditions.append(dataframe['fastd'] > params['fastd-value']) - if 'adx-enabled' in params and params['adx-enabled']: - conditions.append(dataframe['adx'] < params['adx-value']) - if 'rsi-enabled' in params and params['rsi-enabled']: - conditions.append(dataframe['rsi'] > params['rsi-value']) - - # TRIGGERS - if 'trigger' in params: - if params['trigger'] == 'bb_upper': - conditions.append(dataframe['close'] > dataframe['bb_upperband']) - if params['trigger'] == 'macd_cross_signal': - conditions.append(qtpylib.crossed_below( - dataframe['macd'], dataframe['macdsignal'] - )) - if params['trigger'] == 'sar_reversal': - conditions.append(qtpylib.crossed_below( - dataframe['close'], dataframe['sar'] - )) - - if conditions: + if exit_short_conditions: dataframe.loc[ - reduce(lambda x, y: x & y, conditions), - 'short'] = 1 - - return dataframe - - return populate_short_trend - - @staticmethod - def short_indicator_space() -> List[Dimension]: - """ - Define your Hyperopt space for searching short strategy parameters. - """ - return [ - Integer(75, 90, name='mfi-value'), - Integer(55, 85, name='fastd-value'), - Integer(50, 80, name='adx-value'), - Integer(60, 80, name='rsi-value'), - Categorical([True, False], name='mfi-enabled'), - Categorical([True, False], name='fastd-enabled'), - Categorical([True, False], name='adx-enabled'), - Categorical([True, False], name='rsi-enabled'), - Categorical(['bb_upper', 'macd_cross_signal', 'sar_reversal'], name='trigger') - ] - - @staticmethod - def exit_short_strategy_generator(params: Dict[str, Any]) -> Callable: - """ - Define the exit_short strategy parameters to be used by Hyperopt. - """ - def populate_exit_short_trend(dataframe: DataFrame, metadata: dict) -> DataFrame: - """ - Exit_short strategy Hyperopt will build and use. - """ - conditions = [] - - # GUARDS AND TRENDS - if 'exit-short-mfi-enabled' in params and params['exit-short-mfi-enabled']: - conditions.append(dataframe['mfi'] < params['exit-short-mfi-value']) - if 'exit-short-fastd-enabled' in params and params['exit-short-fastd-enabled']: - conditions.append(dataframe['fastd'] < params['exit-short-fastd-value']) - if 'exit-short-adx-enabled' in params and params['exit-short-adx-enabled']: - conditions.append(dataframe['adx'] > params['exit-short-adx-value']) - if 'exit-short-rsi-enabled' in params and params['exit-short-rsi-enabled']: - conditions.append(dataframe['rsi'] < params['exit-short-rsi-value']) - - # TRIGGERS - if 'exit-short-trigger' in params: - if params['exit-short-trigger'] == 'exit-short-bb_lower': - conditions.append(dataframe['close'] < dataframe['bb_lowerband']) - if params['exit-short-trigger'] == 'exit-short-macd_cross_signal': - conditions.append(qtpylib.crossed_below( - dataframe['macdsignal'], dataframe['macd'] - )) - if params['exit-short-trigger'] == 'exit-short-sar_reversal': - conditions.append(qtpylib.crossed_below( - dataframe['sar'], dataframe['close'] - )) - - if conditions: - dataframe.loc[ - reduce(lambda x, y: x & y, conditions), + reduce(lambda x, y: x & y, exit_short_conditions), 'exit_short'] = 1 return dataframe - return populate_exit_short_trend - - @staticmethod - def exit_short_indicator_space() -> List[Dimension]: - """ - Define your Hyperopt space for searching exit short strategy parameters. - """ - return [ - Integer(1, 25, name='exit_short-mfi-value'), - Integer(1, 50, name='exit_short-fastd-value'), - Integer(1, 50, name='exit_short-adx-value'), - Integer(1, 40, name='exit_short-rsi-value'), - Categorical([True, False], name='exit_short-mfi-enabled'), - Categorical([True, False], name='exit_short-fastd-enabled'), - Categorical([True, False], name='exit_short-adx-enabled'), - Categorical([True, False], name='exit_short-rsi-enabled'), - Categorical(['exit_short-bb_lower', - 'exit_short-macd_cross_signal', - 'exit_short-sar_reversal'], name='exit_short-trigger') - ] + return populate_sell_trend diff --git a/freqtrade/templates/sample_hyperopt_advanced.py b/freqtrade/templates/sample_hyperopt_advanced.py index cee343bb6..feb617aae 100644 --- a/freqtrade/templates/sample_hyperopt_advanced.py +++ b/freqtrade/templates/sample_hyperopt_advanced.py @@ -70,11 +70,15 @@ class AdvancedSampleHyperOpt(IHyperOpt): Integer(15, 45, name='fastd-value'), Integer(20, 50, name='adx-value'), Integer(20, 40, name='rsi-value'), + Integer(75, 90, name='short-mfi-value'), + Integer(55, 85, name='short-fastd-value'), + Integer(50, 80, name='short-adx-value'), + Integer(60, 80, name='short-rsi-value'), Categorical([True, False], name='mfi-enabled'), Categorical([True, False], name='fastd-enabled'), Categorical([True, False], name='adx-enabled'), Categorical([True, False], name='rsi-enabled'), - Categorical(['bb_lower', 'macd_cross_signal', 'sar_reversal'], name='trigger') + Categorical(['boll', 'macd_cross_signal', 'sar_reversal'], name='trigger') ] @staticmethod @@ -86,38 +90,60 @@ class AdvancedSampleHyperOpt(IHyperOpt): """ Buy strategy Hyperopt will build and use """ - conditions = [] + long_conditions = [] + short_conditions = [] # GUARDS AND TRENDS if 'mfi-enabled' in params and params['mfi-enabled']: - conditions.append(dataframe['mfi'] < params['mfi-value']) + long_conditions.append(dataframe['mfi'] < params['mfi-value']) + short_conditions.append(dataframe['mfi'] > params['short-mfi-value']) if 'fastd-enabled' in params and params['fastd-enabled']: - conditions.append(dataframe['fastd'] < params['fastd-value']) + long_conditions.append(dataframe['fastd'] < params['fastd-value']) + short_conditions.append(dataframe['fastd'] > params['short-fastd-value']) if 'adx-enabled' in params and params['adx-enabled']: - conditions.append(dataframe['adx'] > params['adx-value']) + long_conditions.append(dataframe['adx'] > params['adx-value']) + short_conditions.append(dataframe['adx'] < params['short-adx-value']) if 'rsi-enabled' in params and params['rsi-enabled']: - conditions.append(dataframe['rsi'] < params['rsi-value']) + long_conditions.append(dataframe['rsi'] < params['rsi-value']) + short_conditions.append(dataframe['rsi'] > params['short-rsi-value']) # TRIGGERS if 'trigger' in params: - if params['trigger'] == 'bb_lower': - conditions.append(dataframe['close'] < dataframe['bb_lowerband']) + if params['trigger'] == 'boll': + long_conditions.append(dataframe['close'] < dataframe['bb_lowerband']) + short_conditions.append(dataframe['close'] > dataframe['bb_upperband']) if params['trigger'] == 'macd_cross_signal': - conditions.append(qtpylib.crossed_above( - dataframe['macd'], dataframe['macdsignal'] + long_conditions.append(qtpylib.crossed_above( + dataframe['macd'], + dataframe['macdsignal'] + )) + short_conditions.append(qtpylib.crossed_below( + dataframe['macd'], + dataframe['macdsignal'] )) if params['trigger'] == 'sar_reversal': - conditions.append(qtpylib.crossed_above( - dataframe['close'], dataframe['sar'] + long_conditions.append(qtpylib.crossed_above( + dataframe['close'], + dataframe['sar'] + )) + short_conditions.append(qtpylib.crossed_below( + dataframe['close'], + dataframe['sar'] )) # Check that volume is not 0 - conditions.append(dataframe['volume'] > 0) + long_conditions.append(dataframe['volume'] > 0) + short_conditions.append(dataframe['volume'] > 0) - if conditions: + if long_conditions: dataframe.loc[ - reduce(lambda x, y: x & y, conditions), + reduce(lambda x, y: x & y, long_conditions), 'buy'] = 1 + if short_conditions: + dataframe.loc[ + reduce(lambda x, y: x & y, short_conditions), + 'enter_short'] = 1 + return dataframe return populate_buy_trend @@ -132,13 +158,18 @@ class AdvancedSampleHyperOpt(IHyperOpt): Integer(50, 100, name='sell-fastd-value'), Integer(50, 100, name='sell-adx-value'), Integer(60, 100, name='sell-rsi-value'), + Integer(1, 25, name='exit_short-mfi-value'), + Integer(1, 50, name='exit_short-fastd-value'), + Integer(1, 50, name='exit_short-adx-value'), + Integer(1, 40, name='exit_short-rsi-value'), Categorical([True, False], name='sell-mfi-enabled'), Categorical([True, False], name='sell-fastd-enabled'), Categorical([True, False], name='sell-adx-enabled'), Categorical([True, False], name='sell-rsi-enabled'), - Categorical(['sell-bb_upper', + Categorical(['sell-boll', 'sell-macd_cross_signal', - 'sell-sar_reversal'], name='sell-trigger') + 'sell-sar_reversal'], + name='sell-trigger') ] @staticmethod @@ -151,163 +182,63 @@ class AdvancedSampleHyperOpt(IHyperOpt): Sell strategy Hyperopt will build and use """ # print(params) - conditions = [] + exit_long_conditions = [] + exit_short_conditions = [] # GUARDS AND TRENDS if 'sell-mfi-enabled' in params and params['sell-mfi-enabled']: - conditions.append(dataframe['mfi'] > params['sell-mfi-value']) + exit_long_conditions.append(dataframe['mfi'] > params['sell-mfi-value']) + exit_short_conditions.append(dataframe['mfi'] < params['exit-short-mfi-value']) if 'sell-fastd-enabled' in params and params['sell-fastd-enabled']: - conditions.append(dataframe['fastd'] > params['sell-fastd-value']) + exit_long_conditions.append(dataframe['fastd'] > params['sell-fastd-value']) + exit_short_conditions.append(dataframe['fastd'] < params['exit-short-fastd-value']) if 'sell-adx-enabled' in params and params['sell-adx-enabled']: - conditions.append(dataframe['adx'] < params['sell-adx-value']) + exit_long_conditions.append(dataframe['adx'] < params['sell-adx-value']) + exit_short_conditions.append(dataframe['adx'] > params['exit-short-adx-value']) if 'sell-rsi-enabled' in params and params['sell-rsi-enabled']: - conditions.append(dataframe['rsi'] > params['sell-rsi-value']) + exit_long_conditions.append(dataframe['rsi'] > params['sell-rsi-value']) + exit_short_conditions.append(dataframe['rsi'] < params['exit-short-rsi-value']) # TRIGGERS if 'sell-trigger' in params: - if params['sell-trigger'] == 'sell-bb_upper': - conditions.append(dataframe['close'] > dataframe['bb_upperband']) + if params['sell-trigger'] == 'sell-boll': + exit_long_conditions.append(dataframe['close'] > dataframe['bb_upperband']) + exit_short_conditions.append(dataframe['close'] < dataframe['bb_lowerband']) if params['sell-trigger'] == 'sell-macd_cross_signal': - conditions.append(qtpylib.crossed_above( - dataframe['macdsignal'], dataframe['macd'] + exit_long_conditions.append(qtpylib.crossed_above( + dataframe['macdsignal'], + dataframe['macd'] + )) + exit_long_conditions.append(qtpylib.crossed_below( + dataframe['macdsignal'], + dataframe['macd'] )) if params['sell-trigger'] == 'sell-sar_reversal': - conditions.append(qtpylib.crossed_above( - dataframe['sar'], dataframe['close'] + exit_long_conditions.append(qtpylib.crossed_above( + dataframe['sar'], + dataframe['close'] + )) + exit_long_conditions.append(qtpylib.crossed_below( + dataframe['sar'], + dataframe['close'] )) # Check that volume is not 0 - conditions.append(dataframe['volume'] > 0) + exit_long_conditions.append(dataframe['volume'] > 0) + exit_short_conditions.append(dataframe['volume'] > 0) - if conditions: + if exit_long_conditions: dataframe.loc[ - reduce(lambda x, y: x & y, conditions), + reduce(lambda x, y: x & y, exit_long_conditions), 'sell'] = 1 - return dataframe - - return populate_sell_trend - - @staticmethod - def short_strategy_generator(params: Dict[str, Any]) -> Callable: - """ - Define the short strategy parameters to be used by Hyperopt. - """ - def populate_short_trend(dataframe: DataFrame, metadata: dict) -> DataFrame: - """ - Buy strategy Hyperopt will build and use. - """ - conditions = [] - - # GUARDS AND TRENDS - if 'mfi-enabled' in params and params['mfi-enabled']: - conditions.append(dataframe['mfi'] > params['mfi-value']) - if 'fastd-enabled' in params and params['fastd-enabled']: - conditions.append(dataframe['fastd'] > params['fastd-value']) - if 'adx-enabled' in params and params['adx-enabled']: - conditions.append(dataframe['adx'] < params['adx-value']) - if 'rsi-enabled' in params and params['rsi-enabled']: - conditions.append(dataframe['rsi'] > params['rsi-value']) - - # TRIGGERS - if 'trigger' in params: - if params['trigger'] == 'bb_upper': - conditions.append(dataframe['close'] > dataframe['bb_upperband']) - if params['trigger'] == 'macd_cross_signal': - conditions.append(qtpylib.crossed_below( - dataframe['macd'], dataframe['macdsignal'] - )) - if params['trigger'] == 'sar_reversal': - conditions.append(qtpylib.crossed_below( - dataframe['close'], dataframe['sar'] - )) - - if conditions: + if exit_short_conditions: dataframe.loc[ - reduce(lambda x, y: x & y, conditions), - 'short'] = 1 - - return dataframe - - return populate_short_trend - - @staticmethod - def short_indicator_space() -> List[Dimension]: - """ - Define your Hyperopt space for searching short strategy parameters. - """ - return [ - Integer(75, 90, name='mfi-value'), - Integer(55, 85, name='fastd-value'), - Integer(50, 80, name='adx-value'), - Integer(60, 80, name='rsi-value'), - Categorical([True, False], name='mfi-enabled'), - Categorical([True, False], name='fastd-enabled'), - Categorical([True, False], name='adx-enabled'), - Categorical([True, False], name='rsi-enabled'), - Categorical(['bb_upper', 'macd_cross_signal', 'sar_reversal'], name='trigger') - ] - - @staticmethod - def exit_short_strategy_generator(params: Dict[str, Any]) -> Callable: - """ - Define the exit_short strategy parameters to be used by Hyperopt. - """ - def populate_exit_short_trend(dataframe: DataFrame, metadata: dict) -> DataFrame: - """ - Exit_short strategy Hyperopt will build and use. - """ - conditions = [] - - # GUARDS AND TRENDS - if 'exit-short-mfi-enabled' in params and params['exit-short-mfi-enabled']: - conditions.append(dataframe['mfi'] < params['exit-short-mfi-value']) - if 'exit-short-fastd-enabled' in params and params['exit-short-fastd-enabled']: - conditions.append(dataframe['fastd'] < params['exit-short-fastd-value']) - if 'exit-short-adx-enabled' in params and params['exit-short-adx-enabled']: - conditions.append(dataframe['adx'] > params['exit-short-adx-value']) - if 'exit-short-rsi-enabled' in params and params['exit-short-rsi-enabled']: - conditions.append(dataframe['rsi'] < params['exit-short-rsi-value']) - - # TRIGGERS - if 'exit-short-trigger' in params: - if params['exit-short-trigger'] == 'exit-short-bb_lower': - conditions.append(dataframe['close'] < dataframe['bb_lowerband']) - if params['exit-short-trigger'] == 'exit-short-macd_cross_signal': - conditions.append(qtpylib.crossed_below( - dataframe['macdsignal'], dataframe['macd'] - )) - if params['exit-short-trigger'] == 'exit-short-sar_reversal': - conditions.append(qtpylib.crossed_below( - dataframe['sar'], dataframe['close'] - )) - - if conditions: - dataframe.loc[ - reduce(lambda x, y: x & y, conditions), + reduce(lambda x, y: x & y, exit_short_conditions), 'exit_short'] = 1 return dataframe - return populate_exit_short_trend - - @staticmethod - def exit_short_indicator_space() -> List[Dimension]: - """ - Define your Hyperopt space for searching exit short strategy parameters. - """ - return [ - Integer(1, 25, name='exit_short-mfi-value'), - Integer(1, 50, name='exit_short-fastd-value'), - Integer(1, 50, name='exit_short-adx-value'), - Integer(1, 40, name='exit_short-rsi-value'), - Categorical([True, False], name='exit_short-mfi-enabled'), - Categorical([True, False], name='exit_short-fastd-enabled'), - Categorical([True, False], name='exit_short-adx-enabled'), - Categorical([True, False], name='exit_short-rsi-enabled'), - Categorical(['exit_short-bb_lower', - 'exit_short-macd_cross_signal', - 'exit_short-sar_reversal'], name='exit_short-trigger') - ] + return populate_sell_trend @staticmethod def generate_roi_table(params: Dict) -> Dict[int, float]: diff --git a/freqtrade/templates/sample_strategy.py b/freqtrade/templates/sample_strategy.py index 3e73d3134..b2d130059 100644 --- a/freqtrade/templates/sample_strategy.py +++ b/freqtrade/templates/sample_strategy.py @@ -29,7 +29,7 @@ class SampleStrategy(IStrategy): You must keep: - the lib in the section "Do not remove these libs" - - the methods: populate_indicators, populate_buy_trend, populate_sell_trend, populate_short_trend, populate_exit_short_trend + - the methods: populate_indicators, populate_buy_trend, populate_sell_trend You should keep: - timeframe, minimal_roi, stoploss, trailing_* """ @@ -356,6 +356,16 @@ class SampleStrategy(IStrategy): ), 'buy'] = 1 + dataframe.loc[ + ( + # Signal: RSI crosses above 70 + (qtpylib.crossed_above(dataframe['rsi'], self.short_rsi.value)) & + (dataframe['tema'] > dataframe['bb_middleband']) & # Guard: tema above BB middle + (dataframe['tema'] < dataframe['tema'].shift(1)) & # Guard: tema is falling + (dataframe['volume'] > 0) # Make sure Volume is not 0 + ), + 'enter_short'] = 1 + return dataframe def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: @@ -374,38 +384,13 @@ class SampleStrategy(IStrategy): (dataframe['volume'] > 0) # Make sure Volume is not 0 ), 'sell'] = 1 - return dataframe - def populate_short_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: - """ - Based on TA indicators, populates the short signal for the given dataframe - :param dataframe: DataFrame populated with indicators - :param metadata: Additional information, like the currently traded pair - :return: DataFrame with short column - """ - dataframe.loc[ - ( - # Signal: RSI crosses above 70 - (qtpylib.crossed_above(dataframe['rsi'], self.short_rsi.value)) & - (dataframe['tema'] > dataframe['bb_middleband']) & # Guard: tema above BB middle - (dataframe['tema'] < dataframe['tema'].shift(1)) & # Guard: tema is falling - (dataframe['volume'] > 0) # Make sure Volume is not 0 - ), - 'short'] = 1 - return dataframe - - def populate_exit_short_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: - """ - Based on TA indicators, populates the exit_short signal for the given dataframe - :param dataframe: DataFrame populated with indicators - :param metadata: Additional information, like the currently traded pair - :return: DataFrame with exit_short column - """ dataframe.loc[ ( # Signal: RSI crosses above 30 (qtpylib.crossed_above(dataframe['rsi'], self.exit_short_rsi.value)) & - (dataframe['tema'] <= dataframe['bb_middleband']) & # Guard: tema below BB middle + # Guard: tema below BB middle + (dataframe['tema'] <= dataframe['bb_middleband']) & (dataframe['tema'] > dataframe['tema'].shift(1)) & # Guard: tema is raising (dataframe['volume'] > 0) # Make sure Volume is not 0 ), diff --git a/tests/optimize/hyperopts/default_hyperopt.py b/tests/optimize/hyperopts/default_hyperopt.py index cc8771d1b..df39188e0 100644 --- a/tests/optimize/hyperopts/default_hyperopt.py +++ b/tests/optimize/hyperopts/default_hyperopt.py @@ -54,36 +54,57 @@ class DefaultHyperOpt(IHyperOpt): """ Buy strategy Hyperopt will build and use. """ - conditions = [] + long_conditions = [] + short_conditions = [] # GUARDS AND TRENDS if 'mfi-enabled' in params and params['mfi-enabled']: - conditions.append(dataframe['mfi'] < params['mfi-value']) + long_conditions.append(dataframe['mfi'] < params['mfi-value']) + short_conditions.append(dataframe['mfi'] > params['short-mfi-value']) if 'fastd-enabled' in params and params['fastd-enabled']: - conditions.append(dataframe['fastd'] < params['fastd-value']) + long_conditions.append(dataframe['fastd'] < params['fastd-value']) + short_conditions.append(dataframe['fastd'] > params['short-fastd-value']) if 'adx-enabled' in params and params['adx-enabled']: - conditions.append(dataframe['adx'] > params['adx-value']) + long_conditions.append(dataframe['adx'] > params['adx-value']) + short_conditions.append(dataframe['adx'] < params['short-adx-value']) if 'rsi-enabled' in params and params['rsi-enabled']: - conditions.append(dataframe['rsi'] < params['rsi-value']) + long_conditions.append(dataframe['rsi'] < params['rsi-value']) + short_conditions.append(dataframe['rsi'] > params['short-rsi-value']) # TRIGGERS if 'trigger' in params: - if params['trigger'] == 'bb_lower': - conditions.append(dataframe['close'] < dataframe['bb_lowerband']) + if params['trigger'] == 'boll': + long_conditions.append(dataframe['close'] < dataframe['bb_lowerband']) + short_conditions.append(dataframe['close'] > dataframe['bb_upperband']) if params['trigger'] == 'macd_cross_signal': - conditions.append(qtpylib.crossed_above( - dataframe['macd'], dataframe['macdsignal'] + long_conditions.append(qtpylib.crossed_above( + dataframe['macd'], + dataframe['macdsignal'] + )) + short_conditions.append(qtpylib.crossed_below( + dataframe['macd'], + dataframe['macdsignal'] )) if params['trigger'] == 'sar_reversal': - conditions.append(qtpylib.crossed_above( - dataframe['close'], dataframe['sar'] + long_conditions.append(qtpylib.crossed_above( + dataframe['close'], + dataframe['sar'] + )) + short_conditions.append(qtpylib.crossed_below( + dataframe['close'], + dataframe['sar'] )) - if conditions: + if long_conditions: dataframe.loc[ - reduce(lambda x, y: x & y, conditions), + reduce(lambda x, y: x & y, long_conditions), 'buy'] = 1 + if short_conditions: + dataframe.loc[ + reduce(lambda x, y: x & y, short_conditions), + 'enter_short'] = 1 + return dataframe return populate_buy_trend @@ -98,71 +119,15 @@ class DefaultHyperOpt(IHyperOpt): Integer(15, 45, name='fastd-value'), Integer(20, 50, name='adx-value'), Integer(20, 40, name='rsi-value'), + Integer(75, 90, name='short-mfi-value'), + Integer(55, 85, name='short-fastd-value'), + Integer(50, 80, name='short-adx-value'), + Integer(60, 80, name='short-rsi-value'), Categorical([True, False], name='mfi-enabled'), Categorical([True, False], name='fastd-enabled'), Categorical([True, False], name='adx-enabled'), Categorical([True, False], name='rsi-enabled'), - Categorical(['bb_lower', 'macd_cross_signal', 'sar_reversal'], name='trigger') - ] - - @staticmethod - def short_strategy_generator(params: Dict[str, Any]) -> Callable: - """ - Define the short strategy parameters to be used by Hyperopt. - """ - def populate_short_trend(dataframe: DataFrame, metadata: dict) -> DataFrame: - """ - Buy strategy Hyperopt will build and use. - """ - conditions = [] - - # GUARDS AND TRENDS - if 'mfi-enabled' in params and params['mfi-enabled']: - conditions.append(dataframe['mfi'] > params['mfi-value']) - if 'fastd-enabled' in params and params['fastd-enabled']: - conditions.append(dataframe['fastd'] > params['fastd-value']) - if 'adx-enabled' in params and params['adx-enabled']: - conditions.append(dataframe['adx'] < params['adx-value']) - if 'rsi-enabled' in params and params['rsi-enabled']: - conditions.append(dataframe['rsi'] > params['rsi-value']) - - # TRIGGERS - if 'trigger' in params: - if params['trigger'] == 'bb_upper': - conditions.append(dataframe['close'] > dataframe['bb_upperband']) - if params['trigger'] == 'macd_cross_signal': - conditions.append(qtpylib.crossed_below( - dataframe['macd'], dataframe['macdsignal'] - )) - if params['trigger'] == 'sar_reversal': - conditions.append(qtpylib.crossed_below( - dataframe['close'], dataframe['sar'] - )) - - if conditions: - dataframe.loc[ - reduce(lambda x, y: x & y, conditions), - 'short'] = 1 - - return dataframe - - return populate_short_trend - - @staticmethod - def short_indicator_space() -> List[Dimension]: - """ - Define your Hyperopt space for searching short strategy parameters. - """ - return [ - Integer(75, 90, name='mfi-value'), - Integer(55, 85, name='fastd-value'), - Integer(50, 80, name='adx-value'), - Integer(60, 80, name='rsi-value'), - Categorical([True, False], name='mfi-enabled'), - Categorical([True, False], name='fastd-enabled'), - Categorical([True, False], name='adx-enabled'), - Categorical([True, False], name='rsi-enabled'), - Categorical(['bb_upper', 'macd_cross_signal', 'sar_reversal'], name='trigger') + Categorical(['boll', 'macd_cross_signal', 'sar_reversal'], name='trigger') ] @staticmethod @@ -174,83 +139,61 @@ class DefaultHyperOpt(IHyperOpt): """ Sell strategy Hyperopt will build and use. """ - conditions = [] + exit_long_conditions = [] + exit_short_conditions = [] # GUARDS AND TRENDS if 'sell-mfi-enabled' in params and params['sell-mfi-enabled']: - conditions.append(dataframe['mfi'] > params['sell-mfi-value']) + exit_long_conditions.append(dataframe['mfi'] > params['sell-mfi-value']) + exit_short_conditions.append(dataframe['mfi'] < params['exit-short-mfi-value']) if 'sell-fastd-enabled' in params and params['sell-fastd-enabled']: - conditions.append(dataframe['fastd'] > params['sell-fastd-value']) + exit_long_conditions.append(dataframe['fastd'] > params['sell-fastd-value']) + exit_short_conditions.append(dataframe['fastd'] < params['exit-short-fastd-value']) if 'sell-adx-enabled' in params and params['sell-adx-enabled']: - conditions.append(dataframe['adx'] < params['sell-adx-value']) + exit_long_conditions.append(dataframe['adx'] < params['sell-adx-value']) + exit_short_conditions.append(dataframe['adx'] > params['exit-short-adx-value']) if 'sell-rsi-enabled' in params and params['sell-rsi-enabled']: - conditions.append(dataframe['rsi'] > params['sell-rsi-value']) + exit_long_conditions.append(dataframe['rsi'] > params['sell-rsi-value']) + exit_short_conditions.append(dataframe['rsi'] < params['exit-short-rsi-value']) # TRIGGERS if 'sell-trigger' in params: - if params['sell-trigger'] == 'sell-bb_upper': - conditions.append(dataframe['close'] > dataframe['bb_upperband']) + if params['sell-trigger'] == 'sell-boll': + exit_long_conditions.append(dataframe['close'] > dataframe['bb_upperband']) + exit_short_conditions.append(dataframe['close'] < dataframe['bb_lowerband']) if params['sell-trigger'] == 'sell-macd_cross_signal': - conditions.append(qtpylib.crossed_above( - dataframe['macdsignal'], dataframe['macd'] + exit_long_conditions.append(qtpylib.crossed_above( + dataframe['macdsignal'], + dataframe['macd'] + )) + exit_short_conditions.append(qtpylib.crossed_below( + dataframe['macdsignal'], + dataframe['macd'] )) if params['sell-trigger'] == 'sell-sar_reversal': - conditions.append(qtpylib.crossed_above( - dataframe['sar'], dataframe['close'] + exit_long_conditions.append(qtpylib.crossed_above( + dataframe['sar'], + dataframe['close'] + )) + exit_short_conditions.append(qtpylib.crossed_below( + dataframe['sar'], + dataframe['close'] )) - if conditions: + if exit_long_conditions: dataframe.loc[ - reduce(lambda x, y: x & y, conditions), + reduce(lambda x, y: x & y, exit_long_conditions), 'sell'] = 1 + if exit_short_conditions: + dataframe.loc[ + reduce(lambda x, y: x & y, exit_short_conditions), + 'exit-short'] = 1 + return dataframe return populate_sell_trend - @staticmethod - def exit_short_strategy_generator(params: Dict[str, Any]) -> Callable: - """ - Define the exit_short strategy parameters to be used by Hyperopt. - """ - def populate_exit_short_trend(dataframe: DataFrame, metadata: dict) -> DataFrame: - """ - Exit_short strategy Hyperopt will build and use. - """ - conditions = [] - - # GUARDS AND TRENDS - if 'exit-short-mfi-enabled' in params and params['exit-short-mfi-enabled']: - conditions.append(dataframe['mfi'] < params['exit-short-mfi-value']) - if 'exit-short-fastd-enabled' in params and params['exit-short-fastd-enabled']: - conditions.append(dataframe['fastd'] < params['exit-short-fastd-value']) - if 'exit-short-adx-enabled' in params and params['exit-short-adx-enabled']: - conditions.append(dataframe['adx'] > params['exit-short-adx-value']) - if 'exit-short-rsi-enabled' in params and params['exit-short-rsi-enabled']: - conditions.append(dataframe['rsi'] < params['exit-short-rsi-value']) - - # TRIGGERS - if 'exit-short-trigger' in params: - if params['exit-short-trigger'] == 'exit-short-bb_lower': - conditions.append(dataframe['close'] < dataframe['bb_lowerband']) - if params['exit-short-trigger'] == 'exit-short-macd_cross_signal': - conditions.append(qtpylib.crossed_below( - dataframe['macdsignal'], dataframe['macd'] - )) - if params['exit-short-trigger'] == 'exit-short-sar_reversal': - conditions.append(qtpylib.crossed_below( - dataframe['sar'], dataframe['close'] - )) - - if conditions: - dataframe.loc[ - reduce(lambda x, y: x & y, conditions), - 'exit_short'] = 1 - - return dataframe - - return populate_exit_short_trend - @staticmethod def sell_indicator_space() -> List[Dimension]: """ @@ -261,32 +204,18 @@ class DefaultHyperOpt(IHyperOpt): Integer(50, 100, name='sell-fastd-value'), Integer(50, 100, name='sell-adx-value'), Integer(60, 100, name='sell-rsi-value'), + Integer(1, 25, name='exit-short-mfi-value'), + Integer(1, 50, name='exit-short-fastd-value'), + Integer(1, 50, name='exit-short-adx-value'), + Integer(1, 40, name='exit-short-rsi-value'), Categorical([True, False], name='sell-mfi-enabled'), Categorical([True, False], name='sell-fastd-enabled'), Categorical([True, False], name='sell-adx-enabled'), Categorical([True, False], name='sell-rsi-enabled'), - Categorical(['sell-bb_upper', + Categorical(['sell-boll', 'sell-macd_cross_signal', - 'sell-sar_reversal'], name='sell-trigger') - ] - - @staticmethod - def exit_short_indicator_space() -> List[Dimension]: - """ - Define your Hyperopt space for searching exit short strategy parameters. - """ - return [ - Integer(1, 25, name='exit_short-mfi-value'), - Integer(1, 50, name='exit_short-fastd-value'), - Integer(1, 50, name='exit_short-adx-value'), - Integer(1, 40, name='exit_short-rsi-value'), - Categorical([True, False], name='exit_short-mfi-enabled'), - Categorical([True, False], name='exit_short-fastd-enabled'), - Categorical([True, False], name='exit_short-adx-enabled'), - Categorical([True, False], name='exit_short-rsi-enabled'), - Categorical(['exit_short-bb_lower', - 'exit_short-macd_cross_signal', - 'exit_short-sar_reversal'], name='exit_short-trigger') + 'sell-sar_reversal'], + name='sell-trigger') ] def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: @@ -304,6 +233,15 @@ class DefaultHyperOpt(IHyperOpt): ), 'buy'] = 1 + dataframe.loc[ + ( + (dataframe['close'] > dataframe['bb_upperband']) & + (dataframe['mfi'] < 84) & + (dataframe['adx'] > 75) & + (dataframe['rsi'] < 79) + ), + 'enter_short'] = 1 + return dataframe def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: @@ -321,31 +259,6 @@ class DefaultHyperOpt(IHyperOpt): ), 'sell'] = 1 - return dataframe - - def populate_short_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: - """ - Based on TA indicators. Should be a copy of same method from strategy. - Must align to populate_indicators in this file. - Only used when --spaces does not include short space. - """ - dataframe.loc[ - ( - (dataframe['close'] > dataframe['bb_upperband']) & - (dataframe['mfi'] < 84) & - (dataframe['adx'] > 75) & - (dataframe['rsi'] < 79) - ), - 'buy'] = 1 - - return dataframe - - def populate_exit_short_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: - """ - Based on TA indicators. Should be a copy of same method from strategy. - Must align to populate_indicators in this file. - Only used when --spaces does not include exit_short space. - """ dataframe.loc[ ( (qtpylib.crossed_below( @@ -353,6 +266,6 @@ class DefaultHyperOpt(IHyperOpt): )) & (dataframe['fastd'] < 46) ), - 'sell'] = 1 + 'exit_short'] = 1 return dataframe diff --git a/tests/optimize/test_backtest_detail.py b/tests/optimize/test_backtest_detail.py index 0205369ba..e5c037f3e 100644 --- a/tests/optimize/test_backtest_detail.py +++ b/tests/optimize/test_backtest_detail.py @@ -597,8 +597,8 @@ def test_backtest_results(default_conf, fee, mocker, caplog, data) -> None: backtesting = Backtesting(default_conf) backtesting._set_strategy(backtesting.strategylist[0]) backtesting.required_startup = 0 - backtesting.strategy.advise_enter = lambda a, m: frame - backtesting.strategy.advise_exit = lambda a, m: frame + backtesting.strategy.advise_buy = lambda a, m: frame + backtesting.strategy.advise_sell = lambda a, m: frame backtesting.strategy.use_custom_stoploss = data.use_custom_stoploss caplog.set_level(logging.DEBUG) diff --git a/tests/optimize/test_backtesting.py b/tests/optimize/test_backtesting.py index afbfcb1c2..deaaf9f2f 100644 --- a/tests/optimize/test_backtesting.py +++ b/tests/optimize/test_backtesting.py @@ -290,8 +290,8 @@ def test_backtesting_init(mocker, default_conf, order_types) -> None: assert backtesting.config == default_conf assert backtesting.timeframe == '5m' assert callable(backtesting.strategy.ohlcvdata_to_dataframe) - assert callable(backtesting.strategy.advise_enter) - assert callable(backtesting.strategy.advise_exit) + assert callable(backtesting.strategy.advise_buy) + assert callable(backtesting.strategy.advise_sell) assert isinstance(backtesting.strategy.dp, DataProvider) get_fee.assert_called() assert backtesting.fee == 0.5 @@ -700,8 +700,8 @@ def test_backtest_clash_buy_sell(mocker, default_conf, testdatadir): backtest_conf = _make_backtest_conf(mocker, conf=default_conf, datadir=testdatadir) backtesting = Backtesting(default_conf) backtesting._set_strategy(backtesting.strategylist[0]) - backtesting.strategy.advise_enter = fun # Override - backtesting.strategy.advise_exit = fun # Override + backtesting.strategy.advise_buy = fun # Override + backtesting.strategy.advise_sell = fun # Override result = backtesting.backtest(**backtest_conf) assert result['results'].empty @@ -716,8 +716,8 @@ def test_backtest_only_sell(mocker, default_conf, testdatadir): backtest_conf = _make_backtest_conf(mocker, conf=default_conf, datadir=testdatadir) backtesting = Backtesting(default_conf) backtesting._set_strategy(backtesting.strategylist[0]) - backtesting.strategy.advise_enter = fun # Override - backtesting.strategy.advise_exit = fun # Override + backtesting.strategy.advise_buy = fun # Override + backtesting.strategy.advise_sell = fun # Override result = backtesting.backtest(**backtest_conf) assert result['results'].empty @@ -731,8 +731,8 @@ def test_backtest_alternate_buy_sell(default_conf, fee, mocker, testdatadir): backtesting = Backtesting(default_conf) backtesting.required_startup = 0 backtesting._set_strategy(backtesting.strategylist[0]) - backtesting.strategy.advise_enter = _trend_alternate # Override - backtesting.strategy.advise_exit = _trend_alternate # Override + backtesting.strategy.advise_buy = _trend_alternate # Override + backtesting.strategy.advise_sell = _trend_alternate # Override result = backtesting.backtest(**backtest_conf) # 200 candles in backtest data # won't buy on first (shifted by 1) @@ -777,8 +777,8 @@ def test_backtest_multi_pair(default_conf, fee, mocker, tres, pair, testdatadir) backtesting = Backtesting(default_conf) backtesting._set_strategy(backtesting.strategylist[0]) - backtesting.strategy.advise_enter = _trend_alternate_hold # Override - backtesting.strategy.advise_exit = _trend_alternate_hold # Override + backtesting.strategy.advise_buy = _trend_alternate_hold # Override + backtesting.strategy.advise_sell = _trend_alternate_hold # Override processed = backtesting.strategy.ohlcvdata_to_dataframe(data) min_date, max_date = get_timerange(processed) diff --git a/tests/optimize/test_hyperopt.py b/tests/optimize/test_hyperopt.py index 855a752ac..333cea971 100644 --- a/tests/optimize/test_hyperopt.py +++ b/tests/optimize/test_hyperopt.py @@ -366,8 +366,8 @@ def test_start_calls_optimizer(mocker, hyperopt_conf, capsys) -> None: # Should be called for historical candle data assert dumper.call_count == 1 assert dumper2.call_count == 1 - assert hasattr(hyperopt.backtesting.strategy, "advise_exit") - assert hasattr(hyperopt.backtesting.strategy, "advise_enter") + assert hasattr(hyperopt.backtesting.strategy, "advise_sell") + assert hasattr(hyperopt.backtesting.strategy, "advise_buy") assert hasattr(hyperopt, "max_open_trades") assert hyperopt.max_open_trades == hyperopt_conf['max_open_trades'] assert hasattr(hyperopt, "position_stacking") @@ -451,6 +451,10 @@ def test_buy_strategy_generator(hyperopt, testdatadir) -> None: 'fastd-value': 20, 'mfi-value': 20, 'rsi-value': 20, + 'short-adx-value': 80, + 'short-fastd-value': 80, + 'short-mfi-value': 80, + 'short-rsi-value': 80, 'adx-enabled': True, 'fastd-enabled': True, 'mfi-enabled': True, @@ -476,6 +480,10 @@ def test_sell_strategy_generator(hyperopt, testdatadir) -> None: 'sell-fastd-value': 75, 'sell-mfi-value': 80, 'sell-rsi-value': 20, + 'exit-short-adx-value': 80, + 'exit-short-fastd-value': 25, + 'exit-short-mfi-value': 20, + 'exit-short-rsi-value': 80, 'sell-adx-enabled': True, 'sell-fastd-enabled': True, 'sell-mfi-enabled': True, @@ -534,6 +542,10 @@ def test_generate_optimizer(mocker, hyperopt_conf) -> None: 'fastd-value': 35, 'mfi-value': 0, 'rsi-value': 0, + 'short-adx-value': 100, + 'short-fastd-value': 65, + 'short-mfi-value': 100, + 'short-rsi-value': 100, 'adx-enabled': False, 'fastd-enabled': True, 'mfi-enabled': False, @@ -543,6 +555,10 @@ def test_generate_optimizer(mocker, hyperopt_conf) -> None: 'sell-fastd-value': 75, 'sell-mfi-value': 0, 'sell-rsi-value': 0, + 'exit-short-adx-value': 100, + 'exit-short-fastd-value': 25, + 'exit-short-mfi-value': 100, + 'exit-short-rsi-value': 100, 'sell-adx-enabled': False, 'sell-fastd-enabled': True, 'sell-mfi-enabled': False, @@ -569,12 +585,16 @@ def test_generate_optimizer(mocker, hyperopt_conf) -> None: ), 'params_details': {'buy': {'adx-enabled': False, 'adx-value': 0, + 'short-adx-value': 100, 'fastd-enabled': True, 'fastd-value': 35, + 'short-fastd-value': 65, 'mfi-enabled': False, 'mfi-value': 0, + 'short-mfi-value': 100, 'rsi-enabled': False, 'rsi-value': 0, + 'short-rsi-value': 100, 'trigger': 'macd_cross_signal'}, 'roi': {"0": 0.12000000000000001, "20.0": 0.02, @@ -583,12 +603,16 @@ def test_generate_optimizer(mocker, hyperopt_conf) -> None: 'protection': {}, 'sell': {'sell-adx-enabled': False, 'sell-adx-value': 0, + 'exit-short-adx-value': 100, 'sell-fastd-enabled': True, 'sell-fastd-value': 75, + 'exit-short-fastd-value': 25, 'sell-mfi-enabled': False, 'sell-mfi-value': 0, + 'exit-short-mfi-value': 100, 'sell-rsi-enabled': False, 'sell-rsi-value': 0, + 'exit-short-rsi-value': 100, 'sell-trigger': 'macd_cross_signal'}, 'stoploss': {'stoploss': -0.4}, 'trailing': {'trailing_only_offset_is_reached': False, @@ -825,8 +849,8 @@ def test_simplified_interface_roi_stoploss(mocker, hyperopt_conf, capsys) -> Non assert dumper.call_count == 1 assert dumper2.call_count == 1 - assert hasattr(hyperopt.backtesting.strategy, "advise_exit") - assert hasattr(hyperopt.backtesting.strategy, "advise_enter") + assert hasattr(hyperopt.backtesting.strategy, "advise_sell") + assert hasattr(hyperopt.backtesting.strategy, "advise_buy") assert hasattr(hyperopt, "max_open_trades") assert hyperopt.max_open_trades == hyperopt_conf['max_open_trades'] assert hasattr(hyperopt, "position_stacking") @@ -906,8 +930,8 @@ def test_simplified_interface_buy(mocker, hyperopt_conf, capsys) -> None: assert dumper.called assert dumper.call_count == 1 assert dumper2.call_count == 1 - assert hasattr(hyperopt.backtesting.strategy, "advise_exit") - assert hasattr(hyperopt.backtesting.strategy, "advise_enter") + assert hasattr(hyperopt.backtesting.strategy, "advise_sell") + assert hasattr(hyperopt.backtesting.strategy, "advise_buy") assert hasattr(hyperopt, "max_open_trades") assert hyperopt.max_open_trades == hyperopt_conf['max_open_trades'] assert hasattr(hyperopt, "position_stacking") @@ -960,8 +984,8 @@ def test_simplified_interface_sell(mocker, hyperopt_conf, capsys) -> None: assert dumper.called assert dumper.call_count == 1 assert dumper2.call_count == 1 - assert hasattr(hyperopt.backtesting.strategy, "advise_exit") - assert hasattr(hyperopt.backtesting.strategy, "advise_enter") + assert hasattr(hyperopt.backtesting.strategy, "advise_sell") + assert hasattr(hyperopt.backtesting.strategy, "advise_buy") assert hasattr(hyperopt, "max_open_trades") assert hyperopt.max_open_trades == hyperopt_conf['max_open_trades'] assert hasattr(hyperopt, "position_stacking") diff --git a/tests/rpc/test_rpc_apiserver.py b/tests/rpc/test_rpc_apiserver.py index 439a99e2f..1517b6fcc 100644 --- a/tests/rpc/test_rpc_apiserver.py +++ b/tests/rpc/test_rpc_apiserver.py @@ -264,7 +264,7 @@ def test_api_UvicornServer(mocker): assert thread_mock.call_count == 1 s.cleanup() - assert s.should_sell is True + assert s.should_exit is True def test_api_UvicornServer_run(mocker): diff --git a/tests/strategy/strats/default_strategy.py b/tests/strategy/strats/default_strategy.py index 3e5695a99..be373e0ee 100644 --- a/tests/strategy/strats/default_strategy.py +++ b/tests/strategy/strats/default_strategy.py @@ -130,6 +130,19 @@ class DefaultStrategy(IStrategy): ), 'buy'] = 1 + dataframe.loc[ + ( + (dataframe['rsi'] > 65) & + (dataframe['fastd'] > 65) & + (dataframe['adx'] < 70) & + (dataframe['plus_di'] < 0.5) # TODO-lev: What to do here + ) | + ( + (dataframe['adx'] < 35) & + (dataframe['plus_di'] < 0.5) # TODO-lev: What to do here + ), + 'enter_short'] = 1 + return dataframe def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: @@ -153,37 +166,7 @@ class DefaultStrategy(IStrategy): (dataframe['minus_di'] > 0.5) ), 'sell'] = 1 - return dataframe - def populate_short_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: - """ - Based on TA indicators, populates the short signal for the given dataframe - :param dataframe: DataFrame - :param metadata: Additional information, like the currently traded pair - :return: DataFrame with short column - """ - dataframe.loc[ - ( - (dataframe['rsi'] > 65) & - (dataframe['fastd'] > 65) & - (dataframe['adx'] < 70) & - (dataframe['plus_di'] < 0.5) # TODO-lev: What to do here - ) | - ( - (dataframe['adx'] < 35) & - (dataframe['plus_di'] < 0.5) # TODO-lev: What to do here - ), - 'short'] = 1 - - return dataframe - - def populate_exit_short_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: - """ - Based on TA indicators, populates the exit_short signal for the given dataframe - :param dataframe: DataFrame - :param metadata: Additional information, like the currently traded pair - :return: DataFrame with exit_short column - """ dataframe.loc[ ( ( @@ -198,4 +181,5 @@ class DefaultStrategy(IStrategy): (dataframe['minus_di'] < 0.5) # TODO-lev: what to do here ), 'exit_short'] = 1 + return dataframe diff --git a/tests/strategy/strats/hyperoptable_strategy.py b/tests/strategy/strats/hyperoptable_strategy.py index 8d428b33d..e45ba03f0 100644 --- a/tests/strategy/strats/hyperoptable_strategy.py +++ b/tests/strategy/strats/hyperoptable_strategy.py @@ -60,7 +60,7 @@ class HyperoptableStrategy(IStrategy): 'sell_minusdi': 0.4 } - short_params = { + enter_short_params = { 'short_rsi': 65, } @@ -87,8 +87,8 @@ class HyperoptableStrategy(IStrategy): }) return prot - short_rsi = IntParameter([50, 100], default=70, space='sell') - short_plusdi = RealParameter(low=0, high=1, default=0.5, space='sell') + enter_short_rsi = IntParameter([50, 100], default=70, space='sell') + enter_short_plusdi = RealParameter(low=0, high=1, default=0.5, space='sell') exit_short_rsi = IntParameter(low=0, high=50, default=30, space='buy') exit_short_minusdi = DecimalParameter(low=0, high=1, default=0.4999, decimals=3, space='buy', load=False) @@ -175,6 +175,19 @@ class HyperoptableStrategy(IStrategy): ), 'buy'] = 1 + dataframe.loc[ + ( + (dataframe['rsi'] > self.enter_short_rsi.value) & + (dataframe['fastd'] > 65) & + (dataframe['adx'] < 70) & + (dataframe['plus_di'] < self.enter_short_plusdi.value) + ) | + ( + (dataframe['adx'] < 35) & + (dataframe['plus_di'] < self.enter_short_plusdi.value) + ), + 'enter_short'] = 1 + return dataframe def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: @@ -198,37 +211,7 @@ class HyperoptableStrategy(IStrategy): (dataframe['minus_di'] > self.sell_minusdi.value) ), 'sell'] = 1 - return dataframe - def populate_short_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: - """ - Based on TA indicators, populates the short signal for the given dataframe - :param dataframe: DataFrame - :param metadata: Additional information, like the currently traded pair - :return: DataFrame with short column - """ - dataframe.loc[ - ( - (dataframe['rsi'] > self.short_rsi.value) & - (dataframe['fastd'] > 65) & - (dataframe['adx'] < 70) & - (dataframe['plus_di'] < self.short_plusdi.value) - ) | - ( - (dataframe['adx'] < 35) & - (dataframe['plus_di'] < self.short_plusdi.value) - ), - 'short'] = 1 - - return dataframe - - def populate_exit_short_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: - """ - Based on TA indicators, populates the exit_short signal for the given dataframe - :param dataframe: DataFrame - :param metadata: Additional information, like the currently traded pair - :return: DataFrame with exit_short column - """ dataframe.loc[ ( ( @@ -243,4 +226,5 @@ class HyperoptableStrategy(IStrategy): (dataframe['minus_di'] < self.exit_short_minusdi.value) ), 'exit_short'] = 1 + return dataframe diff --git a/tests/strategy/strats/legacy_strategy.py b/tests/strategy/strats/legacy_strategy.py index a5531b42f..20f24d6a3 100644 --- a/tests/strategy/strats/legacy_strategy.py +++ b/tests/strategy/strats/legacy_strategy.py @@ -84,35 +84,5 @@ class TestStrategyLegacy(IStrategy): (dataframe['volume'] > 0) ), 'sell'] = 1 - return dataframe - - def populate_short_trend(self, dataframe: DataFrame) -> DataFrame: - """ - Based on TA indicators, populates the buy signal for the given dataframe - :param dataframe: DataFrame - :return: DataFrame with buy column - """ - dataframe.loc[ - ( - (dataframe['adx'] > 30) & - (dataframe['tema'] > dataframe['tema'].shift(1)) & - (dataframe['volume'] > 0) - ), - 'buy'] = 1 return dataframe - - def populate_exit_short_trend(self, dataframe: DataFrame) -> DataFrame: - """ - Based on TA indicators, populates the sell signal for the given dataframe - :param dataframe: DataFrame - :return: DataFrame with buy column - """ - dataframe.loc[ - ( - (dataframe['adx'] > 70) & - (dataframe['tema'] < dataframe['tema'].shift(1)) & - (dataframe['volume'] > 0) - ), - 'sell'] = 1 - return dataframe diff --git a/tests/strategy/test_default_strategy.py b/tests/strategy/test_default_strategy.py index 420cf8f46..42b1cc0a0 100644 --- a/tests/strategy/test_default_strategy.py +++ b/tests/strategy/test_default_strategy.py @@ -14,8 +14,6 @@ def test_default_strategy_structure(): assert hasattr(DefaultStrategy, 'populate_indicators') assert hasattr(DefaultStrategy, 'populate_buy_trend') assert hasattr(DefaultStrategy, 'populate_sell_trend') - assert hasattr(DefaultStrategy, 'populate_short_trend') - assert hasattr(DefaultStrategy, 'populate_exit_short_trend') def test_default_strategy(result, fee): @@ -29,10 +27,6 @@ def test_default_strategy(result, fee): assert type(indicators) is DataFrame assert type(strategy.populate_buy_trend(indicators, metadata)) is DataFrame assert type(strategy.populate_sell_trend(indicators, metadata)) is DataFrame - # TODO-lev: I think these two should be commented out in the strategy by default - # TODO-lev: so they can be tested, but the tests can't really remain - assert type(strategy.populate_short_trend(indicators, metadata)) is DataFrame - assert type(strategy.populate_exit_short_trend(indicators, metadata)) is DataFrame trade = Trade( open_rate=19_000, @@ -43,28 +37,11 @@ def test_default_strategy(result, fee): assert strategy.confirm_trade_entry(pair='ETH/BTC', order_type='limit', amount=0.1, rate=20000, time_in_force='gtc', - is_short=False, current_time=datetime.utcnow()) is True - + current_time=datetime.utcnow()) is True 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_short=False, current_time=datetime.utcnow()) is True + current_time=datetime.utcnow()) is True # TODO-lev: Test for shorts? assert strategy.custom_stoploss(pair='ETH/BTC', trade=trade, current_time=datetime.now(), current_rate=20_000, current_profit=0.05) == strategy.stoploss - - short_trade = Trade( - open_rate=21_000, - amount=0.1, - pair='ETH/BTC', - fee_open=fee.return_value - ) - - assert strategy.confirm_trade_entry(pair='ETH/BTC', order_type='limit', amount=0.1, - rate=20000, time_in_force='gtc', - is_short=True, current_time=datetime.utcnow()) is True - - assert strategy.confirm_trade_exit(pair='ETH/BTC', trade=short_trade, order_type='limit', - amount=0.1, rate=20000, time_in_force='gtc', - sell_reason='roi', is_short=True, - current_time=datetime.utcnow()) is True diff --git a/tests/strategy/test_interface.py b/tests/strategy/test_interface.py index 1e47575dc..7b7354bda 100644 --- a/tests/strategy/test_interface.py +++ b/tests/strategy/test_interface.py @@ -482,20 +482,20 @@ def test_custom_sell(default_conf, fee, caplog) -> None: def test_analyze_ticker_default(ohlcv_history, mocker, caplog) -> None: caplog.set_level(logging.DEBUG) ind_mock = MagicMock(side_effect=lambda x, meta: x) - enter_mock = MagicMock(side_effect=lambda x, meta, is_short: x) - exit_mock = MagicMock(side_effect=lambda x, meta, is_short: x) + buy_mock = MagicMock(side_effect=lambda x, meta: x) + sell_mock = MagicMock(side_effect=lambda x, meta: x) mocker.patch.multiple( 'freqtrade.strategy.interface.IStrategy', advise_indicators=ind_mock, - advise_enter=enter_mock, - advise_exit=exit_mock, + advise_buy=buy_mock, + advise_sell=sell_mock, ) strategy = DefaultStrategy({}) strategy.analyze_ticker(ohlcv_history, {'pair': 'ETH/BTC'}) assert ind_mock.call_count == 1 - assert enter_mock.call_count == 2 - assert enter_mock.call_count == 2 + assert buy_mock.call_count == 1 + assert buy_mock.call_count == 1 assert log_has('TA Analysis Launched', caplog) assert not log_has('Skipping TA Analysis for already analyzed candle', caplog) @@ -504,8 +504,8 @@ def test_analyze_ticker_default(ohlcv_history, mocker, caplog) -> None: strategy.analyze_ticker(ohlcv_history, {'pair': 'ETH/BTC'}) # No analysis happens as process_only_new_candles is true assert ind_mock.call_count == 2 - assert enter_mock.call_count == 4 - assert enter_mock.call_count == 4 + assert buy_mock.call_count == 2 + assert buy_mock.call_count == 2 assert log_has('TA Analysis Launched', caplog) assert not log_has('Skipping TA Analysis for already analyzed candle', caplog) @@ -513,13 +513,13 @@ def test_analyze_ticker_default(ohlcv_history, mocker, caplog) -> None: def test__analyze_ticker_internal_skip_analyze(ohlcv_history, mocker, caplog) -> None: caplog.set_level(logging.DEBUG) ind_mock = MagicMock(side_effect=lambda x, meta: x) - enter_mock = MagicMock(side_effect=lambda x, meta, is_short: x) - exit_mock = MagicMock(side_effect=lambda x, meta, is_short: x) + buy_mock = MagicMock(side_effect=lambda x, meta: x) + sell_mock = MagicMock(side_effect=lambda x, meta: x) mocker.patch.multiple( 'freqtrade.strategy.interface.IStrategy', advise_indicators=ind_mock, - advise_enter=enter_mock, - advise_exit=exit_mock, + advise_buy=buy_mock, + advise_sell=sell_mock, ) strategy = DefaultStrategy({}) @@ -532,8 +532,8 @@ def test__analyze_ticker_internal_skip_analyze(ohlcv_history, mocker, caplog) -> assert 'close' in ret.columns assert isinstance(ret, DataFrame) assert ind_mock.call_count == 1 - assert enter_mock.call_count == 2 # Once for buy, once for short - assert enter_mock.call_count == 2 + assert buy_mock.call_count == 1 + assert buy_mock.call_count == 1 assert log_has('TA Analysis Launched', caplog) assert not log_has('Skipping TA Analysis for already analyzed candle', caplog) caplog.clear() @@ -541,8 +541,8 @@ def test__analyze_ticker_internal_skip_analyze(ohlcv_history, mocker, caplog) -> ret = strategy._analyze_ticker_internal(ohlcv_history, {'pair': 'ETH/BTC'}) # No analysis happens as process_only_new_candles is true assert ind_mock.call_count == 1 - assert enter_mock.call_count == 2 - assert enter_mock.call_count == 2 + assert buy_mock.call_count == 1 + assert buy_mock.call_count == 1 # only skipped analyze adds buy and sell columns, otherwise it's all mocked assert 'buy' in ret.columns assert 'sell' in ret.columns diff --git a/tests/strategy/test_strategy_loading.py b/tests/strategy/test_strategy_loading.py index 2cf77b172..8f8a71097 100644 --- a/tests/strategy/test_strategy_loading.py +++ b/tests/strategy/test_strategy_loading.py @@ -117,16 +117,12 @@ def test_strategy(result, default_conf): df_indicators = strategy.advise_indicators(result, metadata=metadata) assert 'adx' in df_indicators - dataframe = strategy.advise_enter(df_indicators, metadata=metadata, is_short=False) + dataframe = strategy.advise_buy(df_indicators, metadata=metadata) assert 'buy' in dataframe.columns + assert 'enter_short' in dataframe.columns - dataframe = strategy.advise_exit(df_indicators, metadata=metadata, is_short=False) + dataframe = strategy.advise_sell(df_indicators, metadata=metadata) assert 'sell' in dataframe.columns - - dataframe = strategy.advise_enter(df_indicators, metadata=metadata, is_short=True) - assert 'short' in dataframe.columns - - dataframe = strategy.advise_exit(df_indicators, metadata=metadata, is_short=True) assert 'exit_short' in dataframe.columns @@ -352,7 +348,7 @@ def test_deprecate_populate_indicators(result, default_conf): with warnings.catch_warnings(record=True) as w: # Cause all warnings to always be triggered. warnings.simplefilter("always") - strategy.advise_enter(indicators, {'pair': 'ETH/BTC'}, is_short=False) # TODO-lev + strategy.advise_buy(indicators, {'pair': 'ETH/BTC'}) assert len(w) == 1 assert issubclass(w[-1].category, DeprecationWarning) assert "deprecated - check out the Sample strategy to see the current function headers!" \ @@ -361,7 +357,7 @@ def test_deprecate_populate_indicators(result, default_conf): with warnings.catch_warnings(record=True) as w: # Cause all warnings to always be triggered. warnings.simplefilter("always") - strategy.advise_exit(indicators, {'pair': 'ETH_BTC'}, is_short=False) # TODO-lev + strategy.advise_sell(indicators, {'pair': 'ETH_BTC'}) assert len(w) == 1 assert issubclass(w[-1].category, DeprecationWarning) assert "deprecated - check out the Sample strategy to see the current function headers!" \ @@ -381,8 +377,6 @@ def test_call_deprecated_function(result, monkeypatch, default_conf, caplog): assert strategy._populate_fun_len == 2 assert strategy._buy_fun_len == 2 assert strategy._sell_fun_len == 2 - # assert strategy._short_fun_len == 2 - # assert strategy._exit_short_fun_len == 2 assert strategy.INTERFACE_VERSION == 1 assert strategy.timeframe == '5m' assert strategy.ticker_interval == '5m' @@ -391,22 +385,14 @@ def test_call_deprecated_function(result, monkeypatch, default_conf, caplog): assert isinstance(indicator_df, DataFrame) assert 'adx' in indicator_df.columns - buydf = strategy.advise_enter(result, metadata=metadata, is_short=False) + buydf = strategy.advise_buy(result, metadata=metadata) assert isinstance(buydf, DataFrame) assert 'buy' in buydf.columns - selldf = strategy.advise_exit(result, metadata=metadata, is_short=False) + selldf = strategy.advise_sell(result, metadata=metadata) assert isinstance(selldf, DataFrame) assert 'sell' in selldf - # shortdf = strategy.advise_enter(result, metadata=metadata, is_short=True) - # assert isinstance(shortdf, DataFrame) - # assert 'short' in shortdf.columns - - # exit_shortdf = strategy.advise_exit(result, metadata=metadata, is_short=True) - # assert isinstance(exit_shortdf, DataFrame) - # assert 'exit_short' in exit_shortdf - assert log_has("DEPRECATED: Please migrate to using 'timeframe' instead of 'ticker_interval'.", caplog) @@ -420,26 +406,18 @@ def test_strategy_interface_versioning(result, monkeypatch, default_conf): assert strategy._populate_fun_len == 3 assert strategy._buy_fun_len == 3 assert strategy._sell_fun_len == 3 - assert strategy._short_fun_len == 3 - assert strategy._exit_short_fun_len == 3 assert strategy.INTERFACE_VERSION == 2 indicator_df = strategy.advise_indicators(result, metadata=metadata) assert isinstance(indicator_df, DataFrame) assert 'adx' in indicator_df.columns - buydf = strategy.advise_enter(result, metadata=metadata, is_short=False) - assert isinstance(buydf, DataFrame) - assert 'buy' in buydf.columns + enterdf = strategy.advise_buy(result, metadata=metadata) + assert isinstance(enterdf, DataFrame) + assert 'buy' in enterdf.columns + assert 'enter_short' in enterdf.columns - selldf = strategy.advise_exit(result, metadata=metadata, is_short=False) - assert isinstance(selldf, DataFrame) - assert 'sell' in selldf - - shortdf = strategy.advise_enter(result, metadata=metadata, is_short=True) - assert isinstance(shortdf, DataFrame) - assert 'short' in shortdf.columns - - exit_shortdf = strategy.advise_exit(result, metadata=metadata, is_short=True) - assert isinstance(exit_shortdf, DataFrame) - assert 'exit_short' in exit_shortdf + exitdf = strategy.advise_sell(result, metadata=metadata) + assert isinstance(exitdf, DataFrame) + assert 'sell' in exitdf + assert 'exit_short' in exitdf.columns From dc4090234de7b49ff908479161a89ba2809345a8 Mon Sep 17 00:00:00 2001 From: Sam Germain Date: Wed, 18 Aug 2021 12:43:44 -0600 Subject: [PATCH 03/31] Added interface leverage method --- freqtrade/strategy/interface.py | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/freqtrade/strategy/interface.py b/freqtrade/strategy/interface.py index b56a54d14..3f886b5a6 100644 --- a/freqtrade/strategy/interface.py +++ b/freqtrade/strategy/interface.py @@ -816,19 +816,3 @@ class IStrategy(ABC, HyperStrategyMixin): return self.populate_sell_trend(dataframe) # type: ignore else: return self.populate_sell_trend(dataframe, metadata) - - def leverage(self, pair: str, current_time: datetime, current_rate: float, - proposed_leverage: float, max_leverage: float, - **kwargs) -> float: - """ - Customize leverage for each new trade. This method is not called when edge module is - enabled. - - :param pair: Pair that's currently analyzed - :param current_time: datetime object, containing the current datetime - :param current_rate: Rate, calculated based on pricing settings in ask_strategy. - :param proposed_leverage: A leverage proposed by the bot. - :param max_leverage: Max leverage allowed on this pair - :return: A stake size, which is between min_stake and max_stake. - """ - return proposed_leverage From 55c070f1bb3ed63871e74883c418c88717d1d168 Mon Sep 17 00:00:00 2001 From: Sam Germain Date: Wed, 18 Aug 2021 12:43:44 -0600 Subject: [PATCH 04/31] Added interface leverage method --- freqtrade/strategy/interface.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/freqtrade/strategy/interface.py b/freqtrade/strategy/interface.py index 3f886b5a6..21d0c70ae 100644 --- a/freqtrade/strategy/interface.py +++ b/freqtrade/strategy/interface.py @@ -816,3 +816,19 @@ class IStrategy(ABC, HyperStrategyMixin): return self.populate_sell_trend(dataframe) # type: ignore else: return self.populate_sell_trend(dataframe, metadata) + + def leverage(self, pair: str, current_time: datetime, current_rate: float, + proposed_leverage: float, max_leverage: float, + **kwargs) -> float: + """ + Customize leverage for each new trade. This method is not called when edge module is + enabled. + + :param pair: Pair that's currently analyzed + :param current_time: datetime object, containing the current datetime + :param current_rate: Rate, calculated based on pricing settings in ask_strategy. + :param proposed_leverage: A leverage proposed by the bot. + :param max_leverage: Max leverage allowed on this pair + :return: A leverage amount, which is between 1.0 and max_leverage. + """ + return 1.0 From 8644449c33b12f11d0f652ea309b8175481372bc Mon Sep 17 00:00:00 2001 From: Sam Germain Date: Sun, 22 Aug 2021 21:38:15 -0600 Subject: [PATCH 05/31] Removed changes from tests/strategy/strats that hyperopted short parameters, because these are supposed to be legacy tests --- tests/strategy/strats/default_strategy.py | 29 ------------ .../strategy/strats/hyperoptable_strategy.py | 44 ------------------- tests/strategy/strats/legacy_strategy.py | 1 - tests/strategy/test_interface.py | 7 ++- tests/strategy/test_strategy_loading.py | 4 -- 5 files changed, 3 insertions(+), 82 deletions(-) diff --git a/tests/strategy/strats/default_strategy.py b/tests/strategy/strats/default_strategy.py index be373e0ee..7171b93ae 100644 --- a/tests/strategy/strats/default_strategy.py +++ b/tests/strategy/strats/default_strategy.py @@ -130,19 +130,6 @@ class DefaultStrategy(IStrategy): ), 'buy'] = 1 - dataframe.loc[ - ( - (dataframe['rsi'] > 65) & - (dataframe['fastd'] > 65) & - (dataframe['adx'] < 70) & - (dataframe['plus_di'] < 0.5) # TODO-lev: What to do here - ) | - ( - (dataframe['adx'] < 35) & - (dataframe['plus_di'] < 0.5) # TODO-lev: What to do here - ), - 'enter_short'] = 1 - return dataframe def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: @@ -166,20 +153,4 @@ class DefaultStrategy(IStrategy): (dataframe['minus_di'] > 0.5) ), 'sell'] = 1 - - dataframe.loc[ - ( - ( - (qtpylib.crossed_below(dataframe['rsi'], 30)) | - (qtpylib.crossed_below(dataframe['fastd'], 30)) - ) & - (dataframe['adx'] < 90) & - (dataframe['minus_di'] < 0) # TODO-lev: what to do here - ) | - ( - (dataframe['adx'] > 30) & - (dataframe['minus_di'] < 0.5) # TODO-lev: what to do here - ), - 'exit_short'] = 1 - return dataframe diff --git a/tests/strategy/strats/hyperoptable_strategy.py b/tests/strategy/strats/hyperoptable_strategy.py index e45ba03f0..1126bd6cf 100644 --- a/tests/strategy/strats/hyperoptable_strategy.py +++ b/tests/strategy/strats/hyperoptable_strategy.py @@ -60,15 +60,6 @@ class HyperoptableStrategy(IStrategy): 'sell_minusdi': 0.4 } - enter_short_params = { - 'short_rsi': 65, - } - - exit_short_params = { - 'exit_short_rsi': 26, - 'exit_short_minusdi': 0.6 - } - buy_rsi = IntParameter([0, 50], default=30, space='buy') buy_plusdi = RealParameter(low=0, high=1, default=0.5, space='buy') sell_rsi = IntParameter(low=50, high=100, default=70, space='sell') @@ -87,12 +78,6 @@ class HyperoptableStrategy(IStrategy): }) return prot - enter_short_rsi = IntParameter([50, 100], default=70, space='sell') - enter_short_plusdi = RealParameter(low=0, high=1, default=0.5, space='sell') - exit_short_rsi = IntParameter(low=0, high=50, default=30, space='buy') - exit_short_minusdi = DecimalParameter(low=0, high=1, default=0.4999, decimals=3, space='buy', - load=False) - def informative_pairs(self): """ Define additional, informative pair/interval combinations to be cached from the exchange. @@ -175,19 +160,6 @@ class HyperoptableStrategy(IStrategy): ), 'buy'] = 1 - dataframe.loc[ - ( - (dataframe['rsi'] > self.enter_short_rsi.value) & - (dataframe['fastd'] > 65) & - (dataframe['adx'] < 70) & - (dataframe['plus_di'] < self.enter_short_plusdi.value) - ) | - ( - (dataframe['adx'] < 35) & - (dataframe['plus_di'] < self.enter_short_plusdi.value) - ), - 'enter_short'] = 1 - return dataframe def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: @@ -211,20 +183,4 @@ class HyperoptableStrategy(IStrategy): (dataframe['minus_di'] > self.sell_minusdi.value) ), 'sell'] = 1 - - dataframe.loc[ - ( - ( - (qtpylib.crossed_below(dataframe['rsi'], self.exit_short_rsi.value)) | - (qtpylib.crossed_below(dataframe['fastd'], 30)) - ) & - (dataframe['adx'] < 90) & - (dataframe['minus_di'] < 0) # TODO-lev: What should this be - ) | - ( - (dataframe['adx'] < 30) & - (dataframe['minus_di'] < self.exit_short_minusdi.value) - ), - 'exit_short'] = 1 - return dataframe diff --git a/tests/strategy/strats/legacy_strategy.py b/tests/strategy/strats/legacy_strategy.py index 20f24d6a3..9ef00b110 100644 --- a/tests/strategy/strats/legacy_strategy.py +++ b/tests/strategy/strats/legacy_strategy.py @@ -84,5 +84,4 @@ class TestStrategyLegacy(IStrategy): (dataframe['volume'] > 0) ), 'sell'] = 1 - return dataframe diff --git a/tests/strategy/test_interface.py b/tests/strategy/test_interface.py index 958f4ebed..5aa18c7db 100644 --- a/tests/strategy/test_interface.py +++ b/tests/strategy/test_interface.py @@ -747,11 +747,10 @@ def test_auto_hyperopt_interface(default_conf): assert strategy.sell_minusdi.value == 0.5 all_params = strategy.detect_all_parameters() assert isinstance(all_params, dict) - # TODO-lev: Should these be 4,4 and 10? - assert len(all_params['buy']) == 4 - assert len(all_params['sell']) == 4 + assert len(all_params['buy']) == 2 + assert len(all_params['sell']) == 2 # Number of Hyperoptable parameters - assert all_params['count'] == 10 + assert all_params['count'] == 6 strategy.__class__.sell_rsi = IntParameter([0, 10], default=5, space='buy') diff --git a/tests/strategy/test_strategy_loading.py b/tests/strategy/test_strategy_loading.py index 73c7cb5f7..1c846ec13 100644 --- a/tests/strategy/test_strategy_loading.py +++ b/tests/strategy/test_strategy_loading.py @@ -119,11 +119,9 @@ def test_strategy(result, default_conf): dataframe = strategy.advise_buy(df_indicators, metadata=metadata) assert 'buy' in dataframe.columns - assert 'enter_short' in dataframe.columns dataframe = strategy.advise_sell(df_indicators, metadata=metadata) assert 'sell' in dataframe.columns - assert 'exit_short' in dataframe.columns def test_strategy_override_minimal_roi(caplog, default_conf): @@ -415,9 +413,7 @@ def test_strategy_interface_versioning(result, monkeypatch, default_conf): enterdf = strategy.advise_buy(result, metadata=metadata) assert isinstance(enterdf, DataFrame) assert 'buy' in enterdf.columns - assert 'enter_short' in enterdf.columns exitdf = strategy.advise_sell(result, metadata=metadata) assert isinstance(exitdf, DataFrame) assert 'sell' in exitdf - assert 'exit_short' in exitdf.columns From 9f6b6f04b4fa953a990ad575b511f33dc05699c1 Mon Sep 17 00:00:00 2001 From: Sam Germain Date: Sun, 22 Aug 2021 23:55:34 -0600 Subject: [PATCH 06/31] Added False to self.strategy.get_signal --- freqtrade/freqtradebot.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 179c99d2c..050818c13 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -423,7 +423,8 @@ class FreqtradeBot(LoggingMixin): (buy, sell, buy_tag) = self.strategy.get_signal( pair, self.strategy.timeframe, - analyzed_df + analyzed_df, + False ) if buy and not sell: From 0afeb269ad1beff0ca9fbc809e34cf390d3d001d Mon Sep 17 00:00:00 2001 From: Sam Germain Date: Mon, 23 Aug 2021 00:15:35 -0600 Subject: [PATCH 07/31] Removed unnecessary TODOs --- freqtrade/strategy/hyper.py | 2 -- tests/strategy/test_strategy_loading.py | 1 - 2 files changed, 3 deletions(-) diff --git a/freqtrade/strategy/hyper.py b/freqtrade/strategy/hyper.py index 87d4241f1..dad282d7e 100644 --- a/freqtrade/strategy/hyper.py +++ b/freqtrade/strategy/hyper.py @@ -22,8 +22,6 @@ from freqtrade.exceptions import OperationalException logger = logging.getLogger(__name__) -# TODO-lev: This file - class BaseParameter(ABC): """ diff --git a/tests/strategy/test_strategy_loading.py b/tests/strategy/test_strategy_loading.py index 1c846ec13..e76990ba9 100644 --- a/tests/strategy/test_strategy_loading.py +++ b/tests/strategy/test_strategy_loading.py @@ -218,7 +218,6 @@ def test_strategy_override_process_only_new_candles(caplog, default_conf): def test_strategy_override_order_types(caplog, default_conf): caplog.set_level(logging.INFO) - # TODO-lev: Maybe change order_types = { 'buy': 'market', 'sell': 'limit', From 53b51ce8cfd4cd0bf318f130697b07f8bd62ee3c Mon Sep 17 00:00:00 2001 From: Sam Germain Date: Mon, 23 Aug 2021 00:17:20 -0600 Subject: [PATCH 08/31] Reverted freqtrade/templates/sample_strategy back to no shorting, and created a separate sample short strategy --- freqtrade/templates/sample_short_strategy.py | 379 +++++++++++++++++++ freqtrade/templates/sample_strategy.py | 24 -- 2 files changed, 379 insertions(+), 24 deletions(-) create mode 100644 freqtrade/templates/sample_short_strategy.py diff --git a/freqtrade/templates/sample_short_strategy.py b/freqtrade/templates/sample_short_strategy.py new file mode 100644 index 000000000..bdd0054e8 --- /dev/null +++ b/freqtrade/templates/sample_short_strategy.py @@ -0,0 +1,379 @@ +# pragma pylint: disable=missing-docstring, invalid-name, pointless-string-statement +# flake8: noqa: F401 +# isort: skip_file +# --- Do not remove these libs --- +import numpy as np # noqa +import pandas as pd # noqa +from pandas import DataFrame + +from freqtrade.strategy import (BooleanParameter, CategoricalParameter, DecimalParameter, + IStrategy, IntParameter) + +# -------------------------------- +# Add your lib to import here +import talib.abstract as ta +import freqtrade.vendor.qtpylib.indicators as qtpylib + + +# This class is a sample. Feel free to customize it. +class SampleStrategy(IStrategy): + """ + This is a sample strategy to inspire you. + More information in https://www.freqtrade.io/en/latest/strategy-customization/ + + You can: + :return: a Dataframe with all mandatory indicators for the strategies + - Rename the class name (Do not forget to update class_name) + - Add any methods you want to build your strategy + - Add any lib you need to build your strategy + + You must keep: + - the lib in the section "Do not remove these libs" + - the methods: populate_indicators, populate_buy_trend, populate_sell_trend + You should keep: + - timeframe, minimal_roi, stoploss, trailing_* + """ + # Strategy interface version - allow new iterations of the strategy interface. + # Check the documentation or the Sample strategy to get the latest version. + INTERFACE_VERSION = 2 + + # Minimal ROI designed for the strategy. + # This attribute will be overridden if the config file contains "minimal_roi". + minimal_roi = { + "60": 0.01, + "30": 0.02, + "0": 0.04 + } + + # Optimal stoploss designed for the strategy. + # This attribute will be overridden if the config file contains "stoploss". + stoploss = -0.10 + + # Trailing stoploss + trailing_stop = False + # trailing_only_offset_is_reached = False + # trailing_stop_positive = 0.01 + # trailing_stop_positive_offset = 0.0 # Disabled / not configured + + # Hyperoptable parameters + short_rsi = IntParameter(low=51, high=100, default=70, space='sell', optimize=True, load=True) + exit_short_rsi = IntParameter(low=1, high=50, default=30, space='buy', optimize=True, load=True) + + # Optimal timeframe for the strategy. + timeframe = '5m' + + # Run "populate_indicators()" only for new candle. + process_only_new_candles = False + + # These values can be overridden in the "ask_strategy" section in the config. + use_sell_signal = True + sell_profit_only = False + ignore_roi_if_buy_signal = False + + # Number of candles the strategy requires before producing valid signals + startup_candle_count: int = 30 + + # Optional order type mapping. + order_types = { + 'buy': 'limit', + 'sell': 'limit', + 'stoploss': 'market', + 'stoploss_on_exchange': False + } + + # Optional order time in force. + order_time_in_force = { + 'buy': 'gtc', + 'sell': 'gtc' + } + + plot_config = { + 'main_plot': { + 'tema': {}, + 'sar': {'color': 'white'}, + }, + 'subplots': { + "MACD": { + 'macd': {'color': 'blue'}, + 'macdsignal': {'color': 'orange'}, + }, + "RSI": { + 'rsi': {'color': 'red'}, + } + } + } + + def informative_pairs(self): + """ + Define additional, informative pair/interval combinations to be cached from the exchange. + These pair/interval combinations are non-tradeable, unless they are part + of the whitelist as well. + For more information, please consult the documentation + :return: List of tuples in the format (pair, interval) + Sample: return [("ETH/USDT", "5m"), + ("BTC/USDT", "15m"), + ] + """ + return [] + + def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame: + """ + Adds several different TA indicators to the given DataFrame + + Performance Note: For the best performance be frugal on the number of indicators + you are using. Let uncomment only the indicator you are using in your strategies + or your hyperopt configuration, otherwise you will waste your memory and CPU usage. + :param dataframe: Dataframe with data from the exchange + :param metadata: Additional information, like the currently traded pair + :return: a Dataframe with all mandatory indicators for the strategies + """ + + # Momentum Indicators + # ------------------------------------ + + # ADX + dataframe['adx'] = ta.ADX(dataframe) + + # # Plus Directional Indicator / Movement + # dataframe['plus_dm'] = ta.PLUS_DM(dataframe) + # dataframe['plus_di'] = ta.PLUS_DI(dataframe) + + # # Minus Directional Indicator / Movement + # dataframe['minus_dm'] = ta.MINUS_DM(dataframe) + # dataframe['minus_di'] = ta.MINUS_DI(dataframe) + + # # Aroon, Aroon Oscillator + # aroon = ta.AROON(dataframe) + # dataframe['aroonup'] = aroon['aroonup'] + # dataframe['aroondown'] = aroon['aroondown'] + # dataframe['aroonosc'] = ta.AROONOSC(dataframe) + + # # Awesome Oscillator + # dataframe['ao'] = qtpylib.awesome_oscillator(dataframe) + + # # Keltner Channel + # keltner = qtpylib.keltner_channel(dataframe) + # dataframe["kc_upperband"] = keltner["upper"] + # dataframe["kc_lowerband"] = keltner["lower"] + # dataframe["kc_middleband"] = keltner["mid"] + # dataframe["kc_percent"] = ( + # (dataframe["close"] - dataframe["kc_lowerband"]) / + # (dataframe["kc_upperband"] - dataframe["kc_lowerband"]) + # ) + # dataframe["kc_width"] = ( + # (dataframe["kc_upperband"] - dataframe["kc_lowerband"]) / dataframe["kc_middleband"] + # ) + + # # Ultimate Oscillator + # dataframe['uo'] = ta.ULTOSC(dataframe) + + # # Commodity Channel Index: values [Oversold:-100, Overbought:100] + # dataframe['cci'] = ta.CCI(dataframe) + + # RSI + dataframe['rsi'] = ta.RSI(dataframe) + + # # Inverse Fisher transform on RSI: values [-1.0, 1.0] (https://goo.gl/2JGGoy) + # rsi = 0.1 * (dataframe['rsi'] - 50) + # dataframe['fisher_rsi'] = (np.exp(2 * rsi) - 1) / (np.exp(2 * rsi) + 1) + + # # Inverse Fisher transform on RSI normalized: values [0.0, 100.0] (https://goo.gl/2JGGoy) + # dataframe['fisher_rsi_norma'] = 50 * (dataframe['fisher_rsi'] + 1) + + # # Stochastic Slow + # stoch = ta.STOCH(dataframe) + # dataframe['slowd'] = stoch['slowd'] + # dataframe['slowk'] = stoch['slowk'] + + # Stochastic Fast + stoch_fast = ta.STOCHF(dataframe) + dataframe['fastd'] = stoch_fast['fastd'] + dataframe['fastk'] = stoch_fast['fastk'] + + # # Stochastic RSI + # Please read https://github.com/freqtrade/freqtrade/issues/2961 before using this. + # STOCHRSI is NOT aligned with tradingview, which may result in non-expected results. + # stoch_rsi = ta.STOCHRSI(dataframe) + # dataframe['fastd_rsi'] = stoch_rsi['fastd'] + # dataframe['fastk_rsi'] = stoch_rsi['fastk'] + + # MACD + macd = ta.MACD(dataframe) + dataframe['macd'] = macd['macd'] + dataframe['macdsignal'] = macd['macdsignal'] + dataframe['macdhist'] = macd['macdhist'] + + # MFI + dataframe['mfi'] = ta.MFI(dataframe) + + # # ROC + # dataframe['roc'] = ta.ROC(dataframe) + + # Overlap Studies + # ------------------------------------ + + # Bollinger Bands + bollinger = qtpylib.bollinger_bands(qtpylib.typical_price(dataframe), window=20, stds=2) + dataframe['bb_lowerband'] = bollinger['lower'] + dataframe['bb_middleband'] = bollinger['mid'] + dataframe['bb_upperband'] = bollinger['upper'] + dataframe["bb_percent"] = ( + (dataframe["close"] - dataframe["bb_lowerband"]) / + (dataframe["bb_upperband"] - dataframe["bb_lowerband"]) + ) + dataframe["bb_width"] = ( + (dataframe["bb_upperband"] - dataframe["bb_lowerband"]) / dataframe["bb_middleband"] + ) + + # Bollinger Bands - Weighted (EMA based instead of SMA) + # weighted_bollinger = qtpylib.weighted_bollinger_bands( + # qtpylib.typical_price(dataframe), window=20, stds=2 + # ) + # dataframe["wbb_upperband"] = weighted_bollinger["upper"] + # dataframe["wbb_lowerband"] = weighted_bollinger["lower"] + # dataframe["wbb_middleband"] = weighted_bollinger["mid"] + # dataframe["wbb_percent"] = ( + # (dataframe["close"] - dataframe["wbb_lowerband"]) / + # (dataframe["wbb_upperband"] - dataframe["wbb_lowerband"]) + # ) + # dataframe["wbb_width"] = ( + # (dataframe["wbb_upperband"] - dataframe["wbb_lowerband"]) / + # dataframe["wbb_middleband"] + # ) + + # # EMA - Exponential Moving Average + # dataframe['ema3'] = ta.EMA(dataframe, timeperiod=3) + # dataframe['ema5'] = ta.EMA(dataframe, timeperiod=5) + # dataframe['ema10'] = ta.EMA(dataframe, timeperiod=10) + # dataframe['ema21'] = ta.EMA(dataframe, timeperiod=21) + # dataframe['ema50'] = ta.EMA(dataframe, timeperiod=50) + # dataframe['ema100'] = ta.EMA(dataframe, timeperiod=100) + + # # SMA - Simple Moving Average + # dataframe['sma3'] = ta.SMA(dataframe, timeperiod=3) + # dataframe['sma5'] = ta.SMA(dataframe, timeperiod=5) + # dataframe['sma10'] = ta.SMA(dataframe, timeperiod=10) + # dataframe['sma21'] = ta.SMA(dataframe, timeperiod=21) + # dataframe['sma50'] = ta.SMA(dataframe, timeperiod=50) + # dataframe['sma100'] = ta.SMA(dataframe, timeperiod=100) + + # Parabolic SAR + dataframe['sar'] = ta.SAR(dataframe) + + # TEMA - Triple Exponential Moving Average + dataframe['tema'] = ta.TEMA(dataframe, timeperiod=9) + + # Cycle Indicator + # ------------------------------------ + # Hilbert Transform Indicator - SineWave + hilbert = ta.HT_SINE(dataframe) + dataframe['htsine'] = hilbert['sine'] + dataframe['htleadsine'] = hilbert['leadsine'] + + # Pattern Recognition - Bullish candlestick patterns + # ------------------------------------ + # # Hammer: values [0, 100] + # dataframe['CDLHAMMER'] = ta.CDLHAMMER(dataframe) + # # Inverted Hammer: values [0, 100] + # dataframe['CDLINVERTEDHAMMER'] = ta.CDLINVERTEDHAMMER(dataframe) + # # Dragonfly Doji: values [0, 100] + # dataframe['CDLDRAGONFLYDOJI'] = ta.CDLDRAGONFLYDOJI(dataframe) + # # Piercing Line: values [0, 100] + # dataframe['CDLPIERCING'] = ta.CDLPIERCING(dataframe) # values [0, 100] + # # Morningstar: values [0, 100] + # dataframe['CDLMORNINGSTAR'] = ta.CDLMORNINGSTAR(dataframe) # values [0, 100] + # # Three White Soldiers: values [0, 100] + # dataframe['CDL3WHITESOLDIERS'] = ta.CDL3WHITESOLDIERS(dataframe) # values [0, 100] + + # Pattern Recognition - Bearish candlestick patterns + # ------------------------------------ + # # Hanging Man: values [0, 100] + # dataframe['CDLHANGINGMAN'] = ta.CDLHANGINGMAN(dataframe) + # # Shooting Star: values [0, 100] + # dataframe['CDLSHOOTINGSTAR'] = ta.CDLSHOOTINGSTAR(dataframe) + # # Gravestone Doji: values [0, 100] + # dataframe['CDLGRAVESTONEDOJI'] = ta.CDLGRAVESTONEDOJI(dataframe) + # # Dark Cloud Cover: values [0, 100] + # dataframe['CDLDARKCLOUDCOVER'] = ta.CDLDARKCLOUDCOVER(dataframe) + # # Evening Doji Star: values [0, 100] + # dataframe['CDLEVENINGDOJISTAR'] = ta.CDLEVENINGDOJISTAR(dataframe) + # # Evening Star: values [0, 100] + # dataframe['CDLEVENINGSTAR'] = ta.CDLEVENINGSTAR(dataframe) + + # Pattern Recognition - Bullish/Bearish candlestick patterns + # ------------------------------------ + # # Three Line Strike: values [0, -100, 100] + # dataframe['CDL3LINESTRIKE'] = ta.CDL3LINESTRIKE(dataframe) + # # Spinning Top: values [0, -100, 100] + # dataframe['CDLSPINNINGTOP'] = ta.CDLSPINNINGTOP(dataframe) # values [0, -100, 100] + # # Engulfing: values [0, -100, 100] + # dataframe['CDLENGULFING'] = ta.CDLENGULFING(dataframe) # values [0, -100, 100] + # # Harami: values [0, -100, 100] + # dataframe['CDLHARAMI'] = ta.CDLHARAMI(dataframe) # values [0, -100, 100] + # # Three Outside Up/Down: values [0, -100, 100] + # dataframe['CDL3OUTSIDE'] = ta.CDL3OUTSIDE(dataframe) # values [0, -100, 100] + # # Three Inside Up/Down: values [0, -100, 100] + # dataframe['CDL3INSIDE'] = ta.CDL3INSIDE(dataframe) # values [0, -100, 100] + + # # Chart type + # # ------------------------------------ + # # Heikin Ashi Strategy + # heikinashi = qtpylib.heikinashi(dataframe) + # dataframe['ha_open'] = heikinashi['open'] + # dataframe['ha_close'] = heikinashi['close'] + # dataframe['ha_high'] = heikinashi['high'] + # dataframe['ha_low'] = heikinashi['low'] + + # Retrieve best bid and best ask from the orderbook + # ------------------------------------ + """ + # first check if dataprovider is available + if self.dp: + if self.dp.runmode.value in ('live', 'dry_run'): + ob = self.dp.orderbook(metadata['pair'], 1) + dataframe['best_bid'] = ob['bids'][0][0] + dataframe['best_ask'] = ob['asks'][0][0] + """ + + return dataframe + + def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: + """ + Based on TA indicators, populates the buy signal for the given dataframe + :param dataframe: DataFrame populated with indicators + :param metadata: Additional information, like the currently traded pair + :return: DataFrame with buy column + """ + + dataframe.loc[ + ( + # Signal: RSI crosses above 70 + (qtpylib.crossed_above(dataframe['rsi'], self.short_rsi.value)) & + (dataframe['tema'] > dataframe['bb_middleband']) & # Guard: tema above BB middle + (dataframe['tema'] < dataframe['tema'].shift(1)) & # Guard: tema is falling + (dataframe['volume'] > 0) # Make sure Volume is not 0 + ), + 'enter_short'] = 1 + + return dataframe + + def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: + """ + Based on TA indicators, populates the sell signal for the given dataframe + :param dataframe: DataFrame populated with indicators + :param metadata: Additional information, like the currently traded pair + :return: DataFrame with sell column + """ + + dataframe.loc[ + ( + # Signal: RSI crosses above 30 + (qtpylib.crossed_above(dataframe['rsi'], self.exit_short_rsi.value)) & + # Guard: tema below BB middle + (dataframe['tema'] <= dataframe['bb_middleband']) & + (dataframe['tema'] > dataframe['tema'].shift(1)) & # Guard: tema is raising + (dataframe['volume'] > 0) # Make sure Volume is not 0 + ), + 'exit_short'] = 1 + + return dataframe diff --git a/freqtrade/templates/sample_strategy.py b/freqtrade/templates/sample_strategy.py index b2d130059..574819949 100644 --- a/freqtrade/templates/sample_strategy.py +++ b/freqtrade/templates/sample_strategy.py @@ -58,8 +58,6 @@ class SampleStrategy(IStrategy): # Hyperoptable parameters buy_rsi = IntParameter(low=1, high=50, default=30, space='buy', optimize=True, load=True) sell_rsi = IntParameter(low=50, high=100, default=70, space='sell', optimize=True, load=True) - short_rsi = IntParameter(low=51, high=100, default=70, space='sell', optimize=True, load=True) - exit_short_rsi = IntParameter(low=1, high=50, default=30, space='buy', optimize=True, load=True) # Optimal timeframe for the strategy. timeframe = '5m' @@ -356,16 +354,6 @@ class SampleStrategy(IStrategy): ), 'buy'] = 1 - dataframe.loc[ - ( - # Signal: RSI crosses above 70 - (qtpylib.crossed_above(dataframe['rsi'], self.short_rsi.value)) & - (dataframe['tema'] > dataframe['bb_middleband']) & # Guard: tema above BB middle - (dataframe['tema'] < dataframe['tema'].shift(1)) & # Guard: tema is falling - (dataframe['volume'] > 0) # Make sure Volume is not 0 - ), - 'enter_short'] = 1 - return dataframe def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: @@ -384,16 +372,4 @@ class SampleStrategy(IStrategy): (dataframe['volume'] > 0) # Make sure Volume is not 0 ), 'sell'] = 1 - - dataframe.loc[ - ( - # Signal: RSI crosses above 30 - (qtpylib.crossed_above(dataframe['rsi'], self.exit_short_rsi.value)) & - # Guard: tema below BB middle - (dataframe['tema'] <= dataframe['bb_middleband']) & - (dataframe['tema'] > dataframe['tema'].shift(1)) & # Guard: tema is raising - (dataframe['volume'] > 0) # Make sure Volume is not 0 - ), - 'exit_short'] = 1 - return dataframe From 61ad38500a903f82015c11bc9a2d7524f30d5eab Mon Sep 17 00:00:00 2001 From: Sam Germain Date: Mon, 23 Aug 2021 00:18:15 -0600 Subject: [PATCH 09/31] Reverted freqtrade/templates/*hyperopt* files back to no shorting --- freqtrade/templates/sample_hyperopt.py | 48 --------------- .../templates/sample_hyperopt_advanced.py | 58 +------------------ 2 files changed, 2 insertions(+), 104 deletions(-) diff --git a/freqtrade/templates/sample_hyperopt.py b/freqtrade/templates/sample_hyperopt.py index ca72e3740..7ed726d7a 100644 --- a/freqtrade/templates/sample_hyperopt.py +++ b/freqtrade/templates/sample_hyperopt.py @@ -55,10 +55,6 @@ class SampleHyperOpt(IHyperOpt): Integer(15, 45, name='fastd-value'), Integer(20, 50, name='adx-value'), Integer(20, 40, name='rsi-value'), - Integer(75, 90, name='short-mfi-value'), - Integer(55, 85, name='short-fastd-value'), - Integer(50, 80, name='short-adx-value'), - Integer(60, 80, name='short-rsi-value'), Categorical([True, False], name='mfi-enabled'), Categorical([True, False], name='fastd-enabled'), Categorical([True, False], name='adx-enabled'), @@ -76,60 +72,40 @@ class SampleHyperOpt(IHyperOpt): Buy strategy Hyperopt will build and use. """ long_conditions = [] - short_conditions = [] # GUARDS AND TRENDS if 'mfi-enabled' in params and params['mfi-enabled']: long_conditions.append(dataframe['mfi'] < params['mfi-value']) - short_conditions.append(dataframe['mfi'] > params['short-mfi-value']) if 'fastd-enabled' in params and params['fastd-enabled']: long_conditions.append(dataframe['fastd'] < params['fastd-value']) - short_conditions.append(dataframe['fastd'] > params['short-fastd-value']) if 'adx-enabled' in params and params['adx-enabled']: long_conditions.append(dataframe['adx'] > params['adx-value']) - short_conditions.append(dataframe['adx'] < params['short-adx-value']) if 'rsi-enabled' in params and params['rsi-enabled']: long_conditions.append(dataframe['rsi'] < params['rsi-value']) - short_conditions.append(dataframe['rsi'] > params['short-rsi-value']) # TRIGGERS if 'trigger' in params: if params['trigger'] == 'boll': long_conditions.append(dataframe['close'] < dataframe['bb_lowerband']) - short_conditions.append(dataframe['close'] > dataframe['bb_upperband']) if params['trigger'] == 'macd_cross_signal': long_conditions.append(qtpylib.crossed_above( dataframe['macd'], dataframe['macdsignal'] )) - short_conditions.append(qtpylib.crossed_below( - dataframe['macd'], - dataframe['macdsignal'] - )) if params['trigger'] == 'sar_reversal': long_conditions.append(qtpylib.crossed_above( dataframe['close'], dataframe['sar'] )) - short_conditions.append(qtpylib.crossed_below( - dataframe['close'], - dataframe['sar'] - )) # Check that volume is not 0 long_conditions.append(dataframe['volume'] > 0) - short_conditions.append(dataframe['volume'] > 0) if long_conditions: dataframe.loc[ reduce(lambda x, y: x & y, long_conditions), 'buy'] = 1 - if short_conditions: - dataframe.loc[ - reduce(lambda x, y: x & y, short_conditions), - 'enter_short'] = 1 - return dataframe return populate_buy_trend @@ -144,10 +120,6 @@ class SampleHyperOpt(IHyperOpt): Integer(50, 100, name='sell-fastd-value'), Integer(50, 100, name='sell-adx-value'), Integer(60, 100, name='sell-rsi-value'), - Integer(1, 25, name='exit-short-mfi-value'), - Integer(1, 50, name='exit-short-fastd-value'), - Integer(1, 50, name='exit-short-adx-value'), - Integer(1, 40, name='exit-short-rsi-value'), Categorical([True, False], name='sell-mfi-enabled'), Categorical([True, False], name='sell-fastd-enabled'), Categorical([True, False], name='sell-adx-enabled'), @@ -169,60 +141,40 @@ class SampleHyperOpt(IHyperOpt): Sell strategy Hyperopt will build and use. """ exit_long_conditions = [] - exit_short_conditions = [] # GUARDS AND TRENDS if 'sell-mfi-enabled' in params and params['sell-mfi-enabled']: exit_long_conditions.append(dataframe['mfi'] > params['sell-mfi-value']) - exit_short_conditions.append(dataframe['mfi'] < params['exit-short-mfi-value']) if 'sell-fastd-enabled' in params and params['sell-fastd-enabled']: exit_long_conditions.append(dataframe['fastd'] > params['sell-fastd-value']) - exit_short_conditions.append(dataframe['fastd'] < params['exit-short-fastd-value']) if 'sell-adx-enabled' in params and params['sell-adx-enabled']: exit_long_conditions.append(dataframe['adx'] < params['sell-adx-value']) - exit_short_conditions.append(dataframe['adx'] > params['exit-short-adx-value']) if 'sell-rsi-enabled' in params and params['sell-rsi-enabled']: exit_long_conditions.append(dataframe['rsi'] > params['sell-rsi-value']) - exit_short_conditions.append(dataframe['rsi'] < params['exit-short-rsi-value']) # TRIGGERS if 'sell-trigger' in params: if params['sell-trigger'] == 'sell-boll': exit_long_conditions.append(dataframe['close'] > dataframe['bb_upperband']) - exit_short_conditions.append(dataframe['close'] < dataframe['bb_lowerband']) if params['sell-trigger'] == 'sell-macd_cross_signal': exit_long_conditions.append(qtpylib.crossed_above( dataframe['macdsignal'], dataframe['macd'] )) - exit_short_conditions.append(qtpylib.crossed_below( - dataframe['macdsignal'], - dataframe['macd'] - )) if params['sell-trigger'] == 'sell-sar_reversal': exit_long_conditions.append(qtpylib.crossed_above( dataframe['sar'], dataframe['close'] )) - exit_short_conditions.append(qtpylib.crossed_below( - dataframe['sar'], - dataframe['close'] - )) # Check that volume is not 0 exit_long_conditions.append(dataframe['volume'] > 0) - exit_short_conditions.append(dataframe['volume'] > 0) if exit_long_conditions: dataframe.loc[ reduce(lambda x, y: x & y, exit_long_conditions), 'sell'] = 1 - if exit_short_conditions: - dataframe.loc[ - reduce(lambda x, y: x & y, exit_short_conditions), - 'exit_short'] = 1 - return dataframe return populate_sell_trend diff --git a/freqtrade/templates/sample_hyperopt_advanced.py b/freqtrade/templates/sample_hyperopt_advanced.py index feb617aae..733f1ef3e 100644 --- a/freqtrade/templates/sample_hyperopt_advanced.py +++ b/freqtrade/templates/sample_hyperopt_advanced.py @@ -70,10 +70,6 @@ class AdvancedSampleHyperOpt(IHyperOpt): Integer(15, 45, name='fastd-value'), Integer(20, 50, name='adx-value'), Integer(20, 40, name='rsi-value'), - Integer(75, 90, name='short-mfi-value'), - Integer(55, 85, name='short-fastd-value'), - Integer(50, 80, name='short-adx-value'), - Integer(60, 80, name='short-rsi-value'), Categorical([True, False], name='mfi-enabled'), Categorical([True, False], name='fastd-enabled'), Categorical([True, False], name='adx-enabled'), @@ -91,59 +87,37 @@ class AdvancedSampleHyperOpt(IHyperOpt): Buy strategy Hyperopt will build and use """ long_conditions = [] - short_conditions = [] # GUARDS AND TRENDS if 'mfi-enabled' in params and params['mfi-enabled']: long_conditions.append(dataframe['mfi'] < params['mfi-value']) - short_conditions.append(dataframe['mfi'] > params['short-mfi-value']) if 'fastd-enabled' in params and params['fastd-enabled']: long_conditions.append(dataframe['fastd'] < params['fastd-value']) - short_conditions.append(dataframe['fastd'] > params['short-fastd-value']) if 'adx-enabled' in params and params['adx-enabled']: long_conditions.append(dataframe['adx'] > params['adx-value']) - short_conditions.append(dataframe['adx'] < params['short-adx-value']) if 'rsi-enabled' in params and params['rsi-enabled']: long_conditions.append(dataframe['rsi'] < params['rsi-value']) - short_conditions.append(dataframe['rsi'] > params['short-rsi-value']) # TRIGGERS if 'trigger' in params: if params['trigger'] == 'boll': long_conditions.append(dataframe['close'] < dataframe['bb_lowerband']) - short_conditions.append(dataframe['close'] > dataframe['bb_upperband']) if params['trigger'] == 'macd_cross_signal': long_conditions.append(qtpylib.crossed_above( - dataframe['macd'], - dataframe['macdsignal'] - )) - short_conditions.append(qtpylib.crossed_below( - dataframe['macd'], - dataframe['macdsignal'] + dataframe['macd'], dataframe['macdsignal'] )) if params['trigger'] == 'sar_reversal': long_conditions.append(qtpylib.crossed_above( - dataframe['close'], - dataframe['sar'] - )) - short_conditions.append(qtpylib.crossed_below( - dataframe['close'], - dataframe['sar'] + dataframe['close'], dataframe['sar'] )) # Check that volume is not 0 long_conditions.append(dataframe['volume'] > 0) - short_conditions.append(dataframe['volume'] > 0) if long_conditions: dataframe.loc[ reduce(lambda x, y: x & y, long_conditions), 'buy'] = 1 - if short_conditions: - dataframe.loc[ - reduce(lambda x, y: x & y, short_conditions), - 'enter_short'] = 1 - return dataframe return populate_buy_trend @@ -158,10 +132,6 @@ class AdvancedSampleHyperOpt(IHyperOpt): Integer(50, 100, name='sell-fastd-value'), Integer(50, 100, name='sell-adx-value'), Integer(60, 100, name='sell-rsi-value'), - Integer(1, 25, name='exit_short-mfi-value'), - Integer(1, 50, name='exit_short-fastd-value'), - Integer(1, 50, name='exit_short-adx-value'), - Integer(1, 40, name='exit_short-rsi-value'), Categorical([True, False], name='sell-mfi-enabled'), Categorical([True, False], name='sell-fastd-enabled'), Categorical([True, False], name='sell-adx-enabled'), @@ -183,59 +153,39 @@ class AdvancedSampleHyperOpt(IHyperOpt): """ # print(params) exit_long_conditions = [] - exit_short_conditions = [] # GUARDS AND TRENDS if 'sell-mfi-enabled' in params and params['sell-mfi-enabled']: exit_long_conditions.append(dataframe['mfi'] > params['sell-mfi-value']) - exit_short_conditions.append(dataframe['mfi'] < params['exit-short-mfi-value']) if 'sell-fastd-enabled' in params and params['sell-fastd-enabled']: exit_long_conditions.append(dataframe['fastd'] > params['sell-fastd-value']) - exit_short_conditions.append(dataframe['fastd'] < params['exit-short-fastd-value']) if 'sell-adx-enabled' in params and params['sell-adx-enabled']: exit_long_conditions.append(dataframe['adx'] < params['sell-adx-value']) - exit_short_conditions.append(dataframe['adx'] > params['exit-short-adx-value']) if 'sell-rsi-enabled' in params and params['sell-rsi-enabled']: exit_long_conditions.append(dataframe['rsi'] > params['sell-rsi-value']) - exit_short_conditions.append(dataframe['rsi'] < params['exit-short-rsi-value']) # TRIGGERS if 'sell-trigger' in params: if params['sell-trigger'] == 'sell-boll': exit_long_conditions.append(dataframe['close'] > dataframe['bb_upperband']) - exit_short_conditions.append(dataframe['close'] < dataframe['bb_lowerband']) if params['sell-trigger'] == 'sell-macd_cross_signal': exit_long_conditions.append(qtpylib.crossed_above( dataframe['macdsignal'], dataframe['macd'] )) - exit_long_conditions.append(qtpylib.crossed_below( - dataframe['macdsignal'], - dataframe['macd'] - )) if params['sell-trigger'] == 'sell-sar_reversal': exit_long_conditions.append(qtpylib.crossed_above( dataframe['sar'], dataframe['close'] )) - exit_long_conditions.append(qtpylib.crossed_below( - dataframe['sar'], - dataframe['close'] - )) # Check that volume is not 0 exit_long_conditions.append(dataframe['volume'] > 0) - exit_short_conditions.append(dataframe['volume'] > 0) if exit_long_conditions: dataframe.loc[ reduce(lambda x, y: x & y, exit_long_conditions), 'sell'] = 1 - if exit_short_conditions: - dataframe.loc[ - reduce(lambda x, y: x & y, exit_short_conditions), - 'exit_short'] = 1 - return dataframe return populate_sell_trend @@ -243,7 +193,6 @@ class AdvancedSampleHyperOpt(IHyperOpt): @staticmethod def generate_roi_table(params: Dict) -> Dict[int, float]: """ - # TODO-lev? Generate the ROI table that will be used by Hyperopt This implementation generates the default legacy Freqtrade ROI tables. @@ -265,7 +214,6 @@ class AdvancedSampleHyperOpt(IHyperOpt): @staticmethod def roi_space() -> List[Dimension]: """ - # TODO-lev? Values to search for each ROI steps Override it if you need some different ranges for the parameters in the @@ -286,7 +234,6 @@ class AdvancedSampleHyperOpt(IHyperOpt): @staticmethod def stoploss_space() -> List[Dimension]: """ - # TODO-lev? Stoploss Value to search Override it if you need some different range for the parameter in the @@ -299,7 +246,6 @@ class AdvancedSampleHyperOpt(IHyperOpt): @staticmethod def trailing_space() -> List[Dimension]: """ - # TODO-lev? Create a trailing stoploss space. You may override it in your custom Hyperopt class. From 317a454c0e179ecc138060288dc437b4e25637f1 Mon Sep 17 00:00:00 2001 From: Sam Germain Date: Mon, 23 Aug 2021 00:18:56 -0600 Subject: [PATCH 10/31] Removed shorting from tests/optimize/hyperopts/default_hyperopt.py and created another tests/optimize/hyperopts/short_hyperopt.py with long and shorting --- tests/optimize/hyperopts/default_hyperopt.py | 104 ++----- tests/optimize/hyperopts/short_hyperopt.py | 271 +++++++++++++++++++ tests/optimize/test_hyperopt.py | 16 -- 3 files changed, 291 insertions(+), 100 deletions(-) create mode 100644 tests/optimize/hyperopts/short_hyperopt.py diff --git a/tests/optimize/hyperopts/default_hyperopt.py b/tests/optimize/hyperopts/default_hyperopt.py index df39188e0..4147f475c 100644 --- a/tests/optimize/hyperopts/default_hyperopt.py +++ b/tests/optimize/hyperopts/default_hyperopt.py @@ -54,57 +54,38 @@ class DefaultHyperOpt(IHyperOpt): """ Buy strategy Hyperopt will build and use. """ - long_conditions = [] - short_conditions = [] + conditions = [] # GUARDS AND TRENDS if 'mfi-enabled' in params and params['mfi-enabled']: - long_conditions.append(dataframe['mfi'] < params['mfi-value']) - short_conditions.append(dataframe['mfi'] > params['short-mfi-value']) + conditions.append(dataframe['mfi'] < params['mfi-value']) if 'fastd-enabled' in params and params['fastd-enabled']: - long_conditions.append(dataframe['fastd'] < params['fastd-value']) - short_conditions.append(dataframe['fastd'] > params['short-fastd-value']) + conditions.append(dataframe['fastd'] < params['fastd-value']) if 'adx-enabled' in params and params['adx-enabled']: - long_conditions.append(dataframe['adx'] > params['adx-value']) - short_conditions.append(dataframe['adx'] < params['short-adx-value']) + conditions.append(dataframe['adx'] > params['adx-value']) if 'rsi-enabled' in params and params['rsi-enabled']: - long_conditions.append(dataframe['rsi'] < params['rsi-value']) - short_conditions.append(dataframe['rsi'] > params['short-rsi-value']) + conditions.append(dataframe['rsi'] < params['rsi-value']) # TRIGGERS if 'trigger' in params: if params['trigger'] == 'boll': - long_conditions.append(dataframe['close'] < dataframe['bb_lowerband']) - short_conditions.append(dataframe['close'] > dataframe['bb_upperband']) + conditions.append(dataframe['close'] < dataframe['bb_lowerband']) if params['trigger'] == 'macd_cross_signal': - long_conditions.append(qtpylib.crossed_above( - dataframe['macd'], - dataframe['macdsignal'] - )) - short_conditions.append(qtpylib.crossed_below( + conditions.append(qtpylib.crossed_above( dataframe['macd'], dataframe['macdsignal'] )) if params['trigger'] == 'sar_reversal': - long_conditions.append(qtpylib.crossed_above( - dataframe['close'], - dataframe['sar'] - )) - short_conditions.append(qtpylib.crossed_below( + conditions.append(qtpylib.crossed_above( dataframe['close'], dataframe['sar'] )) - if long_conditions: + if conditions: dataframe.loc[ - reduce(lambda x, y: x & y, long_conditions), + reduce(lambda x, y: x & y, conditions), 'buy'] = 1 - if short_conditions: - dataframe.loc[ - reduce(lambda x, y: x & y, short_conditions), - 'enter_short'] = 1 - return dataframe return populate_buy_trend @@ -119,10 +100,6 @@ class DefaultHyperOpt(IHyperOpt): Integer(15, 45, name='fastd-value'), Integer(20, 50, name='adx-value'), Integer(20, 40, name='rsi-value'), - Integer(75, 90, name='short-mfi-value'), - Integer(55, 85, name='short-fastd-value'), - Integer(50, 80, name='short-adx-value'), - Integer(60, 80, name='short-rsi-value'), Categorical([True, False], name='mfi-enabled'), Categorical([True, False], name='fastd-enabled'), Categorical([True, False], name='adx-enabled'), @@ -139,57 +116,38 @@ class DefaultHyperOpt(IHyperOpt): """ Sell strategy Hyperopt will build and use. """ - exit_long_conditions = [] - exit_short_conditions = [] + conditions = [] # GUARDS AND TRENDS if 'sell-mfi-enabled' in params and params['sell-mfi-enabled']: - exit_long_conditions.append(dataframe['mfi'] > params['sell-mfi-value']) - exit_short_conditions.append(dataframe['mfi'] < params['exit-short-mfi-value']) + conditions.append(dataframe['mfi'] > params['sell-mfi-value']) if 'sell-fastd-enabled' in params and params['sell-fastd-enabled']: - exit_long_conditions.append(dataframe['fastd'] > params['sell-fastd-value']) - exit_short_conditions.append(dataframe['fastd'] < params['exit-short-fastd-value']) + conditions.append(dataframe['fastd'] > params['sell-fastd-value']) if 'sell-adx-enabled' in params and params['sell-adx-enabled']: - exit_long_conditions.append(dataframe['adx'] < params['sell-adx-value']) - exit_short_conditions.append(dataframe['adx'] > params['exit-short-adx-value']) + conditions.append(dataframe['adx'] < params['sell-adx-value']) if 'sell-rsi-enabled' in params and params['sell-rsi-enabled']: - exit_long_conditions.append(dataframe['rsi'] > params['sell-rsi-value']) - exit_short_conditions.append(dataframe['rsi'] < params['exit-short-rsi-value']) + conditions.append(dataframe['rsi'] > params['sell-rsi-value']) # TRIGGERS if 'sell-trigger' in params: if params['sell-trigger'] == 'sell-boll': - exit_long_conditions.append(dataframe['close'] > dataframe['bb_upperband']) - exit_short_conditions.append(dataframe['close'] < dataframe['bb_lowerband']) + conditions.append(dataframe['close'] > dataframe['bb_upperband']) if params['sell-trigger'] == 'sell-macd_cross_signal': - exit_long_conditions.append(qtpylib.crossed_above( - dataframe['macdsignal'], - dataframe['macd'] - )) - exit_short_conditions.append(qtpylib.crossed_below( + conditions.append(qtpylib.crossed_above( dataframe['macdsignal'], dataframe['macd'] )) if params['sell-trigger'] == 'sell-sar_reversal': - exit_long_conditions.append(qtpylib.crossed_above( - dataframe['sar'], - dataframe['close'] - )) - exit_short_conditions.append(qtpylib.crossed_below( + conditions.append(qtpylib.crossed_above( dataframe['sar'], dataframe['close'] )) - if exit_long_conditions: + if conditions: dataframe.loc[ - reduce(lambda x, y: x & y, exit_long_conditions), + reduce(lambda x, y: x & y, conditions), 'sell'] = 1 - if exit_short_conditions: - dataframe.loc[ - reduce(lambda x, y: x & y, exit_short_conditions), - 'exit-short'] = 1 - return dataframe return populate_sell_trend @@ -204,10 +162,6 @@ class DefaultHyperOpt(IHyperOpt): Integer(50, 100, name='sell-fastd-value'), Integer(50, 100, name='sell-adx-value'), Integer(60, 100, name='sell-rsi-value'), - Integer(1, 25, name='exit-short-mfi-value'), - Integer(1, 50, name='exit-short-fastd-value'), - Integer(1, 50, name='exit-short-adx-value'), - Integer(1, 40, name='exit-short-rsi-value'), Categorical([True, False], name='sell-mfi-enabled'), Categorical([True, False], name='sell-fastd-enabled'), Categorical([True, False], name='sell-adx-enabled'), @@ -233,15 +187,6 @@ class DefaultHyperOpt(IHyperOpt): ), 'buy'] = 1 - dataframe.loc[ - ( - (dataframe['close'] > dataframe['bb_upperband']) & - (dataframe['mfi'] < 84) & - (dataframe['adx'] > 75) & - (dataframe['rsi'] < 79) - ), - 'enter_short'] = 1 - return dataframe def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: @@ -259,13 +204,4 @@ class DefaultHyperOpt(IHyperOpt): ), 'sell'] = 1 - dataframe.loc[ - ( - (qtpylib.crossed_below( - dataframe['macdsignal'], dataframe['macd'] - )) & - (dataframe['fastd'] < 46) - ), - 'exit_short'] = 1 - return dataframe diff --git a/tests/optimize/hyperopts/short_hyperopt.py b/tests/optimize/hyperopts/short_hyperopt.py new file mode 100644 index 000000000..df39188e0 --- /dev/null +++ b/tests/optimize/hyperopts/short_hyperopt.py @@ -0,0 +1,271 @@ +# pragma pylint: disable=missing-docstring, invalid-name, pointless-string-statement + +from functools import reduce +from typing import Any, Callable, Dict, List + +import talib.abstract as ta +from pandas import DataFrame +from skopt.space import Categorical, Dimension, Integer + +import freqtrade.vendor.qtpylib.indicators as qtpylib +from freqtrade.optimize.hyperopt_interface import IHyperOpt + + +class DefaultHyperOpt(IHyperOpt): + """ + Default hyperopt provided by the Freqtrade bot. + You can override it with your own Hyperopt + """ + @staticmethod + def populate_indicators(dataframe: DataFrame, metadata: dict) -> DataFrame: + """ + Add several indicators needed for buy and sell strategies defined below. + """ + # ADX + dataframe['adx'] = ta.ADX(dataframe) + # MACD + macd = ta.MACD(dataframe) + dataframe['macd'] = macd['macd'] + dataframe['macdsignal'] = macd['macdsignal'] + # MFI + dataframe['mfi'] = ta.MFI(dataframe) + # RSI + dataframe['rsi'] = ta.RSI(dataframe) + # Stochastic Fast + stoch_fast = ta.STOCHF(dataframe) + dataframe['fastd'] = stoch_fast['fastd'] + # Minus-DI + dataframe['minus_di'] = ta.MINUS_DI(dataframe) + # Bollinger bands + bollinger = qtpylib.bollinger_bands(qtpylib.typical_price(dataframe), window=20, stds=2) + dataframe['bb_lowerband'] = bollinger['lower'] + dataframe['bb_upperband'] = bollinger['upper'] + # SAR + dataframe['sar'] = ta.SAR(dataframe) + + return dataframe + + @staticmethod + def buy_strategy_generator(params: Dict[str, Any]) -> Callable: + """ + Define the buy strategy parameters to be used by Hyperopt. + """ + def populate_buy_trend(dataframe: DataFrame, metadata: dict) -> DataFrame: + """ + Buy strategy Hyperopt will build and use. + """ + long_conditions = [] + short_conditions = [] + + # GUARDS AND TRENDS + if 'mfi-enabled' in params and params['mfi-enabled']: + long_conditions.append(dataframe['mfi'] < params['mfi-value']) + short_conditions.append(dataframe['mfi'] > params['short-mfi-value']) + if 'fastd-enabled' in params and params['fastd-enabled']: + long_conditions.append(dataframe['fastd'] < params['fastd-value']) + short_conditions.append(dataframe['fastd'] > params['short-fastd-value']) + if 'adx-enabled' in params and params['adx-enabled']: + long_conditions.append(dataframe['adx'] > params['adx-value']) + short_conditions.append(dataframe['adx'] < params['short-adx-value']) + if 'rsi-enabled' in params and params['rsi-enabled']: + long_conditions.append(dataframe['rsi'] < params['rsi-value']) + short_conditions.append(dataframe['rsi'] > params['short-rsi-value']) + + # TRIGGERS + if 'trigger' in params: + if params['trigger'] == 'boll': + long_conditions.append(dataframe['close'] < dataframe['bb_lowerband']) + short_conditions.append(dataframe['close'] > dataframe['bb_upperband']) + if params['trigger'] == 'macd_cross_signal': + long_conditions.append(qtpylib.crossed_above( + dataframe['macd'], + dataframe['macdsignal'] + )) + short_conditions.append(qtpylib.crossed_below( + dataframe['macd'], + dataframe['macdsignal'] + )) + if params['trigger'] == 'sar_reversal': + long_conditions.append(qtpylib.crossed_above( + dataframe['close'], + dataframe['sar'] + )) + short_conditions.append(qtpylib.crossed_below( + dataframe['close'], + dataframe['sar'] + )) + + if long_conditions: + dataframe.loc[ + reduce(lambda x, y: x & y, long_conditions), + 'buy'] = 1 + + if short_conditions: + dataframe.loc[ + reduce(lambda x, y: x & y, short_conditions), + 'enter_short'] = 1 + + return dataframe + + return populate_buy_trend + + @staticmethod + def indicator_space() -> List[Dimension]: + """ + Define your Hyperopt space for searching buy strategy parameters. + """ + return [ + Integer(10, 25, name='mfi-value'), + Integer(15, 45, name='fastd-value'), + Integer(20, 50, name='adx-value'), + Integer(20, 40, name='rsi-value'), + Integer(75, 90, name='short-mfi-value'), + Integer(55, 85, name='short-fastd-value'), + Integer(50, 80, name='short-adx-value'), + Integer(60, 80, name='short-rsi-value'), + Categorical([True, False], name='mfi-enabled'), + Categorical([True, False], name='fastd-enabled'), + Categorical([True, False], name='adx-enabled'), + Categorical([True, False], name='rsi-enabled'), + Categorical(['boll', 'macd_cross_signal', 'sar_reversal'], name='trigger') + ] + + @staticmethod + def sell_strategy_generator(params: Dict[str, Any]) -> Callable: + """ + Define the sell strategy parameters to be used by Hyperopt. + """ + def populate_sell_trend(dataframe: DataFrame, metadata: dict) -> DataFrame: + """ + Sell strategy Hyperopt will build and use. + """ + exit_long_conditions = [] + exit_short_conditions = [] + + # GUARDS AND TRENDS + if 'sell-mfi-enabled' in params and params['sell-mfi-enabled']: + exit_long_conditions.append(dataframe['mfi'] > params['sell-mfi-value']) + exit_short_conditions.append(dataframe['mfi'] < params['exit-short-mfi-value']) + if 'sell-fastd-enabled' in params and params['sell-fastd-enabled']: + exit_long_conditions.append(dataframe['fastd'] > params['sell-fastd-value']) + exit_short_conditions.append(dataframe['fastd'] < params['exit-short-fastd-value']) + if 'sell-adx-enabled' in params and params['sell-adx-enabled']: + exit_long_conditions.append(dataframe['adx'] < params['sell-adx-value']) + exit_short_conditions.append(dataframe['adx'] > params['exit-short-adx-value']) + if 'sell-rsi-enabled' in params and params['sell-rsi-enabled']: + exit_long_conditions.append(dataframe['rsi'] > params['sell-rsi-value']) + exit_short_conditions.append(dataframe['rsi'] < params['exit-short-rsi-value']) + + # TRIGGERS + if 'sell-trigger' in params: + if params['sell-trigger'] == 'sell-boll': + exit_long_conditions.append(dataframe['close'] > dataframe['bb_upperband']) + exit_short_conditions.append(dataframe['close'] < dataframe['bb_lowerband']) + if params['sell-trigger'] == 'sell-macd_cross_signal': + exit_long_conditions.append(qtpylib.crossed_above( + dataframe['macdsignal'], + dataframe['macd'] + )) + exit_short_conditions.append(qtpylib.crossed_below( + dataframe['macdsignal'], + dataframe['macd'] + )) + if params['sell-trigger'] == 'sell-sar_reversal': + exit_long_conditions.append(qtpylib.crossed_above( + dataframe['sar'], + dataframe['close'] + )) + exit_short_conditions.append(qtpylib.crossed_below( + dataframe['sar'], + dataframe['close'] + )) + + if exit_long_conditions: + dataframe.loc[ + reduce(lambda x, y: x & y, exit_long_conditions), + 'sell'] = 1 + + if exit_short_conditions: + dataframe.loc[ + reduce(lambda x, y: x & y, exit_short_conditions), + 'exit-short'] = 1 + + return dataframe + + return populate_sell_trend + + @staticmethod + def sell_indicator_space() -> List[Dimension]: + """ + Define your Hyperopt space for searching sell strategy parameters. + """ + return [ + Integer(75, 100, name='sell-mfi-value'), + Integer(50, 100, name='sell-fastd-value'), + Integer(50, 100, name='sell-adx-value'), + Integer(60, 100, name='sell-rsi-value'), + Integer(1, 25, name='exit-short-mfi-value'), + Integer(1, 50, name='exit-short-fastd-value'), + Integer(1, 50, name='exit-short-adx-value'), + Integer(1, 40, name='exit-short-rsi-value'), + Categorical([True, False], name='sell-mfi-enabled'), + Categorical([True, False], name='sell-fastd-enabled'), + Categorical([True, False], name='sell-adx-enabled'), + Categorical([True, False], name='sell-rsi-enabled'), + Categorical(['sell-boll', + 'sell-macd_cross_signal', + 'sell-sar_reversal'], + name='sell-trigger') + ] + + def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: + """ + Based on TA indicators. Should be a copy of same method from strategy. + Must align to populate_indicators in this file. + Only used when --spaces does not include buy space. + """ + dataframe.loc[ + ( + (dataframe['close'] < dataframe['bb_lowerband']) & + (dataframe['mfi'] < 16) & + (dataframe['adx'] > 25) & + (dataframe['rsi'] < 21) + ), + 'buy'] = 1 + + dataframe.loc[ + ( + (dataframe['close'] > dataframe['bb_upperband']) & + (dataframe['mfi'] < 84) & + (dataframe['adx'] > 75) & + (dataframe['rsi'] < 79) + ), + 'enter_short'] = 1 + + return dataframe + + def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: + """ + Based on TA indicators. Should be a copy of same method from strategy. + Must align to populate_indicators in this file. + Only used when --spaces does not include sell space. + """ + dataframe.loc[ + ( + (qtpylib.crossed_above( + dataframe['macdsignal'], dataframe['macd'] + )) & + (dataframe['fastd'] > 54) + ), + 'sell'] = 1 + + dataframe.loc[ + ( + (qtpylib.crossed_below( + dataframe['macdsignal'], dataframe['macd'] + )) & + (dataframe['fastd'] < 46) + ), + 'exit_short'] = 1 + + return dataframe diff --git a/tests/optimize/test_hyperopt.py b/tests/optimize/test_hyperopt.py index 333cea971..dab10fc89 100644 --- a/tests/optimize/test_hyperopt.py +++ b/tests/optimize/test_hyperopt.py @@ -542,10 +542,6 @@ def test_generate_optimizer(mocker, hyperopt_conf) -> None: 'fastd-value': 35, 'mfi-value': 0, 'rsi-value': 0, - 'short-adx-value': 100, - 'short-fastd-value': 65, - 'short-mfi-value': 100, - 'short-rsi-value': 100, 'adx-enabled': False, 'fastd-enabled': True, 'mfi-enabled': False, @@ -555,10 +551,6 @@ def test_generate_optimizer(mocker, hyperopt_conf) -> None: 'sell-fastd-value': 75, 'sell-mfi-value': 0, 'sell-rsi-value': 0, - 'exit-short-adx-value': 100, - 'exit-short-fastd-value': 25, - 'exit-short-mfi-value': 100, - 'exit-short-rsi-value': 100, 'sell-adx-enabled': False, 'sell-fastd-enabled': True, 'sell-mfi-enabled': False, @@ -585,16 +577,12 @@ def test_generate_optimizer(mocker, hyperopt_conf) -> None: ), 'params_details': {'buy': {'adx-enabled': False, 'adx-value': 0, - 'short-adx-value': 100, 'fastd-enabled': True, 'fastd-value': 35, - 'short-fastd-value': 65, 'mfi-enabled': False, 'mfi-value': 0, - 'short-mfi-value': 100, 'rsi-enabled': False, 'rsi-value': 0, - 'short-rsi-value': 100, 'trigger': 'macd_cross_signal'}, 'roi': {"0": 0.12000000000000001, "20.0": 0.02, @@ -603,16 +591,12 @@ def test_generate_optimizer(mocker, hyperopt_conf) -> None: 'protection': {}, 'sell': {'sell-adx-enabled': False, 'sell-adx-value': 0, - 'exit-short-adx-value': 100, 'sell-fastd-enabled': True, 'sell-fastd-value': 75, - 'exit-short-fastd-value': 25, 'sell-mfi-enabled': False, 'sell-mfi-value': 0, - 'exit-short-mfi-value': 100, 'sell-rsi-enabled': False, 'sell-rsi-value': 0, - 'exit-short-rsi-value': 100, 'sell-trigger': 'macd_cross_signal'}, 'stoploss': {'stoploss': -0.4}, 'trailing': {'trailing_only_offset_is_reached': False, From 07de5d11caccbf88dc58e5e00fcfbf3d09c71777 Mon Sep 17 00:00:00 2001 From: Sam Germain Date: Mon, 23 Aug 2021 00:25:08 -0600 Subject: [PATCH 11/31] Removed a bug causing errors from freqtradebot --- freqtrade/freqtradebot.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 050818c13..179c99d2c 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -423,8 +423,7 @@ class FreqtradeBot(LoggingMixin): (buy, sell, buy_tag) = self.strategy.get_signal( pair, self.strategy.timeframe, - analyzed_df, - False + analyzed_df ) if buy and not sell: From 9add3bf8088765d9c621e834e86bebf3fd98fcbc Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 23 Aug 2021 21:12:46 +0200 Subject: [PATCH 12/31] Add enter_long compatibility layer --- freqtrade/enums/signaltype.py | 6 +++--- freqtrade/strategy/interface.py | 12 ++++++++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/freqtrade/enums/signaltype.py b/freqtrade/enums/signaltype.py index fcebd9f0e..ca4b8482e 100644 --- a/freqtrade/enums/signaltype.py +++ b/freqtrade/enums/signaltype.py @@ -5,9 +5,9 @@ class SignalType(Enum): """ Enum to distinguish between buy and sell signals """ - BUY = "buy" - SELL = "sell" - SHORT = "short" + BUY = "buy" # To be renamed to enter_long + SELL = "sell" # To be renamed to exit_long + SHORT = "short" # Should be "enter_short" EXIT_SHORT = "exit_short" diff --git a/freqtrade/strategy/interface.py b/freqtrade/strategy/interface.py index 50677c064..a1e820808 100644 --- a/freqtrade/strategy/interface.py +++ b/freqtrade/strategy/interface.py @@ -207,6 +207,7 @@ class IStrategy(ABC, HyperStrategyMixin): """ pass + # TODO-lev: add side def confirm_trade_entry(self, pair: str, order_type: str, amount: float, rate: float, time_in_force: str, current_time: datetime, **kwargs) -> bool: """ @@ -304,6 +305,7 @@ class IStrategy(ABC, HyperStrategyMixin): """ return None + # TODO-lev: add side def custom_stake_amount(self, pair: str, current_time: datetime, current_rate: float, proposed_stake: float, min_stake: float, max_stake: float, **kwargs) -> float: @@ -804,7 +806,11 @@ class IStrategy(ABC, HyperStrategyMixin): "the current function headers!", DeprecationWarning) return self.populate_buy_trend(dataframe) # type: ignore else: - return self.populate_buy_trend(dataframe, metadata) + df = self.populate_buy_trend(dataframe, metadata) + # TODO-lev: IF both buy and enter_long exist, this will fail. + df = df.rename({'buy': 'enter_long', 'buy_tag': 'long_tag'}, axis='columns') + + return df def advise_sell(self, dataframe: DataFrame, metadata: dict) -> DataFrame: """ @@ -822,7 +828,9 @@ class IStrategy(ABC, HyperStrategyMixin): "the current function headers!", DeprecationWarning) return self.populate_sell_trend(dataframe) # type: ignore else: - return self.populate_sell_trend(dataframe, metadata) + df = self.populate_sell_trend(dataframe, metadata) + # TODO-lev: IF both sell and exit_long exist, this will fail at a later point + return df.rename({'sell': 'exit_long'}, axis='columns') def leverage(self, pair: str, current_time: datetime, current_rate: float, proposed_leverage: float, max_leverage: float, From 3e8164bfcafe5ffa473c585a5888dd626d4a5ea7 Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 23 Aug 2021 21:13:47 +0200 Subject: [PATCH 13/31] Use proper exchange name in backtesting --- freqtrade/optimize/backtesting.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index 8b3eb46ca..1883f9670 100644 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -65,8 +65,8 @@ class Backtesting: remove_credentials(self.config) self.strategylist: List[IStrategy] = [] self.all_results: Dict[str, Dict] = {} - - self.exchange = ExchangeResolver.load_exchange(self.config['exchange']['name'], self.config) + self._exchange_name = self.config['exchange']['name'] + self.exchange = ExchangeResolver.load_exchange(self._exchange_name, self.config) self.dataprovider = DataProvider(self.config, None) if self.config.get('strategy_list', None): @@ -388,7 +388,7 @@ class Backtesting: fee_close=self.fee, is_open=True, buy_tag=row[BUY_TAG_IDX] if has_buy_tag else None, - exchange='backtesting', + exchange=self._exchange_name, ) return trade return None From 7373b39015ec109fea422dee7aa657c809eff20b Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 23 Aug 2021 21:15:56 +0200 Subject: [PATCH 14/31] Initial support for backtesting with short --- freqtrade/optimize/backtesting.py | 71 ++++++++++++++++++++++--------- 1 file changed, 50 insertions(+), 21 deletions(-) diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index 1883f9670..5e972f297 100644 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -37,13 +37,16 @@ logger = logging.getLogger(__name__) # Indexes for backtest tuples DATE_IDX = 0 -BUY_IDX = 1 -OPEN_IDX = 2 -CLOSE_IDX = 3 -SELL_IDX = 4 -LOW_IDX = 5 -HIGH_IDX = 6 -BUY_TAG_IDX = 7 +OPEN_IDX = 1 +HIGH_IDX = 2 +LOW_IDX = 3 +CLOSE_IDX = 4 +BUY_IDX = 5 +SELL_IDX = 6 +SHORT_IDX = 7 +ESHORT_IDX = 8 +BUY_TAG_IDX = 9 +SHORT_TAG_IDX = 10 class Backtesting: @@ -215,7 +218,8 @@ class Backtesting: """ # Every change to this headers list must evaluate further usages of the resulting tuple # and eventually change the constants for indexes at the top - headers = ['date', 'buy', 'open', 'close', 'sell', 'low', 'high'] + headers = ['date', 'open', 'high', 'low', 'close', 'enter_long', 'exit_long', + 'enter_short', 'exit_short'] data: Dict = {} self.progress.init_step(BacktestState.CONVERT, len(processed)) @@ -223,13 +227,21 @@ class Backtesting: for pair, pair_data in processed.items(): self.check_abort() self.progress.increment() - has_buy_tag = 'buy_tag' in pair_data - headers = headers + ['buy_tag'] if has_buy_tag else headers + has_buy_tag = 'long_tag' in pair_data + has_short_tag = 'short_tag' in pair_data + headers = headers + ['long_tag'] if has_buy_tag else headers + headers = headers + ['short_tag'] if has_short_tag else headers if not pair_data.empty: - pair_data.loc[:, 'buy'] = 0 # cleanup if buy_signal is exist - pair_data.loc[:, 'sell'] = 0 # cleanup if sell_signal is exist + # Cleanup from prior runs + pair_data.loc[:, 'buy'] = 0 # TODO: Should be renamed to enter_long + pair_data.loc[:, 'enter_short'] = 0 + pair_data.loc[:, 'sell'] = 0 # TODO: should be renamed to exit_long + pair_data.loc[:, 'exit_short'] = 0 + # pair_data.loc[:, 'sell'] = 0 if has_buy_tag: - pair_data.loc[:, 'buy_tag'] = None # cleanup if buy_tag is exist + pair_data.loc[:, 'long_tag'] = None # cleanup if buy_tag is exist + if has_short_tag: + pair_data.loc[:, 'short_tag'] = None # cleanup if short_tag is exist df_analyzed = self.strategy.advise_sell( self.strategy.advise_buy(pair_data, {'pair': pair}), @@ -240,10 +252,12 @@ class Backtesting: startup_candles=self.required_startup) # To avoid using data from future, we use buy/sell signals shifted # from the previous candle - df_analyzed.loc[:, 'buy'] = df_analyzed.loc[:, 'buy'].shift(1) - df_analyzed.loc[:, 'sell'] = df_analyzed.loc[:, 'sell'].shift(1) + df_analyzed.loc[:, 'enter_long'] = df_analyzed.loc[:, 'enter_long'].shift(1) + df_analyzed.loc[:, 'enter_short'] = df_analyzed.loc[:, 'enter_short'].shift(1) + df_analyzed.loc[:, 'exit_long'] = df_analyzed.loc[:, 'exit_long'].shift(1) + df_analyzed.loc[:, 'exit_short'] = df_analyzed.loc[:, 'exit_short'].shift(1) if has_buy_tag: - df_analyzed.loc[:, 'buy_tag'] = df_analyzed.loc[:, 'buy_tag'].shift(1) + df_analyzed.loc[:, 'long_tag'] = df_analyzed.loc[:, 'long_tag'].shift(1) df_analyzed.drop(df_analyzed.head(1).index, inplace=True) @@ -322,7 +336,7 @@ class Backtesting: return sell_row[OPEN_IDX] def _get_sell_trade_entry(self, trade: LocalTrade, sell_row: Tuple) -> Optional[LocalTrade]: - + # TODO: short exits sell = self.strategy.should_sell(trade, sell_row[OPEN_IDX], # type: ignore sell_row[DATE_IDX].to_pydatetime(), sell_row[BUY_IDX], sell_row[SELL_IDX], @@ -349,7 +363,7 @@ class Backtesting: return None - def _enter_trade(self, pair: str, row: List) -> Optional[LocalTrade]: + def _enter_trade(self, pair: str, row: List, direction: str) -> Optional[LocalTrade]: try: stake_amount = self.wallets.get_trade_stake_amount(pair, None) except DependencyException: @@ -389,6 +403,7 @@ class Backtesting: is_open=True, buy_tag=row[BUY_TAG_IDX] if has_buy_tag else None, exchange=self._exchange_name, + is_short=(direction == 'short'), ) return trade return None @@ -422,6 +437,20 @@ class Backtesting: self.rejected_trades += 1 return False + def check_for_trade_entry(self, row) -> Optional[str]: + enter_long = row[BUY_IDX] == 1 + exit_long = row[SELL_IDX] == 1 + enter_short = row[SHORT_IDX] == 1 + exit_short = row[ESHORT_IDX] == 1 + + if enter_long == 1 and not any([exit_long, enter_short]): + # Long + return 'long' + if enter_short == 1 and not any([exit_short, enter_long]): + # Short + return 'short' + return None + def backtest(self, processed: Dict, start_date: datetime, end_date: datetime, max_open_trades: int = 0, position_stacking: bool = False, @@ -482,15 +511,15 @@ class Backtesting: # without positionstacking, we can only have one open trade per pair. # max_open_trades must be respected # don't open on the last row + trade_dir = self.check_for_trade_entry(row) if ( (position_stacking or len(open_trades[pair]) == 0) and self.trade_slot_available(max_open_trades, open_trade_count_start) and tmp != end_date - and row[BUY_IDX] == 1 - and row[SELL_IDX] != 1 + and trade_dir is not None and not PairLocks.is_pair_locked(pair, row[DATE_IDX]) ): - trade = self._enter_trade(pair, row) + trade = self._enter_trade(pair, row, trade_dir) if trade: # TODO: hacky workaround to avoid opening > max_open_trades # This emulates previous behaviour - not sure if this is correct From faf5cfa66d7e6a3228ad638d83ec30b0022e8bdb Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 23 Aug 2021 21:35:01 +0200 Subject: [PATCH 15/31] Update some tests for updated backtest interface --- freqtrade/strategy/interface.py | 9 +++++---- tests/optimize/__init__.py | 8 ++++++-- tests/optimize/test_backtest_detail.py | 13 +++++++------ 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/freqtrade/strategy/interface.py b/freqtrade/strategy/interface.py index a1e820808..f721acafb 100644 --- a/freqtrade/strategy/interface.py +++ b/freqtrade/strategy/interface.py @@ -807,8 +807,8 @@ class IStrategy(ABC, HyperStrategyMixin): return self.populate_buy_trend(dataframe) # type: ignore else: df = self.populate_buy_trend(dataframe, metadata) - # TODO-lev: IF both buy and enter_long exist, this will fail. - df = df.rename({'buy': 'enter_long', 'buy_tag': 'long_tag'}, axis='columns') + if 'enter_long' not in df.columns: + df = df.rename({'buy': 'enter_long', 'buy_tag': 'long_tag'}, axis='columns') return df @@ -829,8 +829,9 @@ class IStrategy(ABC, HyperStrategyMixin): return self.populate_sell_trend(dataframe) # type: ignore else: df = self.populate_sell_trend(dataframe, metadata) - # TODO-lev: IF both sell and exit_long exist, this will fail at a later point - return df.rename({'sell': 'exit_long'}, axis='columns') + if 'exit_long' not in df.columns: + df = df.rename({'sell': 'exit_long'}, axis='columns') + return df def leverage(self, pair: str, current_time: datetime, current_rate: float, proposed_leverage: float, max_leverage: float, diff --git a/tests/optimize/__init__.py b/tests/optimize/__init__.py index f29d8d585..dffe3209f 100644 --- a/tests/optimize/__init__.py +++ b/tests/optimize/__init__.py @@ -44,8 +44,12 @@ def _get_frame_time_from_offset(offset): def _build_backtest_dataframe(data): - columns = ['date', 'open', 'high', 'low', 'close', 'volume', 'buy', 'sell'] - columns = columns + ['buy_tag'] if len(data[0]) == 9 else columns + columns = ['date', 'open', 'high', 'low', 'close', 'volume', 'enter_long', 'exit_long', + 'enter_short', 'exit_short'] + if len(data[0]) == 8: + # No short columns + data = [d + [0, 0] for d in data] + columns = columns + ['long_tag'] if len(data[0]) == 11 else columns frame = DataFrame.from_records(data, columns=columns) frame['date'] = frame['date'].apply(_get_frame_time_from_offset) diff --git a/tests/optimize/test_backtest_detail.py b/tests/optimize/test_backtest_detail.py index e5c037f3e..e14f82c33 100644 --- a/tests/optimize/test_backtest_detail.py +++ b/tests/optimize/test_backtest_detail.py @@ -519,12 +519,12 @@ tc32 = BTContainer(data=[ # Test 33: trailing_stop should be triggered immediately on trade open candle. # stop-loss: 1%, ROI: 10% (should not apply) tc33 = BTContainer(data=[ - # D O H L C V B S BT - [0, 5000, 5050, 4950, 5000, 6172, 1, 0, 'buy_signal_01'], - [1, 5000, 5500, 5000, 4900, 6172, 0, 0, None], # enter trade (signal on last candle) and stop - [2, 4900, 5250, 4500, 5100, 6172, 0, 0, None], - [3, 5100, 5100, 4650, 4750, 6172, 0, 0, None], - [4, 4750, 4950, 4350, 4750, 6172, 0, 0, None]], + # D O H L C V EL XL ES Xs BT + [0, 5000, 5050, 4950, 5000, 6172, 1, 0, 0, 0, 'buy_signal_01'], + [1, 5000, 5500, 5000, 4900, 6172, 0, 0, 0, 0, None], # enter trade (signal on last candle) and stop + [2, 4900, 5250, 4500, 5100, 6172, 0, 0, 0, 0, None], + [3, 5100, 5100, 4650, 4750, 6172, 0, 0, 0, 0, None], + [4, 4750, 4950, 4350, 4750, 6172, 0, 0, 0, 0, None]], stop_loss=-0.01, roi={"0": 0.10}, profit_perc=-0.01, trailing_stop=True, trailing_only_offset_is_reached=True, trailing_stop_positive_offset=0.02, trailing_stop_positive=0.01, use_custom_stoploss=True, @@ -571,6 +571,7 @@ TESTS = [ tc31, tc32, tc33, + # TODO-lev: Add tests for short here ] From 11bd8e912e7fa577ce760c7c0500e76c0312f940 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 24 Aug 2021 06:45:09 +0200 Subject: [PATCH 16/31] Fix some tests --- freqtrade/optimize/backtesting.py | 10 +++---- freqtrade/strategy/interface.py | 3 +- tests/optimize/__init__.py | 6 ++-- tests/optimize/test_backtest_detail.py | 2 +- tests/optimize/test_backtesting.py | 40 ++++++++++++++++--------- tests/strategy/test_strategy_loading.py | 10 +++++-- 6 files changed, 44 insertions(+), 27 deletions(-) diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index ee784200f..ee8e3b050 100644 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -233,12 +233,12 @@ class Backtesting: if not pair_data.empty: # Cleanup from prior runs - pair_data.loc[:, 'buy'] = 0 # TODO: Should be renamed to enter_long + pair_data.loc[:, 'enter_long'] = 0 pair_data.loc[:, 'enter_short'] = 0 - pair_data.loc[:, 'sell'] = 0 # TODO: should be renamed to exit_long + pair_data.loc[:, 'exit_long'] = 0 pair_data.loc[:, 'exit_short'] = 0 - pair_data.loc[:, 'long_tag'] = None # cleanup if buy_tag is exist - pair_data.loc[:, 'short_tag'] = None # cleanup if short_tag is exist + pair_data.loc[:, 'long_tag'] = None + pair_data.loc[:, 'short_tag'] = None df_analyzed = self.strategy.advise_sell( self.strategy.advise_buy(pair_data, {'pair': pair}), @@ -255,8 +255,6 @@ class Backtesting: df_analyzed.loc[:, 'exit_short'] = df_analyzed.loc[:, 'exit_short'].shift(1) df_analyzed.loc[:, 'long_tag'] = df_analyzed.loc[:, 'long_tag'].shift(1) - df_analyzed.drop(df_analyzed.head(1).index, inplace=True) - # Update dataprovider cache self.dataprovider._set_cached_df(pair, self.timeframe, df_analyzed) diff --git a/freqtrade/strategy/interface.py b/freqtrade/strategy/interface.py index c6cf7c0dc..63217df68 100644 --- a/freqtrade/strategy/interface.py +++ b/freqtrade/strategy/interface.py @@ -871,7 +871,7 @@ class IStrategy(ABC, HyperStrategyMixin): return df def leverage(self, pair: str, current_time: datetime, current_rate: float, - proposed_leverage: float, max_leverage: float, + proposed_leverage: float, max_leverage: float, side: str, **kwargs) -> float: """ Customize leverage for each new trade. This method is not called when edge module is @@ -882,6 +882,7 @@ class IStrategy(ABC, HyperStrategyMixin): :param current_rate: Rate, calculated based on pricing settings in ask_strategy. :param proposed_leverage: A leverage proposed by the bot. :param max_leverage: Max leverage allowed on this pair + :param side: 'long' or 'short' - indicating the direction of the proposed trade :return: A leverage amount, which is between 1.0 and max_leverage. """ return 1.0 diff --git a/tests/optimize/__init__.py b/tests/optimize/__init__.py index c40d11456..2ba9485fd 100644 --- a/tests/optimize/__init__.py +++ b/tests/optimize/__init__.py @@ -56,6 +56,8 @@ def _build_backtest_dataframe(data): # Ensure floats are in place for column in ['open', 'high', 'low', 'close', 'volume']: frame[column] = frame[column].astype('float64') - if 'buy_tag' not in columns: - frame['buy_tag'] = None + if 'long_tag' not in columns: + frame['long_tag'] = None + if 'short_tag' not in columns: + frame['short_tag'] = None return frame diff --git a/tests/optimize/test_backtest_detail.py b/tests/optimize/test_backtest_detail.py index e14f82c33..9b99648b1 100644 --- a/tests/optimize/test_backtest_detail.py +++ b/tests/optimize/test_backtest_detail.py @@ -521,7 +521,7 @@ tc32 = BTContainer(data=[ tc33 = BTContainer(data=[ # D O H L C V EL XL ES Xs BT [0, 5000, 5050, 4950, 5000, 6172, 1, 0, 0, 0, 'buy_signal_01'], - [1, 5000, 5500, 5000, 4900, 6172, 0, 0, 0, 0, None], # enter trade (signal on last candle) and stop + [1, 5000, 5500, 5000, 4900, 6172, 0, 0, 0, 0, None], # enter trade and stop [2, 4900, 5250, 4500, 5100, 6172, 0, 0, 0, 0, None], [3, 5100, 5100, 4650, 4750, 6172, 0, 0, 0, 0, None], [4, 4750, 4950, 4350, 4750, 6172, 0, 0, 0, 0, None]], diff --git a/tests/optimize/test_backtesting.py b/tests/optimize/test_backtesting.py index 998b2d837..11ca4b0ab 100644 --- a/tests/optimize/test_backtesting.py +++ b/tests/optimize/test_backtesting.py @@ -123,12 +123,14 @@ def _trend(signals, buy_value, sell_value): n = len(signals['low']) buy = np.zeros(n) sell = np.zeros(n) - for i in range(0, len(signals['buy'])): + for i in range(0, len(signals['enter_long'])): if random.random() > 0.5: # Both buy and sell signals at same timeframe buy[i] = buy_value sell[i] = sell_value - signals['buy'] = buy - signals['sell'] = sell + signals['enter_long'] = buy + signals['exit_long'] = sell + signals['enter_short'] = 0 + signals['exit_short'] = 0 return signals @@ -143,8 +145,10 @@ def _trend_alternate(dataframe=None, metadata=None): buy[i] = 1 else: sell[i] = 1 - signals['buy'] = buy - signals['sell'] = sell + signals['enter_long'] = buy + signals['exit_long'] = sell + signals['enter_short'] = 0 + signals['exit_short'] = 0 return dataframe @@ -499,41 +503,47 @@ def test_backtest__enter_trade(default_conf, fee, mocker) -> None: 0.0012, # High '', # Buy Signal Name ] - trade = backtesting._enter_trade(pair, row=row) + trade = backtesting._enter_trade(pair, row=row, direction='long') assert isinstance(trade, LocalTrade) assert trade.stake_amount == 495 # Fake 2 trades, so there's not enough amount for the next trade left. LocalTrade.trades_open.append(trade) LocalTrade.trades_open.append(trade) - trade = backtesting._enter_trade(pair, row=row) + trade = backtesting._enter_trade(pair, row=row, direction='long') assert trade is None LocalTrade.trades_open.pop() - trade = backtesting._enter_trade(pair, row=row) + trade = backtesting._enter_trade(pair, row=row, direction='long') assert trade is not None backtesting.strategy.custom_stake_amount = lambda **kwargs: 123.5 - trade = backtesting._enter_trade(pair, row=row) + trade = backtesting._enter_trade(pair, row=row, direction='long') assert trade assert trade.stake_amount == 123.5 # In case of error - use proposed stake backtesting.strategy.custom_stake_amount = lambda **kwargs: 20 / 0 - trade = backtesting._enter_trade(pair, row=row) + trade = backtesting._enter_trade(pair, row=row, direction='long') assert trade assert trade.stake_amount == 495 + assert trade.is_short is False + + trade = backtesting._enter_trade(pair, row=row, direction='short') + assert trade + assert trade.stake_amount == 495 + assert trade.is_short is True # Stake-amount too high! mocker.patch("freqtrade.exchange.Exchange.get_min_pair_stake_amount", return_value=600.0) - trade = backtesting._enter_trade(pair, row=row) + trade = backtesting._enter_trade(pair, row=row, direction='long') assert trade is None # Stake-amount throwing error mocker.patch("freqtrade.wallets.Wallets.get_trade_stake_amount", side_effect=DependencyException) - trade = backtesting._enter_trade(pair, row=row) + trade = backtesting._enter_trade(pair, row=row, direction='long') assert trade is None backtesting.cleanup() @@ -766,8 +776,10 @@ def test_backtest_multi_pair(default_conf, fee, mocker, tres, pair, testdatadir) multi = 20 else: multi = 18 - dataframe['buy'] = np.where(dataframe.index % multi == 0, 1, 0) - dataframe['sell'] = np.where((dataframe.index + multi - 2) % multi == 0, 1, 0) + dataframe['enter_long'] = np.where(dataframe.index % multi == 0, 1, 0) + dataframe['exit_long'] = np.where((dataframe.index + multi - 2) % multi == 0, 1, 0) + dataframe['enter_short'] = 0 + dataframe['exit_short'] = 0 return dataframe mocker.patch("freqtrade.exchange.Exchange.get_min_pair_stake_amount", return_value=0.00001) diff --git a/tests/strategy/test_strategy_loading.py b/tests/strategy/test_strategy_loading.py index e76990ba9..7e94b7ccc 100644 --- a/tests/strategy/test_strategy_loading.py +++ b/tests/strategy/test_strategy_loading.py @@ -394,7 +394,8 @@ def test_call_deprecated_function(result, monkeypatch, default_conf, caplog): caplog) -def test_strategy_interface_versioning(result, monkeypatch, default_conf): +def test_strategy_interface_versioning(result, default_conf): + # Tests interface compatibility with Interface version 2. default_conf.update({'strategy': 'DefaultStrategy'}) strategy = StrategyResolver.load_strategy(default_conf) metadata = {'pair': 'ETH/BTC'} @@ -411,8 +412,11 @@ def test_strategy_interface_versioning(result, monkeypatch, default_conf): enterdf = strategy.advise_buy(result, metadata=metadata) assert isinstance(enterdf, DataFrame) - assert 'buy' in enterdf.columns + + assert 'buy' not in enterdf.columns + assert 'enter_long' in enterdf.columns exitdf = strategy.advise_sell(result, metadata=metadata) assert isinstance(exitdf, DataFrame) - assert 'sell' in exitdf + assert 'sell' not in exitdf + assert 'exit_long' in exitdf From eb71ee847c11be74c7907534b08d742e3a9eed56 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 24 Aug 2021 06:54:55 +0200 Subject: [PATCH 17/31] Rename backtest index constants --- freqtrade/optimize/backtesting.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index ee8e3b050..100cf6548 100644 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -41,10 +41,10 @@ OPEN_IDX = 1 HIGH_IDX = 2 LOW_IDX = 3 CLOSE_IDX = 4 -BUY_IDX = 5 -SELL_IDX = 6 +LONG_IDX = 5 +ELONG_IDX = 6 # Exit long SHORT_IDX = 7 -ESHORT_IDX = 8 +ESHORT_IDX = 8 # Exit short BUY_TAG_IDX = 9 SHORT_TAG_IDX = 10 @@ -335,8 +335,8 @@ class Backtesting: # TODO: short exits sell_candle_time = sell_row[DATE_IDX].to_pydatetime() sell = self.strategy.should_sell(trade, sell_row[OPEN_IDX], # type: ignore - sell_candle_time, sell_row[BUY_IDX], - sell_row[SELL_IDX], + sell_candle_time, buy=sell_row[LONG_IDX], + sell=sell_row[ELONG_IDX], low=sell_row[LOW_IDX], high=sell_row[HIGH_IDX]) if sell.sell_flag: @@ -435,8 +435,8 @@ class Backtesting: return False def check_for_trade_entry(self, row) -> Optional[str]: - enter_long = row[BUY_IDX] == 1 - exit_long = row[SELL_IDX] == 1 + enter_long = row[LONG_IDX] == 1 + exit_long = row[ELONG_IDX] == 1 enter_short = row[SHORT_IDX] == 1 exit_short = row[ESHORT_IDX] == 1 From b40f985b1372feeba9470b90154a6e1b90d7b214 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 24 Aug 2021 19:55:00 +0200 Subject: [PATCH 18/31] Add short-exit logic to backtesting --- freqtrade/freqtradebot.py | 8 ++++---- freqtrade/optimize/backtesting.py | 11 ++++++----- freqtrade/strategy/interface.py | 25 ++++++++++++++++--------- tests/strategy/test_interface.py | 20 ++++++++++++++++---- 4 files changed, 42 insertions(+), 22 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index ce09e715e..c620e1a84 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -856,14 +856,14 @@ class FreqtradeBot(LoggingMixin): """ Check and execute sell """ - should_sell = self.strategy.should_sell( + should_exit: SellCheckTuple = self.strategy.should_exit( trade, sell_rate, datetime.now(timezone.utc), buy, sell, force_stoploss=self.edge.stoploss(trade.pair) if self.edge else 0 ) - if should_sell.sell_flag: - logger.info(f'Executing Sell for {trade.pair}. Reason: {should_sell.sell_type}') - self.execute_sell(trade, sell_rate, should_sell) + if should_exit.sell_flag: + logger.info(f'Executing Sell for {trade.pair}. Reason: {should_exit.sell_type}') + self.execute_sell(trade, sell_rate, should_exit) return True return False diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index 100cf6548..c3cd5b114 100644 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -332,12 +332,13 @@ class Backtesting: return sell_row[OPEN_IDX] def _get_sell_trade_entry(self, trade: LocalTrade, sell_row: Tuple) -> Optional[LocalTrade]: - # TODO: short exits sell_candle_time = sell_row[DATE_IDX].to_pydatetime() - sell = self.strategy.should_sell(trade, sell_row[OPEN_IDX], # type: ignore - sell_candle_time, buy=sell_row[LONG_IDX], - sell=sell_row[ELONG_IDX], - low=sell_row[LOW_IDX], high=sell_row[HIGH_IDX]) + sell = self.strategy.should_exit( + trade, sell_row[OPEN_IDX], sell_candle_time, # type: ignore + enter_long=sell_row[LONG_IDX], enter_short=sell_row[SHORT_IDX], + exit_long=sell_row[ELONG_IDX], exit_short=sell_row[ESHORT_IDX], + low=sell_row[LOW_IDX], high=sell_row[HIGH_IDX] + ) if sell.sell_flag: trade.close_date = sell_candle_time diff --git a/freqtrade/strategy/interface.py b/freqtrade/strategy/interface.py index 63217df68..1aa9d3867 100644 --- a/freqtrade/strategy/interface.py +++ b/freqtrade/strategy/interface.py @@ -614,8 +614,10 @@ class IStrategy(ABC, HyperStrategyMixin): else: return False - def should_sell(self, trade: Trade, rate: float, date: datetime, buy: bool, - sell: bool, low: float = None, high: float = None, + def should_exit(self, trade: Trade, rate: float, date: datetime, *, + enter_long: bool, enter_short: bool, + exit_long: bool, exit_short: bool, + low: float = None, high: float = None, force_stoploss: float = 0) -> SellCheckTuple: """ This function evaluates if one of the conditions required to trigger a sell/exit_short @@ -625,6 +627,10 @@ class IStrategy(ABC, HyperStrategyMixin): :param force_stoploss: Externally provided stoploss :return: True if trade should be exited, False otherwise """ + + enter = enter_short if trade.is_short else enter_long + exit_ = exit_short if trade.is_short else exit_long + current_rate = rate current_profit = trade.calc_profit_ratio(current_rate) @@ -639,7 +645,7 @@ class IStrategy(ABC, HyperStrategyMixin): current_profit = trade.calc_profit_ratio(current_rate) # if enter signal and ignore_roi is set, we don't need to evaluate min_roi. - roi_reached = (not (buy and self.ignore_roi_if_buy_signal) + roi_reached = (not (enter and self.ignore_roi_if_buy_signal) and self.min_roi_reached(trade=trade, current_profit=current_profit, current_time=date)) @@ -652,8 +658,8 @@ class IStrategy(ABC, HyperStrategyMixin): if (self.sell_profit_only and current_profit <= self.sell_profit_offset): # sell_profit_only and profit doesn't reach the offset - ignore sell signal pass - elif self.use_sell_signal and not buy: - if sell: + elif self.use_sell_signal and not enter: + if exit_: sell_signal = SellType.SELL_SIGNAL else: trade_type = "exit_short" if trade.is_short else "sell" @@ -712,10 +718,10 @@ class IStrategy(ABC, HyperStrategyMixin): # Initiate stoploss with open_rate. Does nothing if stoploss is already set. trade.adjust_stop_loss(trade.open_rate, stop_loss_value, initial=True) - dir_correct = ( - trade.stop_loss < (low or current_rate) and not trade.is_short or - trade.stop_loss > (low or current_rate) and trade.is_short - ) + dir_correct = (trade.stop_loss < (low or current_rate) + if not trade.is_short else + trade.stop_loss > (high or current_rate) + ) if self.use_custom_stoploss and dir_correct: stop_loss_value = strategy_safe_wrapper(self.custom_stoploss, default_retval=None @@ -735,6 +741,7 @@ class IStrategy(ABC, HyperStrategyMixin): sl_offset = self.trailing_stop_positive_offset # Make sure current_profit is calculated using high for backtesting. + # TODO-lev: Check this function - high / low usage must be inversed for short trades! high_profit = current_profit if not high else trade.calc_profit_ratio(high) # Don't update stoploss if trailing_only_offset_is_reached is true. diff --git a/tests/strategy/test_interface.py b/tests/strategy/test_interface.py index af603e611..bfdf88dbb 100644 --- a/tests/strategy/test_interface.py +++ b/tests/strategy/test_interface.py @@ -452,27 +452,39 @@ def test_custom_sell(default_conf, fee, caplog) -> None: ) now = arrow.utcnow().datetime - res = strategy.should_sell(trade, 1, now, False, False, None, None, 0) + res = strategy.should_exit(trade, 1, now, + enter_long=False, enter_short=False, + exit_long=False, exit_short=False, + low=None, high=None) assert res.sell_flag is False assert res.sell_type == SellType.NONE strategy.custom_sell = MagicMock(return_value=True) - res = strategy.should_sell(trade, 1, now, False, False, None, None, 0) + res = strategy.should_exit(trade, 1, now, + enter_long=False, enter_short=False, + exit_long=False, exit_short=False, + low=None, high=None) assert res.sell_flag is True assert res.sell_type == SellType.CUSTOM_SELL assert res.sell_reason == 'custom_sell' strategy.custom_sell = MagicMock(return_value='hello world') - res = strategy.should_sell(trade, 1, now, False, False, None, None, 0) + res = strategy.should_exit(trade, 1, now, + enter_long=False, enter_short=False, + exit_long=False, exit_short=False, + low=None, high=None) assert res.sell_type == SellType.CUSTOM_SELL assert res.sell_flag is True assert res.sell_reason == 'hello world' caplog.clear() strategy.custom_sell = MagicMock(return_value='h' * 100) - res = strategy.should_sell(trade, 1, now, False, False, None, None, 0) + res = strategy.should_exit(trade, 1, now, + enter_long=False, enter_short=False, + exit_long=False, exit_short=False, + low=None, high=None) assert res.sell_type == SellType.CUSTOM_SELL assert res.sell_flag is True assert res.sell_reason == 'h' * 64 From 46285cd77e5c0e4f0edd45ca90230ed4b6c91dc0 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 24 Aug 2021 20:07:39 +0200 Subject: [PATCH 19/31] Fix some namings in freqtradebot --- freqtrade/freqtradebot.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index c620e1a84..75f8d93ec 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -420,24 +420,24 @@ class FreqtradeBot(LoggingMixin): return False # running get_signal on historical data fetched - (buy, sell, buy_tag) = self.strategy.get_signal( + (enter, exit_, enter_tag) = self.strategy.get_signal( pair, self.strategy.timeframe, analyzed_df ) - if buy and not sell: + if enter and not exit_: stake_amount = self.wallets.get_trade_stake_amount(pair, self.edge) bid_check_dom = self.config.get('bid_strategy', {}).get('check_depth_of_market', {}) if ((bid_check_dom.get('enabled', False)) and (bid_check_dom.get('bids_to_ask_delta', 0) > 0)): if self._check_depth_of_market_buy(pair, bid_check_dom): - return self.execute_buy(pair, stake_amount, buy_tag=buy_tag) + return self.execute_buy(pair, stake_amount, enter_tag=enter_tag) else: return False - return self.execute_buy(pair, stake_amount, buy_tag=buy_tag) + return self.execute_buy(pair, stake_amount, enter_tag=enter_tag) else: return False @@ -466,7 +466,7 @@ class FreqtradeBot(LoggingMixin): return False def execute_buy(self, pair: str, stake_amount: float, price: Optional[float] = None, - forcebuy: bool = False, buy_tag: Optional[str] = None) -> bool: + forcebuy: bool = False, enter_tag: Optional[str] = None) -> bool: """ Executes a limit buy for the given pair :param pair: pair for which we want to create a LIMIT_BUY @@ -575,7 +575,8 @@ class FreqtradeBot(LoggingMixin): exchange=self.exchange.id, open_order_id=order_id, strategy=self.strategy.get_strategy_name(), - buy_tag=buy_tag, + # TODO-lev: compatibility layer for buy_tag (!) + buy_tag=enter_tag, timeframe=timeframe_to_minutes(self.config['timeframe']) ) trade.orders.append(order_obj) From 9a03cb96f5386c3c0f17061756cffb6933750075 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 24 Aug 2021 20:24:51 +0200 Subject: [PATCH 20/31] Update get_signal --- freqtrade/enums/__init__.py | 2 +- freqtrade/enums/signaltype.py | 5 ++ freqtrade/freqtradebot.py | 14 ++--- freqtrade/strategy/interface.py | 101 ++++++++++++++++++++++++-------- 4 files changed, 89 insertions(+), 33 deletions(-) diff --git a/freqtrade/enums/__init__.py b/freqtrade/enums/__init__.py index 692a7fcb6..e9d166258 100644 --- a/freqtrade/enums/__init__.py +++ b/freqtrade/enums/__init__.py @@ -4,6 +4,6 @@ from freqtrade.enums.collateral import Collateral from freqtrade.enums.rpcmessagetype import RPCMessageType from freqtrade.enums.runmode import NON_UTIL_MODES, OPTIMIZE_MODES, TRADING_MODES, RunMode from freqtrade.enums.selltype import SellType -from freqtrade.enums.signaltype import SignalTagType, SignalType +from freqtrade.enums.signaltype import SignalDirection, SignalTagType, SignalType from freqtrade.enums.state import State from freqtrade.enums.tradingmode import TradingMode diff --git a/freqtrade/enums/signaltype.py b/freqtrade/enums/signaltype.py index ca4b8482e..28f0676dd 100644 --- a/freqtrade/enums/signaltype.py +++ b/freqtrade/enums/signaltype.py @@ -17,3 +17,8 @@ class SignalTagType(Enum): """ BUY_TAG = "buy_tag" SHORT_TAG = "short_tag" + + +class SignalDirection(Enum): + LONG = 'long' + SHORT = 'short' diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 75f8d93ec..9d4e6b26f 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -420,19 +420,19 @@ class FreqtradeBot(LoggingMixin): return False # running get_signal on historical data fetched - (enter, exit_, enter_tag) = self.strategy.get_signal( - pair, - self.strategy.timeframe, - analyzed_df - ) + (side, enter_tag) = self.strategy.get_enter_signal( + pair, self.strategy.timeframe, analyzed_df + ) - if enter and not exit_: + if side: stake_amount = self.wallets.get_trade_stake_amount(pair, self.edge) bid_check_dom = self.config.get('bid_strategy', {}).get('check_depth_of_market', {}) if ((bid_check_dom.get('enabled', False)) and (bid_check_dom.get('bids_to_ask_delta', 0) > 0)): + # TODO-lev: Does the below need to be adjusted for shorts? if self._check_depth_of_market_buy(pair, bid_check_dom): + # TODO-lev: pass in "enter" as side. return self.execute_buy(pair, stake_amount, enter_tag=enter_tag) else: return False @@ -707,7 +707,7 @@ class FreqtradeBot(LoggingMixin): analyzed_df, _ = self.dataprovider.get_analyzed_dataframe(trade.pair, self.strategy.timeframe) - (buy, sell, _) = self.strategy.get_signal( + (buy, sell) = self.strategy.get_exit_signal( trade.pair, self.strategy.timeframe, analyzed_df diff --git a/freqtrade/strategy/interface.py b/freqtrade/strategy/interface.py index 1aa9d3867..a8e6d7f76 100644 --- a/freqtrade/strategy/interface.py +++ b/freqtrade/strategy/interface.py @@ -13,7 +13,7 @@ from pandas import DataFrame from freqtrade.constants import ListPairsWithTimeframes from freqtrade.data.dataprovider import DataProvider -from freqtrade.enums import SellType, SignalTagType, SignalType +from freqtrade.enums import SellType, SignalTagType, SignalType, SignalDirection from freqtrade.exceptions import OperationalException, StrategyError from freqtrade.exchange import timeframe_to_minutes, timeframe_to_seconds from freqtrade.exchange.exchange import timeframe_to_next_date @@ -538,22 +538,18 @@ class IStrategy(ABC, HyperStrategyMixin): else: raise StrategyError(message) - def get_signal( + def get_latest_candle( self, pair: str, timeframe: str, dataframe: DataFrame, - is_short: bool = False - ) -> Tuple[bool, bool, Optional[str]]: + ) -> Tuple[Optional[DataFrame], arrow.Arrow]: """ - Calculates current signal based based on the buy/short or sell/exit_short - columns of the dataframe. - Used by Bot to get the signal to buy, sell, short, or exit_short + Get the latest candle. Used only during real mode :param pair: pair in format ANT/BTC :param timeframe: timeframe to use :param dataframe: Analyzed dataframe to get signal from. - :return: (Buy, Sell)/(Short, Exit_short) A bool-tuple indicating - (buy/sell)/(short/exit_short) signal + :return: (None, None) or (Dataframe, latest_date) - corresponding to the last candle """ if not isinstance(dataframe, DataFrame) or dataframe.empty: logger.warning(f'Empty candle (OHLCV) data for pair {pair}') @@ -572,34 +568,89 @@ class IStrategy(ABC, HyperStrategyMixin): 'Outdated history for pair %s. Last tick is %s minutes old', pair, int((arrow.utcnow() - latest_date).total_seconds() // 60) ) + return None, None + return latest, latest_date + + def get_exit_signal( + self, + pair: str, + timeframe: str, + dataframe: DataFrame, + is_short: bool = None + ) -> Tuple[bool, bool]: + """ + Calculates current exit signal based based on the buy/short or sell/exit_short + columns of the dataframe. + Used by Bot to get the signal to exit. + depending on is_short, looks at "short" or "long" columns. + :param pair: pair in format ANT/BTC + :param timeframe: timeframe to use + :param dataframe: Analyzed dataframe to get signal from. + :param is_short: Indicating existing trade direction. + :return: (enter, exit) A bool-tuple with enter / exit values. + """ + latest, latest_date = self.get_latest_candle(pair, timeframe, dataframe) + if latest is None: return False, False, None - (enter_type, enter_tag) = ( - (SignalType.SHORT, SignalTagType.SHORT_TAG) - if is_short else - (SignalType.BUY, SignalTagType.BUY_TAG) - ) - exit_type = SignalType.EXIT_SHORT if is_short else SignalType.SELL + if is_short: + enter = latest[SignalType.SHORT] == 1 + exit_ = latest[SignalType.EXIT_SHORT] == 1 + else: + enter = latest[SignalType.BUY] == 1 + exit_ = latest[SignalType.SELL] == 1 - enter = latest[enter_type.value] == 1 + logger.debug(f"exit-trigger: {latest['date']} (pair={pair}) " + f"enter={enter} exit={exit_}") - exit = False - if exit_type.value in latest: - exit = latest[exit_type.value] == 1 + return enter, exit_ - enter_tag_value = latest.get(enter_tag.value, None) + def get_enter_signal( + self, + pair: str, + timeframe: str, + dataframe: DataFrame, + ) -> Tuple[Optional[SignalDirection], Optional[str]]: + """ + Calculates current entry signal based based on the buy/short or sell/exit_short + columns of the dataframe. + Used by Bot to get the signal to buy, sell, short, or exit_short + :param pair: pair in format ANT/BTC + :param timeframe: timeframe to use + :param dataframe: Analyzed dataframe to get signal from. + :return: (SignalDirection, entry_tag) + """ + latest, latest_date = self.get_latest_candle(pair, timeframe, dataframe) + if latest is None: + return False, False, None + + enter_long = latest[SignalType.BUY] == 1 + exit_long = latest[SignalType.SELL] == 1 + enter_short = latest[SignalType.SHORT] == 1 + exit_short = latest[SignalType.EXIT_SHORT] == 1 + + enter_signal: Optional[SignalDirection] = None + enter_tag_value = None + if enter_long == 1 and not any([exit_long, enter_short]): + enter_signal = SignalDirection.LONG + enter_tag_value = latest.get(SignalTagType.BUY_TAG, None) + if enter_short == 1 and not any([exit_short, enter_long]): + enter_signal = SignalDirection.SHORT + enter_tag_value = latest.get(SignalTagType.SHORT_TAG, None) - logger.debug(f'trigger: %s (pair=%s) {enter_type.value}=%s {exit_type.value}=%s', - latest['date'], pair, str(enter), str(exit)) timeframe_seconds = timeframe_to_seconds(timeframe) + if self.ignore_expired_candle( latest_date=latest_date, current_time=datetime.now(timezone.utc), timeframe_seconds=timeframe_seconds, - enter=enter + enter=enter_signal ): - return False, exit, enter_tag_value - return enter, exit, enter_tag_value + return False, enter_tag_value + + logger.debug(f"entry trigger: {latest['date']} (pair={pair}) " + f"enter={enter_long} enter_tag_value={enter_tag_value}") + return enter_signal, enter_tag_value def ignore_expired_candle( self, From f9f32a15bb6d9122030a72af58a84eb66f7a1019 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 24 Aug 2021 20:30:42 +0200 Subject: [PATCH 21/31] Update plotting tests for new strategy interface --- freqtrade/plot/plotting.py | 9 +++++---- tests/test_plotting.py | 8 ++++---- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/freqtrade/plot/plotting.py b/freqtrade/plot/plotting.py index 509c03e90..43b61cf67 100644 --- a/freqtrade/plot/plotting.py +++ b/freqtrade/plot/plotting.py @@ -386,8 +386,9 @@ def generate_candlestick_graph(pair: str, data: pd.DataFrame, trades: pd.DataFra ) fig.add_trace(candles, 1, 1) - if 'buy' in data.columns: - df_buy = data[data['buy'] == 1] + # TODO-lev: Needs short equivalent + if 'enter_long' in data.columns: + df_buy = data[data['enter_long'] == 1] if len(df_buy) > 0: buys = go.Scatter( x=df_buy.date, @@ -405,8 +406,8 @@ def generate_candlestick_graph(pair: str, data: pd.DataFrame, trades: pd.DataFra else: logger.warning("No buy-signals found.") - if 'sell' in data.columns: - df_sell = data[data['sell'] == 1] + if 'exit_long' in data.columns: + df_sell = data[data['exit_long'] == 1] if len(df_sell) > 0: sells = go.Scatter( x=df_sell.date, diff --git a/tests/test_plotting.py b/tests/test_plotting.py index ecadc3f8b..773fe8a5d 100644 --- a/tests/test_plotting.py +++ b/tests/test_plotting.py @@ -203,8 +203,8 @@ def test_generate_candlestick_graph_no_signals_no_trades(default_conf, mocker, t timerange = TimeRange(None, 'line', 0, -1000) data = history.load_pair_history(pair=pair, timeframe='1m', datadir=testdatadir, timerange=timerange) - data['buy'] = 0 - data['sell'] = 0 + data['enter_long'] = 0 + data['exit_long'] = 0 indicators1 = [] indicators2 = [] @@ -264,12 +264,12 @@ def test_generate_candlestick_graph_no_trades(default_conf, mocker, testdatadir) buy = find_trace_in_fig_data(figure.data, "buy") assert isinstance(buy, go.Scatter) # All buy-signals should be plotted - assert int(data.buy.sum()) == len(buy.x) + assert int(data['enter_long'].sum()) == len(buy.x) sell = find_trace_in_fig_data(figure.data, "sell") assert isinstance(sell, go.Scatter) # All buy-signals should be plotted - assert int(data.sell.sum()) == len(sell.x) + assert int(data['exit_long'].sum()) == len(sell.x) assert find_trace_in_fig_data(figure.data, "Bollinger Band") From f3b6a0a7973699755f4a276932bc0de06a09563d Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 24 Aug 2021 20:40:35 +0200 Subject: [PATCH 22/31] Fix some type errors --- freqtrade/freqtradebot.py | 18 +++++++++--------- freqtrade/strategy/interface.py | 18 +++++++++--------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 9d4e6b26f..0ddee5292 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -700,22 +700,22 @@ class FreqtradeBot(LoggingMixin): logger.debug('Handling %s ...', trade) - (buy, sell) = (False, False) + (enter, exit_) = (False, False) if (self.config.get('use_sell_signal', True) or self.config.get('ignore_roi_if_buy_signal', False)): analyzed_df, _ = self.dataprovider.get_analyzed_dataframe(trade.pair, self.strategy.timeframe) - (buy, sell) = self.strategy.get_exit_signal( + (enter, exit_) = self.strategy.get_exit_signal( trade.pair, self.strategy.timeframe, analyzed_df ) - logger.debug('checking sell') + # TODO-lev: side should depend on trade side. sell_rate = self.exchange.get_rate(trade.pair, refresh=True, side="sell") - if self._check_and_execute_sell(trade, sell_rate, buy, sell): + if self._check_and_execute_exit(trade, sell_rate, enter, exit_): return True logger.debug('Found no sell signal for %s.', trade) @@ -852,18 +852,18 @@ class FreqtradeBot(LoggingMixin): logger.warning(f"Could not create trailing stoploss order " f"for pair {trade.pair}.") - def _check_and_execute_sell(self, trade: Trade, sell_rate: float, - buy: bool, sell: bool) -> bool: + def _check_and_execute_exit(self, trade: Trade, sell_rate: float, + enter: bool, exit_: bool) -> bool: """ - Check and execute sell + Check and execute trade exit """ should_exit: SellCheckTuple = self.strategy.should_exit( - trade, sell_rate, datetime.now(timezone.utc), buy, sell, + trade, sell_rate, datetime.now(timezone.utc), enter, exit_, force_stoploss=self.edge.stoploss(trade.pair) if self.edge else 0 ) if should_exit.sell_flag: - logger.info(f'Executing Sell for {trade.pair}. Reason: {should_exit.sell_type}') + logger.info(f'Exit for {trade.pair} detected. Reason: {should_exit.sell_type}') self.execute_sell(trade, sell_rate, should_exit) return True return False diff --git a/freqtrade/strategy/interface.py b/freqtrade/strategy/interface.py index a8e6d7f76..000e2b2dd 100644 --- a/freqtrade/strategy/interface.py +++ b/freqtrade/strategy/interface.py @@ -543,7 +543,7 @@ class IStrategy(ABC, HyperStrategyMixin): pair: str, timeframe: str, dataframe: DataFrame, - ) -> Tuple[Optional[DataFrame], arrow.Arrow]: + ) -> Tuple[Optional[DataFrame], Optional[arrow.Arrow]]: """ Get the latest candle. Used only during real mode :param pair: pair in format ANT/BTC @@ -553,7 +553,7 @@ class IStrategy(ABC, HyperStrategyMixin): """ if not isinstance(dataframe, DataFrame) or dataframe.empty: logger.warning(f'Empty candle (OHLCV) data for pair {pair}') - return False, False, None + return None, None latest_date = dataframe['date'].max() latest = dataframe.loc[dataframe['date'] == latest_date].iloc[-1] @@ -591,7 +591,7 @@ class IStrategy(ABC, HyperStrategyMixin): """ latest, latest_date = self.get_latest_candle(pair, timeframe, dataframe) if latest is None: - return False, False, None + return False, False if is_short: enter = latest[SignalType.SHORT] == 1 @@ -621,8 +621,8 @@ class IStrategy(ABC, HyperStrategyMixin): :return: (SignalDirection, entry_tag) """ latest, latest_date = self.get_latest_candle(pair, timeframe, dataframe) - if latest is None: - return False, False, None + if latest is None or latest_date is None: + return None, None enter_long = latest[SignalType.BUY] == 1 exit_long = latest[SignalType.SELL] == 1 @@ -630,7 +630,7 @@ class IStrategy(ABC, HyperStrategyMixin): exit_short = latest[SignalType.EXIT_SHORT] == 1 enter_signal: Optional[SignalDirection] = None - enter_tag_value = None + enter_tag_value: Optional[str] = None if enter_long == 1 and not any([exit_long, enter_short]): enter_signal = SignalDirection.LONG enter_tag_value = latest.get(SignalTagType.BUY_TAG, None) @@ -641,12 +641,12 @@ class IStrategy(ABC, HyperStrategyMixin): timeframe_seconds = timeframe_to_seconds(timeframe) if self.ignore_expired_candle( - latest_date=latest_date, + latest_date=latest_date.datetime, current_time=datetime.now(timezone.utc), timeframe_seconds=timeframe_seconds, - enter=enter_signal + enter=bool(enter_signal) ): - return False, enter_tag_value + return None, enter_tag_value logger.debug(f"entry trigger: {latest['date']} (pair={pair}) " f"enter={enter_long} enter_tag_value={enter_tag_value}") From 6524edbb4e17472b6893d9a669cd31825fafa9d8 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 24 Aug 2021 20:47:54 +0200 Subject: [PATCH 23/31] Simplify should_exit interface --- freqtrade/freqtradebot.py | 2 +- freqtrade/optimize/backtesting.py | 5 +++-- freqtrade/strategy/interface.py | 6 +----- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 0ddee5292..7c43b599d 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -858,7 +858,7 @@ class FreqtradeBot(LoggingMixin): Check and execute trade exit """ should_exit: SellCheckTuple = self.strategy.should_exit( - trade, sell_rate, datetime.now(timezone.utc), enter, exit_, + trade, sell_rate, datetime.now(timezone.utc), enter=enter, exit_=exit_, force_stoploss=self.edge.stoploss(trade.pair) if self.edge else 0 ) diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index c3cd5b114..3bd7f178c 100644 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -333,10 +333,11 @@ class Backtesting: def _get_sell_trade_entry(self, trade: LocalTrade, sell_row: Tuple) -> Optional[LocalTrade]: sell_candle_time = sell_row[DATE_IDX].to_pydatetime() + enter = sell_row[LONG_IDX] if trade.is_short else sell_row[SHORT_IDX] + exit_ = sell_row[ELONG_IDX] if trade.is_short else sell_row[ESHORT_IDX] sell = self.strategy.should_exit( trade, sell_row[OPEN_IDX], sell_candle_time, # type: ignore - enter_long=sell_row[LONG_IDX], enter_short=sell_row[SHORT_IDX], - exit_long=sell_row[ELONG_IDX], exit_short=sell_row[ESHORT_IDX], + enter=enter, exit_=exit_, low=sell_row[LOW_IDX], high=sell_row[HIGH_IDX] ) diff --git a/freqtrade/strategy/interface.py b/freqtrade/strategy/interface.py index 000e2b2dd..f9919877c 100644 --- a/freqtrade/strategy/interface.py +++ b/freqtrade/strategy/interface.py @@ -666,8 +666,7 @@ class IStrategy(ABC, HyperStrategyMixin): return False def should_exit(self, trade: Trade, rate: float, date: datetime, *, - enter_long: bool, enter_short: bool, - exit_long: bool, exit_short: bool, + enter: bool, exit_: bool, low: float = None, high: float = None, force_stoploss: float = 0) -> SellCheckTuple: """ @@ -679,9 +678,6 @@ class IStrategy(ABC, HyperStrategyMixin): :return: True if trade should be exited, False otherwise """ - enter = enter_short if trade.is_short else enter_long - exit_ = exit_short if trade.is_short else exit_long - current_rate = rate current_profit = trade.calc_profit_ratio(current_rate) From b951f59f89e8f9e98b3f5338328af9972700a2db Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 24 Aug 2021 21:03:13 +0200 Subject: [PATCH 24/31] Fix patch_get_signal --- freqtrade/freqtradebot.py | 2 +- freqtrade/strategy/interface.py | 2 +- tests/conftest.py | 28 ++++++++++++++++++++++++++-- tests/test_integration.py | 4 ++-- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 7c43b599d..e6be897f2 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -710,7 +710,7 @@ class FreqtradeBot(LoggingMixin): (enter, exit_) = self.strategy.get_exit_signal( trade.pair, self.strategy.timeframe, - analyzed_df + analyzed_df, is_short=trade.is_short ) # TODO-lev: side should depend on trade side. diff --git a/freqtrade/strategy/interface.py b/freqtrade/strategy/interface.py index f9919877c..04740b845 100644 --- a/freqtrade/strategy/interface.py +++ b/freqtrade/strategy/interface.py @@ -13,7 +13,7 @@ from pandas import DataFrame from freqtrade.constants import ListPairsWithTimeframes from freqtrade.data.dataprovider import DataProvider -from freqtrade.enums import SellType, SignalTagType, SignalType, SignalDirection +from freqtrade.enums import SellType, SignalDirection, SignalTagType, SignalType from freqtrade.exceptions import OperationalException, StrategyError from freqtrade.exchange import timeframe_to_minutes, timeframe_to_seconds from freqtrade.exchange.exchange import timeframe_to_next_date diff --git a/tests/conftest.py b/tests/conftest.py index 2b75956c4..03859d05c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -6,6 +6,7 @@ from copy import deepcopy from datetime import datetime, timedelta from functools import reduce from pathlib import Path +from typing import Optional from unittest.mock import MagicMock, Mock, PropertyMock import arrow @@ -18,6 +19,7 @@ from freqtrade.commands import Arguments from freqtrade.data.converter import ohlcv_to_dataframe from freqtrade.edge import Edge, PairInfo from freqtrade.enums import RunMode +from freqtrade.enums.signaltype import SignalDirection from freqtrade.exchange import Exchange from freqtrade.freqtradebot import FreqtradeBot from freqtrade.persistence import LocalTrade, Trade, init_db @@ -182,13 +184,35 @@ def get_patched_worker(mocker, config) -> Worker: return Worker(args=None, config=config) -def patch_get_signal(freqtrade: FreqtradeBot, value=(True, False, None)) -> None: +def patch_get_signal(freqtrade: FreqtradeBot, enter_long=True, exit_long=False, + enter_short=False, exit_short=False, enter_tag: Optional[str] = None) -> None: """ :param mocker: mocker to patch IStrategy class :param value: which value IStrategy.get_signal() must return + (buy, sell, buy_tag) :return: None """ - freqtrade.strategy.get_signal = lambda e, s, x: value + # returns (Signal-direction, signaname) + def patched_get_enter_signal(*args, **kwargs): + direction = None + if enter_long and not any([exit_long, enter_short]): + direction = SignalDirection.LONG + if enter_short and not any([exit_short, enter_long]): + direction = SignalDirection.SHORT + + return direction, enter_tag + + freqtrade.strategy.get_enter_signal = patched_get_enter_signal + + def patched_get_exit_signal(pair, timeframe, dataframe, is_short): + if is_short: + return enter_short, exit_short + else: + return enter_long, exit_long + + # returns (enter, exit) + freqtrade.strategy.get_exit_signal = patched_get_exit_signal + freqtrade.exchange.refresh_latest_ohlcv = lambda p: None diff --git a/tests/test_integration.py b/tests/test_integration.py index b12959a03..0f0d6f067 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -72,7 +72,7 @@ def test_may_execute_sell_stoploss_on_exchange_multi(default_conf, ticker, fee, create_stoploss_order=MagicMock(return_value=True), _notify_sell=MagicMock(), ) - mocker.patch("freqtrade.strategy.interface.IStrategy.should_sell", should_sell_mock) + mocker.patch("freqtrade.strategy.interface.IStrategy.should_exit", should_sell_mock) wallets_mock = mocker.patch("freqtrade.wallets.Wallets.update", MagicMock()) mocker.patch("freqtrade.wallets.Wallets.get_free", MagicMock(return_value=1000)) @@ -163,7 +163,7 @@ def test_forcebuy_last_unlimited(default_conf, ticker, fee, limit_buy_order, moc SellCheckTuple(sell_type=SellType.NONE), SellCheckTuple(sell_type=SellType.NONE)] ) - mocker.patch("freqtrade.strategy.interface.IStrategy.should_sell", should_sell_mock) + mocker.patch("freqtrade.strategy.interface.IStrategy.should_exit", should_sell_mock) freqtrade = get_patched_freqtradebot(mocker, default_conf) rpc = RPC(freqtrade) From 6b93c71d15da8ae9d76f9597856c7e4f1b74fe13 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 25 Aug 2021 06:43:58 +0200 Subject: [PATCH 25/31] Small refactorings, use only enter_long columns --- freqtrade/enums/signaltype.py | 6 ++-- freqtrade/freqtradebot.py | 2 +- freqtrade/strategy/interface.py | 22 +++++++------- tests/conftest.py | 4 +-- tests/strategy/test_interface.py | 39 ++++++++++++------------- tests/strategy/test_strategy_loading.py | 6 ++-- 6 files changed, 39 insertions(+), 40 deletions(-) diff --git a/freqtrade/enums/signaltype.py b/freqtrade/enums/signaltype.py index 28f0676dd..23316c15a 100644 --- a/freqtrade/enums/signaltype.py +++ b/freqtrade/enums/signaltype.py @@ -5,9 +5,9 @@ class SignalType(Enum): """ Enum to distinguish between buy and sell signals """ - BUY = "buy" # To be renamed to enter_long - SELL = "sell" # To be renamed to exit_long - SHORT = "short" # Should be "enter_short" + ENTER_LONG = "enter_long" + EXIT_LONG = "exit_long" + ENTER_SHORT = "enter_short" EXIT_SHORT = "exit_short" diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index e6be897f2..ab5ae383a 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -420,7 +420,7 @@ class FreqtradeBot(LoggingMixin): return False # running get_signal on historical data fetched - (side, enter_tag) = self.strategy.get_enter_signal( + (side, enter_tag) = self.strategy.get_entry_signal( pair, self.strategy.timeframe, analyzed_df ) diff --git a/freqtrade/strategy/interface.py b/freqtrade/strategy/interface.py index 04740b845..7daec6b8f 100644 --- a/freqtrade/strategy/interface.py +++ b/freqtrade/strategy/interface.py @@ -594,18 +594,18 @@ class IStrategy(ABC, HyperStrategyMixin): return False, False if is_short: - enter = latest[SignalType.SHORT] == 1 - exit_ = latest[SignalType.EXIT_SHORT] == 1 + enter = latest.get(SignalType.ENTER_SHORT, 0) == 1 + exit_ = latest.get(SignalType.EXIT_SHORT, 0) == 1 else: - enter = latest[SignalType.BUY] == 1 - exit_ = latest[SignalType.SELL] == 1 + enter = latest[SignalType.ENTER_LONG] == 1 + exit_ = latest.get(SignalType.EXIT_LONG, 0) == 1 logger.debug(f"exit-trigger: {latest['date']} (pair={pair}) " f"enter={enter} exit={exit_}") return enter, exit_ - def get_enter_signal( + def get_entry_signal( self, pair: str, timeframe: str, @@ -624,19 +624,19 @@ class IStrategy(ABC, HyperStrategyMixin): if latest is None or latest_date is None: return None, None - enter_long = latest[SignalType.BUY] == 1 - exit_long = latest[SignalType.SELL] == 1 - enter_short = latest[SignalType.SHORT] == 1 - exit_short = latest[SignalType.EXIT_SHORT] == 1 + enter_long = latest[SignalType.ENTER_LONG.value] == 1 + exit_long = latest.get(SignalType.EXIT_LONG.value, 0) == 1 + enter_short = latest.get(SignalType.ENTER_SHORT.value, 0) == 1 + exit_short = latest.get(SignalType.EXIT_SHORT.value, 0) == 1 enter_signal: Optional[SignalDirection] = None enter_tag_value: Optional[str] = None if enter_long == 1 and not any([exit_long, enter_short]): enter_signal = SignalDirection.LONG - enter_tag_value = latest.get(SignalTagType.BUY_TAG, None) + enter_tag_value = latest.get(SignalTagType.BUY_TAG.value, None) if enter_short == 1 and not any([exit_short, enter_long]): enter_signal = SignalDirection.SHORT - enter_tag_value = latest.get(SignalTagType.SHORT_TAG, None) + enter_tag_value = latest.get(SignalTagType.SHORT_TAG.value, None) timeframe_seconds = timeframe_to_seconds(timeframe) diff --git a/tests/conftest.py b/tests/conftest.py index 03859d05c..c146fd9ce 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -193,7 +193,7 @@ def patch_get_signal(freqtrade: FreqtradeBot, enter_long=True, exit_long=False, :return: None """ # returns (Signal-direction, signaname) - def patched_get_enter_signal(*args, **kwargs): + def patched_get_entry_signal(*args, **kwargs): direction = None if enter_long and not any([exit_long, enter_short]): direction = SignalDirection.LONG @@ -202,7 +202,7 @@ def patch_get_signal(freqtrade: FreqtradeBot, enter_long=True, exit_long=False, return direction, enter_tag - freqtrade.strategy.get_enter_signal = patched_get_enter_signal + freqtrade.strategy.get_entry_signal = patched_get_entry_signal def patched_get_exit_signal(pair, timeframe, dataframe, is_short): if is_short: diff --git a/tests/strategy/test_interface.py b/tests/strategy/test_interface.py index bfdf88dbb..831a06991 100644 --- a/tests/strategy/test_interface.py +++ b/tests/strategy/test_interface.py @@ -1,4 +1,5 @@ # pragma pylint: disable=missing-docstring, C0103 +from freqtrade.enums.signaltype import SignalDirection import logging from datetime import datetime, timedelta, timezone from pathlib import Path @@ -30,7 +31,7 @@ _STRATEGY = DefaultStrategy(config={}) _STRATEGY.dp = DataProvider({}, None, None) -def test_returns_latest_signal(mocker, default_conf, ohlcv_history): +def test_returns_latest_signal(default_conf, ohlcv_history): ohlcv_history.loc[1, 'date'] = arrow.utcnow() # Take a copy to correctly modify the call mocked_history = ohlcv_history.copy() @@ -67,18 +68,18 @@ def test_analyze_pair_empty(default_conf, mocker, caplog, ohlcv_history): assert log_has('Empty dataframe for pair ETH/BTC', caplog) -def test_get_signal_empty(default_conf, mocker, caplog): - assert (False, False, None) == _STRATEGY.get_signal( +def test_get_signal_empty(default_conf, caplog): + assert (None, None) == _STRATEGY.get_latest_candle( 'foo', default_conf['timeframe'], DataFrame() ) assert log_has('Empty candle (OHLCV) data for pair foo', caplog) caplog.clear() - assert (False, False, None) == _STRATEGY.get_signal('bar', default_conf['timeframe'], None) + assert (None, None) == _STRATEGY.get_latest_candle('bar', default_conf['timeframe'], None) assert log_has('Empty candle (OHLCV) data for pair bar', caplog) caplog.clear() - assert (False, False, None) == _STRATEGY.get_signal( + assert (None, None) == _STRATEGY.get_latest_candle( 'baz', default_conf['timeframe'], DataFrame([]) @@ -86,7 +87,7 @@ def test_get_signal_empty(default_conf, mocker, caplog): assert log_has('Empty candle (OHLCV) data for pair baz', caplog) -def test_get_signal_exception_valueerror(default_conf, mocker, caplog, ohlcv_history): +def test_get_signal_exception_valueerror(mocker, caplog, ohlcv_history): caplog.set_level(logging.INFO) mocker.patch.object(_STRATEGY.dp, 'ohlcv', return_value=ohlcv_history) mocker.patch.object( @@ -111,14 +112,14 @@ def test_get_signal_old_dataframe(default_conf, mocker, caplog, ohlcv_history): ohlcv_history.loc[1, 'date'] = arrow.utcnow().shift(minutes=-16) # Take a copy to correctly modify the call mocked_history = ohlcv_history.copy() - mocked_history['sell'] = 0 - mocked_history['buy'] = 0 - mocked_history.loc[1, 'buy'] = 1 + mocked_history['exit_long'] = 0 + mocked_history['enter_long'] = 0 + mocked_history.loc[1, 'enter_long'] = 1 caplog.set_level(logging.INFO) mocker.patch.object(_STRATEGY, 'assert_df') - assert (False, False, None) == _STRATEGY.get_signal( + assert (None, None) == _STRATEGY.get_latest_candle( 'xyz', default_conf['timeframe'], mocked_history @@ -134,13 +135,13 @@ def test_get_signal_no_sell_column(default_conf, mocker, caplog, ohlcv_history): mocked_history = ohlcv_history.copy() # Intentionally don't set sell column # mocked_history['sell'] = 0 - mocked_history['buy'] = 0 - mocked_history.loc[1, 'buy'] = 1 + mocked_history['enter_long'] = 0 + mocked_history.loc[1, 'enter_long'] = 1 caplog.set_level(logging.INFO) mocker.patch.object(_STRATEGY, 'assert_df') - assert (True, False, None) == _STRATEGY.get_signal( + assert (SignalDirection.LONG, None) == _STRATEGY.get_entry_signal( 'xyz', default_conf['timeframe'], mocked_history @@ -453,8 +454,7 @@ def test_custom_sell(default_conf, fee, caplog) -> None: now = arrow.utcnow().datetime res = strategy.should_exit(trade, 1, now, - enter_long=False, enter_short=False, - exit_long=False, exit_short=False, + enter=False, exit_=False, low=None, high=None) assert res.sell_flag is False @@ -462,8 +462,7 @@ def test_custom_sell(default_conf, fee, caplog) -> None: strategy.custom_sell = MagicMock(return_value=True) res = strategy.should_exit(trade, 1, now, - enter_long=False, enter_short=False, - exit_long=False, exit_short=False, + enter=False, exit_=False, low=None, high=None) assert res.sell_flag is True assert res.sell_type == SellType.CUSTOM_SELL @@ -472,8 +471,7 @@ def test_custom_sell(default_conf, fee, caplog) -> None: strategy.custom_sell = MagicMock(return_value='hello world') res = strategy.should_exit(trade, 1, now, - enter_long=False, enter_short=False, - exit_long=False, exit_short=False, + enter=False, exit_=False, low=None, high=None) assert res.sell_type == SellType.CUSTOM_SELL assert res.sell_flag is True @@ -482,8 +480,7 @@ def test_custom_sell(default_conf, fee, caplog) -> None: caplog.clear() strategy.custom_sell = MagicMock(return_value='h' * 100) res = strategy.should_exit(trade, 1, now, - enter_long=False, enter_short=False, - exit_long=False, exit_short=False, + enter=False, exit_=False, low=None, high=None) assert res.sell_type == SellType.CUSTOM_SELL assert res.sell_flag is True diff --git a/tests/strategy/test_strategy_loading.py b/tests/strategy/test_strategy_loading.py index 7e94b7ccc..7a15f8c0c 100644 --- a/tests/strategy/test_strategy_loading.py +++ b/tests/strategy/test_strategy_loading.py @@ -118,10 +118,12 @@ def test_strategy(result, default_conf): assert 'adx' in df_indicators dataframe = strategy.advise_buy(df_indicators, metadata=metadata) - assert 'buy' in dataframe.columns + assert 'buy' not in dataframe.columns + assert 'enter_long' in dataframe.columns dataframe = strategy.advise_sell(df_indicators, metadata=metadata) - assert 'sell' in dataframe.columns + assert 'sell' not in dataframe.columns + assert 'exit_long' in dataframe.columns def test_strategy_override_minimal_roi(caplog, default_conf): From cb4889398be8e3f2e9c3cd4afa80900313412faf Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 25 Aug 2021 07:03:48 +0200 Subject: [PATCH 26/31] Fix backtesting bug --- freqtrade/optimize/backtesting.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index 3bd7f178c..0ebb36b7c 100644 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -233,9 +233,12 @@ class Backtesting: if not pair_data.empty: # Cleanup from prior runs - pair_data.loc[:, 'enter_long'] = 0 + # TODO-lev: The below is not 100% compatible with the interface compatibility layer + if 'enter_long' in pair_data.columns: + pair_data.loc[:, 'enter_long'] = 0 pair_data.loc[:, 'enter_short'] = 0 - pair_data.loc[:, 'exit_long'] = 0 + if 'exit_long' in pair_data.columns: + pair_data.loc[:, 'exit_long'] = 0 pair_data.loc[:, 'exit_short'] = 0 pair_data.loc[:, 'long_tag'] = None pair_data.loc[:, 'short_tag'] = None From b61735937c70c94465a716409add7b463433d5d7 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 25 Aug 2021 20:56:16 +0200 Subject: [PATCH 27/31] Replace Patch_get_signal with proper calls --- tests/test_freqtradebot.py | 47 +++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index cbaf7c22c..7fa02d706 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -254,7 +254,7 @@ def test_edge_overrides_stoploss(limit_buy_order, fee, caplog, mocker, edge_conf # stoploss shoud be hit assert freqtrade.handle_trade(trade) is True - assert log_has('Executing Sell for NEO/BTC. Reason: stop_loss', caplog) + assert log_has('Exit for NEO/BTC detected. Reason: stop_loss', caplog) assert trade.sell_reason == SellType.STOP_LOSS.value @@ -536,7 +536,7 @@ def test_create_trade_no_signal(default_conf, fee, mocker) -> None: ) default_conf['stake_amount'] = 10 freqtrade = FreqtradeBot(default_conf) - patch_get_signal(freqtrade, value=(False, False, None)) + patch_get_signal(freqtrade, enter_long=False) Trade.query = MagicMock() Trade.query.filter = MagicMock() @@ -757,9 +757,10 @@ def test_process_informative_pairs_added(default_conf, ticker, mocker) -> None: refresh_latest_ohlcv=refresh_mock, ) inf_pairs = MagicMock(return_value=[("BTC/ETH", '1m'), ("ETH/USDT", "1h")]) - mocker.patch( - 'freqtrade.strategy.interface.IStrategy.get_signal', - return_value=(False, False, '') + mocker.patch.multiple( + 'freqtrade.strategy.interface.IStrategy', + get_exit_signal=MagicMock(return_value=(False, False)), + get_entry_signal=MagicMock(return_value=(None, None)) ) mocker.patch('time.sleep', return_value=None) @@ -1915,7 +1916,7 @@ def test_handle_trade(default_conf, limit_buy_order, limit_sell_order_open, limi assert trade.is_open is True freqtrade.wallets.update() - patch_get_signal(freqtrade, value=(False, True, None)) + patch_get_signal(freqtrade, enter_long=False, exit_long=True) assert freqtrade.handle_trade(trade) is True assert trade.open_order_id == limit_sell_order['id'] @@ -1943,7 +1944,7 @@ def test_handle_overlapping_signals(default_conf, ticker, limit_buy_order_open, ) freqtrade = FreqtradeBot(default_conf) - patch_get_signal(freqtrade, value=(True, True, None)) + patch_get_signal(freqtrade, enter_long=True, exit_long=True) freqtrade.strategy.min_roi_reached = MagicMock(return_value=False) freqtrade.enter_positions() @@ -1962,7 +1963,7 @@ def test_handle_overlapping_signals(default_conf, ticker, limit_buy_order_open, assert trades[0].is_open is True # Buy and Sell are not triggering, so doing nothing ... - patch_get_signal(freqtrade, value=(False, False, None)) + patch_get_signal(freqtrade, enter_long=False) assert freqtrade.handle_trade(trades[0]) is False trades = Trade.query.all() nb_trades = len(trades) @@ -1970,7 +1971,7 @@ def test_handle_overlapping_signals(default_conf, ticker, limit_buy_order_open, assert trades[0].is_open is True # Buy and Sell are triggering, so doing nothing ... - patch_get_signal(freqtrade, value=(True, True, None)) + patch_get_signal(freqtrade, enter_long=True, exit_long=True) assert freqtrade.handle_trade(trades[0]) is False trades = Trade.query.all() nb_trades = len(trades) @@ -1978,7 +1979,7 @@ def test_handle_overlapping_signals(default_conf, ticker, limit_buy_order_open, assert trades[0].is_open is True # Sell is triggering, guess what : we are Selling! - patch_get_signal(freqtrade, value=(False, True, None)) + patch_get_signal(freqtrade, enter_long=False, exit_long=True) trades = Trade.query.all() assert freqtrade.handle_trade(trades[0]) is True @@ -2012,7 +2013,7 @@ def test_handle_trade_roi(default_conf, ticker, limit_buy_order_open, # we might just want to check if we are in a sell condition without # executing # if ROI is reached we must sell - patch_get_signal(freqtrade, value=(False, True, None)) + patch_get_signal(freqtrade, enter_long=False, exit_long=True) assert freqtrade.handle_trade(trade) assert log_has("ETH/BTC - Required profit reached. sell_type=SellType.ROI", caplog) @@ -2041,10 +2042,10 @@ def test_handle_trade_use_sell_signal(default_conf, ticker, limit_buy_order_open trade = Trade.query.first() trade.is_open = True - patch_get_signal(freqtrade, value=(False, False, None)) + patch_get_signal(freqtrade, enter_long=False, exit_long=False) assert not freqtrade.handle_trade(trade) - patch_get_signal(freqtrade, value=(False, True, None)) + patch_get_signal(freqtrade, enter_long=False, exit_long=True) assert freqtrade.handle_trade(trade) assert log_has("ETH/BTC - Sell signal received. sell_type=SellType.SELL_SIGNAL", caplog) @@ -3154,7 +3155,7 @@ def test_sell_profit_only_enable_profit(default_conf, limit_buy_order, limit_buy trade = Trade.query.first() trade.update(limit_buy_order) freqtrade.wallets.update() - patch_get_signal(freqtrade, value=(False, True, None)) + patch_get_signal(freqtrade, enter_long=False, exit_long=True) assert freqtrade.handle_trade(trade) is False freqtrade.strategy.sell_profit_offset = 0.0 @@ -3192,7 +3193,7 @@ def test_sell_profit_only_disable_profit(default_conf, limit_buy_order, limit_bu trade = Trade.query.first() trade.update(limit_buy_order) freqtrade.wallets.update() - patch_get_signal(freqtrade, value=(False, True, None)) + patch_get_signal(freqtrade, enter_long=False, exit_long=True) assert freqtrade.handle_trade(trade) is True assert trade.sell_reason == SellType.SELL_SIGNAL.value @@ -3226,7 +3227,7 @@ def test_sell_profit_only_enable_loss(default_conf, limit_buy_order, limit_buy_o trade = Trade.query.first() trade.update(limit_buy_order) - patch_get_signal(freqtrade, value=(False, True, None)) + patch_get_signal(freqtrade, enter_long=False, exit_long=True) assert freqtrade.handle_trade(trade) is False @@ -3261,7 +3262,7 @@ def test_sell_profit_only_disable_loss(default_conf, limit_buy_order, limit_buy_ trade = Trade.query.first() trade.update(limit_buy_order) freqtrade.wallets.update() - patch_get_signal(freqtrade, value=(False, True, None)) + patch_get_signal(freqtrade, enter_long=False, exit_long=True) assert freqtrade.handle_trade(trade) is True assert trade.sell_reason == SellType.SELL_SIGNAL.value @@ -3293,7 +3294,7 @@ def test_sell_not_enough_balance(default_conf, limit_buy_order, limit_buy_order_ trade = Trade.query.first() amnt = trade.amount trade.update(limit_buy_order) - patch_get_signal(freqtrade, value=(False, True, None)) + patch_get_signal(freqtrade, enter_long=False, exit_long=True) mocker.patch('freqtrade.wallets.Wallets.get_free', MagicMock(return_value=trade.amount * 0.985)) assert freqtrade.handle_trade(trade) is True @@ -3415,11 +3416,11 @@ def test_ignore_roi_if_buy_signal(default_conf, limit_buy_order, limit_buy_order trade = Trade.query.first() trade.update(limit_buy_order) freqtrade.wallets.update() - patch_get_signal(freqtrade, value=(True, True, None)) + patch_get_signal(freqtrade, enter_long=True, exit_long=True) assert freqtrade.handle_trade(trade) is False # Test if buy-signal is absent (should sell due to roi = true) - patch_get_signal(freqtrade, value=(False, True, None)) + patch_get_signal(freqtrade, enter_long=False, exit_long=True) assert freqtrade.handle_trade(trade) is True assert trade.sell_reason == SellType.ROI.value @@ -3693,11 +3694,11 @@ def test_disable_ignore_roi_if_buy_signal(default_conf, limit_buy_order, limit_b trade = Trade.query.first() trade.update(limit_buy_order) # Sell due to min_roi_reached - patch_get_signal(freqtrade, value=(True, True, None)) + patch_get_signal(freqtrade, enter_long=True, exit_long=True) assert freqtrade.handle_trade(trade) is True # Test if buy-signal is absent - patch_get_signal(freqtrade, value=(False, True, None)) + patch_get_signal(freqtrade, enter_long=False, exit_long=True) assert freqtrade.handle_trade(trade) is True assert trade.sell_reason == SellType.SELL_SIGNAL.value @@ -4238,7 +4239,7 @@ def test_order_book_ask_strategy(default_conf, limit_buy_order_open, limit_buy_o freqtrade.wallets.update() assert trade.is_open is True - patch_get_signal(freqtrade, value=(False, True, None)) + patch_get_signal(freqtrade, enter_long=False, exit_long=True) assert freqtrade.handle_trade(trade) is True assert trade.close_rate_requested == order_book_l2.return_value['asks'][0][0] From 2e50948699fb5c241e5711e3b2f7ed739036a5ba Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 4 Sep 2021 20:23:51 +0200 Subject: [PATCH 28/31] Fix some tests --- freqtrade/freqtradebot.py | 4 +-- freqtrade/strategy/interface.py | 8 ++--- tests/optimize/test_backtesting.py | 46 ++++++++++++++---------- tests/strategy/test_interface.py | 56 ++++++++++++++++++++++-------- 4 files changed, 75 insertions(+), 39 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index f9bb8e77d..8ba1dcecc 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -434,11 +434,11 @@ class FreqtradeBot(LoggingMixin): if self._check_depth_of_market_buy(pair, bid_check_dom): # TODO-lev: pass in "enter" as side. - return self.execute_entry(pair, stake_amount, buy_tag=enter_tag) + return self.execute_entry(pair, stake_amount, enter_tag=enter_tag) else: return False - return self.execute_entry(pair, stake_amount, buy_tag=enter_tag) + return self.execute_entry(pair, stake_amount, enter_tag=enter_tag) else: return False diff --git a/freqtrade/strategy/interface.py b/freqtrade/strategy/interface.py index 5fc975ef7..e89811bd0 100644 --- a/freqtrade/strategy/interface.py +++ b/freqtrade/strategy/interface.py @@ -596,11 +596,11 @@ class IStrategy(ABC, HyperStrategyMixin): return False, False if is_short: - enter = latest.get(SignalType.ENTER_SHORT, 0) == 1 - exit_ = latest.get(SignalType.EXIT_SHORT, 0) == 1 + enter = latest.get(SignalType.ENTER_SHORT.value, 0) == 1 + exit_ = latest.get(SignalType.EXIT_SHORT.value, 0) == 1 else: - enter = latest[SignalType.ENTER_LONG] == 1 - exit_ = latest.get(SignalType.EXIT_LONG, 0) == 1 + enter = latest[SignalType.ENTER_LONG.value] == 1 + exit_ = latest.get(SignalType.EXIT_LONG.value, 0) == 1 logger.debug(f"exit-trigger: {latest['date']} (pair={pair}) " f"enter={enter} exit={exit_}") diff --git a/tests/optimize/test_backtesting.py b/tests/optimize/test_backtesting.py index bdb491441..3e3b16371 100644 --- a/tests/optimize/test_backtesting.py +++ b/tests/optimize/test_backtesting.py @@ -570,47 +570,54 @@ def test_backtest__get_sell_trade_entry(default_conf, fee, mocker) -> None: pair = 'UNITTEST/BTC' row = [ pd.Timestamp(year=2020, month=1, day=1, hour=4, minute=55, tzinfo=timezone.utc), - 1, # Buy 200, # Open - 201, # Close - 0, # Sell - 195, # Low 201.5, # High - '', # Buy Signal Name + 195, # Low + 201, # Close + 1, # enter_long + 0, # exit_long + 0, # enter_short + 0, # exit_hsort + '', # Long Signal Name + '', # Short Signal Name ] - trade = backtesting._enter_trade(pair, row=row) + trade = backtesting._enter_trade(pair, row=row, direction='long') assert isinstance(trade, LocalTrade) row_sell = [ pd.Timestamp(year=2020, month=1, day=1, hour=5, minute=0, tzinfo=timezone.utc), - 0, # Buy 200, # Open - 201, # Close - 0, # Sell - 195, # Low 210.5, # High - '', # Buy Signal Name + 195, # Low + 201, # Close + 0, # enter_long + 0, # exit_long + 0, # enter_short + 0, # exit_short + '', # long Signal Name + '', # Short Signal Name ] row_detail = pd.DataFrame( [ [ pd.Timestamp(year=2020, month=1, day=1, hour=5, minute=0, tzinfo=timezone.utc), - 1, 200, 199, 0, 197, 200.1, '', + 200, 200.1, 197, 199, 1, 0, 0, 0, '', '', ], [ pd.Timestamp(year=2020, month=1, day=1, hour=5, minute=1, tzinfo=timezone.utc), - 0, 199, 199.5, 0, 199, 199.7, '', + 199, 199.7, 199, 199.5, 0, 0, 0, 0, '', '' ], [ pd.Timestamp(year=2020, month=1, day=1, hour=5, minute=2, tzinfo=timezone.utc), - 0, 199.5, 200.5, 0, 199, 200.8, '', + 199.5, 200.8, 199, 200.9, 0, 0, 0, 0, '', '' ], [ pd.Timestamp(year=2020, month=1, day=1, hour=5, minute=3, tzinfo=timezone.utc), - 0, 200.5, 210.5, 0, 193, 210.5, '', # ROI sell (?) + 200.5, 210.5, 193, 210.5, 0, 0, 0, 0, '', '' # ROI sell (?) ], [ pd.Timestamp(year=2020, month=1, day=1, hour=5, minute=4, tzinfo=timezone.utc), - 0, 200, 199, 0, 193, 200.1, '', + 200, 200.1, 193, 199, 0, 0, 0, 0, '', '' ], - ], columns=["date", "buy", "open", "close", "sell", "low", "high", "buy_tag"] + ], columns=['date', 'open', 'high', 'low', 'close', 'enter_long', 'exit_long', + 'enter_short', 'exit_short', 'long_tag', 'short_tag'] ) # No data available. @@ -620,11 +627,12 @@ def test_backtest__get_sell_trade_entry(default_conf, fee, mocker) -> None: assert res.close_date_utc == datetime(2020, 1, 1, 5, 0, tzinfo=timezone.utc) # Enter new trade - trade = backtesting._enter_trade(pair, row=row) + trade = backtesting._enter_trade(pair, row=row, direction='long') assert isinstance(trade, LocalTrade) # Assign empty ... no result. backtesting.detail_data[pair] = pd.DataFrame( - [], columns=["date", "buy", "open", "close", "sell", "low", "high", "buy_tag"]) + [], columns=['date', 'open', 'high', 'low', 'close', 'enter_long', 'exit_long', + 'enter_short', 'exit_short', 'long_tag', 'short_tag']) res = backtesting._get_sell_trade_entry(trade, row) assert res is None diff --git a/tests/strategy/test_interface.py b/tests/strategy/test_interface.py index 39f0b8009..6f2adad33 100644 --- a/tests/strategy/test_interface.py +++ b/tests/strategy/test_interface.py @@ -31,28 +31,56 @@ _STRATEGY = StrategyTestV2(config={}) _STRATEGY.dp = DataProvider({}, None, None) -def test_returns_latest_signal(default_conf, ohlcv_history): +def test_returns_latest_signal(ohlcv_history): ohlcv_history.loc[1, 'date'] = arrow.utcnow() # Take a copy to correctly modify the call mocked_history = ohlcv_history.copy() - mocked_history['sell'] = 0 - mocked_history['buy'] = 0 - mocked_history.loc[1, 'sell'] = 1 + mocked_history['enter_long'] = 0 + mocked_history['exit_long'] = 0 + mocked_history['enter_short'] = 0 + mocked_history['exit_short'] = 0 + mocked_history.loc[1, 'exit_long'] = 1 - assert _STRATEGY.get_signal('ETH/BTC', '5m', mocked_history) == (False, True, None) - mocked_history.loc[1, 'sell'] = 0 - mocked_history.loc[1, 'buy'] = 1 + assert _STRATEGY.get_entry_signal('ETH/BTC', '5m', mocked_history) == (None, None) + assert _STRATEGY.get_exit_signal('ETH/BTC', '5m', mocked_history) == (False, True) + assert _STRATEGY.get_exit_signal('ETH/BTC', '5m', mocked_history, True) == (False, False) + mocked_history.loc[1, 'exit_long'] = 0 + mocked_history.loc[1, 'enter_long'] = 1 - assert _STRATEGY.get_signal('ETH/BTC', '5m', mocked_history) == (True, False, None) - mocked_history.loc[1, 'sell'] = 0 - mocked_history.loc[1, 'buy'] = 0 + assert _STRATEGY.get_entry_signal('ETH/BTC', '5m', mocked_history + ) == (SignalDirection.LONG, None) + assert _STRATEGY.get_exit_signal('ETH/BTC', '5m', mocked_history) == (True, False) + assert _STRATEGY.get_exit_signal('ETH/BTC', '5m', mocked_history, True) == (False, False) + mocked_history.loc[1, 'exit_long'] = 0 + mocked_history.loc[1, 'enter_long'] = 0 - assert _STRATEGY.get_signal('ETH/BTC', '5m', mocked_history) == (False, False, None) - mocked_history.loc[1, 'sell'] = 0 - mocked_history.loc[1, 'buy'] = 1 + assert _STRATEGY.get_entry_signal('ETH/BTC', '5m', mocked_history) == (None, None) + assert _STRATEGY.get_exit_signal('ETH/BTC', '5m', mocked_history) == (False, False) + assert _STRATEGY.get_exit_signal('ETH/BTC', '5m', mocked_history, True) == (False, False) + mocked_history.loc[1, 'exit_long'] = 0 + mocked_history.loc[1, 'enter_long'] = 1 mocked_history.loc[1, 'buy_tag'] = 'buy_signal_01' - assert _STRATEGY.get_signal('ETH/BTC', '5m', mocked_history) == (True, False, 'buy_signal_01') + assert _STRATEGY.get_entry_signal( + 'ETH/BTC', '5m', mocked_history) == (SignalDirection.LONG, 'buy_signal_01') + assert _STRATEGY.get_exit_signal('ETH/BTC', '5m', mocked_history) == (True, False) + assert _STRATEGY.get_exit_signal('ETH/BTC', '5m', mocked_history, True) == (False, False) + + mocked_history.loc[1, 'exit_long'] = 0 + mocked_history.loc[1, 'enter_long'] = 0 + mocked_history.loc[1, 'enter_short'] = 1 + mocked_history.loc[1, 'exit_short'] = 0 + assert _STRATEGY.get_entry_signal( + 'ETH/BTC', '5m', mocked_history) == (SignalDirection.SHORT, None) + assert _STRATEGY.get_exit_signal('ETH/BTC', '5m', mocked_history) == (False, False) + assert _STRATEGY.get_exit_signal('ETH/BTC', '5m', mocked_history, True) == (True, False) + + mocked_history.loc[1, 'enter_short'] = 0 + mocked_history.loc[1, 'exit_short'] = 1 + assert _STRATEGY.get_entry_signal( + 'ETH/BTC', '5m', mocked_history) == (None, None) + assert _STRATEGY.get_exit_signal('ETH/BTC', '5m', mocked_history) == (False, False) + assert _STRATEGY.get_exit_signal('ETH/BTC', '5m', mocked_history, True) == (False, True) def test_analyze_pair_empty(default_conf, mocker, caplog, ohlcv_history): From 49350f2a8ee6c2c3293325929fd0ffdece01bf15 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 5 Sep 2021 08:36:22 +0200 Subject: [PATCH 29/31] Fix backtesting test --- tests/optimize/test_backtesting.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/optimize/test_backtesting.py b/tests/optimize/test_backtesting.py index 3e3b16371..d2ccef9db 100644 --- a/tests/optimize/test_backtesting.py +++ b/tests/optimize/test_backtesting.py @@ -123,7 +123,7 @@ def _trend(signals, buy_value, sell_value): n = len(signals['low']) buy = np.zeros(n) sell = np.zeros(n) - for i in range(0, len(signals['enter_long'])): + for i in range(0, len(signals['date'])): if random.random() > 0.5: # Both buy and sell signals at same timeframe buy[i] = buy_value sell[i] = sell_value From 68b75af08e654e226c0993124875b85f3ca98336 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 5 Sep 2021 08:59:18 +0200 Subject: [PATCH 30/31] Fix bug with inversed sell signals in backtesting --- freqtrade/optimize/backtesting.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index ad6bdbf18..cf670f87d 100644 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -365,8 +365,8 @@ class Backtesting: def _get_sell_trade_entry_for_candle(self, trade: LocalTrade, sell_row: Tuple) -> Optional[LocalTrade]: sell_candle_time = sell_row[DATE_IDX].to_pydatetime() - enter = sell_row[LONG_IDX] if trade.is_short else sell_row[SHORT_IDX] - exit_ = sell_row[ELONG_IDX] if trade.is_short else sell_row[ESHORT_IDX] + enter = sell_row[SHORT_IDX] if trade.is_short else sell_row[LONG_IDX] + exit_ = sell_row[ESHORT_IDX] if trade.is_short else sell_row[ELONG_IDX] sell = self.strategy.should_exit( trade, sell_row[OPEN_IDX], sell_candle_time, # type: ignore enter=enter, exit_=exit_, From b752516f65604e6e06bed0f2282c85777dfbc3cf Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 5 Sep 2021 15:23:27 +0200 Subject: [PATCH 31/31] Edge should use new columns, too --- freqtrade/edge/edge_positioning.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/freqtrade/edge/edge_positioning.py b/freqtrade/edge/edge_positioning.py index 8fe87d674..b945dd1bd 100644 --- a/freqtrade/edge/edge_positioning.py +++ b/freqtrade/edge/edge_positioning.py @@ -159,7 +159,8 @@ class Edge: logger.info(f'Measuring data from {min_date.strftime(DATETIME_PRINT_FORMAT)} ' f'up to {max_date.strftime(DATETIME_PRINT_FORMAT)} ' f'({(max_date - min_date).days} days)..') - headers = ['date', 'buy', 'open', 'close', 'sell', 'high', 'low'] + # TODO-lev: Should edge support shorts? needs to be investigated further... + headers = ['date', 'open', 'high', 'low', 'close', 'enter_long', 'exit_long'] trades: list = [] for pair, pair_data in preprocessed.items(): @@ -387,8 +388,8 @@ class Edge: return final def _find_trades_for_stoploss_range(self, df, pair, stoploss_range): - buy_column = df['buy'].values - sell_column = df['sell'].values + buy_column = df['enter_long'].values + sell_column = df['exit_long'].values date_column = df['date'].values ohlc_columns = df[['open', 'high', 'low', 'close']].values