Skip to content

Commit 829cefa

Browse files
committed
fix build
1 parent 27bfb3f commit 829cefa

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ Avg. Trade Duration 32 days 00:00:00
7979
Profit Factor 2.13
8080
Expectancy [%] 6.91
8181
SQN 1.78
82+
Kelly Criterion 0.6134
8283
_strategy SmaCross(n1=10, n2=20)
8384
_equity_curve Equ...
8485
_trades Size EntryB...

backtesting/backtesting.py

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,7 @@ def run(self, **kwargs) -> pd.Series:
11271127
Profit Factor 2.08802
11281128
Expectancy [%] 8.79171
11291129
SQN 0.916893
1130+
Kelly Criterion 0.6134
11301131
_strategy SmaCross
11311132
_equity_curve Eq...
11321133
_trades Size EntryB...
@@ -1298,7 +1299,7 @@ def constraint(_):
12981299
"of strategy parameters and returns a bool whether "
12991300
"the combination of parameters is admissible or not")
13001301

1301-
if return_optimization and method != 'skopt':
1302+
if return_optimization and method not in ('skopt', 'skopt_gp'):
13021303
raise ValueError("return_optimization=True only valid if method='skopt'")
13031304

13041305
def _tuple(x):
@@ -1398,7 +1399,7 @@ def _optimize_skopt() -> Union[pd.Series,
13981399
Tuple[pd.Series, pd.Series],
13991400
Tuple[pd.Series, pd.Series, dict]]:
14001401
try:
1401-
from skopt import forest_minimize
1402+
from skopt import forest_minimize, gp_minimize
14021403
from skopt.space import Integer, Real, Categorical
14031404
from skopt.utils import use_named_args
14041405
from skopt.callbacks import DeltaXStopper
@@ -1453,17 +1454,29 @@ def objective_function(**params):
14531454
warnings.filterwarnings(
14541455
'ignore', 'The objective has been evaluated at this point before.')
14551456

1456-
res = forest_minimize(
1457-
func=objective_function,
1458-
dimensions=dimensions,
1459-
n_calls=max_tries,
1460-
base_estimator=ExtraTreesRegressor(n_estimators=20, min_samples_leaf=2),
1461-
acq_func='LCB',
1462-
kappa=3,
1463-
n_initial_points=min(max_tries, 20 + 3 * len(kwargs)),
1464-
initial_point_generator='lhs', # 'sobel' requires n_initial_points ~ 2**N
1465-
callback=DeltaXStopper(9e-7),
1466-
random_state=random_state)
1457+
if method == 'skopt_gp':
1458+
res = gp_minimize(
1459+
func=objective_function,
1460+
dimensions=dimensions,
1461+
n_calls=max_tries,
1462+
n_jobs=-1,
1463+
acq_func='gp_hedge',
1464+
n_initial_points=min(max_tries, 20 + 3 * len(kwargs)),
1465+
initial_point_generator='random', # 'sobel' requires n_initial_points ~ 2**N
1466+
callback=DeltaXStopper(9e-7),
1467+
random_state=random_state)
1468+
else:
1469+
res = forest_minimize(
1470+
func=objective_function,
1471+
dimensions=dimensions,
1472+
n_calls=max_tries,
1473+
base_estimator=ExtraTreesRegressor(n_estimators=20, min_samples_leaf=2),
1474+
acq_func='LCB',
1475+
kappa=3,
1476+
n_initial_points=min(max_tries, 20 + 3 * len(kwargs)),
1477+
initial_point_generator='lhs', # 'sobel' requires n_initial_points ~ 2**N
1478+
callback=DeltaXStopper(9e-7),
1479+
random_state=random_state)
14671480

14681481
stats = self.run(**dict(zip(kwargs.keys(), res.x)))
14691482
output = [stats]
@@ -1486,7 +1499,7 @@ def objective_function(**params):
14861499

14871500
if method == 'grid':
14881501
output = _optimize_grid()
1489-
elif method == 'skopt':
1502+
elif method in ('skopt', 'skopt_gp'):
14901503
output = _optimize_skopt()
14911504
else:
14921505
raise ValueError(f"Method should be 'grid' or 'skopt', not {method!r}")

backtesting/test/_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ def test_compute_stats(self):
276276
'Return [%]': 414.2298999999996,
277277
'Volatility (Ann.) [%]': 36.49390889140787,
278278
'SQN': 1.0766187356697705,
279+
'Kelly Criterion': 0.7875234266909678,
279280
'Sharpe Ratio': 0.5803778344714113,
280281
'Sortino Ratio': 1.0847880675854096,
281282
'Start': pd.Timestamp('2004-08-19 00:00:00'),

0 commit comments

Comments
 (0)