2022-02-06 14:40:54 +00:00
|
|
|
"""
|
|
|
|
ProfitDrawDownHyperOptLoss
|
|
|
|
|
|
|
|
This module defines the alternative HyperOptLoss class based on Profit &
|
|
|
|
Drawdown objective which can be used for Hyperoptimization.
|
|
|
|
|
|
|
|
Possible to change `DRAWDOWN_MULT` to penalize drawdown objective for
|
|
|
|
individual needs.
|
|
|
|
"""
|
|
|
|
from pandas import DataFrame
|
2022-02-06 16:13:09 +00:00
|
|
|
|
2022-04-30 12:47:27 +00:00
|
|
|
from freqtrade.data.metrics import calculate_max_drawdown
|
2022-02-06 16:13:09 +00:00
|
|
|
from freqtrade.optimize.hyperopt import IHyperOptLoss
|
|
|
|
|
2022-02-06 14:40:54 +00:00
|
|
|
|
|
|
|
# higher numbers penalize drawdowns more severely
|
2022-02-07 05:31:16 +00:00
|
|
|
DRAWDOWN_MULT = 0.075
|
2022-02-06 14:40:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ProfitDrawDownHyperOptLoss(IHyperOptLoss):
|
|
|
|
@staticmethod
|
|
|
|
def hyperopt_loss_function(results: DataFrame, trade_count: int, *args, **kwargs) -> float:
|
|
|
|
total_profit = results["profit_abs"].sum()
|
|
|
|
|
|
|
|
try:
|
2022-02-07 13:12:07 +00:00
|
|
|
max_drawdown_abs = calculate_max_drawdown(results, value_col="profit_abs")[5]
|
2022-02-06 14:40:54 +00:00
|
|
|
except ValueError:
|
2022-02-07 05:22:27 +00:00
|
|
|
max_drawdown_abs = 0
|
2022-02-06 14:40:54 +00:00
|
|
|
|
2022-02-07 05:22:27 +00:00
|
|
|
return -1 * (total_profit * (1 - max_drawdown_abs * DRAWDOWN_MULT))
|