Moving average crossover strategy
Loading the chart…
About this strategy
Goes long when a fast EMA crosses above a slow EMA and exits when it crosses back below. The classic trend-following test.
The moving average crossover is the simplest trend-following strategy there is. It tracks two exponential moving averages of the close: a fast one (9 bars by default) and a slow one (21 bars). When the fast average crosses above the slow one, the recent trend has turned up and the strategy buys. When it crosses back below, it closes the trade.
It does well in long, steady trends and loses small amounts in sideways markets, where the averages cross back and forth. That trade-off is the main thing to look at in the backtest: count the losing trades in flat periods and compare them with the gains from the trending ones.
Shorter lengths react faster but trade more and get whipsawed more. Longer lengths trade less and give back more of each move before they exit. On daily Nifty or stock charts many traders start with 20 and 50; on 15 minute intraday charts, 9 and 21 is a common starting point.
How to use it
- Click Use on chart to open it in the strategy tester on your symbol.
- Change the fast and slow lengths in its settings and compare the results.
- Check the list of trades for losing runs in sideways markets before trusting it.
Settings and what it draws
Settings
| Input | Type | Default |
|---|---|---|
| Input 1 | Whole number | 9 |
| Input 2 | Whole number | 21 |
Draws on the price chart
- Fast EMALine
- Slow EMALine
Source code
strategy("MA crossover", overlay=true, initial_capital=100000, default_qty_type=strategy.percent_of_equity, default_qty_value=10, pyramiding=1)
// Inputs
fastLength = input.int(9, "Fast length", minval=1, maxval=200)
slowLength = input.int(21, "Slow length", minval=1, maxval=400)
// Calculations
ema = ta.ema(close, fastLength)
ema2 = ta.ema(close, slowLength)
crossUp = ta.crossover(ema, ema2)
crossDown = ta.crossunder(ema, ema2)
// Plots and signals
plot(ema, "Fast EMA", color=#2962FF, linewidth=2)
plot(ema2, "Slow EMA", color=#FF9800, linewidth=2)
// Orders
if crossUp
strategy.entry("Long", strategy.long)
if crossDown
strategy.close("Long")
// @builder-graph v1 5e380364
// Visual builder data. Edit the code freely; it will then open as plain code.
// eyJuIjoiTUEgY3Jvc3NvdmVyIHN0cmF0ZWd5Iiwibm9kZXMiOlt7ImlkIjoibjEiLCJ0IjoiZGVjbGFyZS5zdHJhdGVneSIsIngi
// OjQwLCJ5Ijo0MCwicCI6eyJ0aXRsZ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sImNvbG9yIjoiI0ZGOTgwMCJ9
// fSx7ImlkIjoibjEwIiwidCI6InN0cmF0LmxvbmciLCJ4Ijo4MzIsInkiOjQwLCJwIjp7ImlkIjoiTG9uZyIsInF0eSI6MH19LHsi
// aWQiOiJuMTEiLCJ0Ijoic3RyYXQuY2xvc2UiLCJ4Ijo4MzIsInkiOjE4NCwicCI6eyJpZCI6IkxvbmcifX1dLCJlZGdlcyI6W1si
// ZTEiLCJuMiIsIm91dCIsIm40IiwibGVuZ3RoIl0sWyJlMiIsIm4zIiwib3V0IiwibjUiLCJsZW5ndGgiXSxbImUzIiwibjQiLCJv
// dXQiLCJuNiIsImEiXSxbImU0IiwibjUiLCJvdXQiLCJuNiIsImIiXSxbImU1IiwibjQiLCJvdXQiLCJuNyIsImEiXSxbImU2Iiwi
// bjUiLCJvdXQiLCJuNyIsImIiXSxbImU3IiwibjQiLCJvdXQiLCJuOCIsInZhbHVlIl0sWyJlOCIsIm41Iiwib3V0IiwibjkiLCJ2
// YWx1ZSJdLFsiZTkiLCJuNiIsIm91dCIsIm4xMCIsIndoZW4iXSxbImUxMCIsIm43Iiwib3V0IiwibjExIiwid2hlbiJdXX0=
Comments (0)
No comments yet. Be the first to say how it worked for you.
More built-in tools
See all-
Majority Rule
Majority Rule shows the percentage of the last bars that closed higher than the bar before. Majority Rule is a simple measure of how often price has gone up over a recent window, as a percentage of…
Other 101 -
Historical Volatility
Shows the annualised standard deviation of log price returns over the last N bars, as a percentage. Historical Volatility measures how much price has actually moved. It takes the logarithm of the…
Volatility 100 -
Double EMA
The double exponential moving average: an EMA built to remove part of the lag of a normal EMA. The Double Exponential Moving Average (DEMA) was introduced by Patrick Mulloy. It takes an EMA and an…
Trend 010