From ceddcd9242209211153e30c728df77f9e907b4a1 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 20 May 2023 11:45:06 +0200 Subject: [PATCH] Move most of the logic to lookahead_analysis helper --- freqtrade/commands/optimize_commands.py | 37 +-------------- .../optimize/lookahead_analysis_helpers.py | 47 ++++++++++++++++++- tests/optimize/test_lookahead_analysis.py | 1 + 3 files changed, 48 insertions(+), 37 deletions(-) diff --git a/freqtrade/commands/optimize_commands.py b/freqtrade/commands/optimize_commands.py index 06e9b7adb..4b8763737 100644 --- a/freqtrade/commands/optimize_commands.py +++ b/freqtrade/commands/optimize_commands.py @@ -6,7 +6,6 @@ from freqtrade.configuration import setup_utils_configuration from freqtrade.enums import RunMode from freqtrade.exceptions import OperationalException from freqtrade.misc import round_coin_value -from freqtrade.resolvers import StrategyResolver logger = logging.getLogger(__name__) @@ -144,40 +143,6 @@ def start_lookahead_analysis(args: Dict[str, Any]) -> None: from freqtrade.optimize.lookahead_analysis_helpers import LookaheadAnalysisSubFunctions config = setup_utils_configuration(args, RunMode.UTIL_NO_EXCHANGE) + LookaheadAnalysisSubFunctions.start(config) - if config['targeted_trade_amount'] < config['minimum_trade_amount']: - # add logic that tells the user to check the configuration - # since this combo doesn't make any sense. - raise OperationalException( - "targeted trade amount can't be smaller than minimum trade amount." - ) - strategy_objs = StrategyResolver.search_all_objects( - config, enum_failed=False, recursive=config.get('recursive_strategy_search', False)) - - lookaheadAnalysis_instances = [] - - # unify --strategy and --strategy_list to one list - if not (strategy_list := config.get('strategy_list', [])): - strategy_list = [config['strategy']] - - # check if strategies can be properly loaded, only check them if they can be. - for strat in strategy_list: - for strategy_obj in strategy_objs: - if strategy_obj['name'] == strat and strategy_obj not in strategy_list: - lookaheadAnalysis_instances.append( - LookaheadAnalysisSubFunctions.initialize_single_lookahead_analysis( - strategy_obj, config)) - break - - # report the results - if lookaheadAnalysis_instances: - LookaheadAnalysisSubFunctions.text_table_lookahead_analysis_instances( - lookaheadAnalysis_instances) - if config.get('lookahead_analysis_exportfilename') is not None: - LookaheadAnalysisSubFunctions.export_to_csv(config, lookaheadAnalysis_instances) - else: - logger.error("There were no strategies specified neither through " - "--strategy nor through " - "--strategy_list " - "or timeframe was not specified.") diff --git a/freqtrade/optimize/lookahead_analysis_helpers.py b/freqtrade/optimize/lookahead_analysis_helpers.py index 987a55b24..e2e9ffb42 100644 --- a/freqtrade/optimize/lookahead_analysis_helpers.py +++ b/freqtrade/optimize/lookahead_analysis_helpers.py @@ -1,10 +1,17 @@ +import logging import time from pathlib import Path from typing import Any, Dict, List import pandas as pd -from freqtrade.optimize.lookahead_analysis import LookaheadAnalysis, logger +from freqtrade.constants import Config +from freqtrade.exceptions import OperationalException +from freqtrade.optimize.lookahead_analysis import LookaheadAnalysis +from freqtrade.resolvers import StrategyResolver + + +logger = logging.getLogger(__name__) class LookaheadAnalysisSubFunctions: @@ -93,3 +100,41 @@ class LookaheadAnalysisSubFunctions: f"of {Path(strategy_obj['location']).name} " f"took {elapsed:.0f} seconds.") return current_instance + + @staticmethod + def start(config: Config): + if config['targeted_trade_amount'] < config['minimum_trade_amount']: + # this combo doesn't make any sense. + raise OperationalException( + "targeted trade amount can't be smaller than minimum trade amount." + ) + + strategy_objs = StrategyResolver.search_all_objects( + config, enum_failed=False, recursive=config.get('recursive_strategy_search', False)) + + lookaheadAnalysis_instances = [] + + # unify --strategy and --strategy_list to one list + if not (strategy_list := config.get('strategy_list', [])): + strategy_list = [config['strategy']] + + # check if strategies can be properly loaded, only check them if they can be. + for strat in strategy_list: + for strategy_obj in strategy_objs: + if strategy_obj['name'] == strat and strategy_obj not in strategy_list: + lookaheadAnalysis_instances.append( + LookaheadAnalysisSubFunctions.initialize_single_lookahead_analysis( + strategy_obj, config)) + break + + # report the results + if lookaheadAnalysis_instances: + LookaheadAnalysisSubFunctions.text_table_lookahead_analysis_instances( + lookaheadAnalysis_instances) + if config.get('lookahead_analysis_exportfilename') is not None: + LookaheadAnalysisSubFunctions.export_to_csv(config, lookaheadAnalysis_instances) + else: + logger.error("There were no strategies specified neither through " + "--strategy nor through " + "--strategy_list " + "or timeframe was not specified.") diff --git a/tests/optimize/test_lookahead_analysis.py b/tests/optimize/test_lookahead_analysis.py index 6872cb73a..3136d6a16 100644 --- a/tests/optimize/test_lookahead_analysis.py +++ b/tests/optimize/test_lookahead_analysis.py @@ -77,4 +77,5 @@ def test_biased_strategy(lookahead_conf, mocker, caplog) -> None: strategy_obj = {} strategy_obj['name'] = "strategy_test_v3_with_lookahead_bias" LookaheadAnalysis(lookahead_conf, strategy_obj) + pass