Add bt-error to UI backtest method.

This commit is contained in:
Matthias 2023-02-22 20:22:59 +01:00
parent 75bc5809a9
commit e6766b9b82
3 changed files with 19 additions and 2 deletions

View File

@ -29,6 +29,7 @@ router = APIRouter()
async def api_start_backtest( # noqa: C901 async def api_start_backtest( # noqa: C901
bt_settings: BacktestRequest, background_tasks: BackgroundTasks, bt_settings: BacktestRequest, background_tasks: BackgroundTasks,
config=Depends(get_config), ws_mode=Depends(is_webserver_mode)): config=Depends(get_config), ws_mode=Depends(is_webserver_mode)):
ApiServer._bt['bt_error'] = None
"""Start backtesting if not done so already""" """Start backtesting if not done so already"""
if ApiServer._bgtask_running: if ApiServer._bgtask_running:
raise RPCException('Bot Background task already running') raise RPCException('Bot Background task already running')
@ -120,6 +121,7 @@ async def api_start_backtest( # noqa: C901
except (Exception, OperationalException, DependencyException) as e: except (Exception, OperationalException, DependencyException) as e:
logger.exception(f"Backtesting caused an error: {e}") logger.exception(f"Backtesting caused an error: {e}")
ApiServer._bt['bt_error'] = str(e)
pass pass
finally: finally:
ApiServer._bgtask_running = False ApiServer._bgtask_running = False
@ -162,6 +164,14 @@ def api_get_backtest(ws_mode=Depends(is_webserver_mode)):
"progress": 0, "progress": 0,
"status_msg": "Backtest not yet executed" "status_msg": "Backtest not yet executed"
} }
if ApiServer._bt['bt_error']:
return {
"status": "error",
"running": False,
"step": "",
"progress": 0,
"status_msg": f"Backtest failed with {ApiServer._bt['bt_error']}"
}
return { return {
"status": "ended", "status": "ended",

View File

@ -41,6 +41,7 @@ class ApiServer(RPCHandler):
'data': None, 'data': None,
'timerange': None, 'timerange': None,
'last_config': {}, 'last_config': {},
'bt_error': None,
} }
_has_rpc: bool = False _has_rpc: bool = False
_bgtask_running: bool = False _bgtask_running: bool = False

View File

@ -1737,9 +1737,15 @@ def test_api_backtesting(botclient, mocker, fee, caplog, tmpdir):
data['stake_amount'] = 101 data['stake_amount'] = 101
mocker.patch('freqtrade.optimize.backtesting.Backtesting.backtest_one_strategy', mocker.patch('freqtrade.optimize.backtesting.Backtesting.backtest_one_strategy',
side_effect=DependencyException()) side_effect=DependencyException('DeadBeef'))
rc = client_post(client, f"{BASE_URI}/backtest", data=data) rc = client_post(client, f"{BASE_URI}/backtest", data=data)
assert log_has("Backtesting caused an error: ", caplog) assert log_has("Backtesting caused an error: DeadBeef", caplog)
rc = client_get(client, f"{BASE_URI}/backtest")
assert_response(rc)
result = rc.json()
assert result['status'] == 'error'
assert 'Backtest failed' in result['status_msg']
# Delete backtesting to avoid leakage since the backtest-object may stick around. # Delete backtesting to avoid leakage since the backtest-object may stick around.
rc = client_delete(client, f"{BASE_URI}/backtest") rc = client_delete(client, f"{BASE_URI}/backtest")