Merge pull request #2825 from yazeed/better_backtesting_tables

more consistent backtesting and sell reasons tables
This commit is contained in:
Matthias 2020-02-01 11:18:50 +01:00 committed by GitHub
commit 4a80c47fd1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 26 deletions

View File

@ -404,12 +404,12 @@ class Backtesting:
) )
# Execute backtest and print results # Execute backtest and print results
all_results[self.strategy.get_strategy_name()] = self.backtest( all_results[self.strategy.get_strategy_name()] = self.backtest(
processed=preprocessed, processed=preprocessed,
stake_amount=self.config['stake_amount'], stake_amount=self.config['stake_amount'],
start_date=min_date, start_date=min_date,
end_date=max_date, end_date=max_date,
max_open_trades=max_open_trades, max_open_trades=max_open_trades,
position_stacking=position_stacking, position_stacking=position_stacking,
) )
for strategy, results in all_results.items(): for strategy, results in all_results.items():
@ -426,7 +426,10 @@ class Backtesting:
results=results)) results=results))
print(' SELL REASON STATS '.center(133, '=')) print(' SELL REASON STATS '.center(133, '='))
print(generate_text_table_sell_reason(data, results)) print(generate_text_table_sell_reason(data,
stake_currency=self.config['stake_currency'],
max_open_trades=self.config['max_open_trades'],
results=results))
print(' LEFT OPEN TRADES REPORT '.center(133, '=')) print(' LEFT OPEN TRADES REPORT '.center(133, '='))
print(generate_text_table(data, print(generate_text_table(data,

View File

@ -19,9 +19,17 @@ def generate_text_table(data: Dict[str, Dict], stake_currency: str, max_open_tra
floatfmt = ('s', 'd', '.2f', '.2f', '.8f', '.2f', 'd', '.1f', '.1f') floatfmt = ('s', 'd', '.2f', '.2f', '.8f', '.2f', 'd', '.1f', '.1f')
tabular_data = [] tabular_data = []
headers = ['pair', 'buy count', 'avg profit %', 'cum profit %', headers = [
f'tot profit {stake_currency}', 'tot profit %', 'avg duration', 'Pair',
'profit', 'loss'] 'Buy Count',
'Avg Profit %',
'Cum Profit %',
f'Tot Profit {stake_currency}',
'Tot Profit %',
'Avg Duration',
'Wins',
'Losses'
]
for pair in data: for pair in data:
result = results[results.pair == pair] result = results[results.pair == pair]
if skip_nan and result.profit_abs.isnull().all(): if skip_nan and result.profit_abs.isnull().all():
@ -58,7 +66,9 @@ def generate_text_table(data: Dict[str, Dict], stake_currency: str, max_open_tra
floatfmt=floatfmt, tablefmt="pipe") # type: ignore floatfmt=floatfmt, tablefmt="pipe") # type: ignore
def generate_text_table_sell_reason(data: Dict[str, Dict], results: DataFrame) -> str: def generate_text_table_sell_reason(
data: Dict[str, Dict], stake_currency: str, max_open_trades: int, results: DataFrame
) -> str:
""" """
Generate small table outlining Backtest results Generate small table outlining Backtest results
:param data: Dict of <pair: dataframe> containing data that was used during backtesting. :param data: Dict of <pair: dataframe> containing data that was used during backtesting.
@ -66,13 +76,36 @@ def generate_text_table_sell_reason(data: Dict[str, Dict], results: DataFrame) -
:return: pretty printed table with tabulate as string :return: pretty printed table with tabulate as string
""" """
tabular_data = [] tabular_data = []
headers = ['Sell Reason', 'Count', 'Profit', 'Loss', 'Profit %'] headers = [
"Sell Reason",
"Sell Count",
"Wins",
"Losses",
"Avg Profit %",
"Cum Profit %",
f"Tot Profit {stake_currency}",
"Tot Profit %",
]
for reason, count in results['sell_reason'].value_counts().iteritems(): for reason, count in results['sell_reason'].value_counts().iteritems():
result = results.loc[results['sell_reason'] == reason] result = results.loc[results['sell_reason'] == reason]
profit = len(result[result['profit_abs'] >= 0]) profit = len(result[result['profit_abs'] >= 0])
loss = len(result[result['profit_abs'] < 0]) loss = len(result[result['profit_abs'] < 0])
profit_mean = round(result['profit_percent'].mean() * 100.0, 2) profit_mean = round(result['profit_percent'].mean() * 100.0, 2)
tabular_data.append([reason.value, count, profit, loss, profit_mean]) profit_sum = round(result["profit_percent"].sum() * 100.0, 2)
profit_tot = result['profit_abs'].sum()
profit_percent_tot = round(result['profit_percent'].sum() * 100.0 / max_open_trades, 2)
tabular_data.append(
[
reason.value,
count,
profit,
loss,
profit_mean,
profit_sum,
profit_tot,
profit_percent_tot,
]
)
return tabulate(tabular_data, headers=headers, tablefmt="pipe") return tabulate(tabular_data, headers=headers, tablefmt="pipe")

View File

@ -21,14 +21,14 @@ def test_generate_text_table(default_conf, mocker):
) )
result_str = ( result_str = (
'| pair | buy count | avg profit % | cum profit % | ' '| Pair | Buy Count | Avg Profit % | Cum Profit % | Tot Profit BTC '
'tot profit BTC | tot profit % | avg duration | profit | loss |\n' '| Tot Profit % | Avg Duration | Wins | Losses |\n'
'|:--------|------------:|---------------:|---------------:|' '|:--------|------------:|---------------:|---------------:|-----------------:'
'-----------------:|---------------:|:---------------|---------:|-------:|\n' '|---------------:|:---------------|-------:|---------:|\n'
'| ETH/BTC | 2 | 15.00 | 30.00 | ' '| ETH/BTC | 2 | 15.00 | 30.00 | 0.60000000 '
'0.60000000 | 15.00 | 0:20:00 | 2 | 0 |\n' '| 15.00 | 0:20:00 | 2 | 0 |\n'
'| TOTAL | 2 | 15.00 | 30.00 | ' '| TOTAL | 2 | 15.00 | 30.00 | 0.60000000 '
'0.60000000 | 15.00 | 0:20:00 | 2 | 0 |' '| 15.00 | 0:20:00 | 2 | 0 |'
) )
assert generate_text_table(data={'ETH/BTC': {}}, assert generate_text_table(data={'ETH/BTC': {}},
stake_currency='BTC', max_open_trades=2, stake_currency='BTC', max_open_trades=2,
@ -50,13 +50,19 @@ def test_generate_text_table_sell_reason(default_conf, mocker):
) )
result_str = ( result_str = (
'| Sell Reason | Count | Profit | Loss | Profit % |\n' '| Sell Reason | Sell Count | Wins | Losses | Avg Profit % |'
'|:--------------|--------:|---------:|-------:|-----------:|\n' ' Cum Profit % | Tot Profit BTC | Tot Profit % |\n'
'| roi | 2 | 2 | 0 | 15 |\n' '|:--------------|-------------:|-------:|---------:|---------------:|'
'| stop_loss | 1 | 0 | 1 | -10 |' '---------------:|-----------------:|---------------:|\n'
'| roi | 2 | 2 | 0 | 15 |'
' 30 | 0.6 | 15 |\n'
'| stop_loss | 1 | 0 | 1 | -10 |'
' -10 | -0.2 | -5 |'
) )
assert generate_text_table_sell_reason( assert generate_text_table_sell_reason(
data={'ETH/BTC': {}}, results=results) == result_str data={'ETH/BTC': {}},
stake_currency='BTC', max_open_trades=2,
results=results) == result_str
def test_generate_text_table_strategy(default_conf, mocker): def test_generate_text_table_strategy(default_conf, mocker):