CODESCRIPT

Your first indicator

This page takes you from opening the editor to seeing your own line on the chart. You start with a single line of code and end up with a working indicator on screen.

An indicator is a small program that computes a value from price and draws it on the chart. You write the formula once; the terminal runs it again for every bar. Your job is simply to say which value you want.

Open the editor, write one line

Open the code editor below the chart and start an empty script. Make your first move a drawing call: plot takes a value and lays it onto the chart, bar by bar.

CODESCRIPT
// her barin kapanis fiyatini grafige cizer
plot(close)

A thin line appears on the price panel, joining the closes. It runs through the closing end of every candle.

A line that starts with two slashes is a comment. The terminal never runs it; it is there for whoever reads the code. Writing comments is the habit that pays off most when you come back to your own script later.

TIP

Remember to run the script after typing it. The editor does not push un-run code to the chart. If no line shows up, first make sure the script really ran.

Bars, series, and close

Every candle on the chart is one bar. A bar carries four prices: the open, the high, the low, and the close. In code they are called open, high, low and close.

close is not a single number. It is a series carrying one value per bar. That is why plot(close) gives you a line from end to end rather than a single dot.

  • open — the price the bar opened at
  • high — the highest price seen inside the bar
  • low — the lowest price seen inside the bar
  • close — the price the bar closed at, the source indicators lean on most
  • hl2 — the midpoint of the high and the low; it jitters less than the close
  • volume — how much changed hands during that bar
CODESCRIPT
// ikinci arguman kunyede gorunen baslik, ucuncusu hex renk
plot(close, "Kapanis", "#787b86")
// hl2 barin orta noktasidir: (high + low) / 2
plot(hl2, "Bar ortasi", "#2962ff")

Two lines on the same panel: a grey close line and a blue midpoint line. The blue one runs smoother.

NOTE

The second value you hand to plot is the title, and it shows up in the chart legend. Skip it and your lines become hard to tell apart — past two lines that turns annoying fast.

Your first indicator: a moving average

A raw price line jitters too much to read direction from. So people average the last few bars instead. sma does that for you: it averages the series you pass over the window you pass, and hands back another series.

CODESCRIPT
// son 20 barin ortalamasi; her yeni barda yeniden hesaplanir
ortalama = sma(close, 20)
plot(close, "Kapanis", "#787b86")
plot(ortalama, "SMA 20", "#2962ff")

A blue SMA 20 line, far flatter than price, running through the middle of the grey close line. As long as price holds above the blue line, the direction counts as up.

CAUTION

SMA 20 stays empty for the first 19 bars — there are not enough bars to average yet. A line starting late at the left edge of the chart is expected, not broken: no indicator can produce a value before it has as many bars as its length.

Giving it a title and a color

Color is the third argument, written as a hex value. Rather than repeating a computation, park it in a variable: the code gets shorter and the same series stays available elsewhere.

CODESCRIPT
hizli = sma(close, 10)
yavas = sma(close, 50)
plot(hizli, "SMA 10", "#26a69a")
plot(yavas, "SMA 50", "#ef5350")
// iki cizgi arasindaki alani yari saydam maviye boya
fill(hizli, yavas, "#2962ff22")

A fast green average and a slow red one, with the gap between them shaded translucent blue. The wider the band, the faster the move.

TIP

Add two more characters to a hex color and you get transparency. #2962ff22 and #2962ff are the same blue; the first one lets the bars behind it show through.

Drawing in a separate panel

Some indicators do not share price's scale. RSI travels between 0 and 100; put it on the price panel and the line gets squashed against the bottom. Pass a panel name as plot's fourth argument and the value moves into its own panel below.

CODESCRIPT
guc = rsi(close, 14)
// dorduncu arguman bir alt pano adidir
plot(guc, "RSI 14", "#7e57c2", "guc")
hline(70, "asiri alim", "#ef5350")
hline(30, "asiri satim", "#26a69a")

A new panel under the price carrying a purple RSI line, with the 70 and 30 thresholds marked by dashed horizontal lines.

NOTE

Every plot call that uses the same panel name ends up in that one panel. Leave the name out and the terminal decides for itself, going by the range of the values.

Making the length adjustable

Hard-code the number 20 and you must reopen the script every time you want a different value. input moves that number into the settings panel; the value changes there while the code stays put.

CODESCRIPT
uzunluk = input(20, min=5, max=200, step=1, name="Ortalama uzunlugu")
ortalama = ema(close, uzunluk)
plot(close, "Kapanis", "#787b86")
plot(ortalama, "EMA", "#ff9800")

A length field in the settings panel, adjustable between 5 and 200; the orange EMA line redraws the moment you change the value.

TIP

Give min and max and the field is bounded — nobody can type 0 or a negative length. Do not skip the name either; a bare number sitting in the panel tells the reader nothing.

Marking the signal on the bar

A line shows direction, but not the moment. To see the bars where price crosses above the average, drop a shape on them. crossover is true only on the bar where the cross happens, and resets right after.

CODESCRIPT
ortalama = sma(close, 20)
plot(ortalama, "SMA 20", "#2962ff")
// yalnizca kesisim barlarinda, barin altina yesil ucgen
plotshape(crossover(close, ortalama), "Yukari kesis", "triangleup", "belowbar", "#26a69a")

A blue SMA 20 line, plus a green triangle under every bar where price crosses above it. Nothing is drawn on the bars in between.

NOTE

If you made it this far, you have a working indicator on screen: an average, a color, and a signal mark. Everything beyond this is the same three steps again — compute a value, draw it, mark the condition.

Common mistakes

  • Passing the color where the title belongs. plot's order is series, title, color, panel; a hex code in the second slot becomes the title, not the color.
  • Writing the same computation twice. Store sma(close, 20) in a variable instead of repeating it in two places.
  • Hunting for a 0-to-100 indicator on the price panel. Plot RSI without a panel name and it gets placed by value range, which may not be where you were looking.
  • Choosing a window that is far too short. sma(close, 2) is very nearly price itself and smooths nothing.
  • Mistaking the gap on the first bars for missing data. An indicator produces nothing until it has collected as many bars as its length.

The functions you used on this page

plot(series, title?, color?, pane?, style?, linewidth?, linestyle?, offset?, display?)

Returns nothing; joins your series bar by bar and draws it on the chart as a line.

series
series
The numeric series to draw. Pass text and you get a clear error.
title
string
Optional. The name shown in the legend.
color
color
Optional. A hex color, for example "#2962ff".
pane
string
Optional. A lower-panel name. Lines sharing a name are grouped into one panel; leave it out and placement follows the value range.
linewidth
number
Optional. Line thickness.

sma(source, length)

A series holding, for each bar, the arithmetic mean of the last length bars.

source
series
The price series to average; usually close.
length
number
How many bars the averaging window spans. A larger value gives a flatter but slower line.

ema(source, length)

An exponential moving average series that leans harder on the most recent bars.

source
series
The series to average; usually close.
length
number
Window length. A smaller value tracks price faster, at the cost of more misleading signals.

rsi(source, length)

A relative strength series that swings between 0 and 100.

source
series
The series fed into the calculation; usually close.
length
number
Calculation period; 14 is the usual choice.

hline(price, title?, color?, style?, width?, display?)

Returns nothing; draws a horizontal line at a fixed level.

price
number
The fixed level to draw at. For a value that moves bar to bar use plot, not hline.
title
string
Optional. A label for the level.
color
color
Optional. Line color, as hex.
style
string
Optional. Line style: solid, dashed or dotted. Defaults to dashed.
width
number
Optional. Line thickness, in pixels.

plotshape(series, title?, style?, location?, color?, offset?, text?, textcolor?, size?, display?)

Returns nothing; places a shape on the price panel for each bar where the condition holds.

series
boolean
A shape is drawn on the bars where this is true, and nothing on the rest.
title
string
Optional. The shape's name in the legend.
style
string
Optional. The shape: triangleup, triangledown, circle, flag and so on. Defaults to circle.
location
string
Optional. Placement: abovebar, belowbar, top, bottom. Defaults to abovebar.
color
color
Optional. Shape color, as hex.