resolved the total profit issue
I resolved the total profit issue and locally ran flak8 and isort
This commit is contained in:
parent
c6b684603c
commit
3b99c84b0a
@ -5,11 +5,13 @@ This module defines the alternative HyperOptLoss class which can be used for
|
|||||||
Hyperoptimization.
|
Hyperoptimization.
|
||||||
"""
|
"""
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from math import sqrt as msqrt
|
||||||
|
from typing import Any, Dict
|
||||||
|
|
||||||
|
from pandas import DataFrame
|
||||||
|
|
||||||
import numpy as np
|
|
||||||
from freqtrade.data.btanalysis import calculate_max_drawdown
|
from freqtrade.data.btanalysis import calculate_max_drawdown
|
||||||
from freqtrade.optimize.hyperopt import IHyperOptLoss
|
from freqtrade.optimize.hyperopt import IHyperOptLoss
|
||||||
from pandas import DataFrame
|
|
||||||
|
|
||||||
|
|
||||||
class CalmarHyperOptLoss(IHyperOptLoss):
|
class CalmarHyperOptLoss(IHyperOptLoss):
|
||||||
@ -20,31 +22,41 @@ class CalmarHyperOptLoss(IHyperOptLoss):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def hyperopt_loss_function(results: DataFrame, trade_count: int,
|
def hyperopt_loss_function(
|
||||||
min_date: datetime, max_date: datetime,
|
results: DataFrame,
|
||||||
*args, **kwargs) -> float:
|
trade_count: int,
|
||||||
|
min_date: datetime,
|
||||||
|
max_date: datetime,
|
||||||
|
backtest_stats: Dict[str, Any],
|
||||||
|
*args,
|
||||||
|
**kwargs
|
||||||
|
) -> float:
|
||||||
"""
|
"""
|
||||||
Objective function, returns smaller number for more optimal results.
|
Objective function, returns smaller number for more optimal results.
|
||||||
|
|
||||||
Uses Calmar Ratio calculation.
|
Uses Calmar Ratio calculation.
|
||||||
"""
|
"""
|
||||||
total_profit = results["profit_ratio"]
|
total_profit = backtest_stats["profit_total"]
|
||||||
days_period = (max_date - min_date).days
|
days_period = (max_date - min_date).days
|
||||||
|
|
||||||
# adding slippage of 0.1% per trade
|
# adding slippage of 0.1% per trade
|
||||||
total_profit = total_profit - 0.0005
|
total_profit = total_profit - 0.0005
|
||||||
expected_returns_mean = total_profit.sum() / days_period
|
expected_returns_mean = total_profit.sum() / days_period * 100
|
||||||
|
|
||||||
# calculate max drawdown
|
# calculate max drawdown
|
||||||
try:
|
try:
|
||||||
_,_,_,high_val,low_val = calculate_max_drawdown(results)
|
_, _, _, high_val, low_val = calculate_max_drawdown(
|
||||||
|
results, value_col="profit_abs"
|
||||||
|
)
|
||||||
max_drawdown = (high_val - low_val) / high_val
|
max_drawdown = (high_val - low_val) / high_val
|
||||||
except ValueError:
|
except ValueError:
|
||||||
max_drawdown = 0
|
max_drawdown = 0
|
||||||
|
|
||||||
if max_drawdown != 0:
|
if max_drawdown != 0:
|
||||||
calmar_ratio = expected_returns_mean / max_drawdown * np.sqrt(365)
|
calmar_ratio = expected_returns_mean / max_drawdown * msqrt(365)
|
||||||
else:
|
else:
|
||||||
calmar_ratio = -20.
|
# Define high (negative) calmar ratio to be clear that this is NOT optimal.
|
||||||
|
calmar_ratio = -20.0
|
||||||
|
|
||||||
|
# print(expected_returns_mean, max_drawdown, calmar_ratio)
|
||||||
return -calmar_ratio
|
return -calmar_ratio
|
||||||
|
Loading…
Reference in New Issue
Block a user