EMA crossover in Pine Script v6: which lengths to test
Test EMA crossover lengths as pairs that match your chart timeframe, then check nearby pairs and held-out data.
On this page
- Which EMA lengths are worth testing?
- What does a length mean on your chart?
- How should a Pine strategy expose the lengths?
- Where should the fast and slow ranges start?
- Should the fast and slow ranges overlap?
- How do you compare length pairs by hand?
- What should you keep fixed during the test?
- How can nearby pairs help you read the result?
- What data should stay outside the search?
- When is the length test ready to be repeated?
- Sources
Key takeaways
- Choose fast and slow EMA ranges in chart bars, with a clear reason for each range.
- Test length pairs together, because the crossover depends on the relationship between the averages.
- Keep strategy rules and test conditions fixed while comparing pairs in the same pass.
- Check nearby pairs and held-out data before drawing a conclusion about the test.
Which EMA lengths are worth testing?
Test a range of fast and slow lengths that represents the price movements your strategy is meant to study, rather than looking for a universal pair. Express each length in chart bars, keep the fast length below the slow length, and compare valid pairs across the range.
An exponential moving average, or EMA, is a moving average that gives more weight to recent prices. A crossover occurs when the fast EMA moves from one side of the slow EMA to the other, changing the relationship that the strategy observes.
The fast EMA generally responds more quickly to recent changes, while the slow EMA gives the comparison a longer context for those same changes. Start with a question about the behavior you want to examine, so each input has a clear role before any backtest is run.
For example, you might compare a fast average that responds within several chart bars with a slow average that reflects a longer movement in the same price series. Each length is a starting hypothesis rather than a forecast, and different pairs can produce different crossover timing and simulated trades under the same rules.
The useful question is whether the behavior remains understandable across a reasonable neighborhood of pairs and in data that was held back from the search. A historical test can describe that behavior, but it cannot establish how the pair will behave in an untested period.
What does a length mean on your chart?
An EMA length describes a period in bars, not a fixed amount of clock time, so its meaning depends on the interval selected for the chart. The same length refers to a different span of market history on a shorter chart than it does on a longer chart.
Before choosing candidates, write down the chart interval and the kind of movement the strategy is intended to capture, using terms that make sense for your experiment. You might describe a fast response over several sessions and a slower comparison over a broader stretch of sessions.
Turn those descriptions into whole-bar input values for the chart you are testing, and record a reason for each endpoint before seeing the results. If an endpoint would make the average much faster or slower than the idea calls for, leave it outside the initial pass and record why.
The longer EMA still responds to new prices, and an EMA is not simply an average of only the most recent chart bars. Earlier EMA values also influence the current calculation, so keep enough earlier chart history available for the averages to develop before the period you intend to judge.
Changing the chart interval changes the question being tested, because each bar now represents a different span of time. Treat that as a separate experiment rather than assuming that a length pair carries the same meaning across charts; the timeframe guide explains the wider comparison.
How should a Pine strategy expose the lengths?
Give the fast EMA and the slow EMA separate named inputs, so the experiment can change each length without changing the rest of the strategy. Name the inputs for their roles, and make the valid values positive whole bars that fit your intended test.
TradingView’s inputs documentation, checked on 2026-11-20, says that changing an input causes the script to run again across available data. The same documentation describes integer inputs and their value limits, which can help make the intended range clear to a person using the script.
TradingView’s built-ins documentation, checked on 2026-11-20, lists ta.ema() among its technical indicator functions. Use the same price source for both averages if the purpose is to isolate the effect of their lengths, because a different source adds another change to explain.
The strategy also needs explicit rules for what happens when the averages cross, including the entry direction and the condition that closes or changes a position. A long-only test and a test that changes direction are different experiments, even when their EMA inputs match.
TradingView’s strategy documentation, checked on 2026-11-20, says strategy scripts simulate trades across historical and realtime bars. Make sure the script is a strategy rather than only a visual indicator, then review its rules and plotted averages before comparing the lengths.
Where should the fast and slow ranges start?
Choose the fast range around the shortest movement that is relevant to the idea, and extend it toward a slower response that still fits that role. Choose the slow range around the broader movement that the fast average is meant to be compared with.
Write a short reason for each endpoint before you inspect the report, so the search reflects your question rather than a result seen afterward. The lower fast endpoint might represent a response that is already near the edge of being too sensitive for the idea you are studying.
The upper slow endpoint might represent a history span beyond which the comparison no longer matches the intended holding horizon for the strategy under review. These boundaries need not match a popular convention, but they should be understandable to someone reading the test notes later.
The increment, or step, determines which values between the endpoints are tested during the pass. A wider step gives a coarse view of the range, while a narrower step examines nearby values in more detail and requires more comparisons.
A range can be too narrow to show meaningful differences in crossover behavior, or so broad that it mixes different ideas without a reason to compare them. Record the spacing before looking at the report, and label any later change as a new pass rather than silently replacing the original plan.
Use the parameter range guide for the general process of setting endpoints and steps. For this crossover, the extra decision is how the fast and slow ranges relate to each other, not just how each range looks on its own.
Should the fast and slow ranges overlap?
The candidate ranges can overlap, but each tested pair needs to preserve the roles assigned to the averages in the strategy. If the fast length equals or exceeds the slow length, the labels no longer describe the intended comparison; an equal pair produces identical averages when the source is the same.
Draw a small table before running the test, with candidate fast lengths down one side and candidate slow lengths across the top of the table. Mark the cells where the fast value is lower, so you can see invalid pairs and whether the planned work actually answers your question.
You can choose nonoverlapping ranges, remove invalid pairs from the manual list, or define a slow length in relation to the fast length you select. The last approach changes the question, because it tests a relationship between lengths rather than independently chosen values for each average.
A very small gap between lengths deserves its own note in the test plan, since it represents a different comparison from a wide gap. The averages may stay close together and cross more often in some price conditions, while a wider gap may describe a different response to those conditions.
Neither gap is inherently preferable, and the test should show how your chosen rules behave across the candidates you planned. If the script rejects or skips invalid pairs, check that behavior before comparing results and record skipped pairs separately from pairs that were tested.
That distinction matters when you return to the table later, because an empty cell should not be mistaken for a result that was tested but not recorded. Keep the reasons for exclusions with the range notes, especially if another pass uses a different boundary.
How do you compare length pairs by hand?
Prepare a list of valid pairs and keep the chart, test period, price source and strategy rules unchanged throughout the pass. For each pair, enter the fast and slow values, allow the test to run, and record the pair beside the observations you intend to compare.
Keep a note of the script version and the settings that affect the simulation, including position rules, assumed trading costs and order timing choices. If one of those changes during the pass, label the later work as a different experiment rather than placing all results in the same comparison.
A useful record includes the pair, the test period, the number of simulated trades and a brief description of the behavior you observed on the chart. Note whether crossings cluster in a choppy part of the chart or whether a long interval passes without a new entry, so the summary has context.
TradingTune is an extension that automates optimization of your own TradingView strategy. It can backtest input combinations and rank runs, while you set the input ranges and decide what the test is meant to examine.
Automation does not decide whether the ranges express a sensible hypothesis or whether the comparison answers the question you originally intended to ask. Review the valid pairs and fixed assumptions first, and keep the manual notes that explain why the experiment was constructed in that particular way.
TradingTune for Chrome
Stop tuning your strategy's inputs by hand.
Free tier, no API keys. A free account is required to run. Not affiliated with TradingView, Inc.
What should you keep fixed during the test?
A comparison of lengths is easier to interpret when other choices remain stable throughout the initial pass. Keep the same symbol, chart interval, price source and test period, along with the same entry and exit rules for every pair in the pass.
Trading costs are part of that setup and need to be recorded alongside the strategy rules, rather than added only after a pair has been selected. Commission is a charge associated with a transaction, while slippage is a difference between an expected price and the simulated fill price.
Record the assumptions used for both, because pairs that cross at different rates can be affected differently by those assumptions in the simulated test. Also record how position size is defined and whether the strategy can add to an existing position during the test.
Changing those choices alongside the EMA lengths would leave more than one possible explanation for a difference in the report. Use ordinary price bars for the initial comparison, and note any special chart construction if you later investigate one.
Synthetic chart prices can make a simulation answer a different question from a test based on ordinary market price bars. For more on the cost assumptions and their role in the test, see modeling slippage and commission.
If you change a cost assumption later, repeat the comparison as a separate check and keep the original notes intact. That makes it possible to see which observation depended on the assumption without losing the context of the earlier pass.
How can nearby pairs help you read the result?
After the initial pass, lay out the recorded pairs so that neighboring fast and slow values are easy to compare on the same page. Look for a region where the strategy’s behavior remains broadly similar, rather than focusing only on the pair that ranks highest under one measure.
A sharp isolated result can depend heavily on the particular history and spacing that were tested during the experiment. Nearby pairs may produce very different trade sequences, which is useful information even when the report makes the isolated pair look attractive.
Examine the simulated trades behind any surprising result, and check whether the difference follows from a small change in crossover timing. A small change in length can move a crossover to a different bar or remove it altogether, altering the sequence of trades the strategy examines.
When a few trades account for much of the difference between pairs, write that down instead of treating the ranking as a complete explanation. An isolated historical result does not establish that the pair will behave similarly on new data, even if the recorded result stands out in the report.
The number of trades matters as context, not as a certificate of quality, because a pair with little activity offers less material for review. The minimum trade count guide discusses that limitation and why it deserves attention when interpreting a backtest.
The guide to parameter sensitivity covers the wider idea of comparing nearby settings. For an EMA crossover, pay attention to both directions of change: a small move in the fast length and a small move in the slow length.
What data should stay outside the search?
Set aside a later portion of the available history before inspecting the candidate pairs, and record its boundaries in the test plan. This is held-out data: a period reserved for checking a choice made using earlier data, rather than for choosing among every possible candidate pair.
Use the earlier period to compare the planned ranges and record the selection rule before opening the held-out report. For example, the rule might require a pair to sit within a region of similar behavior and to have enough simulated activity for a useful review.
That rule is a research choice rather than a promise about future behavior, so explain why it fits the particular question you are testing. Run the chosen pair on the held-out period without adjusting the lengths to improve that report.
Compare the character of the trades and the effect of the recorded cost assumptions with what you observed in the earlier period. A difference is a finding to investigate and record, not a reason to conceal the check or quietly change the selection rule.
If the held-out result leads you to revise the ranges or rules, that period has become part of the research process. Record the revision and reserve different data for a later independent check if suitable data is available, since repeated selection weakens the role of the original holdout.
The in-sample and out-of-sample guide explains this split in more detail and describes the distinction between selection and checking. A held-out check cannot prove how a strategy will behave in a future market, even when the earlier selection process was carefully planned and recorded.
When is the length test ready to be repeated?
A repeatable test has a written question, valid fast and slow ranges, fixed rules and a record of what was actually tested. It also identifies the data used for selection and any data reserved for a later check, so another pass has a clear starting point.
Before repeating the work on another symbol or interval, state what will remain unchanged and what will change in the new comparison. Keeping the pair fixed asks whether the original choice behaves differently in a new setting, while searching for a new pair asks a separate question.
A new search needs its own selection and held-out process, rather than treating the earlier holdout as untouched evidence for the newly revised experiment. Record the chart history and assumptions used in each pass, so differences in the setup remain visible when you review the notes.
Save the cases that did not fit the idea as well as the cases that looked coherent under the rules you wrote down before testing. An invalid pair, a period with little activity or a result that changes sharply with a neighboring value can explain the limits of the experiment.
Removing those observations makes the final notes less useful, because a later reader cannot tell which alternatives were considered or excluded. Revisit the script if the plotted averages, crossover timing or simulated trades do not match the rules you wrote down before testing.
Fixing a rule changes the strategy, so repeat the relevant comparison under a new version rather than combining the old and the new records. The outcome is a documented test of a particular crossover idea, not a universal length recommendation or a statement about an untested market period.
Sources
TradingTune for Chrome
Stop tuning your strategy's inputs by hand.
TradingTune runs systematic parameter searches on your own TradingView strategy, right inside the chart page. You keep the strategy, you read the results.
Free tier, no API keys. A free account is required to run. Not affiliated with TradingView, Inc.
TradingTune for Chrome
Stop tuning your strategy's inputs by hand.
TradingTune runs systematic parameter searches on your own TradingView strategy, right inside the chart page. You keep the strategy, you read the results.
Free tier, no API keys. A free account is required to run. Not affiliated with TradingView, Inc.
TradingTune for Chrome
Stop tuning your strategy's inputs by hand.
TradingTune runs systematic parameter searches on your own TradingView strategy, right inside the chart page. You keep the strategy, you read the results.
Free tier, no API keys. A free account is required to run. Not affiliated with TradingView, Inc.
TradingTune for Chrome
Stop tuning your strategy's inputs by hand.
TradingTune runs systematic parameter searches on your own TradingView strategy, right inside the chart page. You keep the strategy, you read the results.
Free tier, no API keys. A free account is required to run. Not affiliated with TradingView, Inc.
TradingTune for Chrome
Stop tuning your strategy's inputs by hand.
TradingTune runs systematic parameter searches on your own TradingView strategy, right inside the chart page. You keep the strategy, you read the results.
Free tier, no API keys. A free account is required to run. Not affiliated with TradingView, Inc.