Optimize strategy parameters to an optimal combination. Returns result `pd.Series` of the best run. `maximize` is a string key from the `backtesting.backtesting.Backtest.run`-returned results series, or a function that accepts this series object and returns
(self, *,
maximize: Union[str, Callable[[pd.Series], float]] = 'SQN',
method: str = 'grid',
max_tries: Optional[Union[int, float]] = None,
constraint: Optional[Callable[[dict], bool]] = None,
return_heatmap: bool = False,
return_optimization: bool = False,
random_state: Optional[int] = None,
**kwargs)
| 1369 | return self._results |
| 1370 | |
| 1371 | def optimize(self, *, |
| 1372 | maximize: Union[str, Callable[[pd.Series], float]] = 'SQN', |
| 1373 | method: str = 'grid', |
| 1374 | max_tries: Optional[Union[int, float]] = None, |
| 1375 | constraint: Optional[Callable[[dict], bool]] = None, |
| 1376 | return_heatmap: bool = False, |
| 1377 | return_optimization: bool = False, |
| 1378 | random_state: Optional[int] = None, |
| 1379 | **kwargs) -> Union[pd.Series, |
| 1380 | Tuple[pd.Series, pd.Series], |
| 1381 | Tuple[pd.Series, pd.Series, dict]]: |
| 1382 | """ |
| 1383 | Optimize strategy parameters to an optimal combination. |
| 1384 | Returns result `pd.Series` of the best run. |
| 1385 | |
| 1386 | `maximize` is a string key from the |
| 1387 | `backtesting.backtesting.Backtest.run`-returned results series, |
| 1388 | or a function that accepts this series object and returns a number; |
| 1389 | the higher the better. By default, the method maximizes |
| 1390 | Van Tharp's [System Quality Number](https://google.com/search?q=System+Quality+Number). |
| 1391 | |
| 1392 | `method` is the optimization method. Currently two methods are supported: |
| 1393 | |
| 1394 | * `"grid"` which does an exhaustive (or randomized) search over the |
| 1395 | cartesian product of parameter combinations, and |
| 1396 | * `"sambo"` which finds close-to-optimal strategy parameters using |
| 1397 | [model-based optimization], making at most `max_tries` evaluations. |
| 1398 | |
| 1399 | [model-based optimization]: https://sambo-optimization.github.io |
| 1400 | |
| 1401 | `max_tries` is the maximal number of strategy runs to perform. |
| 1402 | If `method="grid"`, this results in randomized grid search. |
| 1403 | If `max_tries` is a floating value between (0, 1], this sets the |
| 1404 | number of runs to approximately that fraction of full grid space. |
| 1405 | Alternatively, if integer, it denotes the absolute maximum number |
| 1406 | of evaluations. If unspecified (default), grid search is exhaustive, |
| 1407 | whereas for `method="sambo"`, `max_tries` is set to 200. |
| 1408 | |
| 1409 | `constraint` is a function that accepts a dict-like object of |
| 1410 | parameters (with values) and returns `True` when the combination |
| 1411 | is admissible to test with. By default, any parameters combination |
| 1412 | is considered admissible. |
| 1413 | |
| 1414 | If `return_heatmap` is `True`, besides returning the result |
| 1415 | series, an additional `pd.Series` is returned with a multiindex |
| 1416 | of all admissible parameter combinations, which can be further |
| 1417 | inspected or projected onto 2D to plot a heatmap |
| 1418 | (see `backtesting.lib.plot_heatmaps()`). |
| 1419 | |
| 1420 | If `return_optimization` is True and `method = 'sambo'`, |
| 1421 | in addition to result series (and maybe heatmap), return raw |
| 1422 | [`scipy.optimize.OptimizeResult`][OptimizeResult] for further |
| 1423 | inspection, e.g. with [SAMBO]'s [plotting tools]. |
| 1424 | |
| 1425 | [OptimizeResult]: https://sambo-optimization.github.io/doc/sambo/#sambo.OptimizeResult |
| 1426 | [SAMBO]: https://sambo-optimization.github.io |
| 1427 | [plotting tools]: https://sambo-optimization.github.io/doc/sambo/plot.html |
| 1428 |
no test coverage detected