42 lines
1.2 KiB
Python
42 lines
1.2 KiB
Python
"""
|
|
MaxDrawDownHyperOptLoss
|
|
|
|
This module defines the alternative HyperOptLoss class which can be used for
|
|
Hyperoptimization.
|
|
"""
|
|
from datetime import datetime
|
|
|
|
from pandas import DataFrame
|
|
|
|
from freqtrade.data.btanalysis import calculate_max_drawdown
|
|
from freqtrade.optimize.hyperopt import IHyperOptLoss
|
|
|
|
|
|
class MaxDrawDownHyperOptLoss(IHyperOptLoss):
|
|
|
|
"""
|
|
Defines the loss function for hyperopt.
|
|
|
|
This implementation optimizes for max draw down and profit
|
|
Less max drawdown more profit -> Lower return value
|
|
"""
|
|
|
|
@staticmethod
|
|
def hyperopt_loss_function(results: DataFrame, trade_count: int,
|
|
min_date: datetime, max_date: datetime,
|
|
*args, **kwargs) -> float:
|
|
|
|
"""
|
|
Objective function.
|
|
|
|
Uses profit ratio weighted max_drawdown when drawdown is available.
|
|
Otherwise directly optimizes profit ratio.
|
|
"""
|
|
total_profit = results['profit_abs'].sum()
|
|
try:
|
|
max_drawdown = calculate_max_drawdown(results, value_col='profit_abs')
|
|
except ValueError:
|
|
# No losing trade, therefore no drawdown.
|
|
return -total_profit
|
|
return -total_profit / max_drawdown[0]
|