• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

freqtrade / freqtrade / 9394559170

26 Apr 2024 06:36AM UTC coverage: 94.656% (-0.02%) from 94.674%
9394559170

push

github

xmatthias
Loader should be passed as kwarg for clarity

20280 of 21425 relevant lines covered (94.66%)

0.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.83
/freqtrade/optimize/hyperopt_loss/hyperopt_loss_sortino_daily.py
1
"""
2
SortinoHyperOptLossDaily
3

4
This module defines the alternative HyperOptLoss class which can be used for
5
Hyperoptimization.
6
"""
7
import math
1✔
8
from datetime import datetime
1✔
9

10
from pandas import DataFrame, date_range
1✔
11

12
from freqtrade.optimize.hyperopt import IHyperOptLoss
1✔
13

14

15
class SortinoHyperOptLossDaily(IHyperOptLoss):
1✔
16
    """
17
    Defines the loss function for hyperopt.
18

19
    This implementation uses the Sortino Ratio calculation.
20
    """
21

22
    @staticmethod
1✔
23
    def hyperopt_loss_function(results: DataFrame, trade_count: int,
1✔
24
                               min_date: datetime, max_date: datetime,
25
                               *args, **kwargs) -> float:
26
        """
27
        Objective function, returns smaller number for more optimal results.
28

29
        Uses Sortino Ratio calculation.
30

31
        Sortino Ratio calculated as described in
32
        http://www.redrockcapital.com/Sortino__A__Sharper__Ratio_Red_Rock_Capital.pdf
33
        """
34
        resample_freq = '1D'
1✔
35
        slippage_per_trade_ratio = 0.0005
1✔
36
        days_in_year = 365
1✔
37
        minimum_acceptable_return = 0.0
1✔
38

39
        # apply slippage per trade to profit_ratio
40
        results.loc[:, 'profit_ratio_after_slippage'] = \
1✔
41
            results['profit_ratio'] - slippage_per_trade_ratio
42

43
        # create the index within the min_date and end max_date
44
        t_index = date_range(start=min_date, end=max_date, freq=resample_freq,
1✔
45
                             normalize=True)
46

47
        sum_daily = (
1✔
48
            results.resample(resample_freq, on='close_date').agg(
49
                {"profit_ratio_after_slippage": 'sum'}).reindex(t_index).fillna(0)
50
        )
51

52
        total_profit = sum_daily["profit_ratio_after_slippage"] - minimum_acceptable_return
1✔
53
        expected_returns_mean = total_profit.mean()
1✔
54

55
        sum_daily['downside_returns'] = 0.0
1✔
56
        sum_daily.loc[total_profit < 0, 'downside_returns'] = total_profit
1✔
57
        total_downside = sum_daily['downside_returns']
1✔
58
        # Here total_downside contains min(0, P - MAR) values,
59
        # where P = sum_daily["profit_ratio_after_slippage"]
60
        down_stdev = math.sqrt((total_downside**2).sum() / len(total_downside))
1✔
61

62
        if down_stdev != 0:
1✔
63
            sortino_ratio = expected_returns_mean / down_stdev * math.sqrt(days_in_year)
1✔
64
        else:
65
            # Define high (negative) sortino ratio to be clear that this is NOT optimal.
66
            sortino_ratio = -20.
×
67

68
        # print(t_index, sum_daily, total_profit)
69
        # print(minimum_acceptable_return, expected_returns_mean, down_stdev, sortino_ratio)
70
        return -sortino_ratio
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc