Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
stash86 committed Jun 18, 2024
1 parent cf9399e commit 6a45ce3
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions user_data/hyperopts/montecarlo_loss.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import numpy as np
import pandas as pd
from scipy.stats import norm
from freqtrade.optimize.hyperopt import IHyperOptLoss

from freqtrade.constants import Config
from freqtrade.data.metrics import calculate_max_drawdown
from freqtrade.optimize.hyperopt import IHyperOptLoss


# Predefined constants to control the behavior of the loss function.
ITERATIONS = 500 # Number of Monte Carlo simulations to run.
Expand All @@ -15,6 +17,7 @@

np.random.seed(1337) # Seed for reproducibility of Monte Carlo simulations.


class montecarlo_loss(IHyperOptLoss):
"""
Custom loss function that combines Monte Carlo simulations, System Quality Number (SQN),
Expand All @@ -24,11 +27,9 @@ class montecarlo_loss(IHyperOptLoss):
Monte Carlo simulation outcomes, encouraging strategies that are both profitable
and robust.
"""

@staticmethod
def hyperopt_loss_function(
results: pd.DataFrame, config: Config, *args, **kwargs
) -> float:
def hyperopt_loss_function(results: pd.DataFrame, config: Config, *args, **kwargs) -> float:
"""
Calculates the loss for a given set of trading results.
Expand All @@ -40,18 +41,13 @@ def hyperopt_loss_function(
- float: Calculated loss. Lower (more negative) values indicate better performance.
"""
# Calculate metrics used to evaluate strategy performance.
mc_profit_ratio = MontecarloSQNLoss._calculate_mc_profit_ratio(results, config)
sqn = MontecarloSQNLoss._calculate_sqn(results)
dd = MontecarloSQNLoss._calculate_drawdown(results, config)
mc_profit_ratio = montecarlo_loss._calculate_mc_profit_ratio(results, config)
sqn = montecarlo_loss._calculate_sqn(results)
dd = montecarlo_loss._calculate_drawdown(results, config)
profit_total = results["profit_abs"].sum()

# Apply penalties based on performance criteria.
if (
profit_total <= 0
or mc_profit_ratio <= 0
or dd >= MAX_DRAWDOWN
or sqn <= MIN_SQN
):
if profit_total <= 0 or mc_profit_ratio <= 0 or dd >= MAX_DRAWDOWN or sqn <= MIN_SQN:
return MAX_LOSS

return -mc_profit_ratio # Negate the profit ratio as we seek to minimize the loss.
Expand Down Expand Up @@ -80,6 +76,7 @@ def _calculate_drawdown(results: pd.DataFrame, config: Config) -> float:
)
except Exception as e:
import logging

logging.error(e) # Log error if drawdown calculation fails.
max_drawdown_relative = 0.0
return max_drawdown_relative
Expand Down

0 comments on commit 6a45ce3

Please sign in to comment.