Some rules in the strategy library, such as the moving average crossover, start at a single moment: the candle where one line crosses another. Today you'll learn to catch that moment in code. By the end of this lesson, you'll know the difference between above and crossed, and how ta dot crossover works. You'll also make sure a cross only counts once its candle has closed.
1 of 8
What you'll learn
- Tell a state from an event. Above lasts many candles. A cross happens on one candle.
- Find crosses with crossover. It's true when one line moves above another.
- Expect the cross after the turn. Price turns first, and the lines cross after.
- Count crosses on closed candles. Add barstate.isconfirmed so a cross can't vanish.
- Cross a fixed level. The second line can be a set number, like 50 on RSI.
Exercise
- Add the EMA cross script to a 5-minute MES1! chart.
- Count the triangles in one morning.
- Then add a third plotshape that marks fast > slow on every candle (use shape.circle), and compare how many marks you get.
Quick check
1. Fast has been above slow for 6 candles. On how many of them is ta.crossover(fast, slow) true?
2. What does adding "and barstate.isconfirmed" do?
3. Which is an event rather than a state?
0 of 3 answered.
Finished this lesson?
Tick it off and keep your place
Read the full notes
ta.crossover(a, b) is true only on the candle where a moves above b after being at or below it on the candle before; adding barstate.isconfirmed counts a cross only once its candle has closed.
- fast > slow is a state, true on every One bar on a price chart. It shows where price opened and closed, and the highest and lowest it reached, in one stretch of time.More in the glossary while it lasts. A cross is an event, true on one candle. Signals are events.
- ta.crossover(fast, slow) is true when fast is above slow on this candle and was at or below it on the candle before (TradingView's definition). The same by hand: fast > slow and fast[1] <= slow[1]. ta.crossunder is the mirror; ta.cross catches either.
- Example: a 9 and 21 A moving average that gives the newest closes more weight, so it reacts sooner than a simple one of the same length.More in the glossary The moment a shorter moving average crosses a longer one: above it for a cross up, below it for a cross down.More in the glossary, as in Common crossover rules. The cross always comes after the turn: see Why indicators lag.
- On the live candle, a cross can appear and vanish. Adding and barstate.isconfirmed counts it only on the candle's final update; old candles are always confirmed. See Repainting indicators and hindsight.
- The second line can be a fixed level: ta.crossover(close, 5020) or ta.crossunder(rsi, 50). These levels are made up.
Worked example
Made-up numbersAbove versus crossed
Made-up values · fast and slow averages over four candles
- Candle 1: fast 5,010.50 < slow 5,011.00
- Candle 2: fast 5,011.25 > slow 5,011.00: crossover true
- Candles 3 and 4: fast still above: fast > slow true, crossover false
A signal that should fire once must be an event (crossover), not a state (above).
Your turn
DrillMade-up values · candle 1: fast 5,020.00, slow 5,020.50 · candle 2: fast 5,020.25, slow 5,020.25 · candle 3: fast 5,020.75, slow 5,020.50 · candle 4: fast 5,021.25, slow 5,020.75
Trading is risky. You can lose money. Test results are hypothetical, and a good test doesn't mean future profits. This lesson explains how Pine Script works. It isn't advice.