Docs

ITMScript

Mark a moving average cross

Mark the bar where a fast moving average crosses a slow one, with a triangle for each direction.

Mark the bar where a fast average crosses above or below a slow one.

itm
//@itmscript 2
study("Average cross", overlay=true)
fastLen = input.int(9, "Fast length", min=2, max=200)
slowLen = input.int(21, "Slow length", min=3, max=500)
fast = ta.sma(close, fastLen)
slow = ta.sma(close, slowLen)
plot(fast, "Fast", color=color.blue, width=2, style=line)
plot(slow, "Slow", color=color.orange, width=2, style=line)
plotshape(ta.crossover(fast, slow), style=triangleup, location=belowbar, color=color.green, text="Up")
plotshape(ta.crossunder(fast, slow), style=triangledown, location=abovebar, color=color.red, text="Down")

What you see

Two simple averages are drawn over the candles. The fast one is blue and the slow one is orange. A green triangle sits below the bar where the fast average crosses above the slow one. A red triangle sits above the bar where it crosses below.

A cross is marked on the bar that completes it and is never moved back to an earlier bar. Nothing is marked until the slow average has enough bars.

Change these inputs

  • Fast length. The window of the fast average, from 2 to 200 bars. A shorter window follows price closely and crosses more often.
  • Slow length. The window of the slow average, from 3 to 500 bars. A longer window crosses less often. Keep it above the fast length, because the labels assume it.

To use exponential averages, replace ta.sma with ta.ema on the two lines that read them.