Get your API key

Lesson 2 of 5

Moving average crossover markers

Two EMAs with inputs, markers only on closed bars, and checking three crossovers by hand on a daily and a 15-minute chart.

9 min Log in to open

Education only. This is not investment advice and nothing here is a buy or sell recommendation. Trading carries risk of loss, and past results do not predict the future. Practise with TradingView's Paper Trading (fake money) only, never connect broker API keys or place live orders from these lessons. For personal advice, talk to a SEBI-registered Investment Adviser.

One step at a time, like flipping pages.

By the end of this lesson you will have an indicator that marks every crossover of two moving averages, only on closed bars, and you will have checked three of them by hand.

You need: your EMA script from lesson 1, TradingView, an AI chat or agent · Time: 9 minutes

  1. A crossover sounds simple: "the fast line crosses the slow line". For code, it needs three exact parts:

    • Which lines: a fast EMA of 9 bars and a slow EMA of 21 bars, both on the closing price, both editable.
    • What "cross up" means: on the previous bar the fast EMA was at or below the slow EMA, and on this bar it is above. Pine's ta.crossover() uses exactly this definition. "Cross down" is the mirror image (ta.crossunder()).
    • When it counts: only when the bar has closed. More on this in step 3.
    Why?

    The numbers 9 and 21 are not special or "best". They are examples so we can test the code. The skill here is turning a vague idea into a rule a computer cannot misread.

Log in free to read the rest of this lesson

Every lesson is free. A free account unlocks all of them and saves your progress.

Still to come in this lesson:

  1. Prompt the crossover indicator
  2. Closed bars only: the live-bar trap
  3. Check three crossovers by hand
  4. Swing vs intraday: same code, different noise
  5. Recap