Add "get backtest historic results" endpoint

This commit is contained in:
Matthias 2022-04-11 19:44:47 +02:00
parent baefda80d1
commit d9039152ba
3 changed files with 42 additions and 2 deletions

View File

@ -160,6 +160,30 @@ def _load_and_merge_backtest_result(strategy_name: str, filename: Path, results:
break break
def _get_backtest_files(dirname: Path) -> List[Path]:
return reversed(sorted(dirname.glob('backtest-result-*-[0-9][0-9].json')))
def get_backtest_resultlist(dirname: Path):
"""
Get list of backtest results read from metadata files
"""
results = []
for filename in _get_backtest_files(dirname):
metadata = load_backtest_metadata(filename)
if not metadata:
continue
for s, v in metadata.items():
results.append({
'filename': filename.name,
'strategy': s,
'run_id': v['run_id'],
'backtest_start_time': v['backtest_start_time'],
})
return results
def find_existing_backtest_stats(dirname: Union[Path, str], run_ids: Dict[str, str], def find_existing_backtest_stats(dirname: Union[Path, str], run_ids: Dict[str, str],
min_backtest_date: datetime = None) -> Dict[str, Any]: min_backtest_date: datetime = None) -> Dict[str, Any]:
""" """
@ -179,7 +203,7 @@ def find_existing_backtest_stats(dirname: Union[Path, str], run_ids: Dict[str, s
} }
# Weird glob expression here avoids including .meta.json files. # Weird glob expression here avoids including .meta.json files.
for filename in reversed(sorted(dirname.glob('backtest-result-*-[0-9][0-9].json'))): for filename in _get_backtest_files(dirname):
metadata = load_backtest_metadata(filename) metadata = load_backtest_metadata(filename)
if not metadata: if not metadata:
# Files are sorted from newest to oldest. When file without metadata is encountered it # Files are sorted from newest to oldest. When file without metadata is encountered it

View File

@ -1,13 +1,16 @@
import asyncio import asyncio
import logging import logging
from copy import deepcopy from copy import deepcopy
from typing import List
from fastapi import APIRouter, BackgroundTasks, Depends from fastapi import APIRouter, BackgroundTasks, Depends
from freqtrade.configuration.config_validation import validate_config_consistency from freqtrade.configuration.config_validation import validate_config_consistency
from freqtrade.data.btanalysis import get_backtest_resultlist
from freqtrade.enums import BacktestState from freqtrade.enums import BacktestState
from freqtrade.exceptions import DependencyException from freqtrade.exceptions import DependencyException
from freqtrade.rpc.api_server.api_schemas import BacktestRequest, BacktestResponse from freqtrade.rpc.api_server.api_schemas import (BacktestHistoryEntry, BacktestRequest,
BacktestResponse)
from freqtrade.rpc.api_server.deps import get_config, is_webserver_mode from freqtrade.rpc.api_server.deps import get_config, is_webserver_mode
from freqtrade.rpc.api_server.webserver import ApiServer from freqtrade.rpc.api_server.webserver import ApiServer
from freqtrade.rpc.rpc import RPCException from freqtrade.rpc.rpc import RPCException
@ -200,3 +203,9 @@ def api_backtest_abort(ws_mode=Depends(is_webserver_mode)):
"progress": 0, "progress": 0,
"status_msg": "Backtest ended", "status_msg": "Backtest ended",
} }
@router.get('/backtest/history', response_model=List[BacktestHistoryEntry], tags=['webserver', 'backtest'])
def api_backtest_history(config=Depends(get_config), ws_mode=Depends(is_webserver_mode)):
# Get backtest result history, read from metadata files
return get_backtest_resultlist(config['user_data_dir'] / 'backtest_results')

View File

@ -421,6 +421,13 @@ class BacktestResponse(BaseModel):
backtest_result: Optional[Dict[str, Any]] backtest_result: Optional[Dict[str, Any]]
class BacktestHistoryEntry(BaseModel):
filename: str
strategy: str
run_id: str
backtest_start_time: int
class SysInfo(BaseModel): class SysInfo(BaseModel):
cpu_pct: List[float] cpu_pct: List[float]
ram_pct: float ram_pct: float