Library of Composable Base Strategies

This tutorial will show how to reuse composable base trading strategies that are part of backtesting.py software distribution. It is, henceforth, assumed you're already familiar with basic package usage.

We'll extend the same moving average cross-over strategy as in Quick Start User Guide, but we'll rewrite it as a vectorized signal strategy and add trailing stop-loss.

Again, we'll use our helper moving average function.

Part of this software distribution is backtesting.lib module that contains various reusable utilities for strategy development. Some of those utilities are composable base strategies we can extend and build upon.

We import and extend two of those strategies here:

Note, since the strategies in lib may require their own intialization and next-tick logic, be sure to always call super().init() and super().next() in your overridden methods.

Let's see how the example strategy fares on historical Google data.

Notice how managing risk with a trailing stop-loss secures our gains and limits our losses.

For other strategies of the sort, and other reusable utilities in general, see backtesting.lib module reference.

Learn more by exploring further examples or find more framework options in the full API reference.