Easy Trading Strategy Optimization with backtesting.py (Python Tutorial)

B/O Trading Blog
7 min readAug 28, 2022

Photo by Markus Winkler on Unsplash.com

Strategy optimization doesn’t have to be hard and you don’t even have to code it yourself. Python backtesting libraries like backtrader, zipline or backtesting.py come with a built-in optimization engine that finds the optimal combination of strategy parameter values.

If you would like to learn how to optimize your trading strategy using backtesting.py this post is for you!

This story is solely for general information purposes, and should not be relied upon for trading recommendations or financial advice. Source code and information is provided for educational purposes only, and should not be relied upon to make an investment decision. Please review my full cautionary guidance before continuing.

Optimization Options

Backtesting.py offers two optimization options: Randomized Grid Search and the scikit-optimize package.

Grid Search randomly searches through the combinations of specified parameters in order to optimize profit.

The second optimization option using the scikit-optimize package uses forests of decision trees. The library uses a tree based regression model to model the expensive backtest function. The model…

--

--

B/O Trading Blog

Blogging about algorithmic trading, Python utilities and passive income opportunities.