freqtrade_origin/freqtrade/optimize/default_hyperopt_loss.py

57 lines
1.9 KiB
Python
Raw Normal View History

2019-07-16 04:27:23 +00:00
"""
ShortTradeDurHyperOptLoss
2019-07-16 04:45:13 +00:00
This module defines the default HyperoptLoss class which is being used for
Hyperoptimization.
2019-07-16 04:27:23 +00:00
"""
from math import exp
from pandas import DataFrame
from freqtrade.optimize.hyperopt import IHyperOptLoss
2019-07-16 04:27:23 +00:00
2019-07-23 15:51:24 +00:00
# Set TARGET_TRADES to suit your number concurrent trades so its realistic
2019-07-16 04:27:23 +00:00
# to the number of days
TARGET_TRADES = 600
2019-07-23 15:51:24 +00:00
2019-07-16 04:27:23 +00:00
# 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,
2019-07-23 15:51:24 +00:00
# expected max profit = 3.85
2019-07-16 04:27:23 +00:00
# Check that the reported Σ% values do not exceed this!
# Note, this is ratio. 3.85 stated above means 385Σ%.
EXPECTED_MAX_PROFIT = 3.0
2019-07-23 15:51:24 +00:00
# Max average trade duration in minutes.
# If eval ends with higher value, we consider it a failed eval.
2019-07-16 04:27:23 +00:00
MAX_ACCEPTED_TRADE_DURATION = 300
class ShortTradeDurHyperOptLoss(IHyperOptLoss):
2019-07-16 04:27:23 +00:00
"""
Defines the default loss function for hyperopt
"""
@staticmethod
def hyperopt_loss_function(results: DataFrame, trade_count: int,
*args, **kwargs) -> float:
"""
Objective function, returns smaller number for better results
2019-07-17 04:32:24 +00:00
This is the Default algorithm
2019-07-16 04:27:23 +00:00
Weights are distributed as follows:
* 0.4 to trade duration
* 0.25: Avoiding trade loss
2019-07-17 04:32:24 +00:00
* 1.0 to total profit, compared to the expected value (`EXPECTED_MAX_PROFIT`) defined above
2019-07-16 04:27:23 +00:00
"""
total_profit = results['profit_percent'].sum()
trade_duration = results['trade_duration'].mean()
2019-07-16 04:27:23 +00:00
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
# Create an alias for This to allow the legacy Method to work as well.
DefaultHyperOptLoss = ShortTradeDurHyperOptLoss