mirror of
https://github.com/freqtrade/freqtrade.git
synced 2026-01-20 14:00:38 +00:00
feat: add starting_balance as argument to hyperopt_loss_function
This commit is contained in:
@@ -28,6 +28,7 @@ from freqtrade.optimize.hyperopt_loss.hyperopt_loss_interface import IHyperOptLo
|
||||
from freqtrade.optimize.hyperopt_tools import HyperoptStateContainer, HyperoptTools
|
||||
from freqtrade.optimize.optimize_reports import generate_strategy_stats
|
||||
from freqtrade.resolvers.hyperopt_resolver import HyperOptLossResolver
|
||||
from freqtrade.util.dry_run_wallet import get_dry_run_wallet
|
||||
|
||||
|
||||
# Suppress scikit-learn FutureWarnings from skopt
|
||||
@@ -363,6 +364,7 @@ class HyperOptimizer:
|
||||
config=self.config,
|
||||
processed=processed,
|
||||
backtest_stats=strat_stats,
|
||||
starting_balance=get_dry_run_wallet(self.config),
|
||||
)
|
||||
return {
|
||||
"loss": loss,
|
||||
|
||||
@@ -12,7 +12,6 @@ from pandas import DataFrame
|
||||
from freqtrade.constants import Config
|
||||
from freqtrade.data.metrics import calculate_calmar
|
||||
from freqtrade.optimize.hyperopt import IHyperOptLoss
|
||||
from freqtrade.util import get_dry_run_wallet
|
||||
|
||||
|
||||
class CalmarHyperOptLoss(IHyperOptLoss):
|
||||
@@ -29,6 +28,7 @@ class CalmarHyperOptLoss(IHyperOptLoss):
|
||||
min_date: datetime,
|
||||
max_date: datetime,
|
||||
config: Config,
|
||||
starting_balance: float,
|
||||
*args,
|
||||
**kwargs,
|
||||
) -> float:
|
||||
@@ -37,7 +37,6 @@ class CalmarHyperOptLoss(IHyperOptLoss):
|
||||
|
||||
Uses Calmar Ratio calculation.
|
||||
"""
|
||||
starting_balance = get_dry_run_wallet(config)
|
||||
calmar_ratio = calculate_calmar(results, min_date, max_date, starting_balance)
|
||||
# print(expected_returns_mean, max_drawdown, calmar_ratio)
|
||||
return -calmar_ratio
|
||||
|
||||
@@ -31,6 +31,7 @@ class IHyperOptLoss(ABC):
|
||||
config: Config,
|
||||
processed: dict[str, DataFrame],
|
||||
backtest_stats: dict[str, Any],
|
||||
starting_balance: float,
|
||||
**kwargs,
|
||||
) -> float:
|
||||
"""
|
||||
|
||||
@@ -10,7 +10,6 @@ from pandas import DataFrame
|
||||
from freqtrade.constants import Config
|
||||
from freqtrade.data.metrics import calculate_underwater
|
||||
from freqtrade.optimize.hyperopt import IHyperOptLoss
|
||||
from freqtrade.util import get_dry_run_wallet
|
||||
|
||||
|
||||
class MaxDrawDownRelativeHyperOptLoss(IHyperOptLoss):
|
||||
@@ -22,7 +21,9 @@ class MaxDrawDownRelativeHyperOptLoss(IHyperOptLoss):
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def hyperopt_loss_function(results: DataFrame, config: Config, *args, **kwargs) -> float:
|
||||
def hyperopt_loss_function(
|
||||
results: DataFrame, config: Config, starting_balance: float, *args, **kwargs
|
||||
) -> float:
|
||||
"""
|
||||
Objective function.
|
||||
|
||||
@@ -32,7 +33,7 @@ class MaxDrawDownRelativeHyperOptLoss(IHyperOptLoss):
|
||||
total_profit = results["profit_abs"].sum()
|
||||
try:
|
||||
drawdown_df = calculate_underwater(
|
||||
results, value_col="profit_abs", starting_balance=get_dry_run_wallet(config)
|
||||
results, value_col="profit_abs", starting_balance=starting_balance
|
||||
)
|
||||
max_drawdown = abs(min(drawdown_df["drawdown"]))
|
||||
relative_drawdown = max(drawdown_df["drawdown_relative"])
|
||||
|
||||
@@ -36,7 +36,6 @@ from pandas import DataFrame
|
||||
from freqtrade.constants import Config
|
||||
from freqtrade.data.metrics import calculate_expectancy, calculate_max_drawdown
|
||||
from freqtrade.optimize.hyperopt import IHyperOptLoss
|
||||
from freqtrade.util import get_dry_run_wallet
|
||||
|
||||
|
||||
# smaller numbers penalize drawdowns more severely
|
||||
@@ -59,6 +58,7 @@ class MultiMetricHyperOptLoss(IHyperOptLoss):
|
||||
results: DataFrame,
|
||||
trade_count: int,
|
||||
config: Config,
|
||||
starting_balance: float,
|
||||
**kwargs,
|
||||
) -> float:
|
||||
total_profit = results["profit_abs"].sum()
|
||||
@@ -84,7 +84,7 @@ class MultiMetricHyperOptLoss(IHyperOptLoss):
|
||||
# Calculate drawdown
|
||||
try:
|
||||
drawdown = calculate_max_drawdown(
|
||||
results, starting_balance=get_dry_run_wallet(config), value_col="profit_abs"
|
||||
results, starting_balance=starting_balance, value_col="profit_abs"
|
||||
)
|
||||
relative_account_drawdown = drawdown.relative_account_drawdown
|
||||
except ValueError:
|
||||
|
||||
@@ -13,7 +13,6 @@ from pandas import DataFrame
|
||||
from freqtrade.constants import Config
|
||||
from freqtrade.data.metrics import calculate_max_drawdown
|
||||
from freqtrade.optimize.hyperopt import IHyperOptLoss
|
||||
from freqtrade.util import get_dry_run_wallet
|
||||
|
||||
|
||||
# smaller numbers penalize drawdowns more severely
|
||||
@@ -22,12 +21,14 @@ DRAWDOWN_MULT = 0.075
|
||||
|
||||
class ProfitDrawDownHyperOptLoss(IHyperOptLoss):
|
||||
@staticmethod
|
||||
def hyperopt_loss_function(results: DataFrame, config: Config, *args, **kwargs) -> float:
|
||||
def hyperopt_loss_function(
|
||||
results: DataFrame, config: Config, starting_balance: float, *args, **kwargs
|
||||
) -> float:
|
||||
total_profit = results["profit_abs"].sum()
|
||||
|
||||
try:
|
||||
drawdown = calculate_max_drawdown(
|
||||
results, starting_balance=get_dry_run_wallet(config), value_col="profit_abs"
|
||||
results, starting_balance=starting_balance, value_col="profit_abs"
|
||||
)
|
||||
relative_account_drawdown = drawdown.relative_account_drawdown
|
||||
except ValueError:
|
||||
|
||||
@@ -9,10 +9,8 @@ from datetime import datetime
|
||||
|
||||
from pandas import DataFrame
|
||||
|
||||
from freqtrade.constants import Config
|
||||
from freqtrade.data.metrics import calculate_sharpe
|
||||
from freqtrade.optimize.hyperopt import IHyperOptLoss
|
||||
from freqtrade.util.dry_run_wallet import get_dry_run_wallet
|
||||
|
||||
|
||||
class SharpeHyperOptLoss(IHyperOptLoss):
|
||||
@@ -25,10 +23,9 @@ class SharpeHyperOptLoss(IHyperOptLoss):
|
||||
@staticmethod
|
||||
def hyperopt_loss_function(
|
||||
results: DataFrame,
|
||||
trade_count: int,
|
||||
min_date: datetime,
|
||||
max_date: datetime,
|
||||
config: Config,
|
||||
starting_balance: float,
|
||||
*args,
|
||||
**kwargs,
|
||||
) -> float:
|
||||
@@ -37,7 +34,6 @@ class SharpeHyperOptLoss(IHyperOptLoss):
|
||||
|
||||
Uses Sharpe Ratio calculation.
|
||||
"""
|
||||
starting_balance = get_dry_run_wallet(config)
|
||||
sharp_ratio = calculate_sharpe(results, min_date, max_date, starting_balance)
|
||||
# print(expected_returns_mean, up_stdev, sharp_ratio)
|
||||
return -sharp_ratio
|
||||
|
||||
@@ -9,10 +9,8 @@ from datetime import datetime
|
||||
|
||||
from pandas import DataFrame
|
||||
|
||||
from freqtrade.constants import Config
|
||||
from freqtrade.data.metrics import calculate_sortino
|
||||
from freqtrade.optimize.hyperopt import IHyperOptLoss
|
||||
from freqtrade.util import get_dry_run_wallet
|
||||
|
||||
|
||||
class SortinoHyperOptLoss(IHyperOptLoss):
|
||||
@@ -25,10 +23,9 @@ class SortinoHyperOptLoss(IHyperOptLoss):
|
||||
@staticmethod
|
||||
def hyperopt_loss_function(
|
||||
results: DataFrame,
|
||||
trade_count: int,
|
||||
min_date: datetime,
|
||||
max_date: datetime,
|
||||
config: Config,
|
||||
starting_balance: float,
|
||||
*args,
|
||||
**kwargs,
|
||||
) -> float:
|
||||
@@ -37,7 +34,6 @@ class SortinoHyperOptLoss(IHyperOptLoss):
|
||||
|
||||
Uses Sortino Ratio calculation.
|
||||
"""
|
||||
starting_balance = get_dry_run_wallet(config)
|
||||
sortino_ratio = calculate_sortino(results, min_date, max_date, starting_balance)
|
||||
# print(expected_returns_mean, down_stdev, sortino_ratio)
|
||||
return -sortino_ratio
|
||||
|
||||
@@ -116,6 +116,7 @@ def test_loss_functions_better_profits(default_conf, hyperopt_results, lossfunct
|
||||
config=default_conf,
|
||||
processed=None,
|
||||
backtest_stats={"profit_total": hyperopt_results["profit_abs"].sum()},
|
||||
starting_balance=default_conf["dry_run_wallet"],
|
||||
)
|
||||
over = hl.hyperopt_loss_function(
|
||||
results_over,
|
||||
|
||||
Reference in New Issue
Block a user