• 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.45
/freqtrade/optimize/hyperopt_loss/hyperopt_loss_sharpe_daily.py
1
"""
2
SharpeHyperOptLossDaily
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 SharpeHyperOptLossDaily(IHyperOptLoss):
1✔
16
    """
17
    Defines the loss function for hyperopt.
18

19
    This implementation uses the Sharpe 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 Sharpe Ratio calculation.
30
        """
31
        resample_freq = '1D'
1✔
32
        slippage_per_trade_ratio = 0.0005
1✔
33
        days_in_year = 365
1✔
34
        annual_risk_free_rate = 0.0
1✔
35
        risk_free_rate = annual_risk_free_rate / days_in_year
1✔
36

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

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

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

50
        total_profit = sum_daily["profit_ratio_after_slippage"] - risk_free_rate
1✔
51
        expected_returns_mean = total_profit.mean()
1✔
52
        up_stdev = total_profit.std()
1✔
53

54
        if up_stdev != 0:
1✔
55
            sharp_ratio = expected_returns_mean / up_stdev * math.sqrt(days_in_year)
1✔
56
        else:
57
            # Define high (negative) sharpe ratio to be clear that this is NOT optimal.
58
            sharp_ratio = -20.
×
59

60
        # print(t_index, sum_daily, total_profit)
61
        # print(risk_free_rate, expected_returns_mean, up_stdev, sharp_ratio)
62
        return -sharp_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