fix formulas and implement new metrics
This commit is contained in:
parent
7a5439321c
commit
6353f3ac1a
@ -222,8 +222,8 @@ def calculate_expectancy(trades: pd.DataFrame) -> float:
|
|||||||
return expectancy
|
return expectancy
|
||||||
|
|
||||||
|
|
||||||
def calculate_sortino(trades: pd.DataFrame,
|
def calculate_sortino(trades: pd.DataFrame, min_date: datetime, max_date: datetime,
|
||||||
min_date: datetime, max_date: datetime) -> float:
|
starting_balance: float) -> float:
|
||||||
"""
|
"""
|
||||||
Calculate sortino
|
Calculate sortino
|
||||||
:param trades: DataFrame containing trades (requires columns profit_ratio)
|
: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):
|
if (len(trades) == 0) or (min_date is None) or (max_date is None) or (min_date == max_date):
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
total_profit = trades["profit_ratio"]
|
total_profit = trades['profit_abs'] / starting_balance
|
||||||
days_period = (max_date - min_date).days
|
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
|
expected_returns_mean = total_profit.sum() / days_period
|
||||||
|
|
||||||
trades['downside_returns'] = 0
|
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'])
|
down_stdev = np.std(trades['downside_returns'])
|
||||||
|
|
||||||
if down_stdev != 0:
|
if down_stdev != 0:
|
||||||
@ -256,8 +251,8 @@ def calculate_sortino(trades: pd.DataFrame,
|
|||||||
return sortino_ratio
|
return sortino_ratio
|
||||||
|
|
||||||
|
|
||||||
def calculate_sharpe(trades: pd.DataFrame,
|
def calculate_sharpe(trades: pd.DataFrame, min_date: datetime, max_date: datetime,
|
||||||
min_date: datetime, max_date: datetime) -> float:
|
starting_balance: float) -> float:
|
||||||
"""
|
"""
|
||||||
Calculate sharpe
|
Calculate sharpe
|
||||||
:param trades: DataFrame containing trades (requires columns close_date and profit_ratio)
|
: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):
|
if (len(trades) == 0) or (min_date is None) or (max_date is None) or (min_date == max_date):
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
total_profit = trades["profit_ratio"]
|
total_profit = trades['profit_abs'] / starting_balance
|
||||||
days_period = (max_date - min_date).days
|
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
|
expected_returns_mean = total_profit.sum() / days_period
|
||||||
up_stdev = np.std(total_profit)
|
up_stdev = np.std(total_profit)
|
||||||
|
|
||||||
|
@ -450,8 +450,8 @@ def generate_strategy_stats(pairlist: List[str],
|
|||||||
'profit_total_short_abs': results.loc[results['is_short'], 'profit_abs'].sum(),
|
'profit_total_short_abs': results.loc[results['is_short'], 'profit_abs'].sum(),
|
||||||
'cagr': calculate_cagr(backtest_days, start_balance, content['final_balance']),
|
'cagr': calculate_cagr(backtest_days, start_balance, content['final_balance']),
|
||||||
'expectancy': calculate_expectancy(results),
|
'expectancy': calculate_expectancy(results),
|
||||||
'sortino': calculate_sortino(results, min_date, max_date),
|
'sortino': calculate_sortino(results, min_date, max_date, start_balance),
|
||||||
'sharpe': calculate_sharpe(results, min_date, max_date),
|
'sharpe': calculate_sharpe(results, min_date, max_date, start_balance),
|
||||||
'calmar': calculate_calmar(results, min_date, max_date, start_balance),
|
'calmar': calculate_calmar(results, min_date, max_date, start_balance),
|
||||||
'profit_factor': profit_factor,
|
'profit_factor': profit_factor,
|
||||||
'backtest_start': min_date.strftime(DATETIME_PRINT_FORMAT),
|
'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'),
|
('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'
|
('Profit factor', f'{strat_results["profit_factor"]:.2f}' if 'profit_factor'
|
||||||
in strat_results else 'N/A'),
|
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']),
|
('Trades per day', strat_results['trades_per_day']),
|
||||||
('Avg. daily profit %',
|
('Avg. daily profit %',
|
||||||
f"{(strat_results['profit_total'] / strat_results['backtest_days']):.2%}"),
|
f"{(strat_results['profit_total'] / strat_results['backtest_days']):.2%}"),
|
||||||
|
Loading…
Reference in New Issue
Block a user