Simplify SortinoLoss

This commit is contained in:
Matthias 2021-03-25 06:49:19 +01:00
parent 958ad7d446
commit 5aaa05f2f2

View File

@ -4,8 +4,8 @@ This module defines the alternative HyperOptLoss class which can be used for
Hyperoptimization.
"""
import logging
import os
from datetime import datetime
from typing import Dict
import numpy as np
from pandas import DataFrame, Timedelta
@ -16,11 +16,7 @@ from freqtrade.optimize.hyperopt import IHyperOptLoss
logger = logging.getLogger(__name__)
interval = os.getenv("FQT_TIMEFRAME") or "5m"
slippage = 0.0005
target = 0
annualize = np.sqrt(365 * (Timedelta("1D") / Timedelta(interval)))
logger.info(f"SortinoLossBalance target is set to: {target}")
@ -31,28 +27,24 @@ class SortinoLossBalance(IHyperOptLoss):
"""
@staticmethod
def hyperopt_loss_function(
results: DataFrame,
trade_count: int,
min_date: datetime,
max_date: datetime,
*args,
**kwargs,
) -> float:
def hyperopt_loss_function(results: DataFrame, trade_count: int,
min_date: datetime, max_date: datetime,
config: Dict, processed: Dict[str, DataFrame],
*args, **kwargs) -> float:
"""
Objective function, returns smaller number for more optimal results.
Uses Sortino Ratio calculation.
"""
hloc = kwargs["processed"]
timeframe = SortinoLossBalance.timeframe
annualize = np.sqrt(365 * (Timedelta("1D") / Timedelta(timeframe)))
balance_total = calculate_outstanding_balance(results, timeframe, hloc)
balance_total = calculate_outstanding_balance(results, timeframe, processed)
returns = balance_total.mean()
# returns = balance_total.values.mean()
downside_returns = np.where(balance_total < 0, balance_total, 0)
downside_risk = np.sqrt((downside_returns ** 2).sum() / len(hloc))
downside_risk = np.sqrt((downside_returns ** 2).sum() / len(processed))
if downside_risk != 0.0:
sortino_ratio = (returns - target) / downside_risk * annualize