2021-09-22 01:04:23 +00:00
|
|
|
"""
|
|
|
|
CalmarHyperOptLoss
|
|
|
|
|
|
|
|
This module defines the alternative HyperOptLoss class which can be used for
|
|
|
|
Hyperoptimization.
|
|
|
|
"""
|
|
|
|
from datetime import datetime
|
2021-09-24 02:31:33 +00:00
|
|
|
|
|
|
|
from pandas import DataFrame
|
2021-09-22 01:04:23 +00:00
|
|
|
|
2022-09-18 11:31:52 +00:00
|
|
|
from freqtrade.constants import Config
|
2023-01-07 00:30:16 +00:00
|
|
|
from freqtrade.data.metrics import calculate_calmar
|
2021-09-22 14:18:17 +00:00
|
|
|
from freqtrade.optimize.hyperopt import IHyperOptLoss
|
2021-09-22 01:04:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CalmarHyperOptLoss(IHyperOptLoss):
|
|
|
|
"""
|
|
|
|
Defines the loss function for hyperopt.
|
|
|
|
|
|
|
|
This implementation uses the Calmar Ratio calculation.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@staticmethod
|
2023-01-07 00:30:16 +00:00
|
|
|
def hyperopt_loss_function(results: DataFrame, trade_count: int,
|
|
|
|
min_date: datetime, max_date: datetime,
|
|
|
|
config: Config, *args, **kwargs) -> float:
|
2021-09-22 01:04:23 +00:00
|
|
|
"""
|
|
|
|
Objective function, returns smaller number for more optimal results.
|
|
|
|
|
|
|
|
Uses Calmar Ratio calculation.
|
|
|
|
"""
|
2023-01-07 00:30:16 +00:00
|
|
|
starting_balance = config['dry_run_wallet']
|
|
|
|
calmar_ratio = calculate_calmar(results, min_date, max_date, starting_balance)
|
2021-09-24 02:31:33 +00:00
|
|
|
# print(expected_returns_mean, max_drawdown, calmar_ratio)
|
2021-09-22 01:04:23 +00:00
|
|
|
return -calmar_ratio
|