Samples: moving averages and trend
Sample programs built on moving averages. A plain average, two averages with cross marks, a trend background, trend candles and a higher timeframe.
Each program on this page is complete: copy it into a new script in the editor, press Save and Add to chart. Change the settings from the indicator's settings panel.
Simple moving average#
The smallest useful indicator: the mean of the last length closes, drawn on the price chart.
// A simple moving average: the mean of the last `length` closes.
// This is a complete indicator; the header says what kind of script it is.
indicator "Simple moving average"
edition = 1
display = price
input length: int = 20, name = "Length", min = 1
let average = tech.sma(close, length)
plot(average, name = "SMA", color = color.orange, width = 2)What it teaches: the header, one input, tech.sma and a named, coloured plot. The line starts once there are length bars to average.
Two averages with cross marks#
A fast and a slow exponential moving average, with a triangle where the fast one crosses the slow one.
// Two exponential moving averages and a mark where the fast one crosses the slow one.
// EMA(t) = alpha * price + (1 - alpha) * EMA(t - 1), with alpha = 2 / (length + 1).
indicator "EMA pair"
edition = 1
display = price
input fast_length: int = 12, name = "Fast length", min = 1
input slow_length: int = 26, name = "Slow length", min = 2
let fast = tech.ema(close, fast_length)
let slow = tech.ema(close, slow_length)
plot(fast, name = "Fast", color = color.green)
plot(slow, name = "Slow", color = color.red)
mark.shape(
tech.crosses_above(fast, slow),
name = "Up",
shape = triangle_up,
place = below,
color = color.green,
)
mark.shape(
tech.crosses_below(fast, slow),
name = "Down",
shape = triangle_down,
place = above,
color = color.red,
)What it teaches: tech.ema, tech.crosses_above and tech.crosses_below, and mark.shape to mark the bars where a condition is true. A mark is placed below or above the bar.
Trend background#
The background turns green while the close is above a moving average and red while it is below.
// A background that follows the trend: green above the moving average, red below it, nothing while it is not known yet.
indicator "Trend background"
edition = 1
display = price
input length: int = 50, name = "Length", min = 2
let trend = tech.sma(close, length)
let tint = if close > trend then color.fade(color.green, 88) else color.fade(color.red, 88)
chart.background(if is_na(trend) then na else tint, name = "Trend")
plot(trend, name = "Trend line", color = color.gray)What it teaches: a colour chosen with if ... then ... else, faded with color.fade, and chart.background. While the average has no value yet the background is left empty (na).
Trend candles#
The candles themselves take the colour of the trend.
// Candles coloured by the trend: green while the close is above the moving average, red below it.
indicator "Trend candles"
edition = 1
display = price
input length: int = 50, name = "Length", min = 2
let trend = tech.ema(close, length)
let tint = if close > trend then color.green else color.red
chart.candles(if is_na(trend) then na else tint, name = "Trend candles")
plot(trend, name = "Trend line", color = color.gray)What it teaches: chart.candles recolours the chart's own candles; na keeps their normal colour.
Distance from the average#
How far the close has moved from its average, in percent. A value far from zero means the price is stretched away from its average.
// How far the close is from its moving average, in percent of the average.
// Above zero the close is above the average; the further from zero, the more stretched the move.
indicator "Distance from the average"
edition = 1
display = pane
input length: int = 50, name = "Length", min = 2
let average = tech.ema(close, length)
let distance = 100 * (close - average) / average
plot(
distance,
name = "Distance %",
style = histogram,
color = if distance >= 0 then color.green else color.red,
)
level(0, name = "Zero", color = color.gray)What it teaches: arithmetic on series, a histogram whose colour follows the sign of the value, and a zero level.
An average from a higher timeframe#
The 20-period EMA of the 4-hour chart, drawn on whatever chart you are looking at, next to the same EMA of the chart itself.
// A request for another timeframe: the 4-hour EMA of the close, drawn on whatever chart the script is on.
// `data.series` evaluates the expression on the other timeframe and gives back its last closed value.
indicator "Higher timeframe average"
edition = 1
display = price
input tf: tf = "240", name = "Timeframe"
input length: int = 20, name = "Length", min = 2
let other = data.series(tf, tech.ema(close, length), symbol = symbol.id)
plot(other, name = "EMA on the other timeframe", color = color.orange, width = 2)
plot(tech.ema(close, length), name = "EMA on this chart", color = color.gray)What it teaches: a tf input and data.series, which works out an expression on another timeframe. By default it gives the value of the last bar of that timeframe that has closed, so the line does not change while the higher bar is still forming; Other timeframes explains the choices.