Allow loading custom hyperopt loss functions
This commit is contained in:
@@ -1,16 +1,30 @@
|
||||
# pragma pylint: disable=missing-docstring, invalid-name, pointless-string-statement
|
||||
|
||||
from functools import reduce
|
||||
from math import exp
|
||||
from typing import Any, Callable, Dict, List
|
||||
from datetime import datetime
|
||||
|
||||
import talib.abstract as ta
|
||||
from pandas import DataFrame
|
||||
from typing import Dict, Any, Callable, List
|
||||
from functools import reduce
|
||||
|
||||
from skopt.space import Categorical, Dimension, Integer, Real
|
||||
|
||||
import freqtrade.vendor.qtpylib.indicators as qtpylib
|
||||
from freqtrade.optimize.hyperopt_interface import IHyperOpt
|
||||
|
||||
class_name = 'DefaultHyperOpts'
|
||||
# set TARGET_TRADES to suit your number concurrent trades so its realistic
|
||||
# to the number of days
|
||||
TARGET_TRADES = 600
|
||||
# This is assumed to be expected avg profit * expected trade count.
|
||||
# For example, for 0.35% avg per trade (or 0.0035 as ratio) and 1100 trades,
|
||||
# self.expected_max_profit = 3.85
|
||||
# Check that the reported Σ% values do not exceed this!
|
||||
# Note, this is ratio. 3.85 stated above means 385Σ%.
|
||||
EXPECTED_MAX_PROFIT = 3.0
|
||||
|
||||
# max average trade duration in minutes
|
||||
# if eval ends with higher value, we consider it a failed eval
|
||||
MAX_ACCEPTED_TRADE_DURATION = 300
|
||||
|
||||
|
||||
class DefaultHyperOpts(IHyperOpt):
|
||||
@@ -19,6 +33,21 @@ class DefaultHyperOpts(IHyperOpt):
|
||||
You can override it with your own hyperopt
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def hyperopt_loss_custom(results: DataFrame, trade_count: int,
|
||||
min_date: datetime, max_date: datetime, *args, **kwargs) -> float:
|
||||
"""
|
||||
Objective function, returns smaller number for more optimal results
|
||||
"""
|
||||
total_profit = results.profit_percent.sum()
|
||||
trade_duration = results.trade_duration.mean()
|
||||
|
||||
trade_loss = 1 - 0.25 * exp(-(trade_count - TARGET_TRADES) ** 2 / 10 ** 5.8)
|
||||
profit_loss = max(0, 1 - total_profit / EXPECTED_MAX_PROFIT)
|
||||
duration_loss = 0.4 * min(trade_duration / MAX_ACCEPTED_TRADE_DURATION, 1)
|
||||
result = trade_loss + profit_loss + duration_loss
|
||||
return result
|
||||
|
||||
@staticmethod
|
||||
def populate_indicators(dataframe: DataFrame, metadata: dict) -> DataFrame:
|
||||
dataframe['adx'] = ta.ADX(dataframe)
|
||||
|
@@ -7,7 +7,7 @@ This module contains the hyperopt logic
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
from math import exp
|
||||
|
||||
from operator import itemgetter
|
||||
from pathlib import Path
|
||||
from pprint import pprint
|
||||
@@ -22,6 +22,7 @@ from freqtrade.configuration import Arguments
|
||||
from freqtrade.data.history import load_data, get_timeframe
|
||||
from freqtrade.optimize.backtesting import Backtesting
|
||||
from freqtrade.resolvers.hyperopt_resolver import HyperOptResolver
|
||||
from freqtrade.optimize.hyperopt_loss import hyperopt_loss_legacy
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -69,6 +70,20 @@ class Hyperopt(Backtesting):
|
||||
self.trials_file = TRIALSDATA_PICKLE
|
||||
self.trials: List = []
|
||||
|
||||
# Assign loss function
|
||||
if self.config['loss_function'] == 'legacy':
|
||||
self.calculate_loss = hyperopt_loss_legacy
|
||||
elif (self.config['loss_function'] == 'custom' and
|
||||
hasattr(self.custom_hyperopt, 'hyperopt_loss_custom')):
|
||||
self.calculate_loss = self.custom_hyperopt.hyperopt_loss_custom
|
||||
|
||||
# Implement fallback to avoid odd crashes when custom-hyperopt fails to load.
|
||||
# TODO: Maybe this should just stop hyperopt completely?
|
||||
if not hasattr(self.custom_hyperopt, 'hyperopt_loss_custom'):
|
||||
logger.warning("Could not load hyperopt configuration. "
|
||||
"Falling back to legacy configuration.")
|
||||
self.calculate_loss = hyperopt_loss_legacy
|
||||
|
||||
# Populate functions here (hasattr is slow so should not be run during "regular" operations)
|
||||
if hasattr(self.custom_hyperopt, 'populate_buy_trend'):
|
||||
self.advise_buy = self.custom_hyperopt.populate_buy_trend # type: ignore
|
||||
@@ -160,16 +175,6 @@ class Hyperopt(Backtesting):
|
||||
print('.', end='')
|
||||
sys.stdout.flush()
|
||||
|
||||
def calculate_loss(self, total_profit: float, trade_count: int, trade_duration: float) -> float:
|
||||
"""
|
||||
Objective function, returns smaller number for more optimal results
|
||||
"""
|
||||
trade_loss = 1 - 0.25 * exp(-(trade_count - self.target_trades) ** 2 / 10 ** 5.8)
|
||||
profit_loss = max(0, 1 - total_profit / self.expected_max_profit)
|
||||
duration_loss = 0.4 * min(trade_duration / self.max_accepted_trade_duration, 1)
|
||||
result = trade_loss + profit_loss + duration_loss
|
||||
return result
|
||||
|
||||
def has_space(self, space: str) -> bool:
|
||||
"""
|
||||
Tell if a space value is contained in the configuration
|
||||
@@ -231,9 +236,7 @@ class Hyperopt(Backtesting):
|
||||
)
|
||||
result_explanation = self.format_results(results)
|
||||
|
||||
total_profit = results.profit_percent.sum()
|
||||
trade_count = len(results.index)
|
||||
trade_duration = results.trade_duration.mean()
|
||||
|
||||
# If this evaluation contains too short amount of trades to be
|
||||
# interesting -- consider it as 'bad' (assigned max. loss value)
|
||||
@@ -246,7 +249,8 @@ class Hyperopt(Backtesting):
|
||||
'result': result_explanation,
|
||||
}
|
||||
|
||||
loss = self.calculate_loss(total_profit, trade_count, trade_duration)
|
||||
loss = self.calculate_loss(results=results, trade_count=trade_count,
|
||||
min_date=min_date.datetime, max_date=max_date.datetime)
|
||||
|
||||
return {
|
||||
'loss': loss,
|
||||
|
37
freqtrade/optimize/hyperopt_loss.py
Normal file
37
freqtrade/optimize/hyperopt_loss.py
Normal file
@@ -0,0 +1,37 @@
|
||||
from math import exp
|
||||
from pandas import DataFrame
|
||||
|
||||
# Define some constants:
|
||||
|
||||
# set TARGET_TRADES to suit your number concurrent trades so its realistic
|
||||
# to the number of days
|
||||
TARGET_TRADES = 600
|
||||
# This is assumed to be expected avg profit * expected trade count.
|
||||
# For example, for 0.35% avg per trade (or 0.0035 as ratio) and 1100 trades,
|
||||
# self.expected_max_profit = 3.85
|
||||
# Check that the reported Σ% values do not exceed this!
|
||||
# Note, this is ratio. 3.85 stated above means 385Σ%.
|
||||
EXPECTED_MAX_PROFIT = 3.0
|
||||
|
||||
# max average trade duration in minutes
|
||||
# if eval ends with higher value, we consider it a failed eval
|
||||
MAX_ACCEPTED_TRADE_DURATION = 300
|
||||
|
||||
|
||||
def hyperopt_loss_legacy(results: DataFrame, trade_count: int,
|
||||
*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:
|
||||
* 0.4 to trade duration
|
||||
* 0.25: Avoiding trade loss
|
||||
"""
|
||||
total_profit = results.profit_percent.sum()
|
||||
trade_duration = results.trade_duration.mean()
|
||||
|
||||
trade_loss = 1 - 0.25 * exp(-(trade_count - TARGET_TRADES) ** 2 / 10 ** 5.8)
|
||||
profit_loss = max(0, 1 - total_profit / EXPECTED_MAX_PROFIT)
|
||||
duration_loss = 0.4 * min(trade_duration / MAX_ACCEPTED_TRADE_DURATION, 1)
|
||||
result = trade_loss + profit_loss + duration_loss
|
||||
return result
|
Reference in New Issue
Block a user