This page explains some advanced Hyperopt topics that may require higher coding skills and Python knowledge than creation of an ordinal hyperoptimization class.

## Derived hyperopt classes¶

Custom hyperop classes can be derived in the same way it can be done for strategies.

Applying to hyperoptimization, as an example, you may override how dimensions are defined in your optimization hyperspace:

class MyAwesomeHyperOpt(IHyperOpt):
...
# Uses default stoploss dimension

class MyAwesomeHyperOpt2(MyAwesomeHyperOpt):
@staticmethod
def stoploss_space() -> List[Dimension]:
# Override boundaries for stoploss
return [
Real(-0.33, -0.01, name='stoploss'),
]

and then quickly switch between hyperopt classes, running optimization process with hyperopt class you need in each particular case:

$freqtrade hyperopt --hyperopt MyAwesomeHyperOpt --hyperopt-loss SharpeHyperOptLossDaily --strategy MyAwesomeStrategy ... or$ freqtrade hyperopt --hyperopt MyAwesomeHyperOpt2 --hyperopt-loss SharpeHyperOptLossDaily --strategy MyAwesomeStrategy ...

## Creating and using a custom loss function¶

To use a custom loss function class, make sure that the function hyperopt_loss_function is defined in your custom hyperopt loss class. For the sample below, you then need to add the command line parameter --hyperopt-loss SuperDuperHyperOptLoss to your hyperopt call so this function is being used.

A sample of this can be found below, which is identical to the Default Hyperopt loss implementation. A full sample can be found in userdata/hyperopts.

EXPECTED_MAX_PROFIT = 3.0

class SuperDuperHyperOptLoss(IHyperOptLoss):
"""
Defines the default loss function for hyperopt
"""

@staticmethod
min_date: datetime, max_date: datetime,
*args, **kwargs) -> float:
"""
Objective function, returns smaller number for better results
This is the legacy algorithm (used until now in freqtrade).
Weights are distributed as follows:
* 1.0 to total profit, compared to the expected value (EXPECTED_MAX_PROFIT) defined above
"""
total_profit = results['profit_percent'].sum()

profit_loss = max(0, 1 - total_profit / EXPECTED_MAX_PROFIT)
result = trade_loss + profit_loss + duration_loss
return result

Currently, the arguments are:

• results: DataFrame containing the result
The following columns are available in results (corresponds to the output-file of backtesting when used with --export trades):
pair, profit_percent, profit_abs, open_date, open_rate, open_fee, close_date, close_rate, close_fee, amount, trade_duration, open_at_end, sell_reason