mirror of
https://github.com/freqtrade/freqtrade.git
synced 2025-12-19 06:11:15 +00:00
feat: load backtest-analysis-data files from zipfile
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import logging
|
import logging
|
||||||
|
import zipfile
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
import joblib
|
import joblib
|
||||||
@@ -20,23 +21,52 @@ logger = logging.getLogger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
def _load_backtest_analysis_data(backtest_dir: Path, name: str):
|
def _load_backtest_analysis_data(backtest_dir: Path, name: str):
|
||||||
|
"""
|
||||||
|
Load backtest analysis data either from a pickle file or from within a zip file
|
||||||
|
:param backtest_dir: Directory containing backtest results
|
||||||
|
:param name: Name of the analysis data to load (signals, rejected, exited)
|
||||||
|
:return: Analysis data
|
||||||
|
"""
|
||||||
if backtest_dir.is_dir():
|
if backtest_dir.is_dir():
|
||||||
scpf = Path(
|
lbf = Path(get_latest_backtest_filename(backtest_dir))
|
||||||
backtest_dir,
|
zip_path = backtest_dir / lbf
|
||||||
Path(get_latest_backtest_filename(backtest_dir)).stem + "_" + name + ".pkl",
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
scpf = Path(backtest_dir.parent / f"{backtest_dir.stem}_{name}.pkl")
|
zip_path = backtest_dir
|
||||||
|
|
||||||
try:
|
if zip_path.suffix == ".zip":
|
||||||
with scpf.open("rb") as scp:
|
# Load from zip file
|
||||||
loaded_data = joblib.load(scp)
|
try:
|
||||||
logger.info(f"Loaded {name} candles: {str(scpf)}")
|
with zipfile.ZipFile(zip_path) as zipf:
|
||||||
except Exception:
|
# Files in zip are stored with just their base names
|
||||||
logger.exception(f"Cannot load {name} data from pickled results.")
|
analysis_name = f"{zip_path.stem}_{name}.pkl"
|
||||||
return None
|
try:
|
||||||
|
with zipf.open(analysis_name) as analysis_file:
|
||||||
|
loaded_data = joblib.load(analysis_file)
|
||||||
|
logger.info(
|
||||||
|
f"Loaded {name} candles from zip: {str(zip_path)}:{analysis_name}"
|
||||||
|
)
|
||||||
|
return loaded_data
|
||||||
|
except KeyError:
|
||||||
|
logger.exception(f"Cannot find {analysis_name} in {zip_path}")
|
||||||
|
return None
|
||||||
|
except zipfile.BadZipFile:
|
||||||
|
logger.exception(f"Bad zip file: {zip_path}")
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
# Load from separate pickle file
|
||||||
|
if backtest_dir.is_dir():
|
||||||
|
scpf = Path(backtest_dir, f"{zip_path.stem}_{name}.pkl")
|
||||||
|
else:
|
||||||
|
scpf = Path(backtest_dir.parent / f"{backtest_dir.stem}_{name}.pkl")
|
||||||
|
|
||||||
return loaded_data
|
try:
|
||||||
|
with scpf.open("rb") as scp:
|
||||||
|
loaded_data = joblib.load(scp)
|
||||||
|
logger.info(f"Loaded {name} candles: {str(scpf)}")
|
||||||
|
return loaded_data
|
||||||
|
except Exception:
|
||||||
|
logger.exception(f"Cannot load {name} data from pickled results.")
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def _load_rejected_signals(backtest_dir: Path):
|
def _load_rejected_signals(backtest_dir: Path):
|
||||||
|
|||||||
Reference in New Issue
Block a user