We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Hyperopt loss going wrong way ?
<!-- 
Have you searched for similar issues before posting it?

If you have discovered a bug in the bot, please [search the issue tracker](https://github.com/freqtrade/freqtrade/issues?q=is%3Aissue). 
If it hasn't been reported, please create a new issue.

Please do not use bug reports to request new features.
-->

## Describe your environment

  * Operating system: ____ Manjaro
  * Python Version: _____ Python 3.11.8
  * CCXT version: _____ ccxt==4.2.76
  * Freqtrade Version: ____ freqtrade 2024.4
  
Note: All issues other than enhancement requests will be closed without further comment if the above template is deleted or not filled out.

## Describe the problem:

Hi. It seems loss function score is broken somehow (maybe smthg to do with its sign).
Example below (as on 2024-05-07) says #5 is best while obviously it should be #10.
Regards.

### Steps to reproduce:

  1. command used:
  `docker-compose run --rm  freqtrade hyperopt --disable-param-export --config ./user_data/config_binanceWEB.json --strategy TAZ_15m_binance000 --timerange=20230901-20240501 --spaces buy --hyperopt-loss ProfitDrawDownHyperOptLoss --epoch 1000 --timeframe 15m --timeframe-detail 1m --j 1`
  
### Observed Results:
```
+--------+---------+----------+--------------------------+--------------+---------------------------+----------------+--------------+---------------------------+
|   Best |   Epoch |   Trades |    Win  Draw  Loss  Win% |   Avg profit |                    Profit |   Avg duration |    Objective |       Max Drawdown (Acct) |
|--------+---------+----------+--------------------------+--------------+---------------------------+----------------+--------------+---------------------------|
| * Best |   1/135 |        4 |      3     0     1  75.0 |        1.59% |        30.785     (3.08%) |        1:35:00 |    -29.70234 |         3.559     (0.35%) |
| *      |   2/135 |       11 |      8     0     3  72.7 |       -0.94% |      -221.333   (-22.13%) |        0:24:00 |    247.98383 |       145.339    (15.98%) |
| *      |   3/135 |       51 |     42     0     9  82.4 |        0.43% |      -315.876   (-31.59%) |        0:54:00 |     87.71371 |       417.773    (40.04%) |
| *      |   4/135 |        1 |      1     0     0   100 |        2.29% |         3.795     (0.38%) |        0:00:00 |     -3.79499 |                        -- |
| * Best |   5/135 |       51 |     42     0     9  82.4 |        0.11% |      -459.923   (-45.99%) |        0:28:00 | -4,180.04668 |       510.315    (50.84%) |
| *      |   6/135 |      170 |    145     0    25  85.3 |        0.69% |      -581.544   (-58.15%) |        0:52:00 |   -100.19843 |       721.638    (68.98%) |
| *      |   7/135 |        2 |      2     0     0   100 |        2.29% |         7.604     (0.76%) |        0:00:00 |     -7.60435 |                        -- |
| *      |   8/135 |       15 |     14     0     1  93.3 |        1.78% |        64.167     (6.42%) |        1:46:00 |    -59.80514 |        56.433     (5.31%) |
| *      |   9/135 |      184 |    158     0    26  85.9 |        0.74% |      -628.566   (-62.86%) |        0:46:00 |     11.19232 |       790.266    (74.53%) |
| *      |  10/135 |      190 |    174     0    16  91.6 |        1.48% |       602.105    (60.21%) |        1:01:00 |   -586.82945 |       304.910    (16.04%) |
| *      |  11/135 |        5 |      5     0     0   100 |        2.29% |        22.971     (2.30%) |        0:03:00 |    -22.97094 |                        -- |
| *      |  12/135 |      345 |    298     0    47  86.4 |        0.94% |      -494.568   (-49.46%) |        1:21:00 |     17.80432 |       746.868    (70.59%) |
| *      |  13/135 |       44 |     37     0     7  84.1 |        0.64% |      -251.262   (-25.13%) |        0:55:00 |   -624.95887 |       352.895    (35.02%) |
| *      |  14/135 |        2 |      2     0     0   100 |        2.29% |         7.604     (0.76%) |        0:00:00 |     -7.60435 |                        -- |
| *      |  15/135 |        4 |      4     0     0   100 |        2.29% |        15.263     (1.53%) |        0:01:00 |    -15.26296 |                        -- |
| *      |  16/135 |       49 |     40     0     9  81.6 |        0.20% |      -429.174   (-42.92%) |        0:35:00 |   -901.90677 |       471.865    (46.65%) |
| *      |  17/135 |      362 |    310     0    52  85.6 |        0.79% |      -759.062   (-75.91%) |        1:10:00 | -1,031.79724 |       842.359    (82.04%) |
```

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `freqtrade/optimize/hyperopt_loss/hyperopt_loss_profit_drawdown.py`
Content:
```
1 """
2 ProfitDrawDownHyperOptLoss
3 
4 This module defines the alternative HyperOptLoss class based on Profit &
5 Drawdown objective which can be used for Hyperoptimization.
6 
7 Possible to change `DRAWDOWN_MULT` to penalize drawdown objective for
8 individual needs.
9 """
10 
11 from pandas import DataFrame
12 
13 from freqtrade.data.metrics import calculate_max_drawdown
14 from freqtrade.optimize.hyperopt import IHyperOptLoss
15 
16 
17 # higher numbers penalize drawdowns more severely
18 DRAWDOWN_MULT = 0.075
19 
20 
21 class ProfitDrawDownHyperOptLoss(IHyperOptLoss):
22     @staticmethod
23     def hyperopt_loss_function(results: DataFrame, trade_count: int, *args, **kwargs) -> float:
24         total_profit = results["profit_abs"].sum()
25 
26         try:
27             drawdown = calculate_max_drawdown(results, value_col="profit_abs")
28             relative_account_drawdown = drawdown.relative_account_drawdown
29         except ValueError:
30             relative_account_drawdown = 0
31 
32         return -1 * (total_profit * (1 - relative_account_drawdown * DRAWDOWN_MULT))
33
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/freqtrade/optimize/hyperopt_loss/hyperopt_loss_profit_drawdown.py b/freqtrade/optimize/hyperopt_loss/hyperopt_loss_profit_drawdown.py
--- a/freqtrade/optimize/hyperopt_loss/hyperopt_loss_profit_drawdown.py
+++ b/freqtrade/optimize/hyperopt_loss/hyperopt_loss_profit_drawdown.py
@@ -10,23 +10,26 @@
 
 from pandas import DataFrame
 
+from freqtrade.constants import Config
 from freqtrade.data.metrics import calculate_max_drawdown
 from freqtrade.optimize.hyperopt import IHyperOptLoss
 
 
-# higher numbers penalize drawdowns more severely
+# smaller numbers penalize drawdowns more severely
 DRAWDOWN_MULT = 0.075
 
 
 class ProfitDrawDownHyperOptLoss(IHyperOptLoss):
     @staticmethod
-    def hyperopt_loss_function(results: DataFrame, trade_count: int, *args, **kwargs) -> float:
+    def hyperopt_loss_function(results: DataFrame, config: Config, *args, **kwargs) -> float:
         total_profit = results["profit_abs"].sum()
 
         try:
-            drawdown = calculate_max_drawdown(results, value_col="profit_abs")
+            drawdown = calculate_max_drawdown(
+                results, starting_balance=config["dry_run_wallet"], value_col="profit_abs"
+            )
             relative_account_drawdown = drawdown.relative_account_drawdown
         except ValueError:
             relative_account_drawdown = 0
 
-        return -1 * (total_profit * (1 - relative_account_drawdown * DRAWDOWN_MULT))
+        return -1 * (total_profit - (relative_account_drawdown * total_profit) / DRAWDOWN_MULT)

{"golden_diff": "diff --git a/freqtrade/optimize/hyperopt_loss/hyperopt_loss_profit_drawdown.py b/freqtrade/optimize/hyperopt_loss/hyperopt_loss_profit_drawdown.py\n--- a/freqtrade/optimize/hyperopt_loss/hyperopt_loss_profit_drawdown.py\n+++ b/freqtrade/optimize/hyperopt_loss/hyperopt_loss_profit_drawdown.py\n@@ -10,23 +10,26 @@\n \n from pandas import DataFrame\n \n+from freqtrade.constants import Config\n from freqtrade.data.metrics import calculate_max_drawdown\n from freqtrade.optimize.hyperopt import IHyperOptLoss\n \n \n-# higher numbers penalize drawdowns more severely\n+# smaller numbers penalize drawdowns more severely\n DRAWDOWN_MULT = 0.075\n \n \n class ProfitDrawDownHyperOptLoss(IHyperOptLoss):\n     @staticmethod\n-    def hyperopt_loss_function(results: DataFrame, trade_count: int, *args, **kwargs) -> float:\n+    def hyperopt_loss_function(results: DataFrame, config: Config, *args, **kwargs) -> float:\n         total_profit = results[\"profit_abs\"].sum()\n \n         try:\n-            drawdown = calculate_max_drawdown(results, value_col=\"profit_abs\")\n+            drawdown = calculate_max_drawdown(\n+                results, starting_balance=config[\"dry_run_wallet\"], value_col=\"profit_abs\"\n+            )\n             relative_account_drawdown = drawdown.relative_account_drawdown\n         except ValueError:\n             relative_account_drawdown = 0\n \n-        return -1 * (total_profit * (1 - relative_account_drawdown * DRAWDOWN_MULT))\n+        return -1 * (total_profit - (relative_account_drawdown * total_profit) / DRAWDOWN_MULT)\n", "issue": "Hyperopt loss going wrong way ?\n<!-- \r\nHave you searched for similar issues before posting it?\r\n\r\nIf you have discovered a bug in the bot, please [search the issue tracker](https://github.com/freqtrade/freqtrade/issues?q=is%3Aissue). \r\nIf it hasn't been reported, please create a new issue.\r\n\r\nPlease do not use bug reports to request new features.\r\n-->\r\n\r\n## Describe your environment\r\n\r\n  * Operating system: ____ Manjaro\r\n  * Python Version: _____ Python 3.11.8\r\n  * CCXT version: _____ ccxt==4.2.76\r\n  * Freqtrade Version: ____ freqtrade 2024.4\r\n  \r\nNote: All issues other than enhancement requests will be closed without further comment if the above template is deleted or not filled out.\r\n\r\n## Describe the problem:\r\n\r\nHi. It seems loss function score is broken somehow (maybe smthg to do with its sign).\r\nExample below (as on 2024-05-07) says #5 is best while obviously it should be #10.\r\nRegards.\r\n\r\n### Steps to reproduce:\r\n\r\n  1. command used:\r\n  `docker-compose run --rm  freqtrade hyperopt --disable-param-export --config ./user_data/config_binanceWEB.json --strategy TAZ_15m_binance000 --timerange=20230901-20240501 --spaces buy --hyperopt-loss ProfitDrawDownHyperOptLoss --epoch 1000 --timeframe 15m --timeframe-detail 1m --j 1`\r\n  \r\n### Observed Results:\r\n```\r\n+--------+---------+----------+--------------------------+--------------+---------------------------+----------------+--------------+---------------------------+\r\n|   Best |   Epoch |   Trades |    Win  Draw  Loss  Win% |   Avg profit |                    Profit |   Avg duration |    Objective |       Max Drawdown (Acct) |\r\n|--------+---------+----------+--------------------------+--------------+---------------------------+----------------+--------------+---------------------------|\r\n| * Best |   1/135 |        4 |      3     0     1  75.0 |        1.59% |        30.785     (3.08%) |        1:35:00 |    -29.70234 |         3.559     (0.35%) |\r\n| *      |   2/135 |       11 |      8     0     3  72.7 |       -0.94% |      -221.333   (-22.13%) |        0:24:00 |    247.98383 |       145.339    (15.98%) |\r\n| *      |   3/135 |       51 |     42     0     9  82.4 |        0.43% |      -315.876   (-31.59%) |        0:54:00 |     87.71371 |       417.773    (40.04%) |\r\n| *      |   4/135 |        1 |      1     0     0   100 |        2.29% |         3.795     (0.38%) |        0:00:00 |     -3.79499 |                        -- |\r\n| * Best |   5/135 |       51 |     42     0     9  82.4 |        0.11% |      -459.923   (-45.99%) |        0:28:00 | -4,180.04668 |       510.315    (50.84%) |\r\n| *      |   6/135 |      170 |    145     0    25  85.3 |        0.69% |      -581.544   (-58.15%) |        0:52:00 |   -100.19843 |       721.638    (68.98%) |\r\n| *      |   7/135 |        2 |      2     0     0   100 |        2.29% |         7.604     (0.76%) |        0:00:00 |     -7.60435 |                        -- |\r\n| *      |   8/135 |       15 |     14     0     1  93.3 |        1.78% |        64.167     (6.42%) |        1:46:00 |    -59.80514 |        56.433     (5.31%) |\r\n| *      |   9/135 |      184 |    158     0    26  85.9 |        0.74% |      -628.566   (-62.86%) |        0:46:00 |     11.19232 |       790.266    (74.53%) |\r\n| *      |  10/135 |      190 |    174     0    16  91.6 |        1.48% |       602.105    (60.21%) |        1:01:00 |   -586.82945 |       304.910    (16.04%) |\r\n| *      |  11/135 |        5 |      5     0     0   100 |        2.29% |        22.971     (2.30%) |        0:03:00 |    -22.97094 |                        -- |\r\n| *      |  12/135 |      345 |    298     0    47  86.4 |        0.94% |      -494.568   (-49.46%) |        1:21:00 |     17.80432 |       746.868    (70.59%) |\r\n| *      |  13/135 |       44 |     37     0     7  84.1 |        0.64% |      -251.262   (-25.13%) |        0:55:00 |   -624.95887 |       352.895    (35.02%) |\r\n| *      |  14/135 |        2 |      2     0     0   100 |        2.29% |         7.604     (0.76%) |        0:00:00 |     -7.60435 |                        -- |\r\n| *      |  15/135 |        4 |      4     0     0   100 |        2.29% |        15.263     (1.53%) |        0:01:00 |    -15.26296 |                        -- |\r\n| *      |  16/135 |       49 |     40     0     9  81.6 |        0.20% |      -429.174   (-42.92%) |        0:35:00 |   -901.90677 |       471.865    (46.65%) |\r\n| *      |  17/135 |      362 |    310     0    52  85.6 |        0.79% |      -759.062   (-75.91%) |        1:10:00 | -1,031.79724 |       842.359    (82.04%) |\r\n```\n", "before_files": [{"content": "\"\"\"\nProfitDrawDownHyperOptLoss\n\nThis module defines the alternative HyperOptLoss class based on Profit &\nDrawdown objective which can be used for Hyperoptimization.\n\nPossible to change `DRAWDOWN_MULT` to penalize drawdown objective for\nindividual needs.\n\"\"\"\n\nfrom pandas import DataFrame\n\nfrom freqtrade.data.metrics import calculate_max_drawdown\nfrom freqtrade.optimize.hyperopt import IHyperOptLoss\n\n\n# higher numbers penalize drawdowns more severely\nDRAWDOWN_MULT = 0.075\n\n\nclass ProfitDrawDownHyperOptLoss(IHyperOptLoss):\n    @staticmethod\n    def hyperopt_loss_function(results: DataFrame, trade_count: int, *args, **kwargs) -> float:\n        total_profit = results[\"profit_abs\"].sum()\n\n        try:\n            drawdown = calculate_max_drawdown(results, value_col=\"profit_abs\")\n            relative_account_drawdown = drawdown.relative_account_drawdown\n        except ValueError:\n            relative_account_drawdown = 0\n\n        return -1 * (total_profit * (1 - relative_account_drawdown * DRAWDOWN_MULT))\n", "path": "freqtrade/optimize/hyperopt_loss/hyperopt_loss_profit_drawdown.py"}], "after_files": [{"content": "\"\"\"\nProfitDrawDownHyperOptLoss\n\nThis module defines the alternative HyperOptLoss class based on Profit &\nDrawdown objective which can be used for Hyperoptimization.\n\nPossible to change `DRAWDOWN_MULT` to penalize drawdown objective for\nindividual needs.\n\"\"\"\n\nfrom pandas import DataFrame\n\nfrom freqtrade.constants import Config\nfrom freqtrade.data.metrics import calculate_max_drawdown\nfrom freqtrade.optimize.hyperopt import IHyperOptLoss\n\n\n# smaller numbers penalize drawdowns more severely\nDRAWDOWN_MULT = 0.075\n\n\nclass ProfitDrawDownHyperOptLoss(IHyperOptLoss):\n    @staticmethod\n    def hyperopt_loss_function(results: DataFrame, config: Config, *args, **kwargs) -> float:\n        total_profit = results[\"profit_abs\"].sum()\n\n        try:\n            drawdown = calculate_max_drawdown(\n                results, starting_balance=config[\"dry_run_wallet\"], value_col=\"profit_abs\"\n            )\n            relative_account_drawdown = drawdown.relative_account_drawdown\n        except ValueError:\n            relative_account_drawdown = 0\n\n        return -1 * (total_profit - (relative_account_drawdown * total_profit) / DRAWDOWN_MULT)\n", "path": "freqtrade/optimize/hyperopt_loss/hyperopt_loss_profit_drawdown.py"}]}