2020-02-07 00:18:15 +00:00
|
|
|
"""
|
|
|
|
SortinoHyperOptLossDaily
|
|
|
|
|
|
|
|
This module defines the alternative HyperOptLoss class which can be used for
|
|
|
|
Hyperoptimization.
|
|
|
|
"""
|
|
|
|
import math
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
from pandas import DataFrame, date_range
|
|
|
|
|
|
|
|
from freqtrade.optimize.hyperopt import IHyperOptLoss
|
|
|
|
|
|
|
|
|
|
|
|
class SortinoHyperOptLossDaily(IHyperOptLoss):
|
|
|
|
"""
|
|
|
|
Defines the loss function for hyperopt.
|
|
|
|
|
|
|
|
This implementation uses the Sortino Ratio calculation.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def hyperopt_loss_function(results: DataFrame, trade_count: int,
|
|
|
|
min_date: datetime, max_date: datetime,
|
|
|
|
*args, **kwargs) -> float:
|
|
|
|
"""
|
|
|
|
Objective function, returns smaller number for more optimal results.
|
|
|
|
|
|
|
|
Uses Sortino Ratio calculation.
|
2020-02-16 10:46:07 +00:00
|
|
|
|
|
|
|
Sortino Ratio calculated as described in
|
|
|
|
http://www.redrockcapital.com/Sortino__A__Sharper__Ratio_Red_Rock_Capital.pdf
|
2020-02-07 00:18:15 +00:00
|
|
|
"""
|
|
|
|
resample_freq = '1D'
|
|
|
|
slippage_per_trade_ratio = 0.0005
|
|
|
|
days_in_year = 365
|
2020-02-16 00:55:16 +00:00
|
|
|
minimum_acceptable_return = 0.0
|
2020-02-07 00:18:15 +00:00
|
|
|
|
2021-01-23 12:02:48 +00:00
|
|
|
# apply slippage per trade to profit_ratio
|
|
|
|
results.loc[:, 'profit_ratio_after_slippage'] = \
|
|
|
|
results['profit_ratio'] - slippage_per_trade_ratio
|
2020-02-07 00:18:15 +00:00
|
|
|
|
|
|
|
# create the index within the min_date and end max_date
|
2020-02-09 18:20:15 +00:00
|
|
|
t_index = date_range(start=min_date, end=max_date, freq=resample_freq,
|
|
|
|
normalize=True)
|
2020-02-07 00:18:15 +00:00
|
|
|
|
|
|
|
sum_daily = (
|
2020-06-26 07:19:44 +00:00
|
|
|
results.resample(resample_freq, on='close_date').agg(
|
2023-01-31 11:22:04 +00:00
|
|
|
{"profit_ratio_after_slippage": 'sum'}).reindex(t_index).fillna(0)
|
2020-02-07 00:18:15 +00:00
|
|
|
)
|
|
|
|
|
2021-01-23 12:02:48 +00:00
|
|
|
total_profit = sum_daily["profit_ratio_after_slippage"] - minimum_acceptable_return
|
2020-02-07 00:18:15 +00:00
|
|
|
expected_returns_mean = total_profit.mean()
|
|
|
|
|
2020-02-07 01:12:24 +00:00
|
|
|
sum_daily['downside_returns'] = 0
|
2020-02-16 01:10:53 +00:00
|
|
|
sum_daily.loc[total_profit < 0, 'downside_returns'] = total_profit
|
2020-02-13 04:07:35 +00:00
|
|
|
total_downside = sum_daily['downside_returns']
|
2020-02-28 20:50:25 +00:00
|
|
|
# Here total_downside contains min(0, P - MAR) values,
|
2021-01-23 12:02:48 +00:00
|
|
|
# where P = sum_daily["profit_ratio_after_slippage"]
|
2020-02-28 20:50:25 +00:00
|
|
|
down_stdev = math.sqrt((total_downside**2).sum() / len(total_downside))
|
2020-02-07 00:18:15 +00:00
|
|
|
|
2020-03-10 10:44:16 +00:00
|
|
|
if down_stdev != 0:
|
2020-02-07 00:18:15 +00:00
|
|
|
sortino_ratio = expected_returns_mean / down_stdev * math.sqrt(days_in_year)
|
|
|
|
else:
|
|
|
|
# Define high (negative) sortino ratio to be clear that this is NOT optimal.
|
|
|
|
sortino_ratio = -20.
|
|
|
|
|
|
|
|
# print(t_index, sum_daily, total_profit)
|
2020-02-16 12:26:40 +00:00
|
|
|
# print(minimum_acceptable_return, expected_returns_mean, down_stdev, sortino_ratio)
|
2020-02-07 00:18:15 +00:00
|
|
|
return -sortino_ratio
|