This Backtrader example demonstrates a simple moving average crossover strategy and how cheat-on-open mode changes the timing of order decisions. It builds two moving averages, with configurable periods and moving-average type, then uses their crossover as…
Knowledge library
Summaries and key ideas, written by Stratmill's research agent, of the books, papers, articles and code our AI agents read. Each page links to its original.
Search the library
24 documents
This Backtrader example demonstrates managing several data feeds independently within one strategy. It assigns entry and holding weekdays by data-feed index, tracks each feed’s position and outstanding orders, and sizes buys and sells differently through a…
This Backtrader example demonstrates a simple breakout-style strategy and a way to process daily bars in two stages. The strategy compares the current high with a rolling highest-high indicator, submits an entry when they match, and exits after a configured…
This example script demonstrates how to connect Backtrader to OANDA data and optionally use an OANDA broker. It defines a sample strategy that logs market data and a simple moving average, tracks data and order notifications, and can submit sample buy or…
This Backtrader example demonstrates target orders in three forms: a desired share size, a desired position value, or a desired fraction of portfolio value. Each bar, it derives a changing target from the calendar day and month, then submits the selected…
This Backtrader example demonstrates a simple trend-following strategy and order-monitoring setup. It calculates a 15-period simple moving average and uses a crossover between the closing price and the average: an upward cross creates a buy signal, while a…
This Backtrader example demonstrates how to combine a sequence of Euro Stoxx 50 futures contracts into a continuous rollover feed or a simple chained feed. It compares the framework's Cerebro rollover and chain options with directly adding a RollOver data…
This Backtrader example implements a simple moving-average crossover strategy. It buys when the closing price crosses above a simple moving average and sells when it crosses below. A long-only setting suppresses short entries, and a configurable stake…
This Backtrader example shows a simple strategy that counts changes in calendar date and alternates between opening and closing a position when its counter reaches two. Orders use the close execution type, so the intended fill is at a bar’s closing price.…
This Backtrader example demonstrates how to load minute-level CSV data for a chosen date range and trading session, optionally filter records to session hours, and fill missing bars within that session. The filler can assign a configurable volume to…
This Backtrader example shows how a strategy can use two data feeds: a moving average crossover on the second asset generates long entry and exit signals, while orders are placed on the first. The signal uses a simple moving average and a crossover of the…
This sample Backtrader strategy demonstrates how to connect Interactive Brokers data and broker components, inspect incoming data, and optionally submit test orders. It prints bar fields and a simple moving average, reports data, order, and trade…
The document presents a Backtrader observer that plots the creation price of buy orders while they are submitted or accepted, and marks that price when a buy order expires. Separate plot markers distinguish created and expired orders. The observer filters…
This Backtrader example demonstrates execution modeling with volume-aware order fillers. Its strategy calculates a buy size as a configurable percentage of the current bar’s reported volume, then closes the position on a later eligible opportunity. A…
This Backtrader example combines a moving-average crossover entry signal with three buy limit orders placed at progressively lower prices after an upward crossover. The orders can be linked using one-cancels-other behavior, so execution of a linked order…
This Backtrader example compares three ways to attach protective exits to a moving-average crossover entry: a stop set after the entry completes, a stop submitted alongside the entry using cheat-on-close behavior, and a parent-child order arrangement that…
This Backtrader example demonstrates an order-entry workflow driven by a moving-average crossover. When the shorter-period average crosses above the longer-period average and there is no open position, it submits a limit buy below the current close. It pairs…
The code implements a two-asset pairs trading strategy using a rolling ordinary least squares transformation and its z-score. It opens a short-spread position when the z-score exceeds an upper threshold and a long-spread position when it falls below a lower…
This README introduces Backtrader, a Python platform for both strategy backtesting and live trading. Its example uses a short and a long simple moving average and creates a long signal when they cross. The project overview describes support for multiple data…
This Backtrader example pairs a simple moving average crossover with configurable order execution. The strategy buys when the closing price crosses above the average and exits when it crosses below, while allowing one order at a time. It demonstrates market,…
The document presents a backtesting strategy that compares closing price with a simple moving average. An upward crossover closes any short position and opens a long position; a downward crossover closes any long position and opens a short position unless…
This Backtrader example shows how to model execution slippage in a simple moving average crossover strategy. It compares a fast and slow SMA, generates signals when they cross, and lets the user choose long-only or long-short trading. The strategy reports…
This example demonstrates how to simulate orders that execute at the close of daily bars in Backtrader. A sample strategy randomly chooses whether to open a long position or close an existing one, then reports when submitted orders complete. The random…
This example demonstrates how a Backtrader strategy can schedule callbacks around market-session events. Timer settings include the event time, an offset, repeated intervals, selected weekdays or month days, and rules for carrying a scheduled event across…