Merge pull request #287 from gcarq/fix_tabulate

Improve backtesting result formatting
This commit is contained in:
Samuel Husso 2018-01-02 19:00:54 +02:00 committed by GitHub
commit 1e3a29c049
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 13 deletions

View File

@ -41,27 +41,29 @@ def generate_text_table(
Generates and returns a text table for the given backtest data and the results dataframe Generates and returns a text table for the given backtest data and the results dataframe
:return: pretty printed table with tabulate as str :return: pretty printed table with tabulate as str
""" """
floatfmt = ('s', 'd', '.2f', '.8f', '.1f')
tabular_data = [] tabular_data = []
headers = ['pair', 'buy count', 'avg profit', 'total profit', 'avg duration'] headers = ['pair', 'buy count', 'avg profit %',
'total profit ' + stake_currency, 'avg duration']
for pair in data: for pair in data:
result = results[results.currency == pair] result = results[results.currency == pair]
tabular_data.append([ tabular_data.append([
pair, pair,
len(result.index), len(result.index),
'{:.2f}%'.format(result.profit_percent.mean() * 100.0), result.profit_percent.mean() * 100.0,
'{:.08f} {}'.format(result.profit_BTC.sum(), stake_currency), result.profit_BTC.sum(),
'{:.2f}'.format(result.duration.mean() * ticker_interval), result.duration.mean() * ticker_interval,
]) ])
# Append Total # Append Total
tabular_data.append([ tabular_data.append([
'TOTAL', 'TOTAL',
len(results.index), len(results.index),
'{:.2f}%'.format(results.profit_percent.mean() * 100.0), results.profit_percent.mean() * 100.0,
'{:.08f} {}'.format(results.profit_BTC.sum(), stake_currency), results.profit_BTC.sum(),
'{:.2f}'.format(results.duration.mean() * ticker_interval), results.duration.mean() * ticker_interval,
]) ])
return tabulate(tabular_data, headers=headers) return tabulate(tabular_data, headers=headers, floatfmt=floatfmt)
def backtest(stake_amount: float, processed: Dict[str, DataFrame], def backtest(stake_amount: float, processed: Dict[str, DataFrame],
@ -173,6 +175,6 @@ def start(args):
config['stake_amount'], preprocess(data), max_open_trades, args.realistic_simulation config['stake_amount'], preprocess(data), max_open_trades, args.realistic_simulation
) )
logger.info( logger.info(
'\n====================== BACKTESTING REPORT ======================================\n%s', '\n====================== BACKTESTING REPORT ================================\n%s',
generate_text_table(data, results, config['stake_currency'], args.ticker_interval) generate_text_table(data, results, config['stake_currency'], args.ticker_interval)
) )

View File

@ -18,11 +18,12 @@ def test_generate_text_table():
'duration': [10, 30] 'duration': [10, 30]
} }
) )
print(generate_text_table({'BTC_ETH': {}}, results, 'BTC', 5))
assert generate_text_table({'BTC_ETH': {}}, results, 'BTC', 5) == ( assert generate_text_table({'BTC_ETH': {}}, results, 'BTC', 5) == (
'pair buy count avg profit total profit avg duration\n' 'pair buy count avg profit % total profit BTC avg duration\n'
'------- ----------- ------------ -------------- --------------\n' '------- ----------- -------------- ------------------ --------------\n'
'BTC_ETH 2 15.00% 0.60000000 BTC 100\n' 'BTC_ETH 2 15.00 0.60000000 100.0\n'
'TOTAL 2 15.00% 0.60000000 BTC 100') 'TOTAL 2 15.00 0.60000000 100.0')
def test_get_timeframe(): def test_get_timeframe():