Gold EMA Crossover for TradingView
Three moving averages (21, 50, 200) with a B or S marker when their stacked order changes.
It reduces three averages to one remembered state and marks only the bar on which that state changes, so repeated crossings while the lines are tangled do not produce markers.

Gold EMA Crossover draws three moving averages on the price chart: fast 21 (blue), medium 50 (red) and slow 200 (green). All three are exponential (EMA) by default, with a simple (SMA) option, and are calculated from the close unless you choose another source. It was designed on gold (XAUUSD) on the 5-minute chart and works on any symbol and timeframe.
A green B marker below the bar shows that the averages have just become stacked fast above medium above slow, after being in the bearish state. A red S marker above the bar shows that they have just entered the bearish state after being stacked bullishly. By default the bearish state is the slow average above both the fast and the medium, in either order; an option switches it to the strict mirror, fast below medium below slow. Any other arrangement is mixed and leaves the state unchanged, so markers always alternate B, S, B, S, and lines can cross several times without a marker.
Markers are set on bar close by default. They describe how the averages are lined up. They are not trade recommendations and not financial advice.
What is in the download
- Gold-EMA-Crossover.pine: the full Pine Script v6 source code, to paste into the Pine Editor in your own TradingView account. Nothing is published on TradingView
- Buyer guide (PDF): installing it, the bullish, bearish and mixed states, every input with its default, alert set-up and how to update to a new version
- Three alert conditions: B marker: bullish stack formed, S marker: bearish state formed, Any marker: stack state changed
What it does
Three averages, EMA or SMA
Fast, medium and slow lengths of 21, 50 and 200 by default, all of the same type, on the close or another source you pick. Colours, line width (1 to 4) and whether the lines show are inputs.
Markers on stack changes
B when the averages become stacked fast above medium above slow after a bearish state; S when they enter the bearish state after a bullish stack. The first state on the chart gets no marker, because there is no earlier state to change from.
A choice of bearish rule
Default: the slow average above both shorter ones, which tends to register a downturn earlier. Symmetric option: fast below medium below slow, so S markers tend to come later.
Stack state in the Data Window
Each bar shows a Stack state value in TradingView's Data Window: 1 bullish, -1 bearish, 0 not yet known.
Three alert conditions
B marker, S marker or any marker, with the symbol, timeframe and closing price in the message. Set alerts to Once Per Bar Close so they match the markers on the chart. Alerts need a paid TradingView plan.
When you would use it
Reviewing a gold session
You scroll back over a few days of 5-minute gold bars. The B and S markers show where the three averages changed order, without tracing each crossing by eye.
Tangled lines in a sideways market
The fast and medium lines cross several times while price moves sideways. No marker appears until the three averages complete a change of state.
An alert on the next change
On a paid TradingView plan, you create the 'Any marker: stack state changed' alert on your gold chart set to Once Per Bar Close, and look at the chart yourself when it fires.
Who it is for
- Gold chart users who read moving averages. People who follow gold on TradingView and judge direction from a fast, a medium and a slow average.
- People who want the changes marked. Chart users who want each change in the order of the averages labelled, and decide for themselves what to do with it.
Probably not for you if
- Anyone wanting a gold trading system: it does not place trades, set stops or targets, or size positions, and it has not been tested for profitability
- Anyone wanting extra filters: there is no session, volatility, trend-strength or higher-timeframe filter
- People who do not use TradingView: the MetaTrader 5 Indicator Pack includes a MetaTrader 5 version
What you need
- Your own TradingView account. You download the .pine file from Gumroad, paste it into your Pine Editor, save it and add it to a chart; the guide gives each step
- Any TradingView plan, including the free Basic plan within its limits (2 indicators per chart, up to 5,000 bars of history). Alerts need a paid plan (Essential or higher); check tradingview.com/pricing for current limits
- Enough loaded history for the 200-bar average before the first state forms
- Licensed for one buyer's personal use on their own TradingView account(s). Do not share, resell or publish the code, including on TradingView
Questions
Are the B and S markers buy and sell signals?
They mark the bar where the order of the averages changed. They are not recommendations, and any trading decision is yours.
Why is there not a marker at every crossover?
A marker needs a full change of state, from bullish stack to bearish state or back. Lines can cross several times while the averages are mixed without completing that change.
Can a marker appear and then disappear?
With 'Signals on bar close only' on (the default), a marker and its alert appear only once the bar has closed, and do not change afterwards. The last point of each average moves with the live price until the bar closes. The indicator uses no other timeframes or symbols.
Does it work on symbols other than gold?
Yes. It was designed and shown on gold on the 5-minute chart, and the lengths, average type and source can be changed for other symbols and timeframes. On intraday gold charts the averages run through the daily break and the weekend gap, and a large gap can place a marker on the first bar after it.
How do I get it onto my chart, and do alerts need a paid plan?
After you buy, you download the .pine file from Gumroad. In your own TradingView account you open the Pine Editor, create a new indicator, paste the code, save it and click Add to chart; the guide gives each step. Nothing is published on TradingView. It works on the free Basic plan within its limits, but alerts need a paid plan (Essential or higher), and no performance figures are claimed.
Also in Trading
RSI Volume Strength for TradingView
Price RSI and a volume-flow RSI in one 0-100 pane, with an agreement ribbon and a volume heat strip.
ADX & DI Notify for TradingView
Fast DI+, DI- and ADX lines with lead shading, extreme-reading bars and seven alert conditions.
All-in-One Signal for TradingView
Markers where five intraday conditions agree, with an opening range, previous-day levels and a status table.
Last reviewed: