From 11bd8e912e7fa577ce760c7c0500e76c0312f940 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 24 Aug 2021 06:45:09 +0200 Subject: [PATCH] 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