Docs

ITMScript

Colour a trend line by its direction

Draw an average in green while it is above its previous value and in red while it is not.

Draw an average in one colour while it rises and in another while it does not.

itm
//@itmscript 2
study("Trend line", overlay=true)
len = input.int(20, "Length", min=2, max=300)
rising = input.color(color.green, "Rising")
falling = input.color(color.red, "Falling")
avg = ta.sma(close, len)
plot(avg, "Trend", color=avg > avg[1] ? rising : falling, width=2, style=line)

What you see

A thick line is drawn over the candles. On a bar where the average is above its value one bar earlier, the line is green. On any other bar it is red.

The first bar that has an average has no earlier value to compare with. A comparison that cannot be worked out picks the second colour, so that one bar is red whatever the data. The bars before it have no average and are not drawn, so only that bar is affected. A bar where the average is unchanged is red as well.

Change these inputs

  • Length. The window of the average, from 2 to 300 bars.
  • Rising. The colour on bars where the average is higher than one bar earlier.
  • Falling. The colour on every other bar.