Skip to content

Indicators

Moving Average Crossover: SMA, EMA and Exact Alert Rules

A moving average crossover occurs when one moving average changes sides relative to another. An upward crossover means the shorter average is above the longer average now and was at or below it on the previous bar. A downward crossover reverses those comparisons. The averaging method, price source, periods, timeframe and evaluation time define the event; a crossover alone does not define an entry, exit or position size.

Illustrative moving average crossover cover with two smoothed curves changing their relative order.
A crossover depends on the relationship between two series, now and on the previous bar.

Moving average crossovers at a glance

Scroll horizontally to read every column.

Term Definition
SMA Simple moving average: equal weight for each price in a fixed window.
EMA Exponential moving average: a recursive average with a larger weight on recent prices.
WMA Linearly weighted moving average: weights increase from the oldest to the newest price.
Crossover A change in the ordering of two series, measured using current and previous values.
Whipsaw A crossing followed by an opposite crossing, potentially producing repeated changes of direction.

MetaQuotes documents simple, exponential and linear weighted averages, alongside a smoothed method, in its Moving Average help.[1] The indicator rules library compares them with momentum and volatility measures. For example, RSI compares smoothed price changes, while Bollinger Bands add a dispersion measure around an average.

What is the difference between SMA, EMA and WMA?

Let P be the chosen price source and n the period. All formulas below assume a contiguous series of valid observations. A period counts bars, so a five-period average on an hourly chart uses different observations from one on a daily chart.

SMA = sum of the latest n prices ÷ n

Every observation has weight 1/n. Once a new bar arrives, the oldest observation leaves the window. The current average can therefore change because of the outgoing price as well as the incoming one.

EMA today = α × price today + (1 − α) × EMA yesterday; α = 2 ÷ (n + 1)

This page uses the EMA weighting documented by Pine Script v6's ta.ema.[2] The previous EMA carries earlier history forward, with declining weights. Its starting value and available history matter when comparing implementations; the recurrence alone does not specify that starting state.

WMA = (1 × oldest price + 2 × next price + … + n × newest price) ÷ (1 + 2 + … + n)

The denominator is n × (n + 1) ÷ 2. Pine Script's ta.wma and MetaTrader 5's linear weighted method give recent prices larger linear weights.[1][2] Unlike an EMA, this WMA uses a fixed window.

All three averages depend on present and past data. Smoothing changes responsiveness but cannot establish future direction. For the same n greater than one, an EMA's newest observation receives 2/(n + 1), while an SMA's receives 1/n. The larger EMA weight does not mean every EMA crossover must occur before the corresponding SMA crossover.

Worked example: identify a three-bar and five-bar SMA crossover

Illustrative prices and periods, not a recommendation. Six consecutive closes are 110, 105, 100, 95, 105 and 115. Call the three-bar SMA “short” and the five-bar SMA “long”. Neither label describes a trade direction.

Observation Short SMA, 3 bars Long SMA, 5 bars
After close 5, at 105 (100 + 95 + 105) ÷ 3 = 100 (110 + 105 + 100 + 95 + 105) ÷ 5 = 103
After close 6, at 115 (95 + 105 + 115) ÷ 3 = 105 (105 + 100 + 95 + 105 + 115) ÷ 5 = 104

Previously, 100 ≤ 103. Now, 105 > 104. Both comparisons pass, so an upward crossover exists at the sixth close. The calculation identifies an event; it provides no trade result or fill price.

Illustrative moving average crossover: the previous short SMA of 100 is below the long SMA of 103; the current short SMA of 105 is above the long SMA of 104.
Illustrative three-bar and five-bar SMAs. The crossing compares each average with the other average on the same bar, then checks both bars.

A rising SMA does not require a rising latest close. If the next close is 100, the three-bar SMA becomes (105 + 115 + 100) ÷ 3, approximately 106.67. It rises from 105 even though the latest close falls from 115 to 100. The window dropped 95 and added 100.

Change in an n-bar SMA = (incoming price − outgoing price) ÷ n

Here the change is (100 − 95) ÷ 3, approximately 1.67. Defining “the average slopes upward” as “the last price rose” would silently change the rule.

What does ta.crossover actually test?

Pine Script v6's ta.crossover(a, b) tests whether a is strictly greater than b on the current bar and was less than or equal to b on the previous bar.[2] For moving averages, both series change over time:

Upward cross = short now > long now AND short previously ≤ long previously

Downward cross = short now < long now AND short previously ≥ long previously

  • Equality now: the averages touching on the current bar is not yet a strict crossover.
  • Equality previously: a current move from equal to strictly above satisfies the upward condition.
  • Already above: remaining above on a later bar is an above-state, not another upward crossover.
  • Same-bar comparisons: compare yesterday's short average with yesterday's long average, not today's long average.

TradingView's strategy documentation includes a two-SMA crossover example.[6] The crossover and crossunder reference covers these Boolean boundaries separately from trade instructions.

What is a golden cross, and does it define a strategy?

A golden cross commonly means a 50-day moving average crossing above a 200-day moving average. TradingView's Moving Averages help uses 50-day and 200-day SMAs as its example and calls the bullish crossover a Golden Cross.[3] The name does not fix every implementation detail: specify the averaging method, price source and confirmation time.

Using 50 and 200 on an hourly chart compares hourly observations. That does not reproduce two daily averages. A daily crossing observed intraday also raises a separate question: whether the daily bar has closed. A label cannot replace these data and timing definitions.

An “EMA crossover strategy” likewise needs more than two lengths. Entry direction, exits, sizing, existing positions, repeated signals and conflicts require separate rules. Choosing familiar periods supplies a reproducible input only when the rest of the specification is explicit.

How can a crossover rule be specified for a bar-close alert?

Illustrative rule specification, not a trading recommendation. The following produces a notification event, with no broker-order message attached.

Field Definition for this illustration
Data Closing prices on standard hourly candles; record the full TradingView feed and symbol.
Calculations Three-bar and five-bar SMAs, with enough valid bars to compare both averages on two consecutive bars.
Event Short SMA crosses above long SMA under the two-bar formula above.
Evaluation The closing update of the hourly chart bar.
Action Record one notification for the qualifying closed bar. An above-state on the next bar creates no new event.

As of 25 September 2026, Pine Script v6 provides ta.sma, ta.ema and ta.crossover for these calculations.[2] TradingView's repainting documentation demonstrates using barstate.isconfirmed to require the closing update when evaluating a crossing.[4]

A crossover seen during an unfinished bar can disappear before the close. For an indicator using alertcondition(), configure the running alert's frequency accordingly. For alert(), the documented alert.freq_once_per_bar_close requires the call to execute on the closing update.[5] Neither approach retroactively cancels a message already sent earlier.

TradingView alerts retain the script and inputs saved at creation, along with the symbol and timeframe. Recreate the affected alert after changing that context.[5] Confirmation on an hourly chart does not by itself confirm a separately requested daily value.

Where do TradingView, PineConnector and MT5 fit?

The indicator evaluates the chosen series in TradingView. A strategy can also simulate orders through TradingView's broker emulator; those simulated positions do not establish an MT5 account position.[6] MetaTrader 5 separately offers its Moving Average indicator.[1]

To connect a defined event, the user supplies an action using PineConnector Syntax: the intended command, broker symbol, volume and any stop or target parameters.[7] The crossover does not supply those decisions.

  1. Condition and alert: establish when the comparison becomes true, then whether the configured TradingView alert actually triggers.
  2. Webhook delivery: follow the message to its destination.
  3. PineConnector processing: inspect the matching Portal → Bridge record and intended account.
  4. EA order request: distinguish submission from the broker's response.
  5. Broker acceptance, deal and position: verify each outcome in the intended broker account.

PineConnector's demo verification procedure separates processing evidence from the actual trade.[8] MetaQuotes distinguishes an order from its deals and resulting position.[9] A crossover-only exit in a strategy is also separate from a stop held at the broker.

TradingView and MetaTrader can use different feeds and timestamps.[10] A broker chart's moving averages need not match the ones that triggered the TradingView event, even when the displayed periods match.

Why do crossovers whipsaw, and what can a backtest miss?

When the difference between the two averages repeatedly changes sign, a crossing rule produces alternating events. Each event can be correctly calculated while the resulting sequence is unsuitable for the user's intended strategy. Calling the events “false signals” does not identify a calculation error.

Adding a minimum separation, another indicator or a waiting period changes the definition. Such a filter also excludes some crossings and changes the event time. Specify those consequences before comparing versions; no filter follows automatically from the crossover formula.

  • Lag: averages summarise observed prices. A cross describes their current relationship, not an unseen future trend.
  • Costs: more order requests can create more exposure to spread, commission and slippage. A chart event count is not a net result.
  • Overfitting: trying many lengths and filters on one history can tailor the rule to that sample. TradingView describes testing a frozen configuration on separate data.[6]
  • Repainting: closing-bar confirmation addresses changing chart-bar inputs, not every source of revised data or higher-timeframe differences.[4]

Frequently asked questions

What is a moving average crossover?

A moving average crossover is a change in the ordering of two moving averages. An upward crossover requires the shorter average to be above the longer average now and at or below it on the previous bar. State the averaging method, price source, periods, timeframe and evaluation time to make the event reproducible.

What is the difference between SMA and EMA?

An SMA gives equal weight to the latest n prices and drops the oldest when a new observation arrives. An EMA recursively combines today's price with the previous EMA. Pine Script's EMA uses a current-price weight of 2/(n + 1). Both smooth observed prices; neither averaging method establishes which crossover rule will suit a particular strategy.

What is a golden cross?

A golden cross commonly refers to a 50-day moving average crossing above a 200-day moving average. The name does not settle the averaging method or whether the daily bar must close before the event is accepted. Using the same period numbers on an hourly chart does not recreate those daily averages.

Does ta.crossover stay true while one average is above another?

No. Pine Script's ta.crossover(a, b) requires a to be above b now and at or below b on the previous bar. If a remains above b on the next bar, the above-state remains true but a new upward crossover does not occur. Alert frequency and broker-position limits are separate rules.

Reviewed 25 September 2026. Facts were checked against the linked sources on that date. Nothing in this article was tested on a trading account and no code was compiled.

Related reading

Sources

  1. MetaQuotes – Moving Average, accessed 25 September 2026.
  2. TradingView – Pine Script v6 reference: ta.sma, ta.ema, ta.wma and ta.crossover, accessed 25 September 2026.
  3. TradingView – Moving Averages, accessed 26 September 2026.
  4. TradingView – Repainting, accessed 25 September 2026.
  5. TradingView – Alerts, accessed 25 September 2026.
  6. TradingView – Strategies, accessed 25 September 2026.
  7. PineConnector – PineConnector Syntax, accessed 25 September 2026.
  8. PineConnector – Test your setup, accessed 25 September 2026.
  9. MetaQuotes – Basic principles: orders, deals and positions, accessed 25 September 2026.
  10. PineConnector – Frequently asked questions: price differences, accessed 25 September 2026.

PineConnector executes the instructions you send it. It does not select trades, manage money, or hold funds. Trading carries risk, and past performance of any strategy does not indicate future results.


Leave a comment

Back To PiCo Blog

Ready when your strategy is

You bring the strategy.We bring the infrastructure.

Connect TradingView to MetaTrader, choose where MT5 runs and put the full PineConnector workflow through its paces from your first month.

Strategy and trading decisions remain yours. The MT5 environment can be ours.

PineConnector Edge

Run the full PineConnector workflow.

$59/mo at launch

Core plan · 1 connection · 1 hosted MT5 environment

Try Core for $7

7 days of Core for $7, then $59/month at the launch price unless you cancel.