Backtest an SMA crossover strategy
Compare the bundled Python and Pine 50/200 moving-average crossover examples with the same market, dates and costs.
Before you start
The bundled moving-average crossover uses fast 50 and slow 200 inputs. A crossing is an event, not simply the state of one average being above another. Both averages need enough prior candles. Read the no-trades guide if no crossing occurs in the chosen window.
Follow the workflow
- Open the Pine example and identify the two lengths and supported moving-average calls.
- Find where the fast average crosses above the slow one and where it crosses below. Inspect the next fill rather than assuming the crossing close was used.
- Record market, timeframe, dates, capital, sizing and fees.
- Compare with the Python example using identical assumptions. Then vary one length and record the new result.
Worked example
This is the current bundled Pine code, shared with the workbench. Its default lengths are 50/200; examples using other lengths answer a different test.
//@version=6
strategy("SMA 50/200 cross", initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.1)
fastLength = input.int(50, "Fast SMA", minval=2)
slowLength = input.int(200, "Slow SMA", minval=2)
fast = ta.sma(close, fastLength)
slow = ta.sma(close, slowLength)
if ta.crossover(fast, slow)
strategy.entry("Long", strategy.long)
if ta.crossunder(fast, slow)
strategy.close("Long")
Limits and interpretation
Averages describe past prices and can repeatedly cross in a range. Transaction costs and an available warm-up window can change the apparent result. The example does not optimize itself or predict future returns. Check fill rules and costs before comparing results with another platform.