fix formulas and implement new metrics

This commit is contained in:
Stefano Ariestasia 2022-12-26 08:19:51 +09:00
parent 7a5439321c
commit 6353f3ac1a
2 changed files with 13 additions and 21 deletions

View File

@ -222,8 +222,8 @@ def calculate_expectancy(trades: pd.DataFrame) -> float:
return expectancy
def calculate_sortino(trades: pd.DataFrame,
min_date: datetime, max_date: datetime) -> float:
def calculate_sortino(trades: pd.DataFrame, min_date: datetime, max_date: datetime,
starting_balance: float) -> float:
"""
Calculate sortino
:param trades: DataFrame containing trades (requires columns profit_ratio)
@ -232,18 +232,13 @@ def calculate_sortino(trades: pd.DataFrame,
if (len(trades) == 0) or (min_date is None) or (max_date is None) or (min_date == max_date):
return 0
total_profit = trades["profit_ratio"]
days_period = (max_date - min_date).days
total_profit = trades['profit_abs'] / starting_balance
days_period = max(1, (max_date - min_date).days)
if days_period == 0:
return 0
# adding slippage of 0.1% per trade
# total_profit = total_profit - 0.0005
expected_returns_mean = total_profit.sum() / days_period
trades['downside_returns'] = 0
trades.loc[total_profit < 0, 'downside_returns'] = trades['profit_ratio']
trades.loc[total_profit < 0, 'downside_returns'] = (trades['profit_abs'] / starting_balance)
down_stdev = np.std(trades['downside_returns'])
if down_stdev != 0:
@ -256,8 +251,8 @@ def calculate_sortino(trades: pd.DataFrame,
return sortino_ratio
def calculate_sharpe(trades: pd.DataFrame,
min_date: datetime, max_date: datetime) -> float:
def calculate_sharpe(trades: pd.DataFrame, min_date: datetime, max_date: datetime,
starting_balance: float) -> float:
"""
Calculate sharpe
:param trades: DataFrame containing trades (requires columns close_date and profit_ratio)
@ -266,14 +261,9 @@ def calculate_sharpe(trades: pd.DataFrame,
if (len(trades) == 0) or (min_date is None) or (max_date is None) or (min_date == max_date):
return 0
total_profit = trades["profit_ratio"]
days_period = (max_date - min_date).days
total_profit = trades['profit_abs'] / starting_balance
days_period = max(1, (max_date - min_date).days)
if days_period == 0:
return 0
# adding slippage of 0.1% per trade
# total_profit = total_profit - 0.0005
expected_returns_mean = total_profit.sum() / days_period
up_stdev = np.std(total_profit)

View File

@ -450,8 +450,8 @@ def generate_strategy_stats(pairlist: List[str],
'profit_total_short_abs': results.loc[results['is_short'], 'profit_abs'].sum(),
'cagr': calculate_cagr(backtest_days, start_balance, content['final_balance']),
'expectancy': calculate_expectancy(results),
'sortino': calculate_sortino(results, min_date, max_date),
'sharpe': calculate_sharpe(results, min_date, max_date),
'sortino': calculate_sortino(results, min_date, max_date, start_balance),
'sharpe': calculate_sharpe(results, min_date, max_date, start_balance),
'calmar': calculate_calmar(results, min_date, max_date, start_balance),
'profit_factor': profit_factor,
'backtest_start': min_date.strftime(DATETIME_PRINT_FORMAT),
@ -795,6 +795,8 @@ def text_table_add_metrics(strat_results: Dict) -> str:
('Calmar', f"{strat_results['calmar']:.2f}" if 'calmar' in strat_results else 'N/A'),
('Profit factor', f'{strat_results["profit_factor"]:.2f}' if 'profit_factor'
in strat_results else 'N/A'),
('Expectancy', f"{strat_results['expectancy']:.2f}" if 'expectancy'
in strat_results else 'N/A'),
('Trades per day', strat_results['trades_per_day']),
('Avg. daily profit %',
f"{(strat_results['profit_total'] / strat_results['backtest_days']):.2%}"),