Backtrader Example of SMA Signals with Fixed and Reversing Position Sizers
Summary
This Backtrader example pairs a simple moving average crossover strategy with two sizing approaches. The strategy buys when price crosses above a 15-period simple moving average and sells when it crosses below. A long-only sizer submits a fixed stake for buys and sells only when a position exists. The alternative reversing sizer uses a fixed stake when flat and doubles the submitted size when a position is already open, allowing an order to reverse the position under the example’s assumptions.
The script loads historical Yahoo Finance CSV data, allows a date range and starting cash to be set, and can plot results. It demonstrates how sizing logic is separated from signal generation, rather than presenting evidence that either sizing rule is profitable. The example does not discuss transaction costs, risk-based sizing, leverage constraints, or performance statistics, so it is best read as an implementation illustration rather than a complete trading plan.
Key ideas
- A moving average crossover generates buy and sell signals around a 15-period average.
- The long-only sizer uses a fixed stake and avoids selling when no position is open.
- The reversing sizer increases order size when a position exists to support a position reversal.
- The script illustrates sizing mechanics but reports no backtest results or risk analysis.
Tags
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.