Moving average settings: 5, 20, 60 and how to test which one holds
Moving average settings are three choices: type (SMA or EMA), length in bars and price source, all set in the indicator's Inputs tab on TradingView. Start with 20, but treat 5, 20 and 60 as candidates: the length worth keeping is the one whose neighbours also test well after costs, not the one that looks right on the chart.
By Soren Huang · Published · updated · Editorial policy
Contents
- How do you set a moving average on TradingView, and which length should you start with?
- SMA or EMA: how do you choose?
- Which moving average length is best: 5, 20 or 60?
- Which length pair should a golden cross use?
- How do you test a range of lengths for a stable plateau?
- Frequently asked questions
- References
How do you set a moving average on TradingView, and which length should you start with?
Moving average settings are three choices: type (SMA or EMA), length in bars, and price source. On TradingView you set all three in the indicator's Inputs tab. Start with 20, but treat 5, 20 and 60 as candidates: the length that holds is the one whose neighbours also test well after costs, not the one that looks right on the chart.
A moving average is a line that shows the average price of the last N bars and is recalculated on every new bar. The three settings decide how that line is drawn. The order to set them on TradingView is:
- Pick the type. Open the Indicators list and search for "Moving Average" for a simple average (SMA) or "Moving Average Exponential" for an exponential average (EMA). Each copy you add is one line, so two lengths means two copies.
- Set Length and Source. Click the gear icon on the indicator and open the Inputs tab. Length is the number of bars averaged (20 is a common start). Source is the price used, with close as the default.
- Check it on the bar size you will trade. Length counts bars, not days. A 20 on a daily chart covers about 20 trading days, roughly one month. The same 20 on a 5-minute chart covers 100 minutes.
The lengths traders start with come from the calendar. On a daily chart, 5 bars is about one trading week, 20 is about one month and 60 is about one quarter, which is why traders in Taiwan call them the week line, month line and quarter line. The 50-day and 200-day lines are the classic pair in US stock commentary. None of these numbers came from a test; they came from how we count days. That is a fine place to start and a poor place to stop. The guide to technical analysis tools and indicators that you can test explains why every indicator setting should be treated as a candidate rather than a fixed truth.
SMA or EMA: how do you choose?
The SMA weights every bar equally and the EMA weights recent bars more, so the EMA turns earlier but reacts more to noise; which one suits you is a trade-off between lag and false signals that only a test on your market and bar size can settle. A simple moving average (SMA) is the plain average of the last N closes. An exponential moving average (EMA) is a weighted average in which the newest bar counts most and each older bar counts a little less. Lag is the delay between the price turning and the average turning.
Think of a class grade. The SMA is a teacher who counts every test of the term equally, so one good result late in the term barely moves the grade. The EMA is a teacher who counts the latest tests most, so the grade responds quickly to a recent change, for better or worse. The one formula worth knowing is the SMA:
SMA = (C1 + C2 + … + CN) ÷ N
Here C1 to CN are the closing prices of the last N bars and N is the length you set. In a 20-bar EMA the newest bar carries roughly a tenth of the total weight, and the weights shrink smoothly toward the oldest bars, which is why the EMA hugs price more closely.
| Property | SMA (simple) | EMA (exponential) |
|---|---|---|
| Weighting | Every bar equal | Recent bars heavier |
| Lag at a turn | Longer | Shorter |
| Behaviour in a sideways market | Fewer crosses, each one late | More crosses, many of them false |
| Same length, same chart | Smoother line | Line closer to price |
Neither is right in general. A rule that enters on a cross pays for lag with late entries and pays for speed with whipsaws, and the balance between the two depends on how your market moves at your bar size. Other averages try to shift the balance: the DEMA, which cuts EMA lag with a second smoothing pass, and adaptive lines such as the VIDYA, which changes its own smoothing with volatility. Each adds a choice, and each choice is one more thing to test rather than assume.
Which moving average length is best: 5, 20 or 60?
No length is best in itself: a length that looks right on a chart was chosen with hindsight, and a real test replays the rule with costs, next-bar fills and a trade count, which is what a report shows and a chart cannot. When you scroll a chart with a 20-bar line and see it hugging a trend, your eye picks the three clean entries and skips the five whipsaws in the sideways stretch before it. You then "confirm" the length on the same prices that suggested it.
A test removes the eye from the process. It takes every signal the rule produces, fills each one on the next bar after the signal (because you cannot trade a close you have already seen), charges commission and slippage on every entry and exit, and adds the results up over the whole history. The output is a trade count, a net result after costs, a drawdown and a profit factor. AlphaPrime (Valynox) does exactly this in its backtesting, charging fees and slippage inside every evaluation with next-bar execution, so the number you read for a length is already the net of what it would have cost to trade it.
The report below shows what that output looks like. The run tested ES daily bars from 2019-01-02 to 2025-06-30, long and short, one contract, with $20 commission and $25 slippage per trade. It did not test a moving average: its only entry and exit was a Volume Profile Levels rule, a stand-in built from standard indicators, with no stop, target or filter. Out of 1,024 candidates tried, the search kept only this one, on the strength of its return against its drawdown. Picking the kept one after the fact flatters it, so read the box as an example of what a report contains, not as a result any rule or reader should expect.
Source: AlphaPrime product engine (CPU), run 2026-10-31. ES daily bars, 2019-01-02 to 2025-06-30, long and short, 1 contract, $20 commission and $25 slippage per trade.
| Number | Result |
|---|---|
| Net profit (closed trades) | $137,912.5 |
| Max drawdown | $37,892.5 |
| Trades | 160 |
| Win rate | 51.25% |
| Profit factor | 1.482 |
| Average trade | $861.95 |
| Time in market | 84.23% |
Four lines in that box answer questions a chart cannot. Trades (160) tells you how much evidence the result rests on. Net profit is after the $45 of costs on every trade. Max drawdown ($37,892.5) is the worst fall from a peak, the number that decides whether you could have kept trading. Profit factor (1.482) is gross wins divided by gross losses. Win rate (51.25%) shows that roughly half the trades lost, which is normal for a rule that stays in the market most of the time (84.23%) and which a chart with hand-picked entries never shows.
The research record on moving average rules tells the same story at a larger scale. Brock, Lakonishok and LeBaron (1992) reported that simple moving average rules on nearly a century of Dow Jones index data behaved differently from a buy-and-hold benchmark. Sullivan, Timmermann and White (1999) revisited the same rules with an adjustment for the number of rules that had been tried and found that the best rule's apparent edge did not repeat in the following decade. A length that looked best over one history was, in part, the best of many tries.
Which length pair should a golden cross use?
Common pairs such as 50/200, 20/60 and 5/20 differ mainly in how many signals they give and how late they arrive, so pick the pair by the trade frequency and holding time you can live with, then verify it the same way as a single length. A golden cross is the moment a shorter moving average crosses above a longer one; a death cross is the reverse. The shorter line is the fast line, the longer one the slow line, and the gap between their lengths decides everything about the signal.
| Pair (fast/slow) | Signals per year | Lag at a turn | Typical holding time | Main weakness |
|---|---|---|---|---|
| 5/20 | Many | Short | Days | Whipsaws in sideways markets; costs add up |
| 20/60 | Some | Medium | Weeks | Misses the first part of each move |
| 50/200 | Few | Long | Months | Very few trades, so very little evidence per decade |
Choose the row first, then the exact numbers. If you want to hold for weeks and can accept a few late entries, start in the 20/60 row. If you want many short trades, start in the 5/20 row and remember that each trade pays the full round-trip cost, so a pair that fires often needs a larger average move per trade to stay ahead of commission and slippage. The 50/200 row produces a handful of trades per market per decade, which is too few to judge a result on, so it is better treated as a regime filter than as a complete rule.
A pair has two lengths, so the number of combinations you can try is much larger than for a single line, and the risk of keeping a lucky pair rises with it. This is one reason to compare a crossover with a rule that has only one length to tune, such as the Donchian channel breakout and its settings, which enters on a new N-bar high instead of a cross. Whatever the pair, test it the way the next section describes: every neighbouring pair, same costs, same bar size.
How do you test a range of lengths for a stable plateau?
Run every length in a range with the same costs and settings, plot the result against the length, and keep only a length whose neighbours behave alike; a single spike surrounded by poor results is luck, and the best of many tries is always flattered by selection. A parameter plateau is a stretch of neighbouring settings that all produce similar, acceptable results. Pardo (2008) describes the plateau as the thing to look for in an optimization, because a rule that works at 30, 35 and 40 is far more likely to keep working at 35 than a rule that works only at 15.
The steps:
- Fix everything except the length. Same market, same bar size, same session, same commission and slippage, same next-bar fills. If two runs differ in anything but the length, you cannot read the result.
- Choose the range before you run. For a daily trend rule, 10 to 60 in steps of 5 is a sensible first sweep. Write the range down so you do not widen it after seeing a result you like.
- Run every length and record net result after costs, trade count and max drawdown. The trade count matters: a long length with 12 trades is not comparable with a short length with 300.
- Plot net result against length. Look for a region where the bars are similar in height, not for the tallest bar.
- Pick from the middle of the plateau, then confirm that length on data the sweep never touched.
Hypothetical example
A sweep of a daily crossover rule on one futures market, lengths 10 to 60 in steps of 5, each run after costs. Lengths 30, 35, 40 and 45 return $18,000, $21,000, $20,000 and $17,000 with similar drawdowns. Length 15 returns $34,000, but 10 returns −$6,000 and 20 returns $2,000. The tallest bar is 15. The length to carry forward is 35 or 40, from the middle of the plateau, because a small shift in market behaviour that moves the "right" length by five bars costs little there and costs everything at 15.
Two things make the spike dangerous. First, the result at 15 is the best of eleven tries, and Bailey, Borwein, López de Prado and Zhu (2014) show that the best result of many tries is inflated by the selection itself, even when none of the rules has any edge. Second, the poor neighbours at 10 and 20 tell you the result at 15 depends on the exact timing of a few trades, which is the definition of a fit to noise. The methods for reading a parameter surface in more detail, including two-dimensional surfaces for crossover pairs, are in parameter stability: plateau or lucky spike.
The next thing to do is run this sweep yourself. Take the market and bar size you actually trade, set the commission and slippage you actually pay, sweep 10 to 60 in steps of 5 with the rest of the rule frozen, and plot the bars. Before you look at the plot, write down the width of plateau you will require, for example four neighbouring lengths within a third of each other. Then keep the recent six to twelve months of your data out of the sweep and run your chosen length on it once.
Frequently asked questions
How many days should a moving average be set to?
There is no single right number. The common lengths 5, 20 and 60 come from the calendar (about a week, a month and a quarter of trading days), not from tests. Treat each as a candidate, sweep the lengths around it with the same costs and bar size, and keep a length whose neighbours give similar results. A length that works only at one exact value is usually a fit to noise.
Does the moving average length on TradingView mean days or bars?
It means bars. On a daily chart a length of 20 covers about 20 trading days. On a 5-minute chart the same 20 covers 100 minutes, and on a 1-hour chart it covers 20 hours of trading. When you change the chart's bar size, the line with the same length now describes a different stretch of time, so a length that looked right on one bar size has to be tested again on another.
How do I add two moving averages on one TradingView chart?
Add the indicator twice. Each copy of "Moving Average" or "Moving Average Exponential" is one line, so open the Indicators list, add it, then add it again and set a different Length in the second copy's Inputs tab. Use the Style tab to give each line a different colour so the fast and slow lines are easy to tell apart.
Is a 200-day moving average better than a 60-day one?
Each one answers a different question. A 200-day line describes the trend over most of a year and turns rarely, so a rule built on it gives very few trades per decade and little evidence to judge. A 60-day line turns several times a year and gives more trades at the cost of more false turns. Neither is better in itself; the useful comparison is a sweep of lengths on your own market with costs included, read for a plateau rather than for the single best length.
References
- Brock, W., Lakonishok, J., & LeBaron, B. (1992). Simple Technical Trading Rules and the Stochastic Properties of Stock Returns. The Journal of Finance, 47(5), 1731–1764.
- Sullivan, R., Timmermann, A., & White, H. (1999). Data-Snooping, Technical Trading Rule Performance, and the Bootstrap. The Journal of Finance, 54(5), 1647–1691.
- Bailey, D. H., Borwein, J., López de Prado, M., & Zhu, Q. J. (2014). Pseudo-Mathematics and Financial Charlatanism: The Effects of Backtest Overfitting on Out-of-Sample Performance. Notices of the American Mathematical Society, 61(5), 458–471.
- Pardo, R. (2008). The Evaluation and Optimization of Trading Strategies (2nd ed.). John Wiley & Sons.

