• 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

98.31
/freqtrade/optimize/hyperopt_epoch_filters.py
1
import logging
1✔
2
from typing import List
1✔
3

4
from freqtrade.exceptions import OperationalException
1✔
5

6

7
logger = logging.getLogger(__name__)
1✔
8

9

10
def hyperopt_filter_epochs(epochs: List, filteroptions: dict, log: bool = True) -> List:
1✔
11
    """
12
    Filter our items from the list of hyperopt results
13
    """
14
    if filteroptions['only_best']:
1✔
15
        epochs = [x for x in epochs if x['is_best']]
1✔
16
    if filteroptions['only_profitable']:
1✔
17
        epochs = [x for x in epochs
1✔
18
                  if x['results_metrics'].get('profit_total', 0) > 0]
19

20
    epochs = _hyperopt_filter_epochs_trade_count(epochs, filteroptions)
1✔
21

22
    epochs = _hyperopt_filter_epochs_duration(epochs, filteroptions)
1✔
23

24
    epochs = _hyperopt_filter_epochs_profit(epochs, filteroptions)
1✔
25

26
    epochs = _hyperopt_filter_epochs_objective(epochs, filteroptions)
1✔
27
    if log:
1✔
28
        logger.info(f"{len(epochs)} " +
1✔
29
                    ("best " if filteroptions['only_best'] else "") +
30
                    ("profitable " if filteroptions['only_profitable'] else "") +
31
                    "epochs found.")
32
    return epochs
1✔
33

34

35
def _hyperopt_filter_epochs_trade(epochs: List, trade_count: int):
1✔
36
    """
37
    Filter epochs with trade-counts > trades
38
    """
39
    return [
1✔
40
        x for x in epochs if x['results_metrics'].get('total_trades', 0) > trade_count
41
    ]
42

43

44
def _hyperopt_filter_epochs_trade_count(epochs: List, filteroptions: dict) -> List:
1✔
45

46
    if filteroptions['filter_min_trades'] > 0:
1✔
47
        epochs = _hyperopt_filter_epochs_trade(epochs, filteroptions['filter_min_trades'])
1✔
48

49
    if filteroptions['filter_max_trades'] > 0:
1✔
50
        epochs = [
1✔
51
            x for x in epochs
52
            if x['results_metrics'].get('total_trades') < filteroptions['filter_max_trades']
53
        ]
54
    return epochs
1✔
55

56

57
def _hyperopt_filter_epochs_duration(epochs: List, filteroptions: dict) -> List:
1✔
58

59
    def get_duration_value(x):
1✔
60
        # Duration in minutes ...
61
        if 'holding_avg_s' in x['results_metrics']:
1✔
62
            avg = x['results_metrics']['holding_avg_s']
1✔
63
            return avg // 60
1✔
64
        raise OperationalException(
×
65
            "Holding-average not available. Please omit the filter on average time, "
66
            "or rerun hyperopt with this version")
67

68
    if filteroptions['filter_min_avg_time'] is not None:
1✔
69
        epochs = _hyperopt_filter_epochs_trade(epochs, 0)
1✔
70
        epochs = [
1✔
71
            x for x in epochs
72
            if get_duration_value(x) > filteroptions['filter_min_avg_time']
73
        ]
74
    if filteroptions['filter_max_avg_time'] is not None:
1✔
75
        epochs = _hyperopt_filter_epochs_trade(epochs, 0)
1✔
76
        epochs = [
1✔
77
            x for x in epochs
78
            if get_duration_value(x) < filteroptions['filter_max_avg_time']
79
        ]
80

81
    return epochs
1✔
82

83

84
def _hyperopt_filter_epochs_profit(epochs: List, filteroptions: dict) -> List:
1✔
85

86
    if filteroptions['filter_min_avg_profit'] is not None:
1✔
87
        epochs = _hyperopt_filter_epochs_trade(epochs, 0)
1✔
88
        epochs = [
1✔
89
            x for x in epochs
90
            if x['results_metrics'].get('profit_mean', 0) * 100
91
            > filteroptions['filter_min_avg_profit']
92
        ]
93
    if filteroptions['filter_max_avg_profit'] is not None:
1✔
94
        epochs = _hyperopt_filter_epochs_trade(epochs, 0)
1✔
95
        epochs = [
1✔
96
            x for x in epochs
97
            if x['results_metrics'].get('profit_mean', 0) * 100
98
            < filteroptions['filter_max_avg_profit']
99
        ]
100
    if filteroptions['filter_min_total_profit'] is not None:
1✔
101
        epochs = _hyperopt_filter_epochs_trade(epochs, 0)
1✔
102
        epochs = [
1✔
103
            x for x in epochs
104
            if x['results_metrics'].get('profit_total_abs', 0)
105
            > filteroptions['filter_min_total_profit']
106
        ]
107
    if filteroptions['filter_max_total_profit'] is not None:
1✔
108
        epochs = _hyperopt_filter_epochs_trade(epochs, 0)
1✔
109
        epochs = [
1✔
110
            x for x in epochs
111
            if x['results_metrics'].get('profit_total_abs', 0)
112
            < filteroptions['filter_max_total_profit']
113
        ]
114
    return epochs
1✔
115

116

117
def _hyperopt_filter_epochs_objective(epochs: List, filteroptions: dict) -> List:
1✔
118

119
    if filteroptions['filter_min_objective'] is not None:
1✔
120
        epochs = _hyperopt_filter_epochs_trade(epochs, 0)
1✔
121

122
        epochs = [x for x in epochs if x['loss'] < filteroptions['filter_min_objective']]
1✔
123
    if filteroptions['filter_max_objective'] is not None:
1✔
124
        epochs = _hyperopt_filter_epochs_trade(epochs, 0)
1✔
125

126
        epochs = [x for x in epochs if x['loss'] > filteroptions['filter_max_objective']]
1✔
127

128
    return epochs
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