Add test for can_short strategy attribute

This commit is contained in:
Matthias 2022-03-11 19:37:45 +01:00
parent 1c9dbb512a
commit 20fc9459f2
4 changed files with 25 additions and 5 deletions

View File

@ -127,10 +127,9 @@ class Backtesting:
self.config['startup_candle_count'] = self.required_startup self.config['startup_candle_count'] = self.required_startup
self.exchange.validate_required_startup_candles(self.required_startup, self.timeframe) self.exchange.validate_required_startup_candles(self.required_startup, self.timeframe)
# TODO-lev: This should come from the configuration setting or better a
# TODO-lev: combination of config/strategy "use_shorts"(?) and "can_short" from the exchange
self.trading_mode: TradingMode = config.get('trading_mode', TradingMode.SPOT) self.trading_mode: TradingMode = config.get('trading_mode', TradingMode.SPOT)
self.margin_mode: MarginMode = config.get('margin_mode', MarginMode.NONE) self.margin_mode: MarginMode = config.get('margin_mode', MarginMode.NONE)
# strategies which define "can_short=True" will fail to load in Spot mode.
self._can_short = self.trading_mode != TradingMode.SPOT self._can_short = self.trading_mode != TradingMode.SPOT
self.progress = BTProgress() self.progress = BTProgress()

View File

@ -1382,6 +1382,7 @@ def test_api_strategies(botclient):
'InformativeDecoratorTest', 'InformativeDecoratorTest',
'StrategyTestV2', 'StrategyTestV2',
'StrategyTestV3', 'StrategyTestV3',
'StrategyTestV3Futures',
'TestStrategyLegacyV1', 'TestStrategyLegacyV1',
]} ]}

View File

@ -187,3 +187,7 @@ class StrategyTestV3(IStrategy):
return round(orders[0].cost, 0) return round(orders[0].cost, 0)
return None return None
class StrategyTestV3Futures(StrategyTestV3):
can_short = True

View File

@ -35,7 +35,7 @@ def test_search_all_strategies_no_failed():
directory = Path(__file__).parent / "strats" directory = Path(__file__).parent / "strats"
strategies = StrategyResolver.search_all_objects(directory, enum_failed=False) strategies = StrategyResolver.search_all_objects(directory, enum_failed=False)
assert isinstance(strategies, list) assert isinstance(strategies, list)
assert len(strategies) == 5 assert len(strategies) == 6
assert isinstance(strategies[0], dict) assert isinstance(strategies[0], dict)
@ -43,10 +43,10 @@ def test_search_all_strategies_with_failed():
directory = Path(__file__).parent / "strats" directory = Path(__file__).parent / "strats"
strategies = StrategyResolver.search_all_objects(directory, enum_failed=True) strategies = StrategyResolver.search_all_objects(directory, enum_failed=True)
assert isinstance(strategies, list) assert isinstance(strategies, list)
assert len(strategies) == 6 assert len(strategies) == 7
# with enum_failed=True search_all_objects() shall find 2 good strategies # with enum_failed=True search_all_objects() shall find 2 good strategies
# and 1 which fails to load # and 1 which fails to load
assert len([x for x in strategies if x['class'] is not None]) == 5 assert len([x for x in strategies if x['class'] is not None]) == 6
assert len([x for x in strategies if x['class'] is None]) == 1 assert len([x for x in strategies if x['class'] is None]) == 1
@ -128,6 +128,22 @@ def test_strategy_pre_v3(result, default_conf, strategy_name):
assert 'exit_long' in dataframe.columns assert 'exit_long' in dataframe.columns
def test_strategy_can_short(caplog, default_conf):
caplog.set_level(logging.INFO)
default_conf.update({
'strategy': CURRENT_TEST_STRATEGY,
})
strat = StrategyResolver.load_strategy(default_conf)
assert isinstance(strat, IStrategy)
default_conf['strategy'] = 'StrategyTestV3Futures'
with pytest.raises(ImportError, match=""):
StrategyResolver.load_strategy(default_conf)
default_conf['trading_mode'] = 'futures'
strat = StrategyResolver.load_strategy(default_conf)
assert isinstance(strat, IStrategy)
def test_strategy_override_minimal_roi(caplog, default_conf): def test_strategy_override_minimal_roi(caplog, default_conf):
caplog.set_level(logging.INFO) caplog.set_level(logging.INFO)
default_conf.update({ default_conf.update({