Allow dynamic stake for backtesting and hyperopt

This commit is contained in:
Matthias 2021-02-12 20:20:32 +01:00
parent e4abe902fc
commit 8d61a26382
4 changed files with 18 additions and 13 deletions

View File

@ -3,7 +3,7 @@ from typing import Any, Dict
from freqtrade import constants
from freqtrade.configuration import setup_utils_configuration
from freqtrade.exceptions import DependencyException, OperationalException
from freqtrade.exceptions import OperationalException
from freqtrade.state import RunMode
@ -23,10 +23,14 @@ def setup_optimize_configuration(args: Dict[str, Any], method: RunMode) -> Dict[
RunMode.HYPEROPT: 'hyperoptimization',
}
if (method in no_unlimited_runmodes.keys() and
config['stake_amount'] == constants.UNLIMITED_STAKE_AMOUNT):
raise DependencyException(
f'The value of `stake_amount` cannot be set as "{constants.UNLIMITED_STAKE_AMOUNT}" '
f'for {no_unlimited_runmodes[method]}')
config['stake_amount'] != constants.UNLIMITED_STAKE_AMOUNT and
config['max_open_trades'] != float('inf')):
pass
# config['dry_run_wallet'] = config['stake_amount'] * \
# config['max_open_trades'] * (2 - config['tradable_balance_ratio'])
# logger.warning(f"Changing dry-run-wallet to {config['dry_run_wallet']} "
# "(max_open_trades * stake_amount).")
return config

View File

@ -445,11 +445,11 @@ class Backtesting:
enable_protections=self.config.get('enable_protections', False),
)
backtest_end_time = datetime.now(timezone.utc)
print(self.wallets.get_all_balances())
self.all_results[self.strategy.get_strategy_name()] = {
'results': results,
'config': self.strategy.config,
'locks': PairLocks.get_all_locks(),
'final_balance': self.wallets.get_total(self.strategy.config['stake_currency']),
'backtest_start_time': int(backtest_start_time.timestamp()),
'backtest_end_time': int(backtest_end_time.timestamp()),
}

View File

@ -17,7 +17,7 @@ from freqtrade.data.btanalysis import BT_DATA_COLUMNS, evaluate_result_multi
from freqtrade.data.converter import clean_ohlcv_dataframe
from freqtrade.data.dataprovider import DataProvider
from freqtrade.data.history import get_timerange
from freqtrade.exceptions import DependencyException, OperationalException
from freqtrade.exceptions import OperationalException
from freqtrade.optimize.backtesting import Backtesting
from freqtrade.resolvers import StrategyResolver
from freqtrade.state import RunMode
@ -242,8 +242,9 @@ def test_setup_optimize_configuration_unlimited_stake_amount(mocker, default_con
'--strategy', 'DefaultStrategy',
]
with pytest.raises(DependencyException, match=r'.`stake_amount`.*'):
setup_optimize_configuration(get_args(args), RunMode.BACKTEST)
# TODO: does this test still make sense?
conf = setup_optimize_configuration(get_args(args), RunMode.BACKTEST)
assert isinstance(conf, dict)
def test_start(mocker, fee, default_conf, caplog) -> None:

View File

@ -15,7 +15,7 @@ from filelock import Timeout
from freqtrade import constants
from freqtrade.commands.optimize_commands import setup_optimize_configuration, start_hyperopt
from freqtrade.data.history import load_data
from freqtrade.exceptions import DependencyException, OperationalException
from freqtrade.exceptions import OperationalException
from freqtrade.optimize.hyperopt import Hyperopt
from freqtrade.resolvers.hyperopt_resolver import HyperOptResolver
from freqtrade.state import RunMode
@ -140,9 +140,9 @@ def test_setup_hyperopt_configuration_unlimited_stake_amount(mocker, default_con
'--config', 'config.json',
'--hyperopt', 'DefaultHyperOpt',
]
with pytest.raises(DependencyException, match=r'.`stake_amount`.*'):
setup_optimize_configuration(get_args(args), RunMode.HYPEROPT)
# TODO: does this test still make sense?
conf = setup_optimize_configuration(get_args(args), RunMode.HYPEROPT)
assert isinstance(conf, dict)
def test_hyperoptresolver(mocker, default_conf, caplog) -> None: