freqtrade_origin/freqtrade/commands/analyze_commands.py

65 lines
2.1 KiB
Python
Raw Normal View History

2022-05-22 22:34:31 +00:00
import logging
from pathlib import Path
from typing import Any
2022-05-22 22:34:31 +00:00
from freqtrade.enums import RunMode
from freqtrade.exceptions import ConfigurationError, OperationalException
2022-05-22 22:34:31 +00:00
logger = logging.getLogger(__name__)
def setup_analyze_configuration(args: dict[str, Any], method: RunMode) -> dict[str, Any]:
2022-05-22 22:34:31 +00:00
"""
Prepare the configuration for the entry/exit reason analysis module
:param args: Cli args from Arguments()
:param method: Bot running mode
:return: Configuration
"""
2024-10-03 03:33:52 +00:00
from freqtrade.configuration import setup_utils_configuration
2022-05-22 22:34:31 +00:00
config = setup_utils_configuration(args, method)
no_unlimited_runmodes = {
2024-05-12 14:27:03 +00:00
RunMode.BACKTEST: "backtesting",
2022-05-22 22:34:31 +00:00
}
if method in no_unlimited_runmodes.keys():
from freqtrade.data.btanalysis import get_latest_backtest_filename
2024-05-12 14:27:03 +00:00
if "exportfilename" in config:
if config["exportfilename"].is_dir():
btfile = Path(get_latest_backtest_filename(config["exportfilename"]))
2022-06-14 15:54:27 +00:00
signals_file = f"{config['exportfilename']}/{btfile.stem}_signals.pkl"
else:
2024-05-12 14:27:03 +00:00
if config["exportfilename"].exists():
btfile = Path(config["exportfilename"])
2022-06-14 15:54:27 +00:00
signals_file = f"{btfile.parent}/{btfile.stem}_signals.pkl"
else:
2024-03-19 06:06:43 +00:00
raise ConfigurationError(f"{config['exportfilename']} does not exist.")
2022-06-14 15:54:27 +00:00
else:
2024-05-12 14:27:03 +00:00
raise ConfigurationError("exportfilename not in config.")
2022-05-22 22:34:31 +00:00
2024-05-12 14:27:03 +00:00
if not Path(signals_file).exists():
raise OperationalException(
2023-03-19 16:50:08 +00:00
f"Cannot find latest backtest signals file: {signals_file}."
"Run backtesting with `--export signals`."
)
2022-05-22 22:34:31 +00:00
return config
def start_analysis_entries_exits(args: dict[str, Any]) -> None:
2022-05-22 22:34:31 +00:00
"""
Start analysis script
:param args: Cli args from Arguments()
:return: None
"""
from freqtrade.data.entryexitanalysis import process_entry_exit_reasons
# Initialize configuration
config = setup_analyze_configuration(args, RunMode.BACKTEST)
2024-05-12 14:27:03 +00:00
logger.info("Starting freqtrade in analysis mode")
2022-05-22 22:34:31 +00:00
process_entry_exit_reasons(config)