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
21 documents
This proposed stock screen combines daily price movement, a morning star candlestick setup, and market activity. It first calls for amplitude above 1, then identifies a morning star pattern and sorts qualifying shares by perceived popularity. The…
This Backtrader example combines two entry and exit signals on a selected data feed: a short and long simple moving average crossover, and a MACD line crossing its signal line. Each bullish crossover adds half of the strategy's target stake, while bearish…
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 Backtrader example demonstrates how to monitor portfolio drawdown while running a simple moving-average crossover strategy. It calculates a 15-period simple moving average and uses a crossover between closing price and the average to generate buy and…
This code defines two pivot-point indicators that calculate a central pivot, two support levels, and two resistance levels from high, low, and close prices. The first uses the previous bar’s prices, a common way to derive levels for the next period without…
This example shows how to compare two long-only signal strategies in Backtrader. One strategy enters based on a crossover between 10-period and 30-period simple moving averages; the other uses a crossover between the closing price and a 10-period simple…
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…
This sample demonstrates how to configure a Backtrader run with a trading calendar and resampled market data. It defines a custom NYSE calendar for 2016, loads Yahoo Finance data either from a feed or a local CSV file, and resamples the input series to…
This script implements a long-only buy-the-dip strategy for a single price series. It measures declines using one of several definitions: close versus prior close, close versus open, close versus high, or low versus high. When the selected measure crosses a…
This Backtrader example shows how to include credit interest in a simple moving average crossover strategy. It computes fast and slow averages, uses their crossover as a signal, and lets the user choose long-short, long-only, or short-only trading. A fixed…
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…
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 example defines a long-only stock strategy that enters when the MACD line crosses above its signal line while a simple moving average is declining relative to its value over a lookback period. It initializes a protective stop several ATR units below the…
This Backtrader example attaches two price data series to one strategy. It calculates a simple moving average on the second series and uses crossovers of that series’ close against its average to create long and exit signals. The sample then submits orders…
This code example implements a long-only moving average crossover strategy in Backtrader. It calculates two simple moving averages, with default lookback periods of 10 and 20 bars, and adds a long signal when the shorter average crosses the longer one. A…
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…
This Backtrader example defines a long signal from a crossover between a shorter and a longer simple moving average, then attaches the Calmar analyzer to the run. It loads price data from a Yahoo Finance CSV feed, permits optional date bounds, and exposes…
This sample strategy initializes two Parabolic SAR indicators on a single price series: TA-Lib's SAR, calculated from the high and low data, and Backtrader's built-in PSAR. It loads historical market data from a CSV feed with optional start and end dates,…