From 5cdae2ce3f79866d49a7b4aa2caf2b2924b8cffd Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 29 Oct 2021 06:42:17 +0200 Subject: [PATCH] Remove CalmarDaily hyperopt loss --- docs/hyperopt.md | 4 +- freqtrade/constants.py | 2 +- .../optimize/hyperopt_loss_calmar_daily.py | 81 ------------------- tests/optimize/test_hyperoptloss.py | 2 - 4 files changed, 2 insertions(+), 87 deletions(-) delete mode 100644 freqtrade/optimize/hyperopt_loss_calmar_daily.py diff --git a/docs/hyperopt.md b/docs/hyperopt.md index 5c98da5e2..b7b6cb772 100644 --- a/docs/hyperopt.md +++ b/docs/hyperopt.md @@ -116,8 +116,7 @@ optional arguments: ShortTradeDurHyperOptLoss, OnlyProfitHyperOptLoss, SharpeHyperOptLoss, SharpeHyperOptLossDaily, SortinoHyperOptLoss, SortinoHyperOptLossDaily, - CalmarHyperOptLoss, CalmarHyperOptLossDaily, - MaxDrawDownHyperOptLoss + CalmarHyperOptLoss, MaxDrawDownHyperOptLoss --disable-param-export Disable automatic hyperopt parameter export. --ignore-missing-spaces, --ignore-unparameterized-spaces @@ -526,7 +525,6 @@ Currently, the following loss functions are builtin: * `SortinoHyperOptLossDaily` - optimizes Sortino Ratio calculated on **daily** trade returns relative to **downside** standard deviation. * `MaxDrawDownHyperOptLoss` - Optimizes Maximum drawdown. * `CalmarHyperOptLoss` - Optimizes Calmar Ratio calculated on trade returns relative to max drawdown. -* `CalmarHyperOptLossDaily` Optimizes Calmar Ratio calculated on **daily** trade returns relative to max drawdown. Creation of a custom loss function is covered in the [Advanced Hyperopt](advanced-hyperopt.md) part of the documentation. diff --git a/freqtrade/constants.py b/freqtrade/constants.py index 6b3652609..656893999 100644 --- a/freqtrade/constants.py +++ b/freqtrade/constants.py @@ -25,7 +25,7 @@ ORDERTIF_POSSIBILITIES = ['gtc', 'fok', 'ioc'] HYPEROPT_LOSS_BUILTIN = ['ShortTradeDurHyperOptLoss', 'OnlyProfitHyperOptLoss', 'SharpeHyperOptLoss', 'SharpeHyperOptLossDaily', 'SortinoHyperOptLoss', 'SortinoHyperOptLossDaily', - 'CalmarHyperOptLoss', 'CalmarHyperOptLossDaily', + 'CalmarHyperOptLoss', 'MaxDrawDownHyperOptLoss'] AVAILABLE_PAIRLISTS = ['StaticPairList', 'VolumePairList', 'AgeFilter', 'OffsetFilter', 'PerformanceFilter', diff --git a/freqtrade/optimize/hyperopt_loss_calmar_daily.py b/freqtrade/optimize/hyperopt_loss_calmar_daily.py deleted file mode 100644 index e99bc2c99..000000000 --- a/freqtrade/optimize/hyperopt_loss_calmar_daily.py +++ /dev/null @@ -1,81 +0,0 @@ -""" -CalmarHyperOptLossDaily - -This module defines the alternative HyperOptLoss class which can be used for -Hyperoptimization. -""" -from datetime import datetime -from math import sqrt as msqrt -from typing import Any, Dict - -from pandas import DataFrame, date_range - -from freqtrade.optimize.hyperopt import IHyperOptLoss - - -class CalmarHyperOptLossDaily(IHyperOptLoss): - """ - Defines the loss function for hyperopt. - - This implementation uses the Calmar Ratio calculation. - """ - - @staticmethod - def hyperopt_loss_function( - results: DataFrame, - trade_count: int, - min_date: datetime, - max_date: datetime, - config: Dict, - processed: Dict[str, DataFrame], - backtest_stats: Dict[str, Any], - *args, - **kwargs - ) -> float: - """ - Objective function, returns smaller number for more optimal results. - - Uses Calmar Ratio calculation. - """ - resample_freq = "1D" - slippage_per_trade_ratio = 0.0005 - days_in_year = 365 - - # create the index within the min_date and end max_date - t_index = date_range( - start=min_date, end=max_date, freq=resample_freq, normalize=True - ) - - # apply slippage per trade to profit_total - results.loc[:, "profit_ratio_after_slippage"] = ( - results["profit_ratio"] - slippage_per_trade_ratio - ) - - sum_daily = ( - results.resample(resample_freq, on="close_date") - .agg({"profit_ratio_after_slippage": sum}) - .reindex(t_index) - .fillna(0) - ) - - total_profit = sum_daily["profit_ratio_after_slippage"] - expected_returns_mean = total_profit.mean() * 100 - - # calculate max drawdown - try: - high_val = total_profit.max() - low_val = total_profit.min() - max_drawdown = (high_val - low_val) / high_val - - except (ValueError, ZeroDivisionError): - max_drawdown = 0 - - if max_drawdown != 0: - calmar_ratio = expected_returns_mean / max_drawdown * msqrt(days_in_year) - else: - # Define high (negative) calmar ratio to be clear that this is NOT optimal. - calmar_ratio = -20.0 - - # print(t_index, sum_daily, total_profit) - # print(expected_returns_mean, max_drawdown, calmar_ratio) - return -calmar_ratio diff --git a/tests/optimize/test_hyperoptloss.py b/tests/optimize/test_hyperoptloss.py index fd835c678..e4a2eec2e 100644 --- a/tests/optimize/test_hyperoptloss.py +++ b/tests/optimize/test_hyperoptloss.py @@ -5,7 +5,6 @@ import pytest from freqtrade.exceptions import OperationalException from freqtrade.optimize.hyperopt_loss_short_trade_dur import ShortTradeDurHyperOptLoss -from freqtrade.optimize.optimize_reports import generate_strategy_stats from freqtrade.resolvers.hyperopt_resolver import HyperOptLossResolver @@ -86,7 +85,6 @@ def test_loss_calculation_has_limited_profit(hyperopt_conf, hyperopt_results) -> "SharpeHyperOptLoss", "SharpeHyperOptLossDaily", "MaxDrawDownHyperOptLoss", - "CalmarHyperOptLossDaily", "CalmarHyperOptLoss", ])