Add print_sales table and test

This commit is contained in:
Matthias 2018-07-12 21:19:43 +02:00
parent 426c25f631
commit 506aa0e3d3
2 changed files with 57 additions and 6 deletions

View File

@ -120,6 +120,16 @@ class Backtesting(object):
]) ])
return tabulate(tabular_data, headers=headers, floatfmt=floatfmt, tablefmt="pipe") return tabulate(tabular_data, headers=headers, floatfmt=floatfmt, tablefmt="pipe")
def _generate_text_table_sell_reason(self, data: Dict[str, Dict], results: DataFrame) -> str:
"""
Generate small table outlining Backtest results
"""
tabular_data = []
headers = ['Sell Reason', 'Count']
for reason, count in results['sell_reason'].value_counts().iteritems():
tabular_data.append([reason.value, count])
return tabulate(tabular_data, headers=headers, tablefmt="pipe")
def _store_backtest_result(self, recordfilename: Optional[str], results: DataFrame) -> None: def _store_backtest_result(self, recordfilename: Optional[str], results: DataFrame) -> None:
records = [(t.pair, t.profit_percent, t.open_time.timestamp(), records = [(t.pair, t.profit_percent, t.open_time.timestamp(),
@ -319,20 +329,32 @@ class Backtesting(object):
self._store_backtest_result(self.config.get('exportfilename'), results) self._store_backtest_result(self.config.get('exportfilename'), results)
logger.info( logger.info(
'\n================================================= ' '\n' + '=' * 49 +
'BACKTESTING REPORT' ' BACKTESTING REPORT ' +
' ==================================================\n' '=' * 50 + '\n'
'%s', '%s',
self._generate_text_table( self._generate_text_table(
data, data,
results results
) )
) )
# logger.info(
# results[['sell_reason']].groupby('sell_reason').count()
# )
logger.info( logger.info(
'\n=============================================== ' '\n' + '=' * 4 +
'LEFT OPEN TRADES REPORT' ' SELL READON STATS ' +
' ===============================================\n' '=' * 4 + '\n'
'%s \n',
self._generate_text_table_sell_reason(data, results)
)
logger.info(
'\n' + '=' * 47 +
' LEFT OPEN TRADES REPORT ' +
'=' * 47 + '\n'
'%s', '%s',
self._generate_text_table( self._generate_text_table(
data, data,

View File

@ -404,6 +404,35 @@ def test_generate_text_table(default_conf, mocker):
assert backtesting._generate_text_table(data={'ETH/BTC': {}}, results=results) == result_str assert backtesting._generate_text_table(data={'ETH/BTC': {}}, results=results) == result_str
def test_generate_text_table_sell_reason(default_conf, mocker):
"""
Test Backtesting.generate_text_table_sell_reason() method
"""
patch_exchange(mocker)
backtesting = Backtesting(default_conf)
results = pd.DataFrame(
{
'pair': ['ETH/BTC', 'ETH/BTC', 'ETH/BTC'],
'profit_percent': [0.1, 0.2, 0.3],
'profit_abs': [0.2, 0.4, 0.5],
'trade_duration': [10, 30, 10],
'profit': [2, 0, 0],
'loss': [0, 0, 1],
'sell_reason': [SellType.ROI, SellType.ROI, SellType.STOP_LOSS]
}
)
result_str = (
'| Sell Reason | Count |\n'
'|:--------------|--------:|\n'
'| roi | 2 |\n'
'| stop_loss | 1 |'
)
assert backtesting._generate_text_table_sell_reason(
data={'ETH/BTC': {}}, results=results) == result_str
def test_backtesting_start(default_conf, mocker, caplog) -> None: def test_backtesting_start(default_conf, mocker, caplog) -> None:
""" """
Test Backtesting.start() method Test Backtesting.start() method