millionchart
فارسی

Lesson 3: Lines, colours and levels

Make a script easy to read. Colours by name and by code, a colour that changes with the bar, plot styles, levels, shaded zones and the background.

Lesson 3 of 18. A script that draws the right numbers is only half done: the lines must also be easy to read at a glance. This lesson shows the ways a script colours and shapes what it draws.

Colours#

Cuprel
indicator "Three colours"
    edition = 1
    display = price

plot(tech.sma(close, 10), name = "Named colour", color = color.orange)
plot(tech.sma(close, 30), name = "Colour code", color = #2962ff)
plot(tech.sma(close, 60), name = "Faded", color = color.fade(color.purple, 50))
  • color.orange is one of the named colours: color.red, color.green, color.blue, color.orange, color.purple, color.gray, color.teal, color.yellow, color.white, color.black and a few more.
  • #2962ff is a colour written as a code (red, green and blue in hexadecimal), the way colour pickers show it. color.rgb(41, 98, 255) is the same colour written as three numbers from 0 to 255.
  • color.fade(c, 50) makes a colour 50 percent more transparent. Faded colours are good for zones and backgrounds, which should not hide the candles.

A colour that changes with the bar#

The colour of a plot can be a different value on every bar. Here the line is green while the average rises and red while it falls:

Cuprel
indicator "Rising or falling"
    edition = 1
    display = price

input length: int = 20, name = "Length", min = 2
let average = tech.ema(close, length)
let rising = average > average[1]
plot(average, name = "Average", color = if rising then color.green else color.red, width = 2)

average[1] is the value of average one bar back, so average > average[1] is true while the average goes up. width = 2 draws a thicker line.

Styles#

style says how a plot is drawn. A histogram suits a value that swings around zero:

Cuprel
indicator "Momentum"
    edition = 1
    display = pane

input length: int = 10, name = "Length", min = 1, max = 200
let change = close - close[length]
plot(
    change,
    name = "Change",
    style = histogram,
    color = if change >= 0 then color.green else color.red,
)
level(0, name = "Zero", color = color.gray)

display = pane in the header puts the script in a pane of its own under the chart, which is right for a value that is not a price. close[length] reads as many bars back as the setting says, so the input has a max: without one the engine warns that it cannot tell how far back the script reads. The styles are line (the default), line_break, step, step_break, step_diamond, histogram, columns, area, area_break, circles and cross; Plots and drawings shows what each does.

Levels and a shaded zone#

Cuprel
indicator "RSI with a zone"
    edition = 1
    display = pane

input length: int = 14, name = "Length", min = 2
let rsi = tech.rsi(close, length)
let top = level(70, name = "Overbought", color = color.red, style = dashed)
let bottom = level(30, name = "Oversold", color = color.green, style = dashed)
shade(top, bottom, color = color.fade(color.purple, 90), name = "Middle zone")
plot(rsi, name = "RSI", color = color.purple)

level(70, ...) draws a horizontal line at a fixed value. Keep the results of two levels (or two plots) in names, and shade(top, bottom, ...) fills the space between them.

The background#

Cuprel
indicator "Above or below the average"
    edition = 1
    display = price

input length: int = 50, name = "Length", min = 2
let average = tech.sma(close, length)
let tint = if close > average then color.fade(color.green, 88) else color.fade(color.red, 88)
chart.background(if is_na(average) then na else tint, name = "Side")
plot(average, name = "Average", color = color.gray)

chart.background(...) paints the background of each bar. On the first bars the average has no value yet (is_na(average) is true), so the background is left empty there.

Try changing#

  • In "Three colours", replace #2962ff with color.rgb(255, 152, 0) and see the line turn orange.
  • In "Rising or falling", colour a flat average grey: color = if average > average[1] then color.green else if average < average[1] then color.red else color.gray.
  • In "Momentum", change style = histogram to style = columns, then to style = area.
  • In "RSI with a zone", change the fade of the zone from 90 to 70: the zone gets darker.
  • In "The background", change both 88s to 95: the background becomes lighter and the candles stand out more.

Checkpoint#

  • You can write a colour by name, by code and faded with color.fade.
  • You can choose a colour for each bar with if ... then ... else.
  • You can draw a plot as a line, a histogram or columns with style.
  • You can draw levels with level, shade between them with shade, and paint the background with chart.background.

See also: Plots and drawings, Settings people can change.

Next: Lesson 4: Signals, arrows and zones