Docs

ITMScript

A tour of the language

Every part of an ITMScript study in order, from inputs and operators to drawing, other symbols, functions and carried state, with a short example for each.

This page walks the language from the first line to the last. Each part has a short study you can paste into the editor. The operator reference has the details of each operator.

A script is a list of lines

A script has one statement per line. A line break ends a statement. A comment starts with // and runs to the end of the line. The first line may name the language version. The study line names the study and chooses whether it draws over the price or in its own pane.

itm
//@itmscript 2
// The closing price, drawn over the candles.
study("Close", overlay=true)
plot(close, "Close", color=color.blue, width=2, style=line)

A name is assigned once. There is no var, no loop and no way to change a name after it is set. This is why the same script on the same bars always gives the same result.

Inputs are settings

An input is a value a reader can change without editing the script. Declare each one on its own line, with the default first and the title second.

itm
//@itmscript 2
study("Average", overlay=true)
len = input.int(20, "Length", min=2, max=500, step=1)
src = input.source(close, "Source")
tint = input.color(color.orange, "Line")
plot(ta.sma(src, len), "Average", color=tint, width=2, style=line)

There are eight kinds: int, float, bool, source, choice, color, symbol and timeframe. A source input is a series. Every other kind is a constant. That is why a window length may be an input and a plotted value may not be a choice. A symbol or timeframe input can name the data a series(...) reads.

The bounds min and max may be negative, as in min=-10. A choice holds one of a list of options. It compares with one of its own options, and the comparison picks a value with ?:. Every branch is worked out on every bar and the comparison picks one value. A choice is not a number, so it cannot be plotted or used in arithmetic.

itm
//@itmscript 2
study("Chosen average", overlay=true)
kind = input.choice("ema", ["sma", "ema", "wma"], "Smoothing")
len = input.int(20, "Length", min=2, max=500)
bias = input.float(0, "Bias", min=-5, max=5)
avg = kind == "sma" ? ta.sma(close, len) : kind == "ema" ? ta.ema(close, len) : ta.wma(close, len)
plot(avg + bias, "Average", color=color.blue, width=2, style=line)

Bars, history and operators

The bar fields are open, high, low, close, volume, hl2, hlc3, ohlc4 and bar_index. Write close[1] to read the close one bar back. The number in the brackets must be known before the first bar, such as a number or an input. It reads backward only. No part of the language can see a bar that has not closed.

Operators do the calculation. Each one is named by its family, such as ta.sma or math.max. An operator that returns several values is read by naming each one.

itm
//@itmscript 2
study("MACD", overlay=false)
[macd, signal, hist] = ta.macd(close, 12, 26, 9)
change = close - close[1]
plot(macd, "MACD", color=color.blue, width=2, style=line)
plot(signal, "Signal", color=color.orange, width=1, style=line)
plot(hist, "Histogram", color=color.gray, width=1, style=histogram)
plot(change, "Change", color=color.gray, width=1, style=columns)

A parameter may also be written by name, as in ta.beta(y=a, x=b, length=60). Write the positional arguments first.

Conditions

A comparison gives a true or false value on every bar. Use ?: to choose between two values, and and, or and not to combine conditions. A true or false value is never a number. To average a condition, turn it into a number first.

itm
//@itmscript 2
study("Up share", overlay=false)
up = close > open ? 1 : 0
share = ta.sma(up, 20)
plot(share, "Share of up bars", color=color.green, width=2, style=line)
hline(0.5, "Half", color=color.gray, style=dashed)

A comparison with a missing value is false. Missing values explains what that means for your study.

Drawing

Six statements draw. plot draws a series. plotshape marks the bars where a condition holds. hline draws a level. fill fills the space between two plots. bgcolor tints the background. alertcondition records the bars where a condition is true. Outputs covers each of them.

itm
//@itmscript 2
study("Cross", overlay=true)
fast = ta.ema(close, 9)
slow = ta.ema(close, 21)
a = plot(fast, "Fast", color=color.blue, width=2, style=line)
b = plot(slow, "Slow", color=color.orange, width=2, style=line)
fill(a, b, color=color.new(color.blue, 90))
up = ta.crossover(fast, slow)
plotshape(up, style=triangleup, location=belowbar, color=color.green, text="Up")
alertcondition(up, "Cross up", "The fast average crossed above the slow one")

A plot's colour may change from bar to bar. Give it a condition that picks between two colours, as the next study does with a rising and a falling average.

itm
//@itmscript 2
study("Trend colour", overlay=true)
avg = ta.sma(close, 20)
plot(avg, "Trend", color=avg > avg[1] ? color.green : color.red, width=2, style=line)

A level takes one colour for the whole study. The cookbook has twenty complete studies, each with the inputs to change.

A study that draws nothing and alerts on nothing is refused. The colours and styles page lists every colour and style word.

Other symbols and timeframes

A study runs on the chart's own bars. To read another symbol or a coarser timeframe, say so. Write SPY.close for the close of another symbol, or close@1d for the daily close of the chart's symbol.

itm
//@itmscript 2
study("Relative to SPY", overlay=false)
ratio = close / SPY.close
plot(ratio, "Ratio", color=color.purple, width=2, style=line)
plot(ta.sma(ratio, 20), "Average", color=color.gray, width=1, style=line)

Other data explains when each value becomes visible.

Functions

A function of one expression is written with an arrow. It is expanded at every place it is called.

itm
//@itmscript 2
study("Spread to mid", overlay=false)
mid(a, b) => (a + b) / 2
plot(close - mid(high, low), "Close less mid", color=color.blue, width=1, style=histogram)

A function may also hold several lines. The indented lines are its own names, and the last line is its result. A result in brackets is a tuple, read by naming each value.

itm
//@itmscript 2
study("Bands", overlay=true)
len = input.int(20, "Length", min=2, max=200)
mult = input.float(2, "Width", min=0.5, max=5, step=0.5)

bands(src, n, m) =>
  basis = ta.sma(src, n)
  dev = m * ta.stdev(src, n)
  [basis, basis + dev, basis - dev]

[mid, hi, lo] = bands(close, len, mult)
plot(mid, "Basis", color=color.orange, width=2, style=line)
plot(hi, "Upper", color=color.blue, width=1, style=line)
plot(lo, "Lower", color=color.blue, width=1, style=line)

A function cannot call itself.

Carried state

Some values depend on the bar before. latch is on from a bar where one condition is true until a bar where another is. hold keeps the value a series had on the last bar where a condition was true. scan carries any value from one bar to the next through a function you write.

itm
//@itmscript 2
study("Trend", overlay=true)
fast = ta.ema(close, 9)
slow = ta.ema(close, 21)
on = latch(ta.crossover(fast, slow), ta.crossunder(fast, slow))
entry = hold(ta.crossover(fast, slow), close)
plot(entry, "Close at last cross up", color=color.gray, width=1, style=stepline)
bgcolor(on ? color.new(color.green, 92) : na)

Mistakes are refused

A mistake is refused with a message that names the fix and a code that does not change. Every code is listed on the error codes page. This study asks for a window that changes from bar to bar.

itm
//@itmscript 2
study("Window", overlay=true)
plot(ta.sma(close, close), "Average", color=color.blue)

The window of an average must be known before the first bar. Use a number or an input.

itm
//@itmscript 2
study("Window", overlay=true)
len = input.int(20, "Length", min=2, max=500)
plot(ta.sma(close, len), "Average", color=color.blue)

A study also runs inside fixed limits. Where to go next is the builder, which edits the same script as text or as blocks.