Samples: channels and levels
Sample programs that draw bands, channels and price levels. Bollinger bands, an ATR channel, Donchian, daily pivots, key levels and a range box.
These scripts draw on the price chart (display = price): bands around an average, the extremes of a window, and levels taken from earlier days.
Bollinger bands#
A simple moving average with bands a number of standard deviations above and below it, shaded between them.
// Bollinger Bands: a simple moving average and bands a number of standard deviations above and below it.
indicator "Bollinger bands"
edition = 1
display = price
input length: int = 20, name = "Length", min = 2
input mult: float = 2.0, name = "Deviations", min = 0.1, step = 0.1
let basis = tech.sma(close, length)
let deviation = mult * tech.stdev(close, length)
let upper = basis + deviation
let lower = basis - deviation
let top = plot(upper, name = "Upper", color = color.blue)
let bottom = plot(lower, name = "Lower", color = color.blue)
plot(basis, name = "Basis", color = color.orange)
shade(top, bottom, color = color.fade(color.blue, 92), name = "Band")What it teaches: tech.stdev, keeping two plots in names, and shade between them.
ATR channel#
An average widened by a multiple of the average true range, the usual size of a bar.
// Average True Range: the Wilder mean of max(high - low, |high - previous close|, |low - previous close|).
// The channel is a moving average of the close, widened by a multiple of the ATR.
indicator "ATR channel"
edition = 1
display = price
input length: int = 20, name = "Length", min = 2
input mult: float = 2.0, name = "Multiple", min = 0.1, step = 0.1
let basis = tech.ema(close, length)
let range = tech.atr(length)
let upper = basis + mult * range
let lower = basis - mult * range
plot(upper, name = "Upper", color = color.red)
plot(lower, name = "Lower", color = color.green)
plot(basis, name = "Basis", color = color.gray)What it teaches: tech.atr, and a float input with a step.
Donchian channel#
The highest high and the lowest low of the last length bars, and the line halfway between them.
// Donchian Channel: the highest high and the lowest low of the last `length` bars, and their middle.
indicator "Donchian channel"
edition = 1
display = price
input length: int = 20, name = "Length", min = 1
let upper = tech.highest(high, length)
let lower = tech.lowest(low, length)
let middle = (upper + lower) / 2
plot(upper, name = "Upper", color = color.green)
plot(lower, name = "Lower", color = color.red)
plot(middle, name = "Middle", color = color.gray)What it teaches: tech.highest and tech.lowest on the high and the low.
Daily pivot levels#
The classic floor pivots, worked out from the previous day's high, low and close.
// Classic floor pivots from the previous day: P = (H + L + C) / 3, R1 = 2P - L, S1 = 2P - H.
// `prior(day, 1)` reads the last finished day, so the levels are known from the first bar of the new day.
indicator "Daily pivot levels"
edition = 1
display = price
let prior_high = prior(day, 1).high
let prior_low = prior(day, 1).low
let prior_close = prior(day, 1).close
let pivot = (prior_high + prior_low + prior_close) / 3
plot(pivot, name = "Pivot", color = color.gray)
plot(2 * pivot - prior_low, name = "R1", color = color.red)
plot(2 * pivot - prior_high, name = "S1", color = color.green)What it teaches: prior(day, 1), the last day that has finished, read from the chart's own bars. Use it on an intraday chart: on a daily chart prior(day) stops the script with E-PERIOD-TF.
Key levels#
Yesterday's high and low and the open of this week, with a mark where the close crosses yesterday's high or low.
// Three levels many traders watch: yesterday's high and low, and the open of the current week.
// prior(day) is the last finished trading day; current(week) is the week that is still forming.
indicator "Key levels"
edition = 1
display = price
let day_high = prior(day).high
let day_low = prior(day).low
let week_open = current(week).open
plot(day_high, name = "Yesterday's high", color = color.green, style = step_break)
plot(day_low, name = "Yesterday's low", color = color.red, style = step_break)
plot(week_open, name = "Week open", color = color.blue, style = step_break)
let above = tech.crosses_above(close, day_high)
let below = tech.crosses_below(close, day_low)
mark.shape(
above,
name = "Above yesterday's high",
shape = triangle_up,
place = below,
color = color.green,
)
mark.shape(
below,
name = "Below yesterday's low",
shape = triangle_down,
place = above,
color = color.red,
)What it teaches: prior(day) and current(week), the step_break style, and crossings of a level. Like the pivots, it needs an intraday chart.
Recent range drawing#
A box around the range of the last bars, a dashed line at the close and a label at the high, drawn once on the newest bar.
// Drawing objects on the last bar: a box around the recent range, a dashed line at the close and a label at the high.
indicator "Recent range"
edition = 1
display = price
input length: int = 30, name = "Length", min = 2
let top = tech.highest(high, length)
let bottom = tech.lowest(low, length)
if bar.is_last
box.new(
bar.index - length + 1,
top,
bar.index,
bottom,
border_color = color.blue,
background = color.fade(color.blue, 90),
)
line.new(
bar.index - length + 1,
close,
bar.index,
close,
extend = right,
style = dashed,
color = color.orange,
)
label.new(bar.index, top, f"High {top:price}", style = label_down)
plot(close, name = "Close", color = color.gray)What it teaches: drawings (box.new, line.new, label.new) placed by bar number and price, and bar.is_last to draw them only once.