Guide
Backtest a Moving-Average Crossover on Indian Stocks
A moving-average crossover backtest buys when a faster average crosses above a slower one and exits when it crosses below. In TradingLead, encode those rules in Python or a supported Pine strategy, then test a fixed symbol, interval and history range. A 20/50 crossover is an educational example, not a proven profitable setting.
Write the hypothesis before running it
Use the same stock, feed and interval for every comparison. For example: on daily candles, buy one unit when SMA20 crosses above SMA50, and close it on the reverse crossing. Do not add a rule later just to remove an inconvenient losing trade.
Create the crossover strategy
Open Indicator Studio, choose TLScript-Pine and enter:
//@version=6
strategy("SMA crossover research", overlay=true, initial_capital=100000, default_qty_value=1)
fastLength = input.int(20, "Fast length", minval=2)
slowLength = input.int(50, "Slow length", minval=3)
fast = ta.sma(close, fastLength)
slow = ta.sma(close, slowLength)
plot(fast, "Fast SMA")
plot(slow, "Slow SMA")
enterLong = ta.crossover(fast, slow)
exitLong = ta.crossunder(fast, slow)
if enterLong
strategy.entry("Long", strategy.long)
if exitLong
strategy.close("Long")
Keep the fast length below the slow length. Compile and use Apply & backtest, following Pine strategy setup. Inspect the execution settings instead of assuming that an entry happens at the signal's displayed close.
Set costs and enough history
Review capital, quantity, commission and slippage. Pine's execution settings preserve declaration defaults when not overridden; slippage is in ticks. Record the assumptions with the result.
The first 50 candles are needed to form the slower average; warmup is not evidence of performance. TradingLead accepts a history range but does not promise unlimited history: source coverage and the 20,000-candle cap still apply.
Diagnose the expected failure mode
Crossover systems often lose repeatedly in sideways markets because a lagging signal follows moves that reverse. Inspect those losses, not just the large trending winner. Compare drawdown, costs and the number of completed trades.
If you explore neighbouring lengths, predeclare a small grid and use holdout or walk-forward testing. A different pair winning every period may indicate instability rather than an opportunity to keep retuning.
Steps
- Fix the symbol, feed, timeframe and evaluation period.
- Enter the 20/50 SMA strategy in TLScript-Pine Studio and compile.
- Review quantity, capital, commission and slippage, then use Apply & backtest.
- Inspect individual trades and drawdown after the run completes.
- Repeat the unchanged test on later data and record any parameter changes.
Limitations
- The example has no stop, profit target, intraday square-off or portfolio allocation logic.
- Pine compatibility is a documented subset and results are not promised to match TradingView.
- Backtests use bounded historical candles, not an order-book liquidity model or live broker orders.
Frequently asked questions
- Is above the moving average the same as crossing above it?
- No. Above is a state that can persist for many candles; a crossover is an event where the faster series moves from at or below to above the slower series.
- Should I use SMA or EMA for a crossover?
- Either defines a valid research hypothesis. EMA puts more weight on recent prices. Pick one before testing rather than switching after seeing which produced the nicest past result.
- Does this example automatically exit before the NSE close?
- No. The example only exits on a bearish crossover and can hold overnight. Intraday square-off rules require additional strategy logic.
Related
Last updated: