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)