Skip to content
Wildflower
Join free
Coding strategies in Pine Script

Free Trading School · Level 7 · Course 7.4

Crossovers and signals

Lesson 8 of 16 in Coding strategies in Pine ScriptModule 2: Indicators and conditions

Advanced · Coding strategies in Pine Script

Crossovers and signals

Wildflower Trading School · Lesson 7.4.2.3

Wildflower

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

  1. Add the EMA cross script to a 5-minute MES1! chart.
  2. Count the triangles in one morning.
  3. Then add a third plotshape that marks fast > slow on every candle (use shape.circle), and compare how many marks you get.

Next: Sessions and time.

Quick check

  1. 1. Fast has been above slow for 6 candles. On how many of them is ta.crossover(fast, slow) true?

  2. 2. What does adding "and barstate.isconfirmed" do?

  3. 3. Which is an event rather than a state?

0 of 3 answered.

Finished this lesson?

Tick it off and keep your place

Make a free account and we'll remember where you are, so “Sessions and time” is waiting next time.

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 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 , 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 numbers

Above versus crossed

Made-up values · fast and slow averages over four candles

  1. Candle 1: fast 5,010.50 < slow 5,011.00
  2. Candle 2: fast 5,011.25 > slow 5,011.00: crossover true
  3. 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

Drill

Made-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.