2020-03-15 14:17:35 +00:00
|
|
|
import logging
|
2020-01-02 06:26:43 +00:00
|
|
|
from datetime import timedelta
|
2020-03-15 14:17:35 +00:00
|
|
|
from pathlib import Path
|
|
|
|
from typing import Dict, Optional
|
2020-01-02 06:26:43 +00:00
|
|
|
|
|
|
|
from pandas import DataFrame
|
|
|
|
from tabulate import tabulate
|
|
|
|
|
2020-03-15 14:17:35 +00:00
|
|
|
from freqtrade.misc import file_dump_json
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def store_backtest_result(recordfilename: Path, results: DataFrame,
|
|
|
|
strategyname: Optional[str] = None) -> None:
|
|
|
|
|
|
|
|
records = [(t.pair, t.profit_percent, t.open_time.timestamp(),
|
|
|
|
t.close_time.timestamp(), t.open_index - 1, t.trade_duration,
|
|
|
|
t.open_rate, t.close_rate, t.open_at_end, t.sell_reason.value)
|
|
|
|
for index, t in results.iterrows()]
|
|
|
|
|
|
|
|
if records:
|
|
|
|
if strategyname:
|
|
|
|
# Inject strategyname to filename
|
|
|
|
recordfilename = Path.joinpath(
|
|
|
|
recordfilename.parent,
|
|
|
|
f'{recordfilename.stem}-{strategyname}').with_suffix(recordfilename.suffix)
|
|
|
|
logger.info(f'Dumping backtest results to {recordfilename}')
|
|
|
|
file_dump_json(recordfilename, records)
|
|
|
|
|
2020-01-02 06:26:43 +00:00
|
|
|
|
|
|
|
def generate_text_table(data: Dict[str, Dict], stake_currency: str, max_open_trades: int,
|
|
|
|
results: DataFrame, skip_nan: bool = False) -> str:
|
|
|
|
"""
|
|
|
|
Generates and returns a text table for the given backtest data and the results dataframe
|
2020-01-02 08:37:54 +00:00
|
|
|
:param data: Dict of <pair: dataframe> containing data that was used during backtesting.
|
|
|
|
:param stake_currency: stake-currency - used to correctly name headers
|
|
|
|
:param max_open_trades: Maximum allowed open trades
|
|
|
|
:param results: Dataframe containing the backtest results
|
|
|
|
:param skip_nan: Print "left open" open trades
|
|
|
|
:return: pretty printed table with tabulate as string
|
2020-01-02 06:26:43 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
floatfmt = ('s', 'd', '.2f', '.2f', '.8f', '.2f', 'd', '.1f', '.1f')
|
|
|
|
tabular_data = []
|
2020-01-31 03:39:18 +00:00
|
|
|
headers = [
|
|
|
|
'Pair',
|
2020-02-07 02:51:50 +00:00
|
|
|
'Buys',
|
2020-01-31 03:39:18 +00:00
|
|
|
'Avg Profit %',
|
|
|
|
'Cum Profit %',
|
|
|
|
f'Tot Profit {stake_currency}',
|
|
|
|
'Tot Profit %',
|
|
|
|
'Avg Duration',
|
|
|
|
'Wins',
|
2020-02-07 02:51:50 +00:00
|
|
|
'Draws',
|
2020-01-31 03:39:18 +00:00
|
|
|
'Losses'
|
|
|
|
]
|
2020-01-02 06:26:43 +00:00
|
|
|
for pair in data:
|
|
|
|
result = results[results.pair == pair]
|
|
|
|
if skip_nan and result.profit_abs.isnull().all():
|
|
|
|
continue
|
|
|
|
|
|
|
|
tabular_data.append([
|
|
|
|
pair,
|
|
|
|
len(result.index),
|
|
|
|
result.profit_percent.mean() * 100.0,
|
|
|
|
result.profit_percent.sum() * 100.0,
|
|
|
|
result.profit_abs.sum(),
|
|
|
|
result.profit_percent.sum() * 100.0 / max_open_trades,
|
|
|
|
str(timedelta(
|
|
|
|
minutes=round(result.trade_duration.mean()))) if not result.empty else '0:00',
|
|
|
|
len(result[result.profit_abs > 0]),
|
2020-02-07 02:51:50 +00:00
|
|
|
len(result[result.profit_abs == 0]),
|
2020-01-02 06:26:43 +00:00
|
|
|
len(result[result.profit_abs < 0])
|
|
|
|
])
|
|
|
|
|
|
|
|
# Append Total
|
|
|
|
tabular_data.append([
|
|
|
|
'TOTAL',
|
|
|
|
len(results.index),
|
|
|
|
results.profit_percent.mean() * 100.0,
|
|
|
|
results.profit_percent.sum() * 100.0,
|
|
|
|
results.profit_abs.sum(),
|
|
|
|
results.profit_percent.sum() * 100.0 / max_open_trades,
|
|
|
|
str(timedelta(
|
|
|
|
minutes=round(results.trade_duration.mean()))) if not results.empty else '0:00',
|
|
|
|
len(results[results.profit_abs > 0]),
|
2020-02-07 02:51:50 +00:00
|
|
|
len(results[results.profit_abs == 0]),
|
2020-01-02 06:26:43 +00:00
|
|
|
len(results[results.profit_abs < 0])
|
|
|
|
])
|
|
|
|
# Ignore type as floatfmt does allow tuples but mypy does not know that
|
|
|
|
return tabulate(tabular_data, headers=headers,
|
2020-02-27 12:28:28 +00:00
|
|
|
floatfmt=floatfmt, tablefmt="orgtbl", stralign="right") # type: ignore
|
2020-01-02 06:28:30 +00:00
|
|
|
|
|
|
|
|
2020-03-15 14:04:48 +00:00
|
|
|
def generate_text_table_sell_reason(stake_currency: str, max_open_trades: int,
|
|
|
|
results: DataFrame) -> str:
|
2020-01-02 06:28:30 +00:00
|
|
|
"""
|
|
|
|
Generate small table outlining Backtest results
|
2020-03-15 14:04:48 +00:00
|
|
|
:param stake_currency: Stakecurrency used
|
|
|
|
:param max_open_trades: Max_open_trades parameter
|
2020-01-02 08:37:54 +00:00
|
|
|
:param results: Dataframe containing the backtest results
|
|
|
|
:return: pretty printed table with tabulate as string
|
2020-01-02 06:28:30 +00:00
|
|
|
"""
|
|
|
|
tabular_data = []
|
2020-01-31 03:39:18 +00:00
|
|
|
headers = [
|
|
|
|
"Sell Reason",
|
2020-02-07 02:51:50 +00:00
|
|
|
"Sells",
|
2020-01-31 03:39:18 +00:00
|
|
|
"Wins",
|
2020-02-07 02:51:50 +00:00
|
|
|
"Draws",
|
2020-01-31 03:39:18 +00:00
|
|
|
"Losses",
|
|
|
|
"Avg Profit %",
|
|
|
|
"Cum Profit %",
|
|
|
|
f"Tot Profit {stake_currency}",
|
|
|
|
"Tot Profit %",
|
|
|
|
]
|
2020-01-02 06:28:30 +00:00
|
|
|
for reason, count in results['sell_reason'].value_counts().iteritems():
|
2020-01-09 05:46:39 +00:00
|
|
|
result = results.loc[results['sell_reason'] == reason]
|
2020-02-07 02:51:50 +00:00
|
|
|
wins = len(result[result['profit_abs'] > 0])
|
|
|
|
draws = len(result[result['profit_abs'] == 0])
|
2020-01-22 05:08:34 +00:00
|
|
|
loss = len(result[result['profit_abs'] < 0])
|
2020-01-09 05:46:39 +00:00
|
|
|
profit_mean = round(result['profit_percent'].mean() * 100.0, 2)
|
2020-01-31 03:39:18 +00:00
|
|
|
profit_sum = round(result["profit_percent"].sum() * 100.0, 2)
|
2020-01-31 19:41:51 +00:00
|
|
|
profit_tot = result['profit_abs'].sum()
|
|
|
|
profit_percent_tot = round(result['profit_percent'].sum() * 100.0 / max_open_trades, 2)
|
2020-01-31 03:39:18 +00:00
|
|
|
tabular_data.append(
|
|
|
|
[
|
|
|
|
reason.value,
|
|
|
|
count,
|
2020-02-07 02:51:50 +00:00
|
|
|
wins,
|
|
|
|
draws,
|
2020-01-31 03:39:18 +00:00
|
|
|
loss,
|
|
|
|
profit_mean,
|
|
|
|
profit_sum,
|
|
|
|
profit_tot,
|
|
|
|
profit_percent_tot,
|
|
|
|
]
|
|
|
|
)
|
2020-02-27 12:28:28 +00:00
|
|
|
return tabulate(tabular_data, headers=headers, tablefmt="orgtbl", stralign="right")
|
2020-01-02 06:32:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
def generate_text_table_strategy(stake_currency: str, max_open_trades: str,
|
|
|
|
all_results: Dict) -> str:
|
|
|
|
"""
|
|
|
|
Generate summary table per strategy
|
2020-01-02 08:37:54 +00:00
|
|
|
:param stake_currency: stake-currency - used to correctly name headers
|
|
|
|
:param max_open_trades: Maximum allowed open trades used for backtest
|
|
|
|
:param all_results: Dict of <Strategyname: BacktestResult> containing results for all strategies
|
|
|
|
:return: pretty printed table with tabulate as string
|
2020-01-02 06:32:12 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
floatfmt = ('s', 'd', '.2f', '.2f', '.8f', '.2f', 'd', '.1f', '.1f')
|
|
|
|
tabular_data = []
|
2020-02-07 02:51:50 +00:00
|
|
|
headers = ['Strategy', 'Buys', 'Avg Profit %', 'Cum Profit %',
|
2020-02-06 05:58:58 +00:00
|
|
|
f'Tot Profit {stake_currency}', 'Tot Profit %', 'Avg Duration',
|
2020-02-07 02:51:50 +00:00
|
|
|
'Wins', 'Draws', 'Losses']
|
2020-01-02 06:32:12 +00:00
|
|
|
for strategy, results in all_results.items():
|
|
|
|
tabular_data.append([
|
|
|
|
strategy,
|
|
|
|
len(results.index),
|
|
|
|
results.profit_percent.mean() * 100.0,
|
|
|
|
results.profit_percent.sum() * 100.0,
|
|
|
|
results.profit_abs.sum(),
|
|
|
|
results.profit_percent.sum() * 100.0 / max_open_trades,
|
|
|
|
str(timedelta(
|
|
|
|
minutes=round(results.trade_duration.mean()))) if not results.empty else '0:00',
|
|
|
|
len(results[results.profit_abs > 0]),
|
2020-02-07 02:51:50 +00:00
|
|
|
len(results[results.profit_abs == 0]),
|
2020-01-02 06:32:12 +00:00
|
|
|
len(results[results.profit_abs < 0])
|
|
|
|
])
|
|
|
|
# Ignore type as floatfmt does allow tuples but mypy does not know that
|
|
|
|
return tabulate(tabular_data, headers=headers,
|
2020-02-27 12:28:28 +00:00
|
|
|
floatfmt=floatfmt, tablefmt="orgtbl", stralign="right") # type: ignore
|
2020-01-09 05:52:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
def generate_edge_table(results: dict) -> str:
|
|
|
|
|
|
|
|
floatfmt = ('s', '.10g', '.2f', '.2f', '.2f', '.2f', 'd', '.d')
|
|
|
|
tabular_data = []
|
2020-02-07 02:51:50 +00:00
|
|
|
headers = ['Pair', 'Stoploss', 'Win Rate', 'Risk Reward Ratio',
|
|
|
|
'Required Risk Reward', 'Expectancy', 'Total Number of Trades',
|
|
|
|
'Average Duration (min)']
|
2020-01-09 05:52:34 +00:00
|
|
|
|
|
|
|
for result in results.items():
|
|
|
|
if result[1].nb_trades > 0:
|
|
|
|
tabular_data.append([
|
|
|
|
result[0],
|
|
|
|
result[1].stoploss,
|
|
|
|
result[1].winrate,
|
|
|
|
result[1].risk_reward_ratio,
|
|
|
|
result[1].required_risk_reward,
|
|
|
|
result[1].expectancy,
|
|
|
|
result[1].nb_trades,
|
|
|
|
round(result[1].avg_trade_duration)
|
|
|
|
])
|
|
|
|
|
|
|
|
# Ignore type as floatfmt does allow tuples but mypy does not know that
|
|
|
|
return tabulate(tabular_data, headers=headers,
|
2020-02-27 12:28:28 +00:00
|
|
|
floatfmt=floatfmt, tablefmt="orgtbl", stralign="right") # type: ignore
|