We will build an EMA band: a centre line (exponential moving average) with a band above and below it whose width follows the current volatility (ATR). It is one of the examples the editor ships with.
- Open the chart of a market and choose Script editor in the indicators menu.
- In the editor click Templates and pick EMA band. The source code appears in the editor.
- Click Apply. After a moment the band lies on the price chart. It is not saved yet.
- Change a number, for example
20 in input.int(20, …) to 50, and apply again. The centre line becomes slower.
- Give the indicator a name at the top and click Save. You will find it under My indicators.
This is the source code of the example:
//@version=5
indicator("EMA band", overlay=true)
len = input.int(20, "Length", minval=1)
mult = input.float(1.5, "ATR factor", minval=0.1, step=0.1)
src = input.source(close, "Source")
basis = ta.ema(src, len)
band = ta.atr(len) * mult
upper = basis + band
lower = basis - band
pu = plot(upper, "Upper", color=color.new(color.blue, 40))
pl = plot(lower, "Lower", color=color.new(color.blue, 40))
plot(basis, "Middle", color=color.blue, linewidth=2)
fill(pu, pl, color=color.new(color.blue, 90))
Line by line:
//@version=5 states which rules the script is written for (more in section 9).
indicator(…) gives the indicator a title. overlay=true puts it on top of the price chart; without it the indicator goes into its own pane below.
- The three
input… lines define settings you can change later in a dialog without touching the source code.
ta.ema and ta.atr are ready-made calculation functions. The results are kept in variables (basis, band, …).
plot draws a line, fill colours the area between two of them. color.new(color.blue, 40) is blue at 40 % transparency.
Two more templates are available: RSI with zones (separate pane, coloured overbought and oversold zones) and SMA cross with candle colour (two averages, candles coloured by their position, triangles mark the crossings).