Add backtest stopping

This commit is contained in:
Matthias 2021-04-05 19:58:53 +02:00
parent 129c7b02d0
commit 830b2548bc
2 changed files with 30 additions and 1 deletions

View File

@ -120,6 +120,7 @@ class Backtesting:
self.required_startup = max([strat.startup_candle_count for strat in self.strategylist]) self.required_startup = max([strat.startup_candle_count for strat in self.strategylist])
self.progress = BTProgress() self.progress = BTProgress()
self.abort = False
def __del__(self): def __del__(self):
LoggingMixin.show_output = True LoggingMixin.show_output = True
@ -202,6 +203,8 @@ class Backtesting:
# Create dict with data # Create dict with data
for pair, pair_data in processed.items(): for pair, pair_data in processed.items():
if self.abort:
raise DependencyException("Stop requested")
self.progress.increment() self.progress.increment()
if not pair_data.empty: if not pair_data.empty:
pair_data.loc[:, 'buy'] = 0 # cleanup if buy_signal is exist pair_data.loc[:, 'buy'] = 0 # cleanup if buy_signal is exist
@ -419,7 +422,8 @@ class Backtesting:
# Loop timerange and get candle for each pair at that point in time # Loop timerange and get candle for each pair at that point in time
while tmp <= end_date: while tmp <= end_date:
open_trade_count_start = open_trade_count open_trade_count_start = open_trade_count
if self.abort:
raise DependencyException("Stop requested")
for i, pair in enumerate(data): for i, pair in enumerate(data):
row_index = indexes[pair] row_index = indexes[pair]
try: try:

View File

@ -6,6 +6,7 @@ from copy import deepcopy
from fastapi import APIRouter, BackgroundTasks, Depends from fastapi import APIRouter, BackgroundTasks, Depends
from freqtrade.enums import BacktestState from freqtrade.enums import BacktestState
from freqtrade.exceptions import DependencyException
from freqtrade.rpc.api_server.api_schemas import BacktestRequest, BacktestResponse from freqtrade.rpc.api_server.api_schemas import BacktestRequest, BacktestResponse
from freqtrade.rpc.api_server.deps import get_config from freqtrade.rpc.api_server.deps import get_config
from freqtrade.rpc.api_server.webserver import ApiServer from freqtrade.rpc.api_server.webserver import ApiServer
@ -64,6 +65,7 @@ async def api_start_backtest(bt_settings: BacktestRequest, background_tasks: Bac
lastconfig['timeframe'] = strat.timeframe lastconfig['timeframe'] = strat.timeframe
ApiServer._backtestdata, ApiServer._bt_timerange = ApiServer._bt.load_bt_data() ApiServer._backtestdata, ApiServer._bt_timerange = ApiServer._bt.load_bt_data()
ApiServer._bt.abort = False
min_date, max_date = ApiServer._bt.backtest_one_strategy( min_date, max_date = ApiServer._bt.backtest_one_strategy(
strat, ApiServer._backtestdata, strat, ApiServer._backtestdata,
ApiServer._bt_timerange) ApiServer._bt_timerange)
@ -72,6 +74,9 @@ async def api_start_backtest(bt_settings: BacktestRequest, background_tasks: Bac
min_date=min_date, max_date=max_date) min_date=min_date, max_date=max_date)
logger.info("Backtest finished.") logger.info("Backtest finished.")
except DependencyException as e:
logger.info(f"Backtesting caused an error: {e}")
pass
finally: finally:
ApiServer._bgtask_running = False ApiServer._bgtask_running = False
@ -147,3 +152,23 @@ def api_delete_backtest():
"progress": 0, "progress": 0,
"status_msg": "Backtest reset", "status_msg": "Backtest reset",
} }
@router.get('/backtest/abort', response_model=BacktestResponse, tags=['webserver', 'backtest'])
def api_backtest_abort():
if not ApiServer._bgtask_running:
return {
"status": "not_running",
"running": False,
"step": "",
"progress": 0,
"status_msg": "Backtest ended",
}
ApiServer._bt.abort = True
return {
"status": "stopping",
"running": False,
"step": "",
"progress": 0,
"status_msg": "Backtest ended",
}