freqtrade_origin/freqtrade/optimize/hyperopt_loss/hyperopt_loss_sharpe.py

43 lines
1.1 KiB
Python
Raw Normal View History

2019-07-16 04:45:13 +00:00
"""
2019-07-23 15:51:24 +00:00
SharpeHyperOptLoss
2019-07-16 04:45:13 +00:00
2019-07-23 15:51:24 +00:00
This module defines the alternative HyperOptLoss class which can be used for
Hyperoptimization.
"""
2024-05-12 15:13:50 +00:00
2019-07-16 04:45:13 +00:00
from datetime import datetime
2020-09-28 17:39:41 +00:00
from pandas import DataFrame
2019-07-16 04:45:13 +00:00
2023-01-07 00:14:56 +00:00
from freqtrade.constants import Config
from freqtrade.data.metrics import calculate_sharpe
2023-01-07 00:50:05 +00:00
from freqtrade.optimize.hyperopt import IHyperOptLoss
2019-07-16 04:45:13 +00:00
2023-01-07 00:46:46 +00:00
2019-07-16 04:45:13 +00:00
class SharpeHyperOptLoss(IHyperOptLoss):
"""
2019-07-23 15:51:24 +00:00
Defines the loss function for hyperopt.
This implementation uses the Sharpe Ratio calculation.
2019-07-16 04:45:13 +00:00
"""
@staticmethod
2024-05-12 15:13:50 +00:00
def hyperopt_loss_function(
results: DataFrame,
trade_count: int,
min_date: datetime,
max_date: datetime,
config: Config,
*args,
**kwargs,
) -> float:
2019-07-16 04:45:13 +00:00
"""
2019-07-23 15:51:24 +00:00
Objective function, returns smaller number for more optimal results.
Uses Sharpe Ratio calculation.
2019-07-16 04:45:13 +00:00
"""
2024-05-12 15:13:50 +00:00
starting_balance = config["dry_run_wallet"]
2023-01-06 23:56:40 +00:00
sharp_ratio = calculate_sharpe(results, min_date, max_date, starting_balance)
# print(expected_returns_mean, up_stdev, sharp_ratio)
2019-07-16 04:45:13 +00:00
return -sharp_ratio